forked from allenai/syrup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.js
More file actions
37 lines (35 loc) · 997 Bytes
/
merge.js
File metadata and controls
37 lines (35 loc) · 997 Bytes
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
'use strict';
/**
* Merges the provided objects into the target. Overlapping values are overwritten
* by objects later in the parameter collection.
*
* @param {object} target The object to merge into.
* @param {...object} on 1-n objects to merge into target.
*
* @return {mixed} An object resulting from the merge, or undefined if no
* target was provided.
*/
function merge() {
var objects = Array.prototype.slice.call(arguments);
var target = objects.shift();
if(typeof target !== 'object' || Array.isArray(target)) {
return;
}
objects.forEach(function(o) {
if(typeof o === 'object') {
Object.getOwnPropertyNames(o).forEach(function(n) {
var v;
if(Array.isArray(o[n])) {
v = o[n].slice();
} else if(typeof o[n] === 'object') {
v = merge({}, o[n]);
} else {
v = o[n];
}
target[n] = v;
});
}
});
return target;
}
module.exports = merge;