{"id":20046404,"url":"https://github.com/aneldev/dyna-switch","last_synced_at":"2025-09-10T20:36:09.589Z","repository":{"id":87577995,"uuid":"319012718","full_name":"aneldev/dyna-switch","owner":"aneldev","description":"Inline switch that returns value. Write complex switch in clean way.","archived":false,"fork":false,"pushed_at":"2023-09-28T07:35:49.000Z","size":189,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-18T19:16:54.499Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aneldev.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":"2020-12-06T11:06:03.000Z","updated_at":"2023-07-19T09:22:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"eb487203-3704-42c0-ad41-aa33efa4dea4","html_url":"https://github.com/aneldev/dyna-switch","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/aneldev/dyna-switch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-switch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-switch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-switch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-switch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aneldev","download_url":"https://codeload.github.com/aneldev/dyna-switch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-switch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267867803,"owners_count":24157357,"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","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2024-11-13T11:23:38.992Z","updated_at":"2025-07-30T12:33:18.637Z","avatar_url":"https://github.com/aneldev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dynaSwitch\n\n`dynaSwitch` combines the benefits of JavaScript's `switch` statement and allows inline value returns.\n\n# How to use JavaScript's switch statement for inline value\n\nSuppose we have the following themes:\n\n```typescript\ninterface ITheme {\n  color: string;\n  backgroundColor: string;\n}\n\nconst lightTheme: ITheme = {color: 'black', backgroundColor: 'white'};\nconst darkTheme: ITheme = {color: 'white', backgroundColor: 'black'};\nconst redTheme: ITheme = {color: 'red', backgroundColor: 'white'};\n```\n\nWe can write the following code to obtain the appropriate theme based on a string value:\n\n```typescript\nconst themeName: string = getUIThemeName();\n\nconst theme: ITheme = (() =\u003e {\n  switch (themeName) {      // The value that is going to be tested\n    case 'light':           // The cases\n      return lightTheme;\n    case 'dark':\n      return darkTheme;\n    case 'red':\n      return redTheme;\n    default:\n      return lightTheme;    // Default value\n  }\n})();\n\n```\n\n# Introducing `dynaSwitch`\n\nWith `dynaSwitch`, we can achieve the same result in a more concise manner:\n\n```typescript\nconst themeName: string = getUIThemeName();\n\nconst result = dynaSwitch\u003cITheme\u003e(\n  themeName,                // The value that is going to be tested    \n  lightTheme,               // Default value\n  {                         // Cases (as object)\n    light: lightTheme,\n    dark: darkTheme,\n    red: () =\u003e redTheme,    // The value would be returned by a function also\n  },\n);\n```\n\n**Benefits**\n\n- Organized like JavaScript's `switch` statement\n- Less code, more readable\n- Prevents accidental fall-through cases\n- Always returns a value (the second argument serves as the default value)\n- Provides type safety in TypeScript, ensuring the expected value is returned\n- The `cases` dictionary argument can be reused and can handle more complex objects.\n\n# Using `dynaSwitch` with functions\n\nIn this example, we utilize functions instead of values in the `dynaSwitch` cases. This allows us to have variables with the same names inside the functions, something not possible with the traditional `switch` statement unless closures are used. This feature is particularly useful in `Redux reducers`, where the cases often require the use of the same variable names.\n\n```typescript\nconst themeName: string = getUIThemeName();\n\nconst result = dynaSwitch\u003cITheme\u003e(\n  themeName,                // The value that is going to be tested    \n  lightTheme,               // Default value\n  {                         // Dictionary with the cases\n    light: () =\u003e {\n      const isUserAdmin: boolean = getUserAdmin();\n      const theme = { ...lightTheme };\n      theme.profile.color = 'red';\n      return theme;\n    },\n    dark: () =\u003e {\n      const isUserAdmin: boolean = getUserAdmin();\n      const theme = { ...darkTheme };\n      theme.profile.color = 'maroon';\n      return theme;\n    },\n    red: () =\u003e {\n      const isUserAdmin: boolean = getUserAdmin();\n      const theme = { ...redTheme };\n      theme.profile.color = 'pink';\n      return theme;\n    },\n  },\n);\n```\n\n# Introducing `dynaSwitchEnum`\n\nWith `dynaSwitchEnum`, you can easily achieve super type-safe values for each option of an Enum, thanks to TypeScript's internal `Record` feature. This means that if the Enum is modified in the future, you will receive TypeScript errors during compilation for any missing or modified Enum options.\n\n```typescript\nenum ETheme {\n  DARK = \"DARK\",\n  LIGHT = \"LIGHT\",\n  REDISH = \"REDISH\",\n}\n\ninterface ITheme {\n  color: string;\n  backgroundColor: string;\n}\n\nconst themeSetups: Record\u003cETheme, ITheme\u003e = {\n  [ETheme.LIGHT]: {\n    color: 'black', backgroundColor: 'white',\n  },\n  [ETheme.DARK]: {\n    color: 'white', backgroundColor: 'black',\n  },\n  [ETheme.REDISH]: {\n    color: 'red', backgroundColor: 'white',\n  },\n};\n\ndynaSwitchEnum\u003cETheme, ITheme\u003e(ETheme.LIGHT, themeSetups).color; // returns \"black\"\n\ndynaSwitchEnum\u003cETheme, ITheme\u003e(\"something\", themeSetups).color; // not compilable, due to TS!\n\n```\n\nAlternatively, you can define the dictionary directly. This is especially helpful when you want to convert an enum to a React component prop.\n\n```\ndynaSwitchEnum\u003cETheme, string\u003e(\n  ETheme.REDISH,\n  {\n    [ETheme.LIGHT]: 'Light',\n    [ETheme.DARK]: 'Darky',\n    [ETheme.REDISH]: 'Redish',\n  },\n); // This returns \"Redish\"!\n```\n\n**Benefits**\n\n- Provides super type safety for both the Enum and the returned value\n- Always returns a value\n\n# Introducing `dynaSwitchIf`\n\n`dynaSwitchIf` is similar to `dynaSwitch`, but instead of using a dictionary, it employs an array of \"if\" statements. The first valid \"if\" statement is executed. This approach makes it easier to assign values compared to using keys in a dictionary.\n\nThis `dynaSwitch` implementation works like an \"if-else\" statement.\n\nExample:\n\n```typescript\nconst themeName = 'dark';\n\nconst theme = dynaSwitchIf\u003cITheme\u003e(\n  themeName,                                // The value that is going to be tested\n  lightTheme,                               // Default value\n  [\n    {if: 'light', then: lightTheme},\n    {if: 'dark', then: () =\u003e darkTheme},    // The `then` would be a function\n    {if: 'red', then: redTheme},\n  ],\n);\n\nexpect(theme.color).toBe('white');\n```\n\n# API\n\nTypeScript signatures of the methods:\n\n## dynaSwitch\n\n```typescript\nexport interface IDynaSwitchCasesDic\u003cTResult = any\u003e {\n  [enumCase: string]: TResult | (() =\u003e TResult);\n}\n\nexport const dynaSwitch = \u003cTResult = any, TTestValue = string | number\u003e(\n  testValue: TTestValue,\n  defaultValue: TResult | (() =\u003e TResult),\n  cases: IDynaSwitchCasesDic\u003cTResult\u003e,\n) =\u003e TResult;\n```\n\n## dynaSwitchEnum\n\n```typescript\nexport const dynaSwitchEnum = \u003cTEnum extends string | number | symbol, TResult\u003e(\n  testValue: TEnum,\n  cases: Record\u003cTEnum, TResult\u003e,\n) =\u003e TResult;\n```\n\n## dynaSwitchIf\n\n```typescript\nexport type TDynamicValue\u003cT = any\u003e = T | (() =\u003e T);\n\nexport const dynaSwitchIf = \u003cTResult = any, TTestValue = any\u003e(\n  testValue: TTestValue,\n  defaultValue: TDynamicValue\u003cTResult\u003e,\n  cases: { if: TDynamicValue\u003cTTestValue\u003e; then: TDynamicValue\u003cTResult\u003e }[],\n) =\u003e TResult;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faneldev%2Fdyna-switch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faneldev%2Fdyna-switch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faneldev%2Fdyna-switch/lists"}