{"id":21669368,"url":"https://github.com/attack-monkey/matcha","last_synced_at":"2026-05-20T06:03:57.770Z","repository":{"id":57291877,"uuid":"294236356","full_name":"attack-monkey/matcha","owner":"attack-monkey","description":"Pattern Matching for Typescript and Javascript","archived":false,"fork":false,"pushed_at":"2020-09-14T19:01:22.000Z","size":37,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-25T08:44:03.558Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/attack-monkey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-09T21:43:37.000Z","updated_at":"2020-09-14T19:01:24.000Z","dependencies_parsed_at":"2022-08-27T16:51:19.573Z","dependency_job_id":null,"html_url":"https://github.com/attack-monkey/matcha","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/attack-monkey%2Fmatcha","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/attack-monkey%2Fmatcha/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/attack-monkey%2Fmatcha/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/attack-monkey%2Fmatcha/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/attack-monkey","download_url":"https://codeload.github.com/attack-monkey/matcha/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244570268,"owners_count":20474021,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-25T12:21:29.227Z","updated_at":"2026-05-20T06:03:57.764Z","avatar_url":"https://github.com/attack-monkey.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# matcha\nPattern Matching for Typescript and Javascript\n\n----\n\nmatcha provides powerful pattern matching - inspired by f# and functional programming.\n\n## Install\n\n`npm i matcha_match`\n\n...\n\n`import { patternMatch, with_ } from 'matcha_match'`\n\n`import { $string } from 'matcha/runtime-interfaces/$string`\n\n## Overview\n\nPattern matching takes a value and matches it against a series of patterns.\nThe first pattern to match, fires the value (with type inferred from the pattern) into an accompanying function.\n\nSo... let's say we have `name`.\n\nWe could do something like...\n\n```typescript\n\npatternMatch(\n  name,\n  with_('garfield', matchedName =\u003e `${matchedName} is a cat`)\n  with_('odie', matchedName =\u003e `${matchedName} is a dog`)\n)\n\n```\n\nIn the above `matchedName` in both cases is inferred to be a string - even though `name` may be of unknown type.\nThat's because `matchedName` infers it's type from the pattern.\n\nPattern Matching can be used to return a value.\nThe result is the result of the function that fires upon match.\nIf there is no match, then the original value is returned instead.\n\n\n```typescript\n\nconst name: string = getName()\n\nconst a = patternMatch(\n  name,\n  with_('garfield', matchedName =\u003e `${matchedName} is a cat`)\n  with_('odie', matchedName =\u003e `${matchedName} is a dog`)\n)\n\n```\n\nIn the above, since the value and both `with_` arms all return a string - the compiler is smart enough to know that the resulting type is always string. Therefore `a` gets an inferred type of string.\n\nIf one of the arms returned a `number` then `a` would have an inferred type of `string | number`.\n\n### Literal matching\n\nWe've already seen how simple equality matches can be made...\n\n```typescript\n\nconst a = 'cat' as unknown\n\nconst b = patternMatch(\n  a,\n  with_('cat', _ =\u003e `hello kitty`),\n  with_('dog', _ =\u003e `hello doggy`)\n)\n\n```\n\nBut Pattern Matching is far more powerful than that...\n\n### Partial Matching \u0026 Destructuring\n\nObjects and arrays can be matched against a partial object / array.\n\n```typescript\n\nconst a = {\n  name: {\n    first: 'johnny',\n    last: 'bravo'\n  }\n}\n\npatternMatch(\n  a,\n  with_({ name: { first: 'johnny '} }, _ =\u003e `matching on first name`)\n)\n\n```\n\nWhich is particularly useful when used in combination with destructuring\n\n```typescript\n\npatternMatch(\n  a,\n  with_({ name: { first: 'johnny '} }, ({ name: { first: b }}) =\u003e `Hey it's ${b}`)\n)\n\n```\n\n### Runtime Interfaces\n\nSpecial runtime interfaces can be used to match against in place of values...\n\nHere we use `$string` in place of the literal 'johnny'.\n\n\n```typescript\n\nconst $matchPattern = {\n  name: {\n    first: $string \n  }\n}\n\npatternMatch(\n  a,\n  with_($matchedPattern, ({ name: { first: b }}) =\u003e `${b} is a string`)\n)\n\n```\n\nIt's also good to point out that a runtime interface automatically binds the correct type to the interface, so `$string` is of type `string`. So when `a` is matched, it infers the type `{ name: { first: string }}`\n\nRuntime interfaces are powerful...\n\n```typescript\n\nconst a = [1, 2, 3]\n\npatternMatch(\n  a,\n  with_($array($number), a =\u003e `${a} is an array of numbers`)\n)\n\n```\n\n```typescript\n\npatternMatch(\n  a,\n  with_([1, $number, 3], ([_, b, __]) =\u003e `${b} is a number`)\n)\n\n```\n\n```typescript\n\nconst a = {\n  a: [1, 2],\n  b: [3, 3, 4],\n  c: [1, 5, 99]\n}\n\npatternMatch(\n  a,\n  with_($record($array($number)), a =\u003e `A record of arrays of numbers - whoa`)\n)\n\n```\n\n```typescript\n\nconst a = 'cat' as unknown\n\nconsole.log(\n  patternMatch(\n    a,\n    with_($lt(100), _ =\u003e `\u003c 100`),\n    with_($gt(100), _ =\u003e `\u003e 100`),\n    with_(100, _ =\u003e `its 100`),\n    with_($unknown, _ =\u003e `no idea ... probably a cat`) // Use $unknown as a catch all\n  )\n)\n\n```\n\n```typescript\n\nconst a = 'cat' as string | number\n\npatternMatch(\n  a,\n  with_($union([$string, $number]), _ =\u003e `a is string | number`)\n)\n\n```\n\nRuntime interfaces include\n\n- `$string`\n- `$number`\n- `$boolean`\n- `$array([])`\n- `$record()`\n- `$union([])`\n- `$unknown`\n- `$nothing` \u003c- Use this to match on undefined \u0026 null\n- `$lt`\n- `$gt`\n- `$lte`\n- `$gte`\n\n## Roll your own Runtime Interfaces\n\n```typescript\n\nconst $even =\n  {\n    runtimeInterface: true,\n    test: (a: number) =\u003e a % 2 === 0\n  } as unknown as number\n\nconst $odd =\n  {\n    runtimeInterface: true,\n    test: (a: number) =\u003e a % 2 !== 0\n  } as unknown as number\n\nconsole.log(\n  patternMatch(\n    101,\n    with_($even, _ =\u003e `number is even`),\n    with_($odd, _ =\u003e `number is odd`)\n  )\n) // number is odd\n\n```\nA Runtime interface is an object with the property `runtimeInterface: true`.\nThis tells the `with_` function to treat the value as a Runtime Interface.\n\nPrimitive Runtime Interfaces have a `type` property, but more complex ones have a `test` function that determines whether a match is being made.\n\nIn both `$odd` and `$even` the subject is piped into the test function and a boolean is returned which determines whether or not the subject matches.\n\nNote that the Runtime Interface object is coerced into the expected type should the path match.\n\n### Simple, Safe Fetch\n\n```typescript\n\nconst $validJson = {\n  userId: $number,\n  id: $number,\n  title: $string,\n  completed: $boolean\n}\n\nfetch('https://jsonplaceholder.typicode.com/todos/1')\n  .then(response =\u003e response.json())\n  .then(json =\u003e\n    patternMatch(\n      json,\n      match($validJson, json =\u003e console.log(`yay - ${ json.title }`)),\n      match($unknown, a =\u003e console.log(`Unexpected JSON response from API`))\n    )\n  )\n\n```\n\n## Type-cirtainty\n\nPattern matching becomes more powerful when used to drive type-cirtainty.\nThe return value of pattern matching is often a `union` type or just plain `unknown`.\n\nInstead we can drive type-cirtainty by not returning a response to a variable at all.\nInstead we call a function passing in the value of cirtain-type from the inferred match.\n\nIn the below `personProgram` only fires if `bob` matches `$person` so if `personProgram` runs at all, then it is with type-cirtainty.\n\n```typescript\n\nconst $person = {\n  name: {\n    first: $string\n  }\n}\n\ntype Person = typeof $person\n\nconst personProgram = (person: Person) =\u003e {\n  //this program runs with type cirtainty :D\n  console.log(`${person.name.first} is safe`)\n}\n\nconst bob = getPerson(123)\n\npatternMatch(\n  bob,\n  with_($person, personProgram /* this only runs if a match occurs */),\n  with_($nothing, _ =\u003e console.log('no match'))\n)\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fattack-monkey%2Fmatcha","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fattack-monkey%2Fmatcha","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fattack-monkey%2Fmatcha/lists"}