@snipsonian/core
Version:
Core/base reusable javascript code snippets
32 lines (31 loc) • 1.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = require("../../assert");
const isSet_1 = require("../../is/isSet");
const isArray_1 = require("../../is/isArray");
function addProp(propName, propValue, { addIfValueUnset = true } = {}) {
return function decorate(target) {
(0, assert_1.default)(propName, isSet_1.default, 'Required input argument \'propName\' is missing.');
if ((0, isArray_1.default)(target)) {
return target.map((entity) => enrichWithProp({
target: entity,
propName,
propValue,
options: { addIfValueUnset },
}));
}
return enrichWithProp({
target,
propName,
propValue,
options: { addIfValueUnset },
});
};
}
exports.default = addProp;
function enrichWithProp({ target, propName, propValue, options = {}, }) {
if (options.addIfValueUnset || (0, isSet_1.default)(propValue)) {
target[propName] = propValue;
}
return target;
}