{"id":21186609,"url":"https://github.com/fetchte/switchreducer","last_synced_at":"2026-04-06T08:31:15.680Z","repository":{"id":65510408,"uuid":"105581267","full_name":"fetchTe/switchreducer","owner":"fetchTe","description":"A small but useful redux switch case helper","archived":false,"fork":false,"pushed_at":"2017-10-02T21:03:47.000Z","size":187,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-02T23:18:31.425Z","etag":null,"topics":["helper","react","reducer","redux"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fetchTe.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-02T20:25:19.000Z","updated_at":"2017-10-23T19:29:35.000Z","dependencies_parsed_at":"2023-01-26T18:01:33.072Z","dependency_job_id":null,"html_url":"https://github.com/fetchTe/switchreducer","commit_stats":null,"previous_names":["fetchte/switchreducer","artisin/switchreducer"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fetchTe/switchreducer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fetchTe%2Fswitchreducer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fetchTe%2Fswitchreducer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fetchTe%2Fswitchreducer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fetchTe%2Fswitchreducer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fetchTe","download_url":"https://codeload.github.com/fetchTe/switchreducer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fetchTe%2Fswitchreducer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31464604,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-05T21:22:52.476Z","status":"online","status_checked_at":"2026-04-06T02:00:07.287Z","response_time":112,"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":["helper","react","reducer","redux"],"created_at":"2024-11-20T18:24:39.081Z","updated_at":"2026-04-06T08:31:15.665Z","avatar_url":"https://github.com/fetchTe.png","language":"JavaScript","readme":"# switchreducerr\n[![npm](https://img.shields.io/npm/l/switchreducer.svg)](https://github.com/artisin/switchreducer/blob/master/LICENSE.txt)\n[![npm](https://img.shields.io/npm/v/switchreducer.svg)](https://www.npmjs.com/package/switchreducer)\n[![wercker status](https://app.wercker.com/status/e388b87aecbfc99fb4b0abb9c16a602c/s/master \"wercker status\")](https://app.wercker.com/project/byKey/e388b87aecbfc99fb4b0abb9c16a602c)\n[![David](https://img.shields.io/david/artisin/switchreducer.svg)](https://github.com/artisin/switchreducer/blob/master/package.json)\n\n## Description\n\nA simple and small [Redux](http://redux.js.org/) library designed to reduce the verbose boiler plate of the \"default\" [switch statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) [reducer](http://redux.js.org/docs/Glossary.html#reducer) pattern.\n\n## `switchreducer`\n\nThe operation of `switchreducer` is dead simple. It takes two arguments, the initial state, and a function that's passed a single argument object parameter of the `state` and the `action` properties, ie `({state, ...action})`. This function expects a return of a lookup table composed of \"action\" cases which are invoked when a \"action\" type matches.\n\n\n```js\nswitchreducer(\u003cinitial-state\u003e, ({\u003cstate\u003e, ...\u003caction\u003e}) =\u003e\n  \u003caction-lookup-table\u003e\n);\n```\n\n__Example__\n\n```js\nimport switchreducer from 'switchreducer';\n\n// actions + state\nconst SET_FILTER = 'SET_FILTER';\nconst SET_COLOR = 'SET_COLOR';\nconst initialState = {};\n\n// reducer\nconst myReducer = switchreducer(initialState, ({state, payload}) =\u003e ({\n  [SET_FILTER]: () =\u003e Object.assign({}, state, {filter: payload}),\n  [SET_COLOR]: () =\u003e Object.assign({}, state, {color: payload}),\n}));\n```\n\n\n__\"default\" reducer comparison__\n\nFor comparison here's what the above reducer would look like using the \"default\" switch statement pattern.\n\n```js\n// actions + state\nconst SET_FILTER = 'SET_FILTER';\nconst SET_COLOR = 'SET_COLOR';\nconst initialState = {};\n\n// reducer\nconst myReducer = (state = initialState, action) =\u003e {\n  const { type, payload } = action;\n  switch (type) {\n      case SET_FILTER:\n        return Object.assign({}, state, {filter: payload});\n      case SET_COLOR:\n        return Object.assign({}, state, {color: payload}),\n      default:\n        return state;\n  }\n};\n```\n\n\n\n## `switchcase`\n\nYou may prefer and/or there may be cases that the `switchcase` lookup table helper is a better fit. The `switchcase` function is the \"real\" logic behind `switchreducer`. It expects a \"action\" lookup table and two curried arguments the state and the \"action\" type.\n\n\n```js\nswitchcase(\u003caction-lookup-table\u003e)(\u003cstate\u003e)(\u003caction-type\u003e);\n```\n\n__Example__\n\n```js\nimport { switchcase } from 'switchreducer';\n\n// actions + state\nconst SET_FILTER = 'SET_FILTER';\nconst SET_COLOR = 'SET_COLOR';\nconst initialState = {};\n\n// reducer\nconst myReducer = (state = initialState, action) =\u003e {\n  const { type, payload } = action;\n  return switchcase(({\n    [SET_FILTER]: () =\u003e Object.assign({}, state, {filter: payload}),\n    [SET_COLOR]: () =\u003e Object.assign({}, state, {color: payload}),\n  }))(state)(type);\n};\n```\n\n\n---\n\nBest, te\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffetchte%2Fswitchreducer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffetchte%2Fswitchreducer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffetchte%2Fswitchreducer/lists"}