forked from Flotype/now
/
nowUtil.js
657 lines (569 loc) · 22.1 KB
/
nowUtil.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/**
* nowUtil.js
*
* Various utility functions used by both client and server side
*
*/
var nowUtil = {
hasProperty: function(obj, prop) {
return Object.prototype.hasOwnProperty.call(Object(obj), prop);
},
// Creates the serialized form of functions to be sent over the wire
serializeFunction: function(fqn, func) {
return {type: "function", fqn: fqn};
},
// Find all fqns of properties in parentObj and add them to the given blacklist, prepened by parentFqn
addChildrenToBlacklist: function(blacklist, parentObj, parentFqn){
for(var prop in parentObj){
if(nowUtil.hasProperty(parentObj, prop)){
blacklist[(parentFqn+"."+prop)] = true;
if(parentObj[prop] && typeof parentObj[prop] === 'object'){
nowUtil.addChildrenToBlacklist(blacklist, parentObj[prop], parentFqn+"."+prop);
}
}
}
},
// Remove all fqns contained in parentObj from given blacklist
removeChildrenFromBlacklist: function(blacklist, parentObj, parentFqn){
for(var prop in parentObj){
if(nowUtil.hasProperty(parentObj, prop)){
delete blacklist[(parentFqn+"."+prop)];
if(parentObj[prop] && typeof parentObj[prop] === 'object'){
nowUtil.removeChildrenFromBlacklist(blacklist, parentObj[prop], parentFqn+"."+prop);
}
}
}
},
// Return array of all fqns of parentObj, prepended by parentFqn, recursively
getAllChildFqns: function(parentObj, parentFqn){
var fqns = [];
function getAllChildFqnsHelper(parentObj, parentFqn){
for(var prop in parentObj){
if(nowUtil.hasProperty(parentObj, prop)) {
fqns.push(parentFqn+"."+prop);
if(parentObj[prop] && typeof parentObj[prop] === 'object'){
getAllChildFqnsHelper(parentObj[prop], parentFqn+"."+prop);
}
}
}
}
getAllChildFqnsHelper(parentObj, parentFqn);
return fqns;
},
// This is the client-side equivalent of the proxy object from wrap.js.
// Takes an object and the name of a property, calls the handler function when that property changes
watch: function (obj, prop, fqn, handler) {
var val = obj[prop];
var getter = function () {
return val;
};
var setter = function (newVal) {
var oldval = val;
val = newVal;
handler.call(obj, prop, fqn, oldval, newVal);
return newVal;
};
if (Object.defineProperty) {// ECMAScript 5
Object.defineProperty(obj, prop, {
get: getter,
set: setter
});
} else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
Object.prototype.__defineGetter__.call(obj, prop, getter);
Object.prototype.__defineSetter__.call(obj, prop, setter);
}
},
// Used in initialization to serialize obj and send it over the given socket
initializeScope: function(obj, socket) {
var data = nowUtil.decycle(obj, 'now', [nowUtil.serializeFunction]);
var scope = data[0];
nowUtil.debug("initializeScope", JSON.stringify(data));
nowUtil.print(scope);
socket.send({type: 'createScope', data: {scope: scope}});
},
// Cross-browser isArray
isArray: Array.isArray || function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
},
// Attempt to traverse the scope and return the property the fqn represents. Returns false if not found
getVarFromFqn: function(fqn, scope){
var path = fqn.split(".");
path.shift();
var currVar = scope;
while(path.length > 0){
var prop = path.shift();
if(nowUtil.hasProperty(currVar, prop)) {
currVar = currVar[prop];
} else {
return false;
}
}
return currVar;
},
// Traverse scope to get the parent object of the given fqn
getVarParentFromFqn: function(fqn, scope){
var path = fqn.split(".");
path.shift();
var currVar = scope;
while(path.length > 1){
var prop = path.shift();
currVar = currVar[prop];
}
return currVar;
},
// Traverse scope to get the parent object of the given fqn, or create and return it if it does't exist
forceGetParentVarAtFqn: function(fqn, scope){
var path = fqn.split(".");
path.shift();
var currVar = scope;
while(path.length > 1){
var prop = path.shift();
if(!nowUtil.hasProperty(currVar, prop)){
if(!isNaN(path[0])) {
currVar[prop] = [];
} else {
currVar[prop] = {};
}
}
currVar = currVar[prop];
}
return currVar;
},
// Check if is <IE9 by defineProperty feature detection
isLegacyIE: function(){
try {
Object.defineProperty({}, '', {});
return false;
} catch (err) {
return true;
}
return true;
},
// Traverse scope to get parent object of given fqn for multiple scopes simultaneously, creating and returning the object if it doesn't exist
multiForceGetParentVarAtFqn: function(fqn, scopes){
var path = fqn.split(".");
path.shift();
var currVar = scopes.slice(0);
while(path.length > 1){
var prop = path.shift();
for(var i in scopes) {
if(!nowUtil.hasProperty(currVar[i], prop)){
if(!isNaN(path[0])) {
currVar[i][prop] = [];
} else {
currVar[i][prop] = {};
}
}
currVar[i] = currVar[i][prop];
}
}
return currVar;
},
// Insert the value in the scope at the fqn, creating objects as necessary so the fqn exists
createVarAtFqn: function(fqn, scope, value){
var path = fqn.split(".");
var currVar = nowUtil.forceGetParentVarAtFqn(fqn, scope);
currVar[path.pop()] = value;
},
// createVarAtFqn for multiple scopes
multiCreateVarAtFqn: function(fqn, scopes, value){
var path = fqn.split(".");
var key = path.pop();
var currVar = nowUtil.multiForceGetParentVarAtFqn(fqn, scopes);
var i;
if (value && typeof value === "object"){
if(nowUtil.isArray(value)) {
for(i in scopes) {
currVar[i][key] = [];
}
} else {
for(i in scopes) {
currVar[i][key] = {};
}
}
nowUtil.multiMergeScopes(currVar, key, value);
} else {
for(i in scopes) {
currVar[i][key] = value;
}
}
},
// Insert the value in the scope at the fqn, but add objects along the traversal to the blacklist, prepended with blacklistFqn
createAndBlacklistVarAtFqn: function(fqn, scope, value, blacklist, blacklistFqn){
var path = fqn.split(".");
path.shift();
var currVar = scope;
while(path.length > 1){
var prop = path.shift();
blacklistFqn += "."+prop;
if(!nowUtil.hasProperty(currVar, prop)){
if(!isNaN(path[0])) {
blacklist[blacklistFqn] = true;
currVar[prop] = [];
} else {
blacklist[blacklistFqn] = true;
currVar[prop] = {};
}
}
currVar = currVar[prop];
}
var finalProp = path.pop();
blacklist[fqn] = true;
currVar[finalProp] = value;
},
// Create var at fqn but instead of simply inserting the value, make a deep copy of the value
deepCreateVarAtFqn: function(fqn, scope, value){
var path = fqn.split(".");
path.shift();
var currVar = nowUtil.getVarParentFromFqn(fqn, scope);
if (value && typeof value === "object"){
var prop = path.pop();
if(nowUtil.isArray(value)) {
currVar[prop] = [];
} else {
currVar[prop] = {};
}
nowUtil.mergeScopes(currVar[prop], value);
} else {
currVar[path.pop()] = value;
}
},
// Deeply copy all children of incoming to current, with overwriting
mergeScopes: function(current, incoming) {
for(var prop in incoming){
if(nowUtil.hasProperty(incoming, prop)) {
if(incoming[prop] && typeof incoming[prop] === "object"){
if(!nowUtil.hasProperty(current, prop)){
if(nowUtil.isArray(incoming[prop])) {
current[prop] = [];
} else {
current[prop] = {};
}
}
nowUtil.mergeScopes(current[prop], incoming[prop]);
} else {
current[prop] = incoming[prop];
}
}
}
},
// Deeply copy all children of incoming into each scope at current[i][key], with overwriting
multiMergeScopes: function(current, key, incoming) {
var i;
for(var prop in incoming){
if(nowUtil.hasProperty(incoming, prop)){
if(incoming[prop] && typeof incoming[prop] === "object"){
var newCurrent = [];
for(i in current) {
if(nowUtil.hasProperty(current, i)) {
var curItem = current[i][key];
if(!nowUtil.hasProperty(curItem, prop)){
if(nowUtil.isArray(incoming[prop])) {
curItem[prop] = [];
} else {
curItem[prop] = {};
}
}
newCurrent.push(current[i][key]);
}
}
nowUtil.multiMergeScopes(newCurrent, prop, incoming[prop]);
} else {
for(i in current) {
if(nowUtil.hasProperty(current, i)) {
current[i][key][prop] = incoming[prop];
}
}
}
}
}
},
// Create a shallow copy of incoming
shallowCopy: function(incoming) {
if(nowUtil.isArray(incoming)) {
return incoming.slice();
} else if(incoming && typeof incoming === "object") {
var target = {};
for(var key in incoming) {
if(nowUtil.hasProperty(incoming, key)) {
target[key] = incoming[key];
}
}
return target;
} else {
return incoming;
}
},
// Like multiMergeScopes but overwrite along the way with new nested objects
multiDeepCopy: function(targets, incoming) {
var i;
if(incoming && typeof incoming === "object") {
for(var prop in incoming){
if(nowUtil.hasProperty(incoming, prop)) {
if(incoming[prop] && typeof incoming[prop] === "object") {
var next = [];
for(i = 0; i < targets.length; i++){
if(nowUtil.isArray(incoming[prop])) {
targets[i][prop] = [];
} else {
targets[i][prop] = {};
}
next[i] = targets[i][prop];
}
nowUtil.multiDeepCopy(next, incoming[prop]);
} else {
for(i = 0; i < targets.length; i++){
targets[i][prop] = incoming[prop];
}
}
}
}
} else {
for(i = 0; i < targets.length; i++){
targets[i] = incoming;
}
}
return targets;
},
// Take the hash of changes and insert into each give scope
// Changes is a map of fqn's to new values
mergeChanges: function(scopes, changes) {
for(var fqn in changes) {
nowUtil.multiCreateVarAtFqn(fqn, scopes, changes[fqn]);
}
},
// Log errors
debug: function(func, msg){
//console.log(func + ": " + msg);
if( 'undefined'!=typeof(window) && window.debug ) {
window.debug(func + ": " + msg);
}
else if( 'undefined'!=typeof(console) && console.__debug__ ) {
console.log(func + ": " + msg);
}
},
// Log errors
error: function(err){
console.log(err);
if(nowUtil.hasProperty(err, 'stack')){
console.log(err.stack);
}
},
// Log errors
print: function(msg) {
//console.log(msg);
},
// Pass in an object, a prefix for the fqn, and an array of funcHandlers that take a function and return a modified version
// Returns an array of objects, each corresponding with one modified by one of the funcHandlers in the parameter array
// Also change cyclical objects to a representation describing which object it is cyclically pointing to
decycle: function decycle(object, key, funcHandlers) {
"use strict";
var objects = [],
paths = [];
return (function derez(value, path, fqn) {
var i, j, nu = [];
var output = [];
switch (typeof value) {
case 'object':
if (!value) {
for(i = 0; i < funcHandlers.length; i += 1) {
nu.push(null);
}
return nu;
}
for (i = 0; i < objects.length; i += 1) {
if (objects[i] === value) {
for(i = 0; i < funcHandlers.length; i += 1) {
nu.push({$ref: paths[i]});
}
return nu;
}
}
objects.push(value);
paths.push(path);
var values;
if (Object.prototype.toString.apply(value) === '[object Array]') {
nu = [];
for(i = 0; i < funcHandlers.length; i += 1) {
nu.push([]);
}
for (i = 0; i < value.length; i += 1) {
values = derez(value[i], path + '[' + i + ']', fqn+"."+i);
for(j in values) {
if (nowUtil.hasProperty(values, j)) {
nu[j][i] = values[j];
}
}
}
} else {
nu = [];
for(i = 0; i < funcHandlers.length; i += 1) {
nu.push({});
}
for (var name in value) {
if (nowUtil.hasProperty(value, name)) {
values = derez(value[name], path + '[' + JSON.stringify(name) + ']', fqn+"."+name);
for(j in values) {
if (nowUtil.hasProperty(values, j)) {
nu[j][name] = values[j];
}
}
}
}
}
return nu;
case 'function':
for(i = 0; i < funcHandlers.length; i += 1) {
output[i] = funcHandlers[i](fqn, value);
}
return output;
case 'undefined':
for(i = 0; i < funcHandlers.length; i += 1) {
nu.push(undefined);
}
return nu;
case 'number':
case 'string':
case 'boolean':
for(i = 0; i < funcHandlers.length; i += 1) {
output[i] = value;
}
return output;
}
}(object, '$', key));
},
// Take $ and deserialize by replacing objects with object.type == function with the return value of funcHandlers
// Restore cyclical structures as serialized by decycle
retrocycle: function retrocycle($, funcHandler) {
"use strict";
var px = /^\$(?:\[(?:\d?|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/;
(function rez(value) {
var i, item, name, path;
if (value && typeof value === 'object') {
if (Object.prototype.toString.apply(value) === '[object Array]') {
for (i = 0; i < value.length; i += 1) {
item = value[i];
if(nowUtil.hasProperty(item, "type") && item.type === 'function') {
value[i] = funcHandler(value[i]);
item = value[i];
}
if (item && typeof item === 'object') {
path = item.$ref;
if (typeof path === 'string' && px.test(path)) {
value[i] = eval(path);
} else {
rez(item);
}
}
}
} else {
for (name in value) {
if (value[name] && typeof value[name] === 'object') {
item = value[name];
if (item) {
if(nowUtil.hasProperty(item, "type") && item.type === 'function') {
value[name] = funcHandler(value[name]);
} else {
path = item.$ref;
if (typeof path === 'string' && px.test(path)) {
value[name] = eval(path);
} else {
rez(item);
}
}
}
}
}
}
}
})($);
return $;
},
// Merge incoming into target , without overwriting
// When a function is encounter, replace with return value of mapFn applied with the function as a parameter
mapAndMergeFunctionsInScopes: function(target, incoming, mapFn){
nowUtil.mapAndMergeFunctionsInScopeHelper(target, incoming, mapFn, 'now');
},
mapAndMergeFunctionsInScopesHelper: function(target, incoming, mapFn, fqn){
for(var key in incoming){
fqn += '.' + key;
if(target && !nowUtil.hasProperty(target, key)){
if(nowUtil.isArray(incoming[key])){
target[key] = [];
nowUtil.mapAndMergeFunctionsInScopeHelper(target[key], incoming[key], mapFn, fqn);
} else if (typeof incoming[key] == 'object'){
target[key] = {};
nowUtil.mapAndMergeFunctionsInScopeHelper(target[key], incoming[key], mapFn, fqn);
} else if(typeof incoming[key] == 'function'){
target[key] = mapFn(fqn, func);
}
}
}
},
// Merge incomingScope into the targetGroups's now object, replacing functions with corresponding multicallers and without overwriting
multiMergeFunctionsToMulticallers: function(targetGroups, incomingScope){
var targetScopes = {};
for(var i in targetGroups){
targetScopes[i] = targetGroups[i].nowScope;
}
nowUtil.multiMergeFunctionsToMulticallersHelper(targetGroups, targetScopes, incomingScope, 'now');
},
multiMergeFunctionsToMulticallersHelper: function(targetGroups, targetScopes, incomingScope, fqn){
for(var key in incomingScope){
var newFqn = fqn + '.' + key;
for(var i in targetScopes){
var target = targetScopes[i];
if(target && !nowUtil.hasProperty(target, key)){
if(nowUtil.isArray(incomingScope[key])){
target[key] = [];
targetScopes[i] = target[key];
nowUtil.multiMergeFunctionsToMulticallersHelper(targetGroups, targetScopes, incomingScope[key], newFqn);
} else if (typeof incomingScope[key] == 'object'){
target[key] = {};
targetScopes[i] = target[key];
nowUtil.multiMergeFunctionsToMulticallersHelper(targetGroups, targetScopes, incomingScope[key], newFqn);
} else if(typeof incomingScope[key] == 'function'){
target[key] = targetGroups[i].generateMultiCaller(newFqn);
}
}
}
}
},
// Generate unique ids
generateRandomString: function(){
return Math.random().toString().substr(2);
}
};
// JSON shim for older IE
if('window' in this) {
window.nowUtil = nowUtil;
if(!('JSON' in window)){
JSON={};
(function(){"use strict";function f(n){return n<10?'0'+n:n;}
if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+'-'+
f(this.getUTCMonth()+1)+'-'+
f(this.getUTCDate())+'T'+
f(this.getUTCHours())+':'+
f(this.getUTCMinutes())+':'+
f(this.getUTCSeconds())+'Z':null;};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};}
var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}
function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);}
if(typeof rep==='function'){value=rep.call(holder,key,value);}
switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}
gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}
v=partial.length===0?'[]':gap?'[\n'+gap+partial.join(',\n'+gap)+'\n'+mind+']':'['+partial.join(',')+']';gap=mind;return v;}
if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){if(typeof rep[i]==='string'){k=rep[i];v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}else{for(k in value){if(nowUtil.hasProperty(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}
v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}
if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' ';}}else if(typeof space==='string'){indent=space;}
rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}
return str('',{'':value});};}
if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(nowUtil.hasProperty(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}
return reviver.call(holder,key,value);}
text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}
if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof reviver==='function'?walk({'':j},''):j;}
throw new SyntaxError('JSON.parse');};}}());
}
} else {
exports.nowUtil = nowUtil;
}