@haravan-tech/util
Version:
utilities
39 lines (32 loc) • 1 kB
JavaScript
/**
* @namespace collection
* @memberof util
*/
const _ = require('lodash');
module.exports = {
setOptions,
};
/**
* Merge all options properties to target, Array value will be assign
* @note This method mutates object.
* @memberof util.collection
* @param {object} target
* @param {*} options
* @param {string[]|object} properties use to pick from options before merging, should contain all options properties.\
* If pass an object, properties will be extracted by Object.keys()
*
* @return {object} target
*/
function setOptions(target, options = {}, properties = []) {
if (typeof properties === 'object' && !Array.isArray(properties)) {
properties = Object.keys(properties);
}
if (Array.isArray(properties) && properties.length > 0) {
options = _.pick(options, properties);
}
return _.mergeWith(target, options, (val, srcVal) => {
if (Array.isArray(val) || Array.isArray(srcVal)) {
return srcVal;
}
});
}