genji-core
Version:
The core lightweight library for genji, based on redux and redux-saga.
45 lines (34 loc) • 1.34 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = prefixNamespace;
var _keys = _interopRequireDefault(require("@babel/runtime/core-js/object/keys"));
var _warning = _interopRequireDefault(require("warning"));
var _utils = require("./utils");
var _constants = require("./constants");
function prefix(obj, namespace, type) {
return (0, _keys.default)(obj).reduce(function (memo, key) {
(0, _warning.default)(key.indexOf("".concat(namespace).concat(_constants.NAMESPACE_SEP)) !== 0, "[prefixNamespace]: ".concat(type, " ").concat(key, " should not be prefixed with namespace ").concat(namespace));
var newKey = "".concat(namespace).concat(_constants.NAMESPACE_SEP).concat(key);
memo[newKey] = obj[key];
return memo;
}, {});
}
function prefixNamespace(model) {
var namespace = model.namespace,
actions = model.actions,
mutations = model.mutations;
if (mutations) {
if ((0, _utils.isArray)(mutations)) {
model.mutations[0] = prefix(mutations[0], namespace, 'mutations');
} else {
model.mutations = prefix(mutations, namespace, 'mutations');
}
}
if (actions) {
model.actions = prefix(actions, namespace, 'actions');
}
return model;
}