diff --git a/.gitignore b/.gitignore index 397b4a7624e35fa60563a9c03b1213d93f7b6546..42d824b70b4c12af1a977b3e835c5c639f7eb2d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.log +.idea \ No newline at end of file diff --git a/README.md b/README.md index 00f23a7aa25c44af851f28fbc946847ba471091c..cfa95cd4ada83c094cd5319485524ccadab86a67 100644 --- a/README.md +++ b/README.md @@ -73,4 +73,4 @@ The library exports a single function that returns a redux-compatible reducer fu ``` reducer(initial: Object, handlers: Map<string, Handler>) ``` -Where initial is the `initial` router state and `handlers` is a string -> function map of action types to action reducers. Each handler is passed `state` and `action` as parameters, which correspond to the assigned reducer state and the current action (without the `type` property) and should return the new version of state after resolving the action. Simple as that. +Where initial is the `initial` router state and `handlers` is a string -> function map of action types to action reducers. Each handler is passed `state` and `action` as parameters, which correspond to the assigned reducer state and the current action and should return the new version of state after resolving the action. Simple as that. diff --git a/index.js b/index.js index ee08eabe2f451d5aae9f46badc7b51fed5fffc9f..f8b1f80a5191e18c61567f59aea0d7af911f25a0 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,9 @@ -module.exports = function(initial, handlers) { - return function(state = initial, {type, ...action}) { - if (handlers.hasOwnProperty(type)) { - return handlers[type](state, action) - } - return state - } +module.exports = function (initial, handlers) { + return function (state, action) { + if (state == null) state = initial + if (handlers.hasOwnProperty(action.type)) { + return handlers[action.type](state, action) + } + return state + } } diff --git a/package.json b/package.json index ad6975f3053a55b76005babca510dc448b6e387c..0037123669bfd061110b2167807c627f6bd0afd0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@commander-lol/redux-reducer", - "version": "1.0.0", + "version": "1.0.1", "description": "Remove the boilerplate for creating redux reducers", "main": "index.js", "author": "Louis Capitanchik <contact@louiscap.co>",