combined-reduction
Advanced tools
Comparing version 1.0.0 to 1.1.0
"use strict"; | ||
const _ = require('lodash'); | ||
const deepUpdate = require('deep-update'); | ||
var _ = require('lodash'); | ||
var deepUpdate = require('deep-update'); | ||
/** | ||
@@ -10,8 +10,14 @@ * Combines **both** keyed reducers and top level reducers in any order. | ||
*/ | ||
function combinedReduction(...reducers) { | ||
const dispatchPairs = _.reduce(reducers, (m, r) => m.concat(_findReducers(r)), []); | ||
return (state = {}, action) => { | ||
for (let [path, reducer] of dispatchPairs) { | ||
let currentState = path.length === 0 ? state : _.get(state, path); | ||
let newState; | ||
function combinedReduction() { | ||
var reducers = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
reducers[_i - 0] = arguments[_i]; | ||
} | ||
var dispatchPairs = _.reduce(reducers, function (m, r) { return m.concat(_findReducers(r)); }, []); | ||
return function (state, action) { | ||
if (state === void 0) { state = {}; } | ||
for (var _i = 0, dispatchPairs_1 = dispatchPairs; _i < dispatchPairs_1.length; _i++) { | ||
var _a = dispatchPairs_1[_i], path = _a[0], reducer = _a[1]; | ||
var currentState = path.length === 0 ? state : _.get(state, path); | ||
var newState = void 0; | ||
try { | ||
@@ -21,3 +27,3 @@ newState = reducer(currentState, action); | ||
catch (error) { | ||
console.error(`Error in reducer mounted at ${path.join('.')}:`, error); | ||
console.error("Error in reducer mounted at " + path.join('.') + ":", error); | ||
continue; | ||
@@ -35,3 +41,4 @@ } | ||
*/ | ||
function _findReducers(reducer, basePath = []) { | ||
function _findReducers(reducer, basePath) { | ||
if (basePath === void 0) { basePath = []; } | ||
if (!reducer) | ||
@@ -44,5 +51,5 @@ return []; // blank entries are ok. | ||
if (!_.isPlainObject(reducer)) { | ||
throw new TypeError(`Cannot combine reducer of type ${typeof reducer} at ${basePath.join('.')}`); | ||
throw new TypeError("Cannot combine reducer of type " + typeof reducer + " at " + basePath.join('.')); | ||
} | ||
return _.reduce(reducer, (result, config, key) => { | ||
return _.reduce(reducer, function (result, config, key) { | ||
return result.concat(_findReducers(config, basePath.concat(key))); | ||
@@ -49,0 +56,0 @@ }, []); |
{ | ||
"name": "combined-reduction", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Like Redux's combineReducers, but more better!", | ||
@@ -22,5 +22,2 @@ "homepage": "https://github.com/convoyinc/combined-reduction", | ||
}, | ||
"engines": { | ||
"node": ">= 4.0.0" | ||
}, | ||
"dependencies": { | ||
@@ -27,0 +24,0 @@ "deep-update": "^1.0.4", |
@@ -10,3 +10,3 @@ # combined-reduction | ||
``` | ||
```js | ||
const reducer = combinedReduction({ | ||
@@ -27,3 +27,3 @@ session: session.reducer, | ||
``` | ||
```js | ||
const reducer = combinedReduction( | ||
@@ -30,0 +30,0 @@ migrations.reducer, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4915
72