{"id":17349199,"url":"https://github.com/co2-git/_switch","last_synced_at":"2025-03-27T11:44:20.361Z","repository":{"id":68259747,"uuid":"80306162","full_name":"co2-git/_switch","owner":"co2-git","description":"Functional replacement of switch in JavaScript","archived":false,"fork":false,"pushed_at":"2017-01-28T19:43:36.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T16:23:12.319Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/co2-git.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2017-01-28T19:40:00.000Z","updated_at":"2017-01-28T19:42:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"2f9f8902-72ad-482b-9c39-24a86aec6cb6","html_url":"https://github.com/co2-git/_switch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co2-git%2F_switch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co2-git%2F_switch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co2-git%2F_switch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/co2-git%2F_switch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/co2-git","download_url":"https://codeload.github.com/co2-git/_switch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245841689,"owners_count":20681184,"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":[],"created_at":"2024-10-15T16:55:01.856Z","updated_at":"2025-03-27T11:44:20.329Z","avatar_url":"https://github.com/co2-git.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"underscore-switch\n===\n\n`_switch` is a functional programming substitute for `switch` statement in JavaScript. It aims at more readability and reasoning. It comes at a small cost since it uses a function instead of native condition blocks. At the end, it comes down to each one's own preferences. For some, it would make no sense sugar-coding a language construct - to others, it helps reason about their scope variables by always using functions.\n\n# Install\n\n```bash\nnpm i -S underscore-switch\n```\n\n# Usage\n\nThe most straight-forward usage is to switch string cases via an object which keys match the possible cases:\n\n```javascript\nimport _switch from 'underscore-switch';\n\nconst role = 'ADMIN';\n\n_switch(role, {ADMIN: true, DEVELOPER: false}); // true\n```\n\nIf you have cases that can not be translated into strings - such as numbers, you would use an array.\n\n```javascript\nimport _switch from 'underscore-switch';\n\nconst roles = {ADMIN: 1, DEVELOPER: 2};\nconst role = 1;\n\n_switch(role, [\n  {case: roles.ADMIN, then: () =\u003e true},\n  {case: roles.DEVELOPER, then: () =\u003e false},\n]); // true\n```\n\nYou can use an array for your cases as well to specify the same behavior for various cases:\n\n```javascript\nimport _switch from 'underscore-switch';\n\nconst roles = {ADMIN: 1, DEVELOPER: 2, TESTER: 3};\nconst role = 1;\n\n_switch(role, [\n  {case: roles.ADMIN, then: () =\u003e true},\n  {case: [roles.DEVELOPER, roles.TESTER], then: () =\u003e false},\n]); // true\n```\n\nYou can specify a default value as the third argument which has to be a function:\n\n```javascript\nimport _switch from 'underscore-switch';\n\nconst myVar = 'barz';\n\n_switch(myVar, {foo: 1, bar: 2}, () =\u003e 3); // 3\n```\n\n# TODO\n\n- Symbol support\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fco2-git%2F_switch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fco2-git%2F_switch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fco2-git%2F_switch/lists"}