@jakesidsmith/redux-create-reducer
Version:
A utility to create redux reducers from a set of handlers
32 lines • 1.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createReducer = void 0;
var INVALID_HANDLER_KEYS = ['undefined', 'null'];
function validateKeys(handlers) {
INVALID_HANDLER_KEYS.forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(handlers, key)) {
throw new Error("Invalid createReducer handler key: " + key);
}
});
}
function createReducer(handlers, initialState) {
if (!handlers ||
typeof handlers !== 'object' ||
Array.isArray(handlers)) {
throw new Error('Invalid createReducer handlers - must be a string keyed object');
}
if (typeof initialState === 'undefined') {
throw new Error('Invalid createReducer initialState value: undefined');
}
validateKeys(handlers);
return function (state, action) {
if (state === void 0) { state = initialState; }
var type = action.type;
if (Object.prototype.hasOwnProperty.call(handlers, type)) {
return handlers[type](state, action);
}
return state;
};
}
exports.createReducer = createReducer;
//# sourceMappingURL=index.js.map
;