All files / utils deep-merge.js

100% Statements 13/13
75% Branches 9/12
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38                          2356x 2356x     2356x 2367x 2367x 48102x 48102x 48102x 1985x   46117x         2356x         48102x    
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
 
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
 
// @flow
 
export default function deepMerge(
  target?: ?{},
  ...sources: Array<null | ?{}>
): {} {
  target = target || {};
  const len = sources.length;
  let obj;
  let value;
  for (let i = 0; i < len; i++) {
    obj = sources[i] || {};
    for (let key in obj) {
      Eif (obj.hasOwnProperty(key)) {
        value = obj[key];
        if (isCloneable(value)) {
          target[key] = deepMerge(target[key] || {}, value);
        } else {
          target[key] = value;
        }
      }
    }
  }
  return target;
}
 
/* eslint-disable-next-line flowtype/no-weak-types */
function isCloneable(obj: mixed) {
  return Array.isArray(obj) || {}.toString.call(obj) == '[object Object]';
}