Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
224 lines (197 loc) · 6 KB
/
Copy pathindex.js
File metadata and controls
224 lines (197 loc) · 6 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var test = require('../util/test')(__filename);
var extend = require('../../packages/object-extend');
test('shallow extend merges properties', function(t) {
t.plan(2);
var src = {a: 3, b: 5};
t.deepEqual(extend(src, {a: 4, c: 8}), {a: 4, b: 5, c: 8});
t.deepEqual(src, {a: 4, b: 5, c: 8});
t.end();
});
test('shallow extend merges into new object', function(t) {
t.plan(2);
var src = {};
t.deepEqual(extend(src, {a: 3, b: 5}, {a: 4, c: 8}), {a: 4, b: 5, c: 8});
t.deepEqual(src, {a: 4, b: 5, c: 8});
t.end();
});
test('shallow extend does not clone child objects', function(t) {
t.plan(5);
var obj = {p: 4};
var src2 = {a: 3, b: 5};
t.deepEqual(extend(src2, {c: obj}), {a: 3, b: 5, c: {p: 4}});
obj.p = 9;
t.deepEqual(src2, {a: 3, b: 5, c: {p: 9}});
var arrInner = [1, 2, 3];
var arrOuter = ['a', 'b', arrInner];
var src1 = {a: 3, b: 5};
t.deepEqual(extend(src1, {c: arrOuter}), {a: 3, b: 5, c: ['a', 'b', [1, 2, 3]]});
t.equal(arrOuter, src1.c);
t.equal(arrInner, src1.c[2]);
t.end();
});
test('shallow extend copies non-plain objects', function(t) {
t.plan(12);
var fn = function(a, b) {
return a + b;
};
var src2 = {a: 3, b: 5};
t.deepEqual(extend(src2, {c: fn}), {a: 3, b: 5, c: fn});
t.deepEqual(src2, {a: 3, b: 5, c: fn});
t.equal(src2.c(4, 2), 6);
fn.x = 34;
t.equal(src2.c.x, 34);
var date = new Date(1510439803151);
var src3 = {a: 3, b: 5};
t.deepEqual(extend(src3, {c: date}), {a: 3, b: 5, c: date});
t.deepEqual(src3, {a: 3, b: 5, c: date});
t.equal(src3.c.getTime(), 1510439803151);
date.x = 34;
t.equal(src3.c.x, 34);
var regex = /abc/;
var src3 = {a: 3, b: 5};
t.deepEqual(extend(src3, {c: regex}), {a: 3, b: 5, c: regex});
t.deepEqual(src3, {a: 3, b: 5, c: regex});
t.equal(src3.c.exec('ddabc').index, 2);
regex.x = 34;
t.equal(src3.c.x, 34);
t.end();
});
test('deep extend merges child objects', function(t) {
t.plan(2);
var obj = {a: {b: 'c'}};
var obj2 = {a: {c: 'd'}};
t.deepEqual(extend(true, obj, obj2), {a: {b: 'c', c: 'd'}});
t.deepEqual(obj, {a: {b: 'c', c: 'd'}});
t.end();
});
test('deep extend clones child plain objects and arrays', function(t) {
t.plan(5);
var obj = {p: 4};
var src2 = {a: 3, b: 5};
t.deepEqual(extend(true, src2, {c: obj}), {a: 3, b: 5, c: {p: 4}});
obj.p = 9;
t.deepEqual(src2, {a: 3, b: 5, c: {p: 4}});
var arrInner = [1, 2, 3];
var arrOuter = ['a', 'b', arrInner];
var src1 = {a: 3, b: 5};
t.deepEqual(extend(true, src1, {c: arrOuter}), {a: 3, b: 5, c: ['a', 'b', [1, 2, 3]]});
t.notEqual(arrOuter, src1.c);
t.notEqual(arrInner, src1.c[2]);
t.end();
});
test('deep extend does not clone child non-plain objects', function(t) {
t.plan(12);
var fn = function(a, b) {
return a + b;
};
var src2 = {a: 3, b: 5};
t.deepEqual(extend(true, src2, {c: fn}), {a: 3, b: 5, c: fn});
t.deepEqual(src2, {a: 3, b: 5, c: fn});
t.equal(src2.c(4, 2), 6);
fn.x = 34;
t.equal(src2.c.x, 34);
var date = new Date(1510439803151);
var src3 = {a: 3, b: 5};
t.deepEqual(extend(true, src3, {c: date}), {a: 3, b: 5, c: date});
t.deepEqual(src3, {a: 3, b: 5, c: date});
t.equal(src3.c.getTime(), 1510439803151);
date.x = 34;
t.equal(src3.c.x, 34);
var regex = /abc/;
var src3 = {a: 3, b: 5};
t.deepEqual(extend(true, src3, {c: regex}), {a: 3, b: 5, c: regex});
t.deepEqual(src3, {a: 3, b: 5, c: regex});
t.equal(src3.c.exec('ddabc').index, 2);
regex.x = 34;
t.equal(src3.c.x, 34);
t.end();
});
test('null values are copied', function(t) {
t.plan(2);
var src = {a: 3, b: 5};
t.deepEqual(extend(src, {a: null, c: null}), {a: null, b: 5, c: null});
t.deepEqual(src, {a: null, b: 5, c: null});
t.end();
});
test('explicitly undefined values are copied', function(t) {
t.plan(2);
var src = {a: 3, b: 5};
t.deepEqual(extend(src, {a: undefined, c: undefined}), {a: undefined, b: 5, c: undefined});
t.deepEqual(src, {a: undefined, b: 5, c: undefined});
t.end();
});
test('when no extenders, extendee is returned unmutated', function(t) {
t.plan(4);
var src = {a: 3, b: 5};
var srcRef = src;
t.deepEqual(src, extend(src));
t.equal(srcRef, src);
t.deepEqual(src, extend(true, src));
t.equal(srcRef, src);
t.end();
});
test('extendee and extenders can be functions', function(t) {
t.plan(4);
var fn = function() {};
var result = extend(fn, {a: 4});
t.ok(typeof result == 'function');
t.ok(result.a === 4);
var src = {a: 3};
var fn2 = function() {};
t.deepEqual(extend(true, src, {b: fn2}), {a: 3, b: fn2});
t.deepEqual(src, {a: 3, b: fn2});
t.end();
});
test('extender can be any primitive', function(t) {
t.plan(12);
var src1 = {a: 4};
t.deepEqual(extend(src1, null), {a: 4});
t.deepEqual(src1, {a: 4});
var src2 = {a: 3};
t.deepEqual(extend(src2, undefined), {a: 3});
t.deepEqual(src2, {a: 3});
var src3 = {a: 4};
t.deepEqual(extend(src3, 'hello'), {0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o', a: 4});
t.deepEqual(src3, {0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o', a: 4});
var src4 = {1: 9, a: 4};
t.deepEqual(extend(src4, 'hello', 4), {0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o', a: 4});
t.deepEqual(src4, {0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o', a: 4});
var src5 = {a: 4};
t.deepEqual(extend(src5, false), {a: 4});
t.deepEqual(src5, {a: 4});
var src6 = {a: 4};
t.deepEqual(extend(src6, 3, true), {a: 4});
t.deepEqual(src6, {a: 4});
t.end();
});
test("extendee can't be a primitive", function(t) {
t.plan(9);
t.throws(function() {
extend(null, {b: 4, c: 5});
});
t.throws(function() {
extend(undefined, {b: 4, c: 5});
});
t.throws(function() {
extend('hello', {b: 4, c: 5});
});
t.throws(function() {
extend(3, {b: 4, c: 5});
});
t.throws(function() {
extend(false, null, {b: 4, c: 5});
});
t.throws(function() {
extend(true, undefined, {b: 4, c: 5});
});
t.throws(function() {
extend(true, 'hello', {b: 4, c: 5});
});
t.throws(function() {
extend(false, 3, {b: 4, c: 5});
});
t.throws(function() {
extend(false, false, {b: 4, c: 5});
});
t.end();
});