{"id":28939222,"url":"https://github.com/ehmicky/switch-functional","last_synced_at":"2026-03-04T22:31:59.211Z","repository":{"id":294773281,"uuid":"988035592","full_name":"ehmicky/switch-functional","owner":"ehmicky","description":"Functional switch statement","archived":false,"fork":false,"pushed_at":"2026-03-01T18:17:11.000Z","size":1693,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-01T20:41:55.246Z","etag":null,"topics":["case","default","filter","function","functional","functional-programming","functional-testing","functions","javascript","library","logic","logic-programming","mapping","nodejs","select","switch","switch-case","switch-case-statements","typescript","wrapper"],"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/ehmicky.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-05-22T00:49:38.000Z","updated_at":"2026-03-01T18:17:08.000Z","dependencies_parsed_at":"2025-05-22T01:34:35.299Z","dependency_job_id":"2c76620f-25fb-4bc6-ad3e-fffc68bce951","html_url":"https://github.com/ehmicky/switch-functional","commit_stats":null,"previous_names":["ehmicky/switch-functional"],"tags_count":2,"template":false,"template_full_name":"ehmicky/template-typescript","purl":"pkg:github/ehmicky/switch-functional","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fswitch-functional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fswitch-functional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fswitch-functional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fswitch-functional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ehmicky","download_url":"https://codeload.github.com/ehmicky/switch-functional/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehmicky%2Fswitch-functional/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30096766,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T21:59:23.547Z","status":"ssl_error","status_checked_at":"2026-03-04T21:57:50.415Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","default","filter","function","functional","functional-programming","functional-testing","functions","javascript","library","logic","logic-programming","mapping","nodejs","select","switch","switch-case","switch-case-statements","typescript","wrapper"],"created_at":"2025-06-23T00:07:16.870Z","updated_at":"2026-03-04T22:31:59.165Z","avatar_url":"https://github.com/ehmicky.png","language":"TypeScript","readme":"[![Node](https://img.shields.io/badge/-Node.js-808080?logo=node.js\u0026colorA=404040\u0026logoColor=66cc33)](https://www.npmjs.com/package/switch-functional)\n[![Browsers](https://img.shields.io/badge/-Browsers-808080?logo=firefox\u0026colorA=404040)](https://unpkg.com/switch-functional?module)\n[![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript\u0026colorA=404040\u0026logoColor=0096ff)](/src/main.ts)\n[![Codecov](https://img.shields.io/badge/-Tested%20100%25-808080?logo=codecov\u0026colorA=404040)](https://codecov.io/gh/ehmicky/switch-functional)\n[![Minified size](https://img.shields.io/bundlephobia/minzip/switch-functional?label\u0026colorA=404040\u0026colorB=808080\u0026logo=webpack)](https://bundlephobia.com/package/switch-functional)\n[![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon\u0026colorA=404040\u0026logoColor=9590F9)](https://fosstodon.org/@ehmicky)\n[![Medium](https://img.shields.io/badge/-Medium-808080.svg?logo=medium\u0026colorA=404040)](https://medium.com/@ehmicky)\n\nFunctional switch statement.\n\n# Features\n\n- [Deep equality testing](#testing-properties)\n- Advanced [testing](#testing-input) and\n  [manipulation](#returning-dynamic-values)\n- Add your own domain-specific [conditions](#custom-conditions) and\n  [transforms](#custom-transforms)\n- [Strictly typed](/src/main.ts)\n\n# Examples\n\n## Basic usage\n\n```js\nimport switchFunctional from 'switch-functional'\n\nconst getUserType = (user) =\u003e\n  switchFunctional(user.type)\n    .case('dev', 'developer')\n    .case(['admin', 'owner'], 'administrator')\n    .default('unknown')\n```\n\nThis is equivalent to:\n\n\u003c!-- eslint-disable no-restricted-syntax, no-fallthrough --\u003e\n\n```js\nconst getUserType = (user) =\u003e {\n  switch (user.type) {\n    case 'dev': {\n      return 'developer'\n    }\n\n    case 'admin':\n\n    case 'owner': {\n      return 'administrator'\n    }\n\n    default: {\n      return 'unknown'\n    }\n  }\n}\n```\n\n## Testing input\n\n```js\nconst getUserType = (user) =\u003e\n  switchFunctional(user)\n    .case(isDeveloper, 'developer')\n    .case([isAdmin, isOwner], 'admin')\n    .default('unknown')\n```\n\nThis is equivalent to:\n\n```js\nconst getUserType = (user) =\u003e {\n  if (isDeveloper(user)) {\n    return 'developer'\n  }\n\n  if (isAdmin(user) || isOwner(user)) {\n    return 'admin'\n  }\n\n  return 'unknown'\n}\n```\n\n## Testing properties\n\n```js\nconst getUserType = (user) =\u003e\n  switchFunctional(user)\n    // Checks `user.hasDevProjects === true`\n    .case({ hasDevProjects: true }, 'developer')\n    // Checks for deep properties\n    .case({ devProjectsCount: 0, permissions: { admin: true } }, 'admin')\n    .default('unknown')\n```\n\n## Returning dynamic values\n\n\u003c!-- eslint-disable no-shadow --\u003e\n\n```js\nconst getUserType = (user) =\u003e\n  switchFunctional(user)\n    .case(isDeveloper, (user) =\u003e user.developerType)\n    .case(isAdmin, (user) =\u003e user.adminType)\n    .default((user) =\u003e user.genericType)\n```\n\n## Custom conditions\n\n```js\nimport { Admin, Developer } from './user-classes.js'\n\n// Augment the `.case()` syntax to support domain-specific conditions.\n// In this example, this allows conditions to be user classes.\nconst mapCondition = (condition) =\u003e\n  USER_CLASSES.has(condition) ? (user) =\u003e user instanceof condition : condition\n\nconst USER_CLASSES = new Set([Admin, Developer])\n\nexport const customSwitch = (user) =\u003e switchFunctional(user, { mapCondition })\n```\n\n```js\nimport { customSwitch } from './custom-switch.js'\nimport { Admin, Developer } from './user-classes.js'\n\nconst getUserType = (user) =\u003e\n  customSwitch(user)\n    .case(Developer, 'developer')\n    .case(Admin, 'admin')\n    .default('unknown')\n```\n\n## Custom transforms\n\n```js\n// Augment the `.case()` and `.default()` syntax to support domain-specific\n// logic applied on the return values.\n// In this example, the return value is kept as is. However, it is logged.\nconst mapReturnValues = (returnValue) =\u003e {\n  console.log(returnValue)\n  return returnValue\n}\n\nexport const customSwitch = (user) =\u003e\n  switchFunctional(user, { mapReturnValues })\n```\n\n```js\nimport { customSwitch } from './custom-switch.js'\n\n// 'developer', 'admin' or 'unknown' will be logged\nconst getUserType = (user) =\u003e\n  customSwitch(user)\n    .case(isDeveloper, 'developer')\n    .case(isAdmin, 'admin')\n    .default('unknown')\n```\n\n# Install\n\n```bash\nnpm install switch-functional\n```\n\nThis package works in both Node.js \u003e=18.18.0 and\n[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/browserslist).\n\nThis is an ES module. It must be loaded using\n[an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c),\nnot `require()`. If TypeScript is used, it must be configured to\n[output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html),\nnot CommonJS.\n\n# API\n\n## switchFunctional(input, options?)\n\n`input`: `unknown`\\\n`options?`: [`Options`](#options)\\\n_Return value_: [`Switch`](#switchcaseconditions-casereturnvalue)\n\nFunctional switch statement. This must be chained with\n[`.case()`](#switchcaseconditions-casereturnvalue) statements and end with\n[`.default()`](#switchdefaultdefaultreturnvalue).\n\n## Switch.case(conditions, caseReturnValue)\n\n`conditions`: [`Condition | Condition[]`](#conditions)\\\n`caseReturnValue`: `unknown | (input) =\u003e unknown`\\\n_Return value_: [`Switch`](#switchcaseconditions-casereturnvalue)\n\nIf the `input` matches the `conditions`, the final return value will be\n`caseReturnValue`.\n\n`caseReturnValue` can optionally be a function taking the `input` as argument.\n\n## Switch.default(defaultReturnValue)\n\n`defaultReturnValue`: `unknown | (input) =\u003e unknown`\\\n_Return value_: `unknown`\n\nIf one of the [`.case()`](#switchcaseconditions-casereturnvalue) statements\nmatched, returns its `caseReturnValue`. Else, returns `defaultReturnValue`.\n\n`defaultReturnValue` can optionally be a function taking the `input` as\nargument.\n\n## Conditions\n\nThe `conditions` can be:\n\n- Any value, checked for equality with\n  [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)\n- An object containing of subset of properties\n- A filtering function taking the `input` as argument and returning a boolean\n- A boolean\n- An array of the above types, checking if _any_ condition in the array matches\n\n## Options\n\n_Type_: `object`\n\n### mapCondition\n\n_Type_: `(unknown) =\u003e Condition`\n\nFunction mapping each value passed to\n[`.case(value)`](#switchcaseconditions-casereturnvalue) or `.case(value[])`.\n\nCan return any value [condition](#conditions), including a function taking the\n`input` as argument. Cannot return an array of conditions.\n\nThis allows augmenting the syntax of `.case()` to support domain-specific\n[custom conditions](#custom-conditions).\n\n### mapReturnValues\n\n_Type_: `(...unknown[]) =\u003e unknown | (input) =\u003e unknown`\n\nFunction mapping each return value passed to\n[`.case(..., caseReturnValue)`](#switchcaseconditions-casereturnvalue) or\n[`.default(defaultReturnValue)`](#switchdefaultdefaultreturnvalue).\n\nCan return any value, including a function taking the `input` as argument.\n\nCan have multiple parameters: this allows calling `.case()` and `.default()`\nwith multiple arguments.\n\nThis allows augmenting the syntax of `.case()` and `.default()` to support\ndomain-specific [custom transforms](#custom-transforms).\n\n# Related projects\n\n- [`modern-errors-switch`](https://github.com/ehmicky/modern-errors-switch):\n  Execute class-specific logic\n\n# Support\n\nFor any question, _don't hesitate_ to [submit an issue on GitHub](../../issues).\n\nEveryone is welcome regardless of personal background. We enforce a\n[Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and\ninclusive environment.\n\n# Contributing\n\nThis project was made with ❤️. The simplest way to give back is by starring and\nsharing it online.\n\nIf the documentation is unclear or has a typo, please click on the page's `Edit`\nbutton (pencil icon) and suggest a correction.\n\nIf you would like to help us fix a bug or add a new feature, please check our\n[guidelines](CONTRIBUTING.md). Pull requests are welcome!\n\n\u003c!-- Thanks go to our wonderful contributors: --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START --\u003e\n\u003c!-- prettier-ignore --\u003e\n\u003c!--\n\u003ctable\u003e\u003ctr\u003e\u003ctd align=\"center\"\u003e\u003ca href=\"https://fosstodon.org/@ehmicky\"\u003e\u003cimg src=\"https://avatars2.githubusercontent.com/u/8136211?v=4\" width=\"100px;\" alt=\"ehmicky\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eehmicky\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/ehmicky/switch-functional/commits?author=ehmicky\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"#design-ehmicky\" title=\"Design\"\u003e🎨\u003c/a\u003e \u003ca href=\"#ideas-ehmicky\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e \u003ca href=\"https://github.com/ehmicky/switch-functional/commits?author=ehmicky\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e\n --\u003e\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehmicky%2Fswitch-functional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fehmicky%2Fswitch-functional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehmicky%2Fswitch-functional/lists"}