{"id":18730063,"url":"https://github.com/b-gran/reducer-redux","last_synced_at":"2026-04-17T05:03:47.201Z","repository":{"id":71119487,"uuid":"66101377","full_name":"b-gran/reducer-redux","owner":"b-gran","description":"Beautiful, functional redux reducers","archived":false,"fork":false,"pushed_at":"2017-09-17T00:31:59.000Z","size":57,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-29T22:59:56.479Z","etag":null,"topics":["action","fp","function","functional","functional-programming","pure","reducer","reducer-redux","redux"],"latest_commit_sha":null,"homepage":"","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/b-gran.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":"2016-08-19T17:48:28.000Z","updated_at":"2024-08-21T16:47:28.364Z","dependencies_parsed_at":"2023-03-13T20:21:44.489Z","dependency_job_id":null,"html_url":"https://github.com/b-gran/reducer-redux","commit_stats":{"total_commits":40,"total_committers":1,"mean_commits":40.0,"dds":0.0,"last_synced_commit":"718ea99de7188f9282870916a443478a921921f7"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b-gran%2Freducer-redux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b-gran%2Freducer-redux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b-gran%2Freducer-redux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b-gran%2Freducer-redux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/b-gran","download_url":"https://codeload.github.com/b-gran/reducer-redux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239599037,"owners_count":19665911,"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":["action","fp","function","functional","functional-programming","pure","reducer","reducer-redux","redux"],"created_at":"2024-11-07T14:34:25.725Z","updated_at":"2025-11-12T08:30:18.256Z","avatar_url":"https://github.com/b-gran.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `reducer-redux`\n\n[![Build Status](https://travis-ci.org/b-gran/reducer-redux.svg?branch=master)](https://travis-ci.org/b-gran/reducer-redux) [![npm version](https://badge.fury.io/js/reducer-redux.svg)](https://badge.fury.io/js/reducer-redux)\n\nCreate functional, reusable redux reducers. Liberate yourself from `switch`.\n\n_`(g∘f)`_ __Composable__: reducers are just plain functions. Nest and compose them with other reducers and libraries.\n\n`♺` __Reusable__: designed for redux but flexible enough to use elsewhere. Create building blocks and reuse them.\n\n_`xⁿ`_ __Powerful__: comes with a utility belt for working with redux actions.\n\n## Installation\n\n```\nnpm install reducer-redux\n```\n\n## 30 second overview\n\nAn example from the redux tutorial:\n\n```\nimport match from 'reducer-redux';\nimport { combineReducers } from 'redux';\n\nimport { ADD_TODO, TOGGLE_TODO, SET_VISIBILITY_FILTER, VisibilityFilters } from './actions';\n\nconst visibilityFilter = match.withDefault(VisibilityFilter.SHOW_ALL)(\n    match.plainAction({ type: SET_VISIBILITY_FILTER })\n        .with(action =\u003e action.filter)\n)\n\nconst todos = match.withDefault([])(\n    match.first(\n        match.plainAction({ type: ADD_TODO })\n            .with((action, state) =\u003e [\n                ...state,\n                { \n                    text: action.text,\n                    completed: false,\n                }\n            ]),\n        match.plainAction({ type: TOGGLE_TODO })\n            .with((action, state) =\u003e state.map(\n                match((todo, index) =\u003e index === action.index)\n                    .with(todo =\u003e ({ ...todo, completed: !todo.completed }))\n            )),\n    ))\n\nexport default combineReducers({\n    visibilityFilter,\n    todos\n});\n```\n\n## Basic usage\n\nThe library exports a function called `match`. `match` returns functions called _Matchers_, which\n are the core abstraction of library. A Matcher is a tuple of `(condition, reducer)`. The condition\nis a predicate, and the reducer is any function. \n\nMatchers created with `match` don't have a reducer yet. You specify a reducer by calling the\nMatcher's `.with()` function.\n\nWhen the Matcher is called with some arguments, it first calls the `condition`.\n* __If the `condition` returns true__, the matcher returns the result of the `reducer` __with the \nsame arguments__.\n* __If the `condition` returns false, the matcher returns __the first argument__.\n\nHere's an example:\n```\nconst matcher = match(value =\u003e value === 'foo')\n    .with(() =\u003e 'bar')\nmatcher('foo') // 'bar'\nmatcher('bar') // 'foo'\nmatcher('bar', 'baz') // 'foo'\n```\n\n## Usage with redux\n\nAlthough the library is powerful enough for use anywhere, it's designed for redux.\n\n__The `reducer` of a Matcher returns the first argument (the state) if the `condition` returns \nfalse__.\nThis property enables us to specify some conditions for which to modify a store state,\nand the Matcher will leave the state unchanged for any other condition!\n\nHere's an example:\n```\nconst counter = match((state, action) =\u003e action.type === 'increment')\n    .with((state, action) =\u003e state + 1)\ncounter(0, { type: 'increment' }) // 1    \ncounter(0, { type: 'something else' }) // 0    \ncounter(0, { type: 'another action' }) // 0    \n```\n\nOf course, a redux application needs to handle more than one action type.\n`reducer-redux` comes with a utility to combine reducers: `match.first()`.\n\n`match.first()` takes a group of reducers and uses the first one whose `condition` returns true.\nHere's an example:\n```\nconst counter = match.first(\n    match((state, action) =\u003e action.type === 'increment')\n        .with((state, action) =\u003e state + action.amount),\n    match((state, action) =\u003e action.type === 'decrement')\n        .with((state, action) =\u003e state - action.amount)\n)\ncounter(1, { type: 'increment', amount: 1 }) // 2    \ncounter(1, { type: 'decrement', amount: 2 }) // -1    \ncounter(1, { type: 'another action' }) // 1    \n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fb-gran%2Freducer-redux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fb-gran%2Freducer-redux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fb-gran%2Freducer-redux/lists"}