forked from mikemaccana/outdated-browser-rework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend.js
45 lines (36 loc) · 1.12 KB
/
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
42
43
44
45
/* Highly dumbed down version of https://github.com/unclechu/node-deep-extend */
/**
* Extening object that entered in first argument.
*
* Returns extended object or false if have no target object or incorrect type.
*
* If you wish to clone source object (without modify it), just use empty new
* object as first argument, like this:
* deepExtend({}, yourObj_1, [yourObj_N]);
*/
module.exports = function deepExtend(/*obj_1, [obj_2], [obj_N]*/) {
if (arguments.length < 1 || typeof arguments[0] !== "object") {
return false
}
if (arguments.length < 2) {
return arguments[0]
}
var target = arguments[0]
for (var i = 1; i < arguments.length; i++) {
var obj = arguments[i]
for (var key in obj) {
var src = target[key]
var val = obj[key]
if (typeof val !== "object" || val === null) {
target[key] = val
// just clone arrays (and recursive clone objects inside)
} else if (typeof src !== "object" || src === null) {
target[key] = deepExtend({}, val)
// source value and new value is objects both, extending...
} else {
target[key] = deepExtend(src, val)
}
}
}
return target
}