-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextend.js
More file actions
53 lines (45 loc) · 1.37 KB
/
extend.js
File metadata and controls
53 lines (45 loc) · 1.37 KB
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
46
47
48
49
50
51
52
53
// Grab from jQuery.extend()
// https://github.com/jquery/jquery/blob/master/src/core.js
function extend() {
var options, name, src, copy, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if (target.constructor == Boolean) {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
for (; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = arguments[i]) != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (deep && copy && (copy.constructor == Object || copy.constructor == Array)) {
clone = src && src.constructor == copy.constructor ? src : new copy.constructor();
// Never move original objects, clone them
target[name] = extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
return target;
}
// CommonJS
if (typeof module != 'undefined' && module.exports) {
module.exports = extend;
}