updux/dist/buildActions/index.js

35 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-10-22 22:15:08 +00:00
"use strict";
2019-10-24 15:52:36 +00:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fp_1 = __importDefault(require("lodash/fp"));
2019-11-05 01:34:14 +00:00
function actionCreator(type, transform) {
if (transform) {
return Object.assign((...args) => ({ type, payload: transform(...args) }), { type });
}
if (transform === null) {
return Object.assign(() => ({ type }), { type });
}
return Object.assign((payload) => ({ type, payload }));
}
exports.actionCreator = actionCreator;
2019-10-22 22:15:08 +00:00
function actionFor(type) {
2019-11-05 01:34:14 +00:00
const f = ((payload = undefined, meta = undefined) => fp_1.default.pickBy(v => v !== undefined)({ type, payload, meta }));
return Object.assign(f, {
_genericAction: true,
type
});
2019-10-24 15:52:36 +00:00
}
function buildActions(generators = {}, actionNames = [], subActions = []) {
const [crafted, generic] = fp_1.default.partition(([type, f]) => !f._genericAction)(subActions);
const actions = [
...(actionNames.map(type => [type, actionFor(type)])),
...generic,
...crafted,
2019-11-05 01:34:14 +00:00
...Object.entries(generators).map(([type, payload]) => [type, payload.type ? payload : (...args) => ({ type, payload: payload(...args) })]),
2019-10-24 15:52:36 +00:00
];
return fp_1.default.fromPairs(actions);
2019-10-22 22:15:08 +00:00
}
2019-10-24 15:52:36 +00:00
exports.default = buildActions;
//# sourceMappingURL=index.js.map