softkave-js-utils
Version:
JavaScript & Typescript utility functions, types, and classes
20 lines • 796 B
JavaScript
import { mergeWith } from 'lodash-es';
export const mergeObjects = (dest, source, meta = { arrayUpdateStrategy: 'replace' }) => {
const result = mergeWith(dest, source, (objValue, srcValue) => {
if (Array.isArray(objValue) && srcValue) {
if (meta.arrayUpdateStrategy === 'concat') {
return objValue.concat(srcValue);
}
else if (meta.arrayUpdateStrategy === 'replace') {
return srcValue;
}
else if (meta.arrayUpdateStrategy === 'retain') {
return objValue;
}
// No need to handle the "merge" arrayUpdateStrategy, it happens by
// default if nothing is returned
}
});
return result;
};
//# sourceMappingURL=mergeObjects.js.map