import { assign, assignWith, merge as _merge, mergeWith } from 'lodash'; import { MergeOptions } from '../types'; export type MergeHandler = (settings: T[], options?: MergeOptions) => T; export const merge = (settings: T[], options?: MergeOptions): T => { const overwrite = options && options.overwrite === true; const concatArrays = options && options.concatArrays === true; if (overwrite) { return concatArrays ? assignConcatArrays(...settings) : assign({}, ...settings); } else { return concatArrays ? mergeConcatArrays(...settings) : _merge({}, ...settings); } }; const mergeConcatArrays = (...settings: T[]): T => { return mergeWith({}, ...settings, (objValue, srcValue) => { if (Array.isArray(objValue)) { return objValue.concat(srcValue); } }); }; const assignConcatArrays = (...settings: T[]): T => { return assignWith({}, ...settings, (objValue, srcValue) => { if (Array.isArray(objValue)) { return objValue.concat(srcValue); } }); };