-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
309 lines (301 loc) · 7.93 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* @author Gilles Coomans <gilles.coomans@gmail.com>
*/
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(["require", "deep-utils/index"], function(require, utils) {
"use strict";
function getJSPrimitiveType(obj) {
if (obj && obj.forEach)
return "array";
return typeof obj;
}
var compiler = {};
compiler.Shared = function(datas) {
datas._deep_shared_ = true;
return datas;
};
compiler.compile = function() {
var base = utils.copy(arguments[0]);
var len = arguments.length;
for (var i = 1; i < len; ++i)
compiler.aup(arguments[i], base);
return base;
};
compiler.decorateUpFrom = function(src, target, properties) {
properties.forEach(function(prop) {
if (typeof src[prop] !== 'undefined')
target[prop] = compiler.aup(src[prop], target[prop]);
});
};
compiler.decorateBottomFrom = function(src, target, properties) {
properties.forEach(function(prop) {
if (typeof src[prop] !== 'undefined')
target[prop] = compiler.abottom(src[prop], target[prop]);
});
};
compiler.upFromArgs = function(base, args, opt) {
for (var len = args.length - 1; len > -1; --len)
base = compiler.aup(args[len], base, opt);
return base;
};
compiler.bottomFromArgs = function(base, args, opt) {
for (var len = args.length - 1; len > -1; --len)
base = compiler.abottom(args[len], base, opt);
return base;
};
compiler.aup = function(src, target, opt) {
if (typeof src === 'undefined')
return target;
if (src === null)
return null;
opt = opt || {};
if (typeof target === 'undefined' || target === null)
return utils.copy(src, false, opt.excludeGrounds);
if (target._deep_compiler_) {
target = target._up(src);
return target;
}
if (src._deep_compiler_) {
if (src._clone)
src = src._clone();
return src._bottom(target);
}
if (src._deep_shared_) {
src._deep_shared_ = false;
target = compiler.abottom(target, src);
src._deep_shared_ = true;
return target;
}
if (target._deep_shared_) {
target._deep_shared_ = false;
target = compiler.abottom(target, src);
target._deep_shared_ = true;
return target;
}
var srcType = getJSPrimitiveType(src);
var targetType = getJSPrimitiveType(target);
if (srcType !== targetType) {
target = utils.copy(src, false, opt.excludeGrounds);
return target;
}
switch (srcType) {
case 'array':
var result = compiler.upArray(src, target);
return result;
case 'object':
if (src instanceof RegExp)
return src;
if (src instanceof Date) {
target = new Date(src.valueOf());
return target;
}
for (var i in src) {
if (i == '_backgrounds' || i == '_foregrounds' || i == "_transformations")
if (opt.excludeGrounds)
continue;
else {
target[i] = target[i] ? target[i].concat(src[i]) : src[i].slice();
continue;
}
if (typeof target[i] === 'undefined') {
target[i] = utils.copy(src[i]);
continue;
}
if (src[i] === null) {
target[i] = null;
continue;
}
if (typeof src[i] === 'object' || typeof src[i] === 'function') {
target[i] = compiler.aup(src[i], target[i]); //, target, i);
} else
target[i] = src[i];
if (target[i] && target[i]._deep_remover_)
delete target[i];
}
return target;
default:
return src;
}
};
compiler.abottom = function(src, target, opt) {
opt = opt || {};
if (src === null || typeof src === "undefined")
return target;
if (target === null)
return target;
if (typeof target === 'undefined') {
target = utils.copy(src, false, opt.excludeGrounds);
return target;
}
if (target._deep_compiler_) {
target = target._bottom(src);
return target;
}
if (src._deep_compiler_) {
if (src._clone)
src = src._clone();
src._up(target);
return src;
}
if (src._deep_shared_) {
src._deep_shared_ = false;
src = compiler.aup(target, src);
src._deep_shared_ = true;
return src;
}
if (target._deep_shared_) {
target._deep_shared_ = false;
src = compiler.aup(target, src); //, parent, key);
target._deep_shared_ = true;
return src;
}
var srcType = getJSPrimitiveType(src);
var targetType = getJSPrimitiveType(target);
if (srcType !== targetType)
return target;
switch (srcType) {
case 'array':
var result = compiler.bottomArray(src, target);
return result;
case 'object':
for (var i in src) {
if (i == '_backgrounds' || i == '_foregrounds' || i == "_transformations")
if (opt.excludeGrounds)
continue;
else {
target[i] = target[i] ? src[i].concat(target[i]) : src[i].slice();
continue;
}
if (src[i] !== null) {
if (typeof target[i] === 'undefined')
target[i] = utils.copy(src[i]);
else if (typeof src[i] === 'object' || typeof src[i] === 'function')
target[i] = compiler.abottom(src[i], target[i]); //, target, i);
if (target[i] && target[i]._deep_remover_)
delete target[i];
}
}
var copied = utils.shallowCopy(target);
for (i in target)
delete target[i];
for (i in src) {
if ((i == '_backgrounds' || i == '_foregrounds' || i == '_transformations') && opt.excludeGrounds)
continue;
target[i] = copied[i];
delete copied[i];
}
for (i in copied)
target[i] = copied[i];
return target;
default:
return target;
}
};
/**
* up : merge object from up
* @return {Dynamic} the merged object
*/
compiler.up = function() {
var target = arguments[0];
for (var i = 1, len = arguments.length; i < len; i++)
target = compiler.aup(arguments[i], target);
return target;
};
/**
* bottom : merge object from bottom
* @return {Dynamic} the merged object
*/
compiler.bottom = function() {
var target = arguments[arguments.length - 1],
src = arguments[0];
for (var i = arguments.length - 2; i > 0; i--)
target = compiler.abottom(arguments[i], target);
return compiler.abottom(src, target);
};
compiler.bottomArray = function(src, target, mergeOn, excludeGrounds) {
if (src.length === 0)
return target;
var map = {};
var len = src.length,
val = null,
i = 0;
for (; i < len; ++i) {
var a = src[i];
if (a && mergeOn)
val = utils.fromPath(a, mergeOn);
else if (a && a.id)
val = a.id;
else
val = String(a);
map[val] = {
ref: a,
index: i
};
}
Array.prototype.unshift.apply(target, src); // prepend src to target
var elem = target[i]; // check target from target[src.length]
while (i < target.length) // seek after collision in target, apply it, and remove given element from target
{
// catch current value
if (elem && mergeOn)
val = utils.fromPath(elem, mergeOn);
else if (elem && elem.id)
val = elem.id;
else
val = String(elem);
if (map[val]) {
target[map[val.index]] = compiler.aup(elem, map[val].ref, {
excludeGrounds: excludeGrounds
});
target.splice(i, 1); // remove collided element from t
}
elem = target[++i];
}
return target;
};
compiler.upArray = function(src, target, mergeOn, excludeGrounds) {
if (src.length === 0)
return target;
var map = {};
var len = target.length,
val = null,
i = 0;
for (; i < len; ++i) {
var a = target[i];
if (a && mergeOn)
val = utils.fromPath(a, mergeOn);
else if (a && a.id)
val = a.id;
else
val = String(a);
map[val] = {
ref: a,
index: i
};
}
i = 0;
var elem = src[i],
length = src.length; // check target from target[src.length]
while (i < length) // seek after collision in target, apply it, and remove given element from target
{
// catch current value
if (elem && mergeOn)
val = utils.fromPath(elem, mergeOn);
else if (elem && elem.id)
val = elem.id;
else
val = String(elem);
if (map[val])
target[map[val.index]] = compiler.aup(elem, map[val].ref, {
excludeGrounds: excludeGrounds
});
else
target.push(elem);
elem = src[++i];
}
return target;
};
return compiler;
});