{"id":27890321,"url":"https://github.com/bensimmers/matchly","last_synced_at":"2025-06-23T15:35:38.711Z","repository":{"id":288607221,"uuid":"968640536","full_name":"BenSimmers/Matchly","owner":"BenSimmers","description":"TypeScript utility for pattern matching in typescript - Inspired by F# match","archived":false,"fork":false,"pushed_at":"2025-06-02T11:19:28.000Z","size":91,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-02T21:19:49.523Z","etag":null,"topics":["functional-programming","library","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/matchly-patterns","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/BenSimmers.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-18T13:02:54.000Z","updated_at":"2025-06-02T11:18:40.000Z","dependencies_parsed_at":"2025-04-19T02:43:05.518Z","dependency_job_id":"0fa698ea-01e6-4563-a874-5d58772fc4a2","html_url":"https://github.com/BenSimmers/Matchly","commit_stats":null,"previous_names":["bensimmers/matchly"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/BenSimmers/Matchly","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenSimmers%2FMatchly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenSimmers%2FMatchly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenSimmers%2FMatchly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenSimmers%2FMatchly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BenSimmers","download_url":"https://codeload.github.com/BenSimmers/Matchly/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BenSimmers%2FMatchly/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261505257,"owners_count":23168996,"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":["functional-programming","library","typescript"],"created_at":"2025-05-05T10:46:48.920Z","updated_at":"2025-06-23T15:35:38.687Z","avatar_url":"https://github.com/BenSimmers.png","language":"TypeScript","readme":"# Matchly\n\n![npm](https://img.shields.io/npm/v/matchly-patterns)\n![license](https://img.shields.io/github/license/bensimmers/matchly)\n![build](https://img.shields.io/github/actions/workflow/status/bensimmers/matchly/main.yml?branch=main)\n\n**Matchly** is a simple, type-safe, and expressive data driven pattern matching library for TypeScript. It allows you to write clean and declarative code for handling conditional logic. Matchly is inspired by the F# `match` expression.\n\n## Features\n\n- **Type-Safe**: Fully leverages TypeScript's type system for safety and autocompletion.\n- **Expressive API**: Write clean and readable conditional logic.\n- **Debugging Support**: Enable debug mode to log matched cases.\n- **Safe Mode**: Handle errors gracefully in predicates or result functions.\n\n---\n\n## Installation\n\nInstall Matchly via npm or pnpm:\n\n```bash\nnpm install matchly-patterns\n# or\npnpm add matchly-patterns\n```\n\n---\n\n## Usage\n\nMatchly provides a fluent API for pattern matching. Here's an example:\n\n```typescript\nimport { matchly } from 'matchly-patterns';\n\n// react example\nconst renderContent = matchly(status)\n  .when(status =\u003e status === Status.IDLE, () =\u003e \u003cIdle /\u003e)\n  .when(status =\u003e status === Status.ERROR, () =\u003e \u003cError /\u003e)\n  .when(status =\u003e status === Status.LOADING, () =\u003e \u003cLoading /\u003e)\n  .when(status =\u003e status === Status.SUCCESS, () =\u003e {\n    return todos.map(todo =\u003e (\n      \u003cReact.Fragment key={todo.id}\u003e\n        \u003cTodo key={todo.id} {...todo} toggleTodo={toggleTodo} /\u003e\n      \u003c/React.Fragment\u003e\n    ));\n  })\n  .otherwise(() =\u003e \u003cdiv\u003eUnknown status\u003c/div\u003e);\n\nconst result = renderContent();\n\n// complex example\nconst data = {\n  name: 'John Doe',\n  age: 30,\n  hobbies: ['reading', 'gaming'],\n};\n\nconst result = matchly(data)\n  .when(data =\u003e data.age \u003e 18, data =\u003e `${data.name} is an adult`)\n  .when(data =\u003e data.hobbies.includes('gaming'), data =\u003e `${data.name} loves gaming`)\n  .otherwise(() =\u003e 'No match found');\n\nconsole.log(result); // Output: \"John Doe loves gaming\"\n```\n\n---\n\n## API\n\n### `matchly(value: T): Matcher\u003cT, never\u003e`\n\nCreates a new `Matcher` instance for the given value.\n\n### Matcher Methods\n\n#### `.when(predicate: Predicate\u003cT\u003e, result: Result\u003cT, R\u003e): Matcher\u003cT, R\u003e`\n\nAdds a case to the matcher. If the `predicate` returns `true`, the `result` function is executed.\n\n#### `.otherwise(defaultResult?: Result\u003cT, R\u003e): R`\n\nSpecifies the default case to execute if no other cases match. Throws an error if no default case is provided.\n\n#### `.enableDebug(): Matcher\u003cT, R\u003e`\n\nEnables debug mode, logging matched cases to the console.\n\n#### `.enableSafeMode(): Matcher\u003cT, R\u003e`\n\nEnables safe mode, catching and logging errors in predicates or result functions.\n\n---\n\n## Example: Number Matching\n\n```typescript\nimport { matchly } from \"matchly-patterns\";\n\nconst result = matchly(5)\n  .when(\n    (x) =\u003e x \u003e 10,\n    (x) =\u003e `${x} is greater than 10`,\n  )\n  .when(\n    (x) =\u003e x \u003c 3,\n    (x) =\u003e `${x} is less than 3`,\n  )\n  .otherwise((x) =\u003e `${x} is between 3 and 10`);\n\nconsole.log(result); // Output: \"5 is between 3 and 10\"\n```\n\n---\n\n## Debugging and Safe Mode\n\n### Debug Mode\n\nEnable debug mode to log matched cases:\n\n```typescript\nmatchly(5)\n  .enableDebug()\n  .when(\n    (x) =\u003e x \u003e 3,\n    (x) =\u003e `${x} is greater than 3`,\n  )\n  .otherwise(() =\u003e \"No match\");\n```\n\n### Safe Mode\n\nEnable safe mode to handle errors gracefully:\n\n```typescript\nmatchly(5)\n  .enableSafeMode()\n  .when(\n    () =\u003e {\n      throw new Error(\"Error in predicate\");\n    },\n    () =\u003e \"This won't run\",\n  )\n  .otherwise(() =\u003e \"Fallback case\");\n```\n\n---\n\n## Testing\n\nMatchly is tested using [Vitest](https://vitest.dev). To run tests:\n\n```bash\npnpm test\n```\n\nTo watch for changes and re-run tests:\n\n```bash\npnpm test:watch\n```\n\n## Contributing\n\nContributions are welcome! Please follow these steps:\n\n1. Fork the repository.\n2. Create a new branch for your feature or bugfix.\n3. If you want your changes to be included in the next version please ensure you add a changeset entry with `npx changeset` on your branch. When your branch is merged, the changeset will be used to create a new version.\n4. Submit a pull request with a clear description of your changes.\n\n---\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n\n---\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbensimmers%2Fmatchly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbensimmers%2Fmatchly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbensimmers%2Fmatchly/lists"}