UNPKG

yax

Version:

Yet another store using redux. (Inspired by vuex and dva)

59 lines (54 loc) 1.36 kB
import { getNestedState } from './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); }; } export 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] = getNestedState(state, path); }); return res; }; }); export 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; }; });