-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
179 lines (150 loc) · 4.54 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"use strict";
// Extends method
// (taken from http://code.jquery.com/jquery-1.9.0.js)
// Populate the class2type map
var class2type = {};
var types = [
"Boolean", "Number", "String", "Function", "Array",
"Date", "RegExp", "Object", "Error"
];
for (var i = 0; i < types.length; i++) {
var typename = types[i];
class2type["[object " + typename + "]"] = typename.toLowerCase();
}
var core_toString = class2type.toString;
var core_hasOwn = class2type.hasOwnProperty;
function type(obj) {
if (obj === null) {
return String(obj);
}
return typeof obj === "object" || typeof obj === "function" ?
class2type[core_toString.call(obj)] || "object" :
typeof obj;
}
function isWindow(obj) {
return obj !== null && obj === obj.window;
}
function isPlainObject(obj) {
// Must be an Object.
// Because of IE, we also have to check the presence of
// the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if (!obj || type(obj) !== "object" || obj.nodeType || isWindow(obj)) {
return false;
}
try {
// Not own constructor property must be Object
if (obj.constructor &&
!core_hasOwn.call(obj, "constructor") &&
!core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
return false;
}
} catch ( e ) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for (key in obj) {}
return key === undefined || core_hasOwn.call(obj, key);
}
function isFunction(obj) {
return type(obj) === "function";
}
var isArray = Array.isArray || function (obj) {
return type(obj) === "array";
};
function extend() {
// originally extend() was recursive, but this ended up giving us
// "call stack exceeded", so it's been unrolled to use a literal stack
// (see https://github.com/pouchdb/pouchdb/issues/2543)
var stack = [];
var i = -1;
var len = arguments.length;
var args = new Array(len);
while (++i < len) {
args[i] = arguments[i];
}
var container = {};
stack.push({args: args, result: {container: container, key: 'key'}});
var next;
while ((next = stack.pop())) {
extendInner(stack, next.args, next.result);
}
return container.key;
}
function extendInner(stack, args, result) {
var options, name, src, copy, copyIsArray, clone,
target = args[0] || {},
i = 1,
length = args.length,
deep = false,
numericStringRegex = /\d+/,
optionsIsArray;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = args[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && !isFunction(target)) {
target = {};
}
// extend jQuery itself if only one argument is passed
if (length === i) {
/* jshint validthis: true */
target = this;
--i;
}
for (; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = args[i]) != null) {
optionsIsArray = isArray(options);
// Extend the base object
for (name in options) {
//if (options.hasOwnProperty(name)) {
if (!(name in Object.prototype)) {
if (optionsIsArray && !numericStringRegex.test(name)) {
continue;
}
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 && (isPlainObject(copy) ||
(copyIsArray = isArray(copy)))) {
if (copyIsArray) {
copyIsArray = false;
clone = src && isArray(src) ? src : [];
} else {
clone = src && isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
stack.push({
args: [deep, clone, copy],
result: {
container: target,
key: name
}
});
// Don't bring in undefined values
} else if (copy !== undefined) {
if (!(isArray(options) && isFunction(copy))) {
target[name] = copy;
}
}
}
}
}
}
// "Return" the modified object by setting the key
// on the given container
result.container[result.key] = target;
}
module.exports = extend;