@enact/core
Version:
Enact is an open source JavaScript framework containing everything you need to create a fast, scalable mobile or web application.
51 lines (49 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.computed = void 0;
/**
* Accepts an object of computed property configurations and a property object, passes the property
* object through each computed property handler, and merges the resulting computed properties with
* the original properties.
*
* ```
* const cfg = {
* sum: ({x,y,z}) => x+y+z,
* product: ({x,y,z}) => x*y*z
* }
* const props = {
* x: 2,
* y: 3,
* z: 4
* }
* computed(cfg)(props) // => {x: 2, y: 3, z: 4, sum: 9, product: 24}
* ```
*
* @method computed
* @param {Object} cfg Configuration object mapping transformation functions to property names
*
* @returns {Function} Function that accepts a props object and mutates it to include the
* computed props
* @private
*/
var computed = exports.computed = function computed(cfg, optProps) {
var keys = Object.keys(cfg);
var renderComputed = function renderComputed(props) {
var updated = {};
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
for (var i = keys.length - 1; i >= 0; i--) {
updated[keys[i]] = cfg[keys[i]].apply(cfg, [props].concat(args));
}
return Object.assign(props, updated);
};
// maintain compatibility with 1.x
if (optProps) {
return renderComputed(optProps);
}
return renderComputed;
};
var _default = exports["default"] = computed;