UNPKG

redux-smart-actions

Version:
38 lines (29 loc) 873 B
"use strict"; exports.__esModule = true; exports.createReducer = exports.joinTypes = void 0; const TYPES_DELIMITER = '@@'; const joinTypes = (...types) => types.map(type => type.toString()).join(TYPES_DELIMITER); exports.joinTypes = joinTypes; const createReducer = (handlers, defaultState) => { handlers = Object.entries(handlers).reduce((prev, [type, handler]) => { if (type.includes(TYPES_DELIMITER)) { type.split(TYPES_DELIMITER).forEach(singleType => { prev[singleType] = handler; }); } else { prev[type.toString()] = handler; } return prev; }, {}); return (state, action) => { if (state === undefined) { return defaultState; } const handler = handlers[action.type]; if (handler) { return handler(state, action); } return state; }; }; exports.createReducer = createReducer;