@snipsonian/core
Version:
Core/base reusable javascript code snippets
29 lines (28 loc) • 925 B
JavaScript
import assert from '../../assert';
import isSet from '../../is/isSet';
import isArray from '../../is/isArray';
export default function addProp(propName, propValue, { addIfValueUnset = true } = {}) {
return function decorate(target) {
assert(propName, isSet, 'Required input argument \'propName\' is missing.');
if (isArray(target)) {
return target.map((entity) => enrichWithProp({
target: entity,
propName,
propValue,
options: { addIfValueUnset },
}));
}
return enrichWithProp({
target,
propName,
propValue,
options: { addIfValueUnset },
});
};
}
function enrichWithProp({ target, propName, propValue, options = {}, }) {
if (options.addIfValueUnset || isSet(propValue)) {
target[propName] = propValue;
}
return target;
}