{"id":19816254,"url":"https://github.com/elixirscript/tailored","last_synced_at":"2025-06-12T16:09:53.974Z","repository":{"id":65374684,"uuid":"47217259","full_name":"elixirscript/tailored","owner":"elixirscript","description":"A Pattern matching library for JavaScript","archived":false,"fork":false,"pushed_at":"2019-08-13T14:06:04.000Z","size":557,"stargazers_count":25,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-22T11:50:02.693Z","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/elixirscript.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":"2015-12-01T21:02:16.000Z","updated_at":"2024-12-24T22:10:06.000Z","dependencies_parsed_at":"2023-01-20T01:25:24.180Z","dependency_job_id":null,"html_url":"https://github.com/elixirscript/tailored","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/elixirscript/tailored","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixirscript%2Ftailored","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixirscript%2Ftailored/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixirscript%2Ftailored/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixirscript%2Ftailored/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elixirscript","download_url":"https://codeload.github.com/elixirscript/tailored/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elixirscript%2Ftailored/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259500231,"owners_count":22867338,"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-12T10:08:48.677Z","updated_at":"2025-06-12T16:09:53.941Z","avatar_url":"https://github.com/elixirscript.png","language":"JavaScript","readme":"# Tailored\n\n## A pattern matching library\n\nThis is the pattern matching library ported from elixirscript. It allows you to\ncreate functions that will perform pattern matching on the input and either execute\nthe corresponding function or throw a `tailored.MatchError`.\n\n```js\nconst tailored = require('tailored');\nconst _ = tailored.wildcard();\nconst $ = tailored.parameter();\n\nlet fact = tailored.defmatch(\n  tailored.clause([0], () =\u003e 1),\n  tailored.clause([$], (n) =\u003e n * fact(n - 1))\n);\n\nlet response = fact(0); //1\nresponse = fact(10); //3628800\n```\n\n### API\n\n * `tailored.defmatch(...clauses): Function` - Takes one or more `tailored.Clause` objects and returns\n a pattern match function. It cycles through the clauses and if a corresponding pattern matches, and the guard is true,\n then the matching parameters are passed to the corresponding function that will execute. If no matching clause is found, a `tailored.MatchError` is thrown.\n\n* `tailored.clause(patterns: Array[any], fn: Function, guard: Function = () =\u003e true): tailored.Clause` - A helper function for creating `tailored.Clause` objects. It takes an array of patterns, the function to execute if the pattern matches, and a guard function.\n\n\n* `tailored.match(pattern: any, expression: any): [any]` - Tries to match the pattern with the given expression\n\n\n* `tailored.wildcard()` - Returns a wildcard pattern. Matches on anything.\n\n* `tailored.variable()` - Returns a variable pattern. Matches on a value and uses it as a parameter for the clause functions\n\n* `tailored.startsWith(prefix: String)` - Returns a startsWith pattern. Matches on strings with the given string as a prefix\n\n* `tailored.headTail()` - Returns a headTail pattern. Matches arrays and returns both the head element and the tail elements as parameters\n\n* `tailored.type(type: any, properties: Object = {})` - Returns a type pattern. Match on the type and it's properties for matching patterns.\n\n* `tailored.capture(pattern: any)` - Returns a capture pattern. Matches on it's patterns, and then returns the pattern as a parameter\n\n\n### Examples\n\n* Matches on anything, returning one\n  ```js\n  var fn = tailored.defmatch(tailored.clause([_], function () {\n    return 1;\n  }));\n\n  fn(\"ABC\") // 1\n  ```\n\n* Using a guard\n  ```js  \n    let fn = tailored.defmatch(\n      tailored.clause([$], (number) =\u003e number, (number) =\u003e number \u003e 0)\n    );\n\n    fn(0); //throws MatchError\n    fn(3); //returns 3;\n    ```\n\n* Match values in an object\n  ```js\n  var fn = tailored.defmatch(\n    tailored.clause([{ value: $ }], function (val) { return 1 + val; }),\n    tailored.clause([{ a: { b: { c: $ } } }], function (val) { return 1 - val; })\n  );\n\n  fn({value: 20}) //21;\n  fn({a: {b: {c: 20}, d: 10 } }) // 19\n  ```\n\n\n  More examples can be found in the tests\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felixirscript%2Ftailored","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felixirscript%2Ftailored","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felixirscript%2Ftailored/lists"}