{"id":18057373,"url":"https://github.com/1computer1/headpats","last_synced_at":"2025-04-11T04:41:08.791Z","repository":{"id":57262129,"uuid":"146049089","full_name":"1Computer1/headpats","owner":"1Computer1","description":"Pattern matching and tagged unions in JavaScript without new syntax.  ","archived":false,"fork":false,"pushed_at":"2018-09-08T16:58:18.000Z","size":115,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-25T02:43:53.768Z","etag":null,"topics":["case","clauses","discriminated-unions","matching","pattern","pattern-matching","patterns","tagged-unions"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/1Computer1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-24T23:38:27.000Z","updated_at":"2025-03-07T20:43:36.000Z","dependencies_parsed_at":"2022-08-25T06:11:10.926Z","dependency_job_id":null,"html_url":"https://github.com/1Computer1/headpats","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/1Computer1%2Fheadpats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1Computer1%2Fheadpats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1Computer1%2Fheadpats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1Computer1%2Fheadpats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/1Computer1","download_url":"https://codeload.github.com/1Computer1/headpats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248345275,"owners_count":21088242,"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":["case","clauses","discriminated-unions","matching","pattern","pattern-matching","patterns","tagged-unions"],"created_at":"2024-10-31T02:07:34.363Z","updated_at":"2025-04-11T04:41:08.768Z","avatar_url":"https://github.com/1Computer1.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Headpats\n\nPattern matching and tagged unions in JavaScript without new syntax.  \nSee the [guide](./docs/0.%20How%20to%20Use.md) on how to use Headpats!  \nRefer to the [documentation](./docs) for specific information on pattern matching and built-in features.  \n\n## Examples\n\n**Setup**  \n\n```js\nconst pat = require('headpats');\nconst { $, $$, _, is, rest } = pat;\n```\n\n**Stringly Typed**  \n\n```js\nfunction doOperation(operation, a, b) {\n    return pat\n        .case('add', () =\u003e a + b)\n        .case('minus', () =\u003e a - b)\n        .case(_, () =\u003e NaN)(operation);\n}\n\ndoOperation('add', 1, 2)\n→ 3\n\ndoOperation('minus', 1, 2)\n→ -1\n\ndoOperation('something', 1, 2)\n→ NaN\n```\n\n**Safe Traversal**  \n\n```js\nconst o = { x: { y: { z: 10 } } };\n\npat.match({ x: { y: { z: $.z } } }, o)\n→ { z: 10 }\n\npat.match({ x: { y: { what: $.what } } }, o)\n→ null\n```\n\n**Result Matching**  \n\n```js\nconst ok = Symbol('ok');\nconst err = Symbol('err');\n\nfunction divide(a, b) {\n    if (b === 0) {\n        return [err, null];\n    }\n\n    return [ok, a / b];\n}\n\npat.match([ok, $.num], divide(10, 2));\n→ { num: 5 }\n```\n\n**Option Type**  \n\n```js\nconst Option = pat.union('Option', 'Some', 'None');\nconst { Some, None } = Option;\n\nconst double = pat\n    .case($$(Some, $.x), ({ x }) =\u003e new Some(x * 2))\n    .case($$(None, _), () =\u003e new None());\n\ndouble(new Some(5))\n→ Some { x: 10 }\n\ndouble(new None())\n→ None {}\n\ndouble(100)\n→ Error\n```\n\n**Recursive Map**  \n\n```js\nconst map = pat\n    .clause([], _, () =\u003e [])\n    .clause([$.x, [rest, $.xs]], $.f, ({ x, xs, f }) =\u003e [f(x)].concat(map(xs, f)));\n\nmap([1, 2, 3, 4], x =\u003e x * 2)\n→ [2, 4, 6, 8]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1computer1%2Fheadpats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F1computer1%2Fheadpats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1computer1%2Fheadpats/lists"}