{"id":22445491,"url":"https://github.com/mistlog/draft-dsl-match","last_synced_at":"2025-03-27T10:44:54.596Z","repository":{"id":44009190,"uuid":"235733981","full_name":"mistlog/draft-dsl-match","owner":"mistlog","description":"Pattern match DSL for TypeDraft","archived":false,"fork":false,"pushed_at":"2023-01-05T05:37:28.000Z","size":706,"stargazers_count":0,"open_issues_count":9,"forks_count":0,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-03-02T16:40:02.351Z","etag":null,"topics":["dsl","typedraft"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/mistlog.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":"2020-01-23T06:08:19.000Z","updated_at":"2021-11-06T00:45:38.000Z","dependencies_parsed_at":"2023-02-03T13:46:46.999Z","dependency_job_id":null,"html_url":"https://github.com/mistlog/draft-dsl-match","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mistlog%2Fdraft-dsl-match","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mistlog%2Fdraft-dsl-match/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mistlog%2Fdraft-dsl-match/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mistlog%2Fdraft-dsl-match/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mistlog","download_url":"https://codeload.github.com/mistlog/draft-dsl-match/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245832680,"owners_count":20679701,"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":["dsl","typedraft"],"created_at":"2024-12-06T03:14:55.990Z","updated_at":"2025-03-27T10:44:54.575Z","avatar_url":"https://github.com/mistlog.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DSL Match \u0026middot; ![Build Status](https://github.com/mistlog/draft-dsl-match/workflows/build/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/mistlog/draft-dsl-match/badge.svg)](https://coveralls.io/github/mistlog/draft-dsl-match)\n\nDSL Match is used in typedraft to support pattern match.\n\n## Inspirations\n\nThis DSL is inspired by [ts-pattern](https://github.com/gvergnaud/ts-pattern) and [tc39/proposal-pattern-matching](https://github.com/tc39/proposal-pattern-matching). Examples and test cases are adapted from [ts-pattern](https://github.com/gvergnaud/ts-pattern).\n\n## Installation\n\nSuppose that you have `typedraft` installed, then:\n\n```shell\nnpm i -D draft-dsl-match\n```\n\nand add it to `typedraft.config.ts`:\n\n```ts\nimport { PatternMatch } from \"draft-dsl-match\";\n\nexport default {\n    DSLs: [{ name: \"match\", dsl: () =\u003e new PatternMatch() }],\n};\n```\n\nYou can use [dsl-match-demo](https://github.com/mistlog/dsl-match-demo) as template project to get started quickly.\n\n## Examples\n\n### Data Structure\n\nSupport objects, arrays, tuples, Sets, Maps, and all primitive types.\n\n#### Object\n\n```ts\nimport { MatchDSL } from \"draft-dsl-match\";\n\ntype Vector1 = { x: number };\ntype Vector2 = { x: number; y: number };\ntype Vector3 = {\n    x: number;\n    y: number;\n    z: number;\n};\ntype Vector = Vector1 | Vector2 | Vector3;\n\nconst vector: Vector = { x: 1 };\nconst result = Λ\u003cstring\u003e(\"match\")` ${vector as Vector} \n    ${{ x: 1, y: 1, z: 1 }} -\u003e ${\"vector3\"}\n    ${{ x: 2, y: 1 }} -\u003e ${\"vector2\"}\n    ${{ x: 1 }} -\u003e ${\"vector1\"}\n`;\n\nconsole.log(result); // \"vector1\"\n```\n\nIn this example, `import { MatchDSL } from \"draft-dsl-match\"` is necessary even `MatchDSL` is not used because `Λ` will be translated to `MatchDSL`.\n\n`Λ` is unicode [U+039B](https://www.compart.com/en/unicode/U+039B), it resembles caret and is used to introduce a new context where you can apply a DSL, and `\u003cstring\u003e` is used to assert the output type.\n\n\"match\" is the name of the DSL. You can customize it in `typedraft.config.ts`.\n\nThe syntax is straightforward, you put whatever you want to match in the first `${}`, and the `${}` at the left of `-\u003e` is pattern you want to match, the `${}` at the right is handler, putting values here implies `() =\u003e { return \u003cvalues\u003e; }`\n\nIt's hard to type `Λ`, thus we recommend that you can create [code snippet](https://code.visualstudio.com/docs/editor/userdefinedsnippets) in vscode so that you can type it in this way:\n\n![snippet](./assets/snippet.gif)\n\nThe snippet you may want to reuse:\n\n```js\n{\n    \"dsl\": {\n        \"scope\": \"javascript,typescript,typescriptreact\",\n        \"prefix\": \"dsl\",\n        \"body\": [\"Λ('$1')` $2`\"],\n        \"description\": \"use dsl in typedraft\"\n    }\n}\n```\n\n#### Array\n\n```ts\nimport { MatchDSL, String, __ } from \"draft-dsl-match\";\n\ntype Input = { title: string; content: string }[];\n\nlet input: Input = [\n    { title: \"Hello world!\", content: \"I‘m a very interesting content\" },\n    { title: \"Bonjour!\", content: \"I‘m a very interesting content too\" },\n];\n\nconst result = Λ(\"match\")` ${input}\n  ${[{ title: String, content: String }]} -\u003e ${\"a list of posts!\"}\n  ${__} -\u003e ${\"something else\"}\n`;\n\nconsole.log(result); // \"a list of posts!\"\n```\n\n`__` will match any value, and `String` is used to denote the `String` type.\n\n#### Tuple\n\n```ts\nimport { MatchDSL, Number } from \"draft-dsl-match\";\n\nconst sum = (args: number[]): number =\u003e Λ\u003cnumber\u003e(\"match\")` ${args} \n    ${[]} -\u003e ${0}\n    ${[Number, Number]} -\u003e ${([x, y]) =\u003e x + y}\n    ${[Number, Number, Number]} -\u003e ${([x, y, z]) =\u003e x + y + z}\n    ${[Number, Number, Number, Number]} -\u003e ${([x, y, z, w]) =\u003e x + y + z + w}\n`;\n\nconst result = sum([2, 3, 2, 4]);\nconsole.log(result); // 11\n```\n\nThen we can have function overloading in this way:\n\n```ts\nimport { MatchDSL, Number, String, __ } from \"draft-dsl-match\";\n\ntype ArgsType = [string, string] | [number, number] | [any, any];\ntype RetrunType = number | string | any;\n\nconst add = (args: ArgsType): RetrunType =\u003e Λ\u003cRetrunType\u003e(\"match\")` ${args as ArgsType} \n    ${[String, String]} -\u003e ${args =\u003e args.join(\" \")}\n    ${[Number, Number]} -\u003e ${([x, y]) =\u003e x + y}\n    ${[__, __]} -\u003e ${args =\u003e `the sum is args.join(\"-\"): ${args.join(\"-\")}`}\n`;\n\nconsole.log(add([\"hello\", \"world\"]));\nconsole.log(add([1, 2]));\nconsole.log(add([\"hi\", 1]));\n\n// hello world\n// 3\n// the sum is args.join(\"-\"): hi-1\n```\n\n#### Sets and Maps\n\n```ts\nimport { MatchDSL, __ } from \"draft-dsl-match\";\n\nconst ContainsGabAndYo = (set: Set\u003cstring | number\u003e) =\u003e Λ\u003c[boolean, boolean]\u003e(\"match\")` ${set}\n    ${new Set([\"gab\", \"yo\"])} -\u003e ${[true, true]}\n    ${new Set([\"gab\"])} -\u003e ${[true, false]}\n    ${new Set([\"yo\"])} -\u003e ${[false, true]}\n    ${__} -\u003e ${[false, false]}\n`;\n\nconsole.log(ContainsGabAndYo(new Set([\"gab\", \"yo\", \"hello\"])));\nconsole.log(ContainsGabAndYo(new Set([\"gab\", \"hello\"])));\nconsole.log(ContainsGabAndYo(new Set([\"yo\", \"hello\"])));\nconsole.log(ContainsGabAndYo(new Set([\"hello\"])));\nconsole.log(ContainsGabAndYo(new Set([])));\nconsole.log(ContainsGabAndYo(new Set([2])));\n\n// [ true, true ]\n// [ true, false ]\n// [ false, true ]\n// [ false, false ]\n// [ false, false ]\n// [ false, false ]\n```\n\n```ts\nimport { MatchDSL, String, __ } from \"draft-dsl-match\";\n\nconst users_map = new Map([\n    [\"gab\", { name: \"gabriel\" }],\n    [\"angégé\", { name: \"angéline\" }],\n]);\n\nconst user_pattern = { name: String };\n\nconst result = Λ(\"match\")` ${users_map as Map\u003cstring, { name: string }\u003e}\n    ${new Map([\n        [\"angégé\", user_pattern],\n        [\"gab\", user_pattern],\n    ])} -\u003e ${map =\u003e ({ name: map.get(\"angégé\")!.name + \" \" + map.get(\"gab\")!.name })}\n    ${new Map([[\"angégé\", user_pattern]])} -\u003e ${map =\u003e map.get(\"angégé\")}\n    ${new Map([[\"gab\", user_pattern]])} -\u003e ${map =\u003e map.get(\"gab\")}\n    ${__} -\u003e ${{ name: \"unknown\" }}\n`;\n\nconsole.log(result); // { name: 'angéline gabriel' }\n```\n\n### Predicate\n\nPredicate can also be used at the left side of `-\u003e`:\n\n```ts\nimport { MatchDSL, __ } from \"draft-dsl-match\";\n\nconst values = [\n    { value: 1, expected: false },\n    { value: -2, expected: false },\n    { value: 3, expected: false },\n    { value: 100, expected: false },\n    { value: 20, expected: true },\n    { value: 39, expected: true },\n];\n\nvalues.forEach(({ value, expected }) =\u003e {\n    const result = Λ\u003cboolean\u003e(\"match\")` ${value}\n        ${(x: number) =\u003e x \u003e 10 \u0026\u0026 x \u003c 50} -\u003e ${true}\n        ${__} -\u003e ${false}\n    `;\n    console.log(result === expected);\n});\n\n// true\n// true\n// true\n// true\n// true\n// true\n```\n\n### Properties selection\n\nYou can use matched value in this way:\n\n```ts\nimport { MatchDSL, __, use } from \"draft-dsl-match\";\n\nconst y = Λ(\"match\")` ${[\"get\", 2]}\n    ${[\"get\", use(\"y\")]} -\u003e ${(_, { y }) =\u003e y}\n`;\n\nconsole.log(y); // 2\n```\n\nThe second param of handler is an object that contains values you want to \"use\".\n\n## License\n\nDSL Match is [MIT licensed](https://github.com/mistlog/draft-dsl-match/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistlog%2Fdraft-dsl-match","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmistlog%2Fdraft-dsl-match","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmistlog%2Fdraft-dsl-match/lists"}