yax
Version:
Yet another store using redux. (Inspired by vuex and dva)
66 lines (59 loc) • 1.5 kB
JavaScript
;
exports.__esModule = true;
exports.mapActions = exports.mapState = void 0;
var _util = require("./util");
function normalizeMap(map) {
return Array.isArray(map) ? map.map(function (key) {
return {
key: key,
val: key
};
}) : Object.keys(map).map(function (key) {
return {
key: key,
val: map[key]
};
});
}
function normalizeNamespace(fn) {
return function (namespace, map) {
if (typeof namespace !== 'string') {
map = namespace;
namespace = '';
} else if (namespace.charAt(namespace.length - 1) !== '/') {
namespace += '/';
}
return fn(namespace, map);
};
}
var mapState = normalizeNamespace(function (namespace, states) {
return function (state) {
var res = {};
normalizeMap(states).forEach(function (_ref) {
var key = _ref.key,
val = _ref.val;
var path = (namespace + val).split('/');
res[key] = (0, _util.getNestedState)(state, path);
});
return res;
};
});
exports.mapState = mapState;
var mapActions = normalizeNamespace(function (namespace, actions) {
return function (dispatch) {
var res = {};
normalizeMap(actions).forEach(function (_ref2) {
var key = _ref2.key,
val = _ref2.val;
var type = namespace + val;
res[key] = function (payload) {
return dispatch({
type: type,
payload: payload
});
};
});
return res;
};
});
exports.mapActions = mapActions;