UNPKG

997 BJavaScriptView Raw
1'use strict';
2
3/**
4 * Merges the provided objects into the target. Overlapping values are overwritten
5 * by objects later in the parameter collection.
6 *
7 * @param {object} target The object to merge into.
8 * @param {...object} on 1-n objects to merge into target.
9 *
10 * @return {mixed} An object resulting from the merge, or undefined if no
11 * target was provided.
12 */
13function merge() {
14 var objects = Array.prototype.slice.call(arguments);
15 var target = objects.shift();
16 if(typeof target !== 'object' || Array.isArray(target)) {
17 return;
18 }
19 objects.forEach(function(o) {
20 if(typeof o === 'object') {
21 Object.getOwnPropertyNames(o).forEach(function(n) {
22 var v;
23 if(Array.isArray(o[n])) {
24 v = o[n].slice();
25 } else if(typeof o[n] === 'object') {
26 v = merge({}, o[n]);
27 } else {
28 v = o[n];
29 }
30 target[n] = v;
31 });
32 }
33 });
34 return target;
35}
36
37module.exports = merge;
\No newline at end of file