All files / core/domain/utils/misc extend.js

100% Statements 7/7
50% Branches 1/2
100% Functions 2/2
100% Lines 6/6

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 39 40 41 42                                                              4x   4x 8x 8x 316x     4x    
/**
 * This function takes any number of objects as arguments, copies from each
 * of these objects each pair key/value into a new object, and finally
 * returns this object.
 *
 * The arguments are parsed from the last one to the first one, such that
 * when several objects have keys in common, the "earliest" object wins.
 *
 * Example:
 * ********
 *  > var o1 = {
 *  >       a: 1,
 *  >       b: 2,
 *  >       c: '3'
 *  >     },
 *  >     o2 = {
 *  >       c: '4',
 *  >       d: [ 5 ]
 *  >     };
 *  > extend(o1, o2);
 *  > // Returns: {
 *  > //   a: 1,
 *  > //   b: 2,
 *  > //   c: '3',
 *  > //   d: [ 5 ]
 *  > // };
 *
 * @param  {object+} Any number of objects.
 * @return {object}  The merged object.
 */
export default function extend(...args) {
  const res = {};
 
  for (let i = args.length - 1; i >= 0; i--) {
    const arg = args[i];
    Object.keys(arg || {}).forEach(key => {
      res[key] = arg[key];
    });
  }
  return res;
}