action-reducer
Version:
Type-safe ActionCreator and Reducer library
33 lines (28 loc) • 1.21 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ActionReducer = {}));
})(this, (function (exports) { 'use strict';
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;
Object.defineProperty(exports, '__esModule', { value: true });
}));