forked from shimondoodkin/nodejs-clone-extend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merger.js
executable file
·243 lines (222 loc) · 5.62 KB
/
merger.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
//var sys = require('sys'); // enable for debugging this module
function replace(a, b)
{
if (!b)
{
return a;
}
var key;
for (key in b)
{
if(b.hasOwnProperty(key))
{
a[key] = b[key];
}
}
return a;
} this.replace=replace;
function add(a, b)
{
if (!b)
{
return a;
}
var key;
for (key in b)
{
if(b.hasOwnProperty(key))
{
if(typeof a[key] === 'undefined' || a[key]===null)
{
a[key] = b[key];
}
}
}
return a;
} this.add=add;
function extend(a, b, context, newobjs, aparent, aname, haveaparent) // context is anti circular references mechanism
{
if (a==b){ return a;}
if (!b) { return a;}
var key, clean_context=false, return_sublevel=false,b_pos;
if(!haveaparent){ aparent={'a':a}; aname='a'; }
if(!context){clean_context=true;context=[];newobjs=[];}
b_pos=context.indexOf(b);
if( b_pos==-1 ) {context.push(b);newobjs.push([aparent, aname]);} else { return newobjs[b_pos][0][ newobjs[b_pos][1] ]; }
for (key in b)
{
if(b.hasOwnProperty(key))
{
if(typeof a[key] === 'undefined')
{
if(typeof b[key] === 'object')
{
if( b[key] instanceof Array ) // http://javascript.crockford.com/remedial.html
{a[key] = extend([], b[key],context,newobjs,a,key,true);}
else if(b[key]===null)
{a[key] = null;}
else
{a[key] = extend({}, b[key],context,newobjs,a,key,true); /*a[key].constructor = b[key].constructor; a[key].prototype = b[key].prototype;*/ }
}
else
{ a[key] = b[key]; }
}
else if(typeof a[key] === 'object' && a[key] !== null)
{ a[key] = extend(a[key], b[key],context,newobjs,a,key,true); /*a[key].constructor = b[key].constructor; a[key].prototype = b[key].prototype;*/ }
else
{ a[key] = b[key]; }
}
}
if(clean_context) {context=null;newobjs=null;}
if(!haveaparent)
{
aparent=null;
return a;
}
if(typeof a === 'object' && !(a instanceof Array) )
{
/*a.constructor = b.constructor;
a.prototype = b.prototype*/;
}
return a;
} this.extend=extend;
function extenduptolevel(a, b, levels, context, newobjs, aparent, aname, haveaparent)
{
if (a==b){ return a;}
if (!b){ return a;}
var key, clean_context=false, return_sublevel=false;
if(!haveaparent){ aparent={'a':a}; aname='a'; }
if(!context){clean_context=true;context=[];newobjs=[];}
b_pos=context.indexOf(b);
if( b_pos==-1 ) {context.push(b);newobjs.push([aparent, aname]);} else { return newobjs[b_pos][0][ newobjs[b_pos][1] ]; }
for (key in b)
{
if(b.hasOwnProperty(key))
{
if(typeof a[key] === 'undefined')
{
if(typeof b[key] === 'object' && levels>0)
{
if( b[key] instanceof Array ) // http://javascript.crockford.com/remedial.html
{ a[key] = extenduptolevel([], b[key],levels-1,context,newobjs,a,key,true); }
else if(b[key]===null)
{ a[key] = null; }
else
{ a[key] = extenduptolevel({}, b[key],levels-1,context,newobjs,a,key,true); }
}
else
{ a[key] = b[key]; }
}
else if(typeof a[key] === 'object' && a[key] !== null && levels>0)
{ a[key] = extenduptolevel(a[key], b[key],levels-1,context,newobjs,a,key,true); }
else
{ a[key] = b[key]; }
}
}
if(clean_context) {context=null;newobjs=null;}
if(!haveaparent)
{
aparent=null;
return a;
}
if(typeof a === 'object' && !(a instanceof Array) )
{
/*a.constructor = b.constructor;
a.prototype = b.prototype;*/
}
return a;
} this.extenduptolevel=extenduptolevel;
function clone(obj)
{
if (typeof obj === 'object')
{
if (obj ===null ) { return null; }
if (obj instanceof Array )
{ return extend([], obj); }
else
{ return extend({}, obj); }
}
return obj;
} this.clone=clone;
function cloneextend(obj,exteddata)
{
if (typeof obj === 'object')
{
if (obj ===null ) { return null; }
return extend(clone(obj),exteddata);
}
return obj;
} this.cloneextend=cloneextend;
function cloneuptolevel(obj,level) // clone only numlevels levels other levels leave references
{
if (typeof obj === 'object')
{
if (obj ===null ) { return null; }
if (obj instanceof Array ) { return extenduptolevel([], obj,level); }
return extenduptolevel({}, obj,level);
}
return obj;
} this.cloneuptolevel=cloneuptolevel;
function ObjforEach(object, block, context)
{
for (var key in object)
{
if(object.hasOwnProperty(key))
{
block.call(context, object[key], key, object);
}
}
}
function foreach(object, block, context)
{
if (object)
{
if (typeof object === "object" && object instanceof Array)
return object.forEach(object, block, context)
else //if (typeof object === "object") // or (object instanceof Function)...
{
if(object)
for (var key in object)
{
if(object.hasOwnProperty(key))
{
block.call(context, object[key], key, object);
}
}
}
}
} this.foreach=foreach;
/*
hasbugs and useless yet interesting maybe developed layer for dot pathed object transformation:
{
'foo.bar':'bluebar',
'foo.color':'blue'
}
transforemed to
{
foo:
{
bar:'bluebar',
color:'blue'
}
}
//
function dotpath(data,dotkeys,create)
{
if(!create){var create=false;}
if(create) if(!data) data={};
var keys= dotkeys.split("."),value=data;
for (var i=0;i<keys.length;i++)
{
if(!value[keys[i]]) //should you create one?
{
if(create)
value[keys[i]]={};
else
return undefined;
}
value = value[keys[i]];
}
return value;
}
*/