UNPKG

nested-combine-reducers

Version:

Allows you to create your root reducer in one go, instead of individually combine slice reducers

38 lines 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.nestedCombineReducers = void 0; const redux_1 = require("redux"); /** * Takes a Reducers maps with multiple levels of nesting and turns it into in a single reducing function. * * @param map An object whose values are either reducing functions or other objects */ function nestedCombineReducers(map) { if (!map) throw new Error('You must specify a reducers map.'); const flatReducersMapObject = {}; const recursiveMapKeys = Object.keys(map); for (const recursiveMapKey of recursiveMapKeys) { const recursiveMapValue = map[recursiveMapKey]; if (recursiveMapValue === null) { continue; } if (recursiveMapValue === undefined) { continue; } //Hopefully a reducer function, let's store it to combine it later if (typeof recursiveMapValue === 'function') { const reducer = recursiveMapValue; flatReducersMapObject[recursiveMapKey] = reducer; } //Nesting found, let's go deeper ! if (typeof recursiveMapValue === 'object') { const nestedRecursiveReducersMapObject = recursiveMapValue; flatReducersMapObject[recursiveMapKey] = nestedCombineReducers(nestedRecursiveReducersMapObject); } } //Once all the properties have been processed, the ReducersMap is no longer incomplete and can be combined one last time return redux_1.combineReducers(flatReducersMapObject); } exports.nestedCombineReducers = nestedCombineReducers; //# sourceMappingURL=index.js.map