forked from stephenmathieson-boneyard/node-cpplint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextend.js
41 lines (36 loc) · 1005 Bytes
/
extend.js
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
'use strict';
/**
* Merges to objects together, favoring the second object's properties.
*
* Details:
* Overwrites `first` properties with `second` properties and adds `second`
* properties if missing in `first`
*
* @param {Object} first
* @param {Object} second
* @param {Boolean} deep Deep-extend the objects
* @return {Object} The merged objects
*/
function extend(first, second, deep) {
var prop, value,
result = {};
// mirror the first object
for (prop in first) {
if (first.hasOwnProperty(prop)) {
result[prop] = first[prop];
}
}
for (prop in second) {
if (second.hasOwnProperty(prop)) {
value = second[prop];
// deep extend an object's objects, but not arrays (when deep is truthy)
if (typeof value === 'object' && deep && !Array.isArray(value)) {
result[prop] = extend(result[prop], second[prop]);
} else {
result[prop] = second[prop];
}
}
}
return result;
}
module.exports = extend;