action-reducer
Version:
Type-safe ActionCreator and Reducer library
27 lines (23 loc) • 822 B
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
let typeId = 0;
const ActionReducer = (initState, prefix) => {
const mutations = /* @__PURE__ */ Object.create(null);
const createAction = (type, mutation) => {
if (mutation === void 0) {
mutation = type;
type = `@@ActionReducer-${++typeId}`;
}
type = null != type.type ? type.type : prefix ? prefix + type : type;
const actionCreator = (...payload) => ({ type, payload });
actionCreator.type = type;
mutations[type] = mutation;
return actionCreator;
};
const reducer = (state = initState, action) => {
const mutation = mutations[action.type];
return mutation ? mutation(state, ...action.payload) : state;
};
return { createAction, reducer };
};
exports["default"] = ActionReducer;
;