{"id":21918032,"url":"https://github.com/zefir-git/enhancedswitch","last_synced_at":"2025-09-10T08:33:14.119Z","repository":{"id":193398378,"uuid":"688721765","full_name":"zefir-git/EnhancedSwitch","owner":"zefir-git","description":"An enhanced switch, similar to Java","archived":false,"fork":false,"pushed_at":"2025-04-07T15:28:53.000Z","size":337,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T16:40:14.212Z","etag":null,"topics":["enhanced-switch","flow-control","switch"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zefir-git.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-08T00:57:15.000Z","updated_at":"2025-04-07T15:28:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"5e669fa4-2948-4415-a9c1-706efd579279","html_url":"https://github.com/zefir-git/EnhancedSwitch","commit_stats":null,"previous_names":["zefir-git/enhancedswitch"],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zefir-git%2FEnhancedSwitch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zefir-git%2FEnhancedSwitch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zefir-git%2FEnhancedSwitch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zefir-git%2FEnhancedSwitch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zefir-git","download_url":"https://codeload.github.com/zefir-git/EnhancedSwitch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249685275,"owners_count":21310576,"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":["enhanced-switch","flow-control","switch"],"created_at":"2024-11-28T19:47:01.104Z","updated_at":"2025-09-10T08:33:14.103Z","avatar_url":"https://github.com/zefir-git.png","language":"TypeScript","readme":"# EnhancedSwitch\n\n[![npm](https://img.shields.io/npm/v/enhanced-switch)](https://www.npmjs.com/package/enhanced-switch)\n[![npm downloads](https://img.shields.io/npm/dt/enhanced-switch?label=downloads)](https://www.npmjs.com/package/enhanced-switch)\n[![Tests](https://github.com/zefir-git/EnhancedSwitch/actions/workflows/test.yml/badge.svg)](https://github.com/zefir-git/EnhancedSwitch/actions/workflows/test.yml)\n[![CodeQL](https://github.com/zefir-git/EnhancedSwitch/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/zefir-git/EnhancedSwitch/actions/workflows/github-code-scanning/codeql)\n\nAn enhanced switch, similar to Java.\n\nThis library targets Node.js 16, 18 and 20, but can also work in the browser.\n\n## Installation\n```sh\nnpm i enhanced-switch\n```\n\n## Usage\n\n### TypeScript\n\n```js\nimport EnhancedSwitch from \"enhanced-switch\";\n\nconst value = 2;\n\nnew EnhancedSwitch(value)\n    .case(1, () =\u003e console.log(\"one\"))\n    .case(2, () =\u003e console.log(\"two\"))\n    .default(() =\u003e console.log(\"default\"));\n\nconst result = new EnhancedSwitch\u003cnumber, string\u003e(value)\n    .case(1, () =\u003e \"one\")\n    .case(2, () =\u003e \"two\")\n    .default(() =\u003e \"default\").value;\n```\n\n\u003cdetails\u003e\n\u003csummary\u003e\n\n### JavaScript\n\u003c/summary\u003e\n\n\n```js\nimport EnhancedSwitch from \"enhanced-switch\";\n\nconst value = 2;\n\nnew EnhancedSwitch(value)\n    .case(1, () =\u003e console.log(\"one\"))\n    .case(2, () =\u003e console.log(\"two\"))\n    .default(() =\u003e console.log(\"default\"));\n\nconst result = new EnhancedSwitch(value)\n    .case(1, () =\u003e \"one\")\n    .case(2, () =\u003e \"two\")\n    .default(() =\u003e \"default\").value;\n```\n\u003c/details\u003e\n\n## Licence\n\nCopyright © 2023–2025 Zefir Kirilov.\n\nThis project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0). A copy of the licence text is included in this repository. If not, see https://www.gnu.org/licenses/.\n\n# Documentation\n\n\u003cdetails open\u003e\n\u003csummary\u003eTable of Contents\u003c/summary\u003e\n\n- [Class `EnhancedSwitch\u003cT, U\u003e`](#class-enhancedswitcht-u)\n  - [`new EnhancedSwitch(expression: T)`](#new-enhancedswitchexpression-t)\n  - [`enhancedSwitch.allowFallthrough`](#enhancedswitchallowfallthrough)\n  - [`enhancedSwitch.break()`](#enhancedswitchbreak)\n  - [`enhancedSwitch.case(valueN: T | T[], code: U)`](#enhancedswitchcasevaluen-t--t-code-u)\n  - [`enhancedSwitch.case(valueN: T | T[], code: (s: EnhancedSwitch\u003cT, U\u003e) =\u003e U | void)`](#enhancedswitchcasevaluen-t--t-code-s-enhancedswitcht-u--u--void)\n  - [`enhancedSwitch.default(code: U)`](#enhancedswitchdefaultcode-u)\n  - [`enhancedSwitch.default(code: (s: EnhancedSwitch\u003cT, U\u003e) =\u003e U | void)`](#enhancedswitchdefaultcode-s-enhancedswitcht-u--u--void)\n  - [`enhancedSwitch.default(code: U, returnvalueN: true)`](#enhancedswitchdefaultcode-u-returnvaluen-true)\n  - [`enhancedSwitch.default(code: (s: EnhancedSwitch\u003cT, U\u003e) =\u003e U | void, returnvalueN: true)`](#enhancedswitchdefaultcode-s-enhancedswitcht-u--u--void-returnvaluen-true)\n  - [`enhancedSwitch.expression`](#enhancedswitchexpression)\n  - [`enhancedSwitch.hasConcluded`](#enhancedswitchhasconcluded)\n  - [`enhancedSwitch.value`](#enhancedswitchvalue)\n\n## Class `EnhancedSwitch\u003cT, U\u003e`\n\nThe EnhancedSwitch evaluates an expression, matching the expression's value against a series of `case` clauses, and executes the statements after the first `case` clause with a matching value. The `default` clause of an EnhancedSwitch will be jumped to if no case matches the expression's value.\n\n| Template parameter | Description            |\n|--------------------|------------------------|\n| `T`                | Switch expression type |\n| `U`                | Switch return type     |\n\n### `new EnhancedSwitch(expression: T)`\n\nCreate a new EnhancedSwitch\n\n| Parameter    | Type | Description                                                       |\n|--------------|------|-------------------------------------------------------------------|\n| `expression` | `T`  | An expression whose result is matched against each `case` clause. |\n\n### `enhancedSwitch.allowFallthrough`\n\nWhether to allow fallthrough. If true, the switch will continue to execute `case` clauses after the first match until a `break` is encountered. Defaults to false.\n\nType: [`boolean`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean). Read-only.\n\n### `enhancedSwitch.break()`\n\nPrevent further execution of any subsequent `case` or `default` clauses.\n\nReturns: `this` ([`EnhancedSwitch\u003cT, U\u003e`](#class-enhancedswitcht-u))\n\n### `enhancedSwitch.case(valueN: T | T[], code: U)`\n\nA `case` clause used to match against `expression`. If the `expression` matches the specified `valueN` (which can be any expression), this case clause is executed.\n\n| Parameter | Type | Description                                                                                                    |\n|-----------|------|----------------------------------------------------------------------------------------------------------------|\n| `valueN`  | `T`  | The `case` clause is executed if the expression matches this value, or at least one value if this is an array. |\n| `code`    | `U`  | The value to return if the case matches.                                                                       |\n\nReturns: `this` ([`EnhancedSwitch\u003cT, U\u003e`](#class-enhancedswitcht-u))\n\n### `enhancedSwitch.case(valueN: T | T[], code: (s: EnhancedSwitch\u003cT, U\u003e) =\u003e U | void)`\n\nA `case` clause used to match against `expression`. If the `expression` matches the specified `valueN` (which can be any expression), this case clause is executed.\n\n| Parameter | Type                                     | Description                                                                                                    |\n|-----------|------------------------------------------|----------------------------------------------------------------------------------------------------------------|\n| `valueN`  | `T`                                      | The `case` clause is executed if the expression matches this value, or at least one value if this is an array. |\n| `code`    | `(s: EnhancedSwitch\u003cT, U\u003e) =\u003e U \\| void` | The function to run if the case matches.                                                                       |\n\nReturns: `this` ([`EnhancedSwitch\u003cT, U\u003e`](#class-enhancedswitcht-u))\n\n### `enhancedSwitch.default(code: U)`\n\nA `default` clause; if provided, this clause is executed if the value of `expression` does not match any of the `case` clauses. An EnhancedSwitch can only have one `default` clause.\n\n| Parameter | Type | Description                             |\n|-----------|------|-----------------------------------------|\n| `code`    | `U`  | The value to return if no case matches. |\n\nReturns: `this` ([`EnhancedSwitch\u003cT, U\u003e`](#class-enhancedswitcht-u))\n\n### `enhancedSwitch.default(code: (s: EnhancedSwitch\u003cT, U\u003e) =\u003e U | void)`\n\nA `default` clause; if provided, this clause is executed if the value of `expression` does not match any of the `case` clauses. An EnhancedSwitch can only have one `default` clause.\n\n| Parameter | Type                                     | Description                              |\n|-----------|------------------------------------------|------------------------------------------|\n| `code`    | `(s: EnhancedSwitch\u003cT, U\u003e) =\u003e U \\| void` | The function to run if no case matches.  |\n\nReturns: `this` ([`EnhancedSwitch\u003cT, U\u003e`](#class-enhancedswitcht-u))\n\n### `enhancedSwitch.default(code: U, returnvalueN: true)`\n\nA `default` clause; if provided, this clause is executed if the value of `expression` does not match any of the `case` clauses. An EnhancedSwitch can only have one `default` clause.\n\n| Parameter      | Type   | Description                                                             |\n|----------------|--------|-------------------------------------------------------------------------|\n| `code`         | `U`    | The value to return if no case matches.                                 |\n| `returnvalueN` | `true` | If true, the provided value is returned instead of the switch instance. |\n\nReturns: `U`\n\n### `enhancedSwitch.default(code: (s: EnhancedSwitch\u003cT, U\u003e) =\u003e U | void, returnvalueN: true)`\n\nA `default` clause; if provided, this clause is executed if the value of `expression` does not match any of the `case` clauses. An EnhancedSwitch can only have one `default` clause.\n\n| Parameter      | Type                                     | Description                                                             |\n|----------------|------------------------------------------|-------------------------------------------------------------------------|\n| `code`         | `(s: EnhancedSwitch\u003cT, U\u003e) =\u003e U \\| void` | The function to run if no case matches.                                 |\n| `returnvalueN` | `true`                                   | If true, the provided value is returned instead of the switch instance. |\n\nReturns: `U`\n\n### `enhancedSwitch.expression`\n\nAn expression whose result is matched against each `case` clause.\n\nType: `T`. Read-only.\n\n### `enhancedSwitch.hasConcluded`\n\nWhether the switch statement has concluded (no further `case` or `default` will be executed). A switch can be concluded by calling [`enhancedSwitch.break()`](#enhancedswitchbreak) or constructing with `allowFallthrough` set to false.\n\nType: [`boolean`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean). Read-only.\n\n### `enhancedSwitch.value`\n\nThe result of the switch statement.\n\n| Throws                                                                                                    | When                                                                                                                                     |\n|-----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|\n| [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) | If the switch statement has no result, e.g. there is no `default` clause and no `case` clause matches the expression or returns a value. |\n\nType: `U`. Read-only.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzefir-git%2Fenhancedswitch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzefir-git%2Fenhancedswitch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzefir-git%2Fenhancedswitch/lists"}