-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJSMapr.js
691 lines (586 loc) · 18 KB
/
JSMapr.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// https://github.com/mikedunker/JSMapr
// License: MIT
// Version: 0.0.1
(function () {
var sep = "/";
var mapCommands = [];
var logFunc = null;
function log() {
if (isFunction(logFunc)) {
var str = "";
for (var i = 0, len=arguments.length; i < len; i++) {
var arg = arguments[i];
if (isObjectOrArray(arg)) {
str += JSON.stringify(arg) + " ";
}
else {
str += arg.toString() + " ";
}
}
logFunc(str);
}
}
function isString(v) {
return (v && v.constructor === String);
}
function isArray(v) {
return (v && v.constructor.name === "Array");
}
function isObject(v) {
return (v && v.constructor.name !== "Array" && v === Object(v));
}
function isObjectOrArray(v) {
return isArray(v) || isObject(v);
}
function isFunction(v) {
return (v && Object.prototype.toString.call(v) == '[object Function]');
}
JSMapr = function(newSep) {
if (isString(newSep)) {
sep = newSep;
}
};
JSMapr.prototype.setMapCommands = function(newCommands) {
if (isArray(newCommands)) {
mapCommands = newCommands;
} else if (isObject(newCommands) && isString(newCommands.op)) {
// looks like a single mapping object, wrap in array
mapCommands = [ newCommands ];
}
};
JSMapr.prototype.setLocSeparator = function(newSep) {
if (isString(newSep) && newSep.length === 1) {
sep = newSep;
}
};
JSMapr.prototype.setLoggingFunc = function(newLogFunc) {
if (isFunction(newLogFunc)) {
logFunc = newLogFunc;
}
};
function isLocValid(loc) {
// root is ok
if (loc === sep) return true;
var locArray = loc.split(sep);
// first element after split should be empty
if (locArray[0] !== "") return false;
for (var i=1, aryLen = locArray.length; i < aryLen; i++) {
// each location part must have some length
if ((locArray[i]).length === 0) return false;
}
return true;
}
function isSubLoc(parent, child) {
if (parent == child.substring(0,parent.length) &&
child[parent.length] === sep) {
return true;
}
return false;
}
function getObjectAtLoc(srcObj, loc) {
if (loc === sep) {
// object at root is srcObj
return srcObj;
}
// split into parts
var locArray = loc.split(sep);
// save last part for end
var p = locArray.pop();
// navigate to location
var navObj = srcObj;
for (var i=1, j; navObj && (j=locArray[i]); i++) {
navObj = (j in navObj ? navObj[j] : undefined);
}
return navObj && p ? (navObj[p]) : undefined;
}
function getTypeofLoc(srcObj, loc) {
var val = getObjectAtLoc(srcObj, loc);
// check null and array first, typeof them === 'object'
if (val === null) return "null";
else if (isArray(val)) return "array";
else return typeof val; // string, number, boolean, object
}
function setObjectAtLoc(srcObj, loc, newObj) {
if (loc === sep) {
// replacing root with newObj
return newObj;
}
// split into parts
var locArray = loc.split(sep);
// save last part for end
var p = locArray.pop();
// navigate to location
var navObj = srcObj;
for (var i=1, j; navObj && (j=locArray[i]); i++) {
navObj = (j in navObj ? navObj[j] : navObj[j]={});
}
// set the object
if (navObj && p) {
navObj[p] = newObj;
}
return srcObj;
}
function deleteObjectAtLoc(srcObj, loc) {
if (loc === sep) {
// deleting root not supported
return false;
}
// split into parts
var locArray = loc.split(sep);
// save last part for end
var p = locArray.pop();
// navigate to location
var navObj = srcObj;
for (var i=1, j; navObj && (j=locArray[i]); i++) {
navObj = (j in navObj ? navObj[j] : undefined);
}
// delete the object
if (navObj && p && navObj[p] !== undefined) {
delete navObj[p];
return true;
}
return false;
}
function opAdd(srcObj, loc, elemToAdd) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
return setObjectAtLoc(srcObj, loc, elemToAdd);
}
function opDel(srcObj, loc) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
deleteObjectAtLoc(srcObj, loc);
return srcObj;
}
function opCopy(srcObj, srcLoc, destLoc) {
// if not valid locations, abort
if (!isLocValid(srcLoc) || !isLocValid(destLoc)) return srcObj;
// if the same location, nothing to do
if (srcLoc == destLoc) return srcObj;
// if destLoc is child of srcLoc, can't do that copy
if (isSubLoc(srcLoc, destLoc)) return srcObj;
var val = getObjectAtLoc(srcObj, srcLoc);
if (val !== undefined) {
if (destLoc === sep) {
// can't copy to root (must have a name)
return srcObj;
}
srcObj = setObjectAtLoc(srcObj, destLoc, val);
}
return srcObj;
}
function opMove(srcObj, srcLoc, destLoc) {
// if not valid locations, abort
if (!isLocValid(srcLoc) || !isLocValid(destLoc)) return srcObj;
// if the same location, nothing to do
if (srcLoc == destLoc) return srcObj;
// if destLoc is child of srcLoc, can't do that move
if (isSubLoc(srcLoc, destLoc)) return srcObj;
var val = getObjectAtLoc(srcObj, srcLoc);
if (val !== undefined) {
if (srcLoc === sep) {
// wrapping root
var newRoot = {};
srcObj = setObjectAtLoc(newRoot, destLoc, val);
return srcObj;
}
else if (destLoc === sep) {
// moving this to root, validate that this is an object or array
if (isObjectOrArray(val)) {
// replace srcObj with val
return val;
} else {
// not valid to move to root
return srcObj;
}
}
deleteObjectAtLoc(srcObj, srcLoc);
srcObj = setObjectAtLoc(srcObj, destLoc, val);
}
return srcObj;
}
function opToString(srcObj, loc) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
var val = getObjectAtLoc(srcObj, loc);
if (val !== undefined) {
val = val.toString();
srcObj = setObjectAtLoc(srcObj, loc, val);
}
return srcObj;
}
function opStringify(srcObj, loc) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
var val = getObjectAtLoc(srcObj, loc);
if (val !== undefined) {
val = JSON.stringify(val);
srcObj = setObjectAtLoc(srcObj, loc, val);
}
return srcObj;
}
function opAddToArray(srcObj, loc, elemToAdd) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
var val = getObjectAtLoc(srcObj, loc);
if (val === undefined || val === null) {
// nothing there, add as single element array
srcObj = setObjectAtLoc(srcObj, loc, [ elemToAdd ]);
} else if (isArray(val)) {
// already an array, push new element
val.push(elemToAdd);
} else if (isObject(val)) {
// this is an object, do nothing
} else {
// something else, do nothing
}
return srcObj;
}
function opMoveToArray(srcObj, srcLoc, destLoc) {
// if not valid locations, abort
if (!isLocValid(srcLoc) || !isLocValid(destLoc)) return srcObj;
// if the same location, nothing to do
if (srcLoc == destLoc) return srcObj;
// if destLoc is child of srcLoc, can't do that move
if (isSubLoc(srcLoc, destLoc)) return srcObj;
var val = getObjectAtLoc(srcObj, srcLoc);
var destVal = getObjectAtLoc(srcObj, destLoc);
if (val !== undefined) {
if (destVal === undefined || destVal === null) {
// nothing there, add as single element array
srcObj = setObjectAtLoc(srcObj, destLoc, [ val ]);
deleteObjectAtLoc(srcObj, srcLoc);
} else if (isArray(destVal)) {
// already an array, push new element
destVal.push(val);
deleteObjectAtLoc(srcObj, srcLoc);
} else if (isObject(destVal)) {
// this is an object, do nothing
} else {
// something else, do nothing
}
}
return srcObj;
}
function opMakeArray(srcObj, loc) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
var val = getObjectAtLoc(srcObj, loc);
if (val === undefined || val === null) {
// nothing there, or null, create empty array
srcObj = setObjectAtLoc(srcObj, loc, []);
} else if (isArray(val)) {
// already an array, do nothing
} else if (isObject(val)) {
// this is an object, wrap it in an array
srcObj = setObjectAtLoc(srcObj, loc, [ val ]);
} else {
// something else -- wrap it in array
srcObj = setObjectAtLoc(srcObj, loc, [ val ]);
}
return srcObj;
}
function opMap1(srcObj, loc, opMap) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
if (!isObjectOrArray(opMap)) return srcObj;
var val = getObjectAtLoc(srcObj, loc);
if (val !== undefined) {
// if opMap is not an array, make it a single element array
if (!isArray(opMap)) {
opMap = [ opMap ];
}
val = doMap(val, opMap);
srcObj = setObjectAtLoc(srcObj, loc, val);
}
return srcObj;
}
function opMapEach(srcObj, loc, opMap) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
if (!isObjectOrArray(opMap)) return srcObj;
var ary = getObjectAtLoc(srcObj, loc);
// ary should be an array
if (isArray(ary)) {
// if opMap is not an array, make it a single element array
if (!isArray(opMap)) {
opMap = [ opMap ];
}
for (var i = 0, aryLen=ary.length; i < aryLen; i++) {
var val = ary[i];
if (val !== undefined) {
ary[i] = doMap(val, opMap);
}
}
srcObj = setObjectAtLoc(srcObj, loc, ary);
}
return srcObj;
}
function opFunc1(srcObj, loc, fn, parms) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
var val = getObjectAtLoc(srcObj, loc);
if (val !== undefined) {
val = fn(val, parms);
srcObj = setObjectAtLoc(srcObj, loc, val);
}
return srcObj;
}
function opFuncEach(srcObj, loc, fn, parms) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
var ary = getObjectAtLoc(srcObj, loc);
// ary should be an array
if (ary !== undefined && ary.constructor === Array) {
for (var i = 0, aryLen=ary.length; i < aryLen; i++) {
var val = ary[i];
ary[i] = fn(val, parms);
}
srcObj = setObjectAtLoc(srcObj, loc, ary);
}
return srcObj;
}
function opSortArray(srcObj, loc, fn) {
// if not valid location, abort
if (!isLocValid(loc)) return srcObj;
var ary = getObjectAtLoc(srcObj, loc);
// ary should be an array
if (ary !== undefined && ary.constructor === Array) {
ary.sort(fn);
srcObj = setObjectAtLoc(srcObj, loc, ary);
}
return srcObj;
}
// returns new srcObj because executed function could change its reference
function opExec(srcObj, fn, parms) {
return fn(srcObj, parms);
}
function opIf(srcObj, condition, trueMap, falseMap) {
if (!isObjectOrArray(trueMap)) return srcObj;
if (falseMap && !isObjectOrArray(falseMap)) return srcObj;
if (condition === true)
{
// if trueMap is not an array, make it a single element array
if (!isArray(trueMap)) {
trueMap = [ trueMap ];
}
srcObj = doMap(srcObj, trueMap);
}
else if (falseMap && condition === false) {
if (!isArray(falseMap)) {
falseMap = [ falseMap ];
}
srcObj = doMap(srcObj, falseMap);
}
return srcObj;
}
function opIfExists(srcObj, loc, trueMap, falseMap) {
if (!isLocValid(loc)) return srcObj;
return opIf(srcObj, getObjectAtLoc(srcObj, loc) !== undefined, trueMap, falseMap);
}
function opIfEqual(srcObj, loc, val, trueMap, falseMap) {
if (!isLocValid(loc)) return srcObj;
return opIf(srcObj, getObjectAtLoc(srcObj, loc) === val, trueMap, falseMap);
}
function opIfType(srcObj, loc, typeStr, trueMap, falseMap) {
if (!isLocValid(loc)) return srcObj;
if (!isString(typeStr)) return srcObj;
var locType = getTypeofLoc(srcObj, loc);
var typeArray = typeStr.split(' ');
var wasFound = false;
for (var i = 0, aLen = typeArray.length; i < aLen; i++) {
if (locType === typeArray[i]) {
wasFound = true;
break;
}
}
return opIf(srcObj, wasFound, trueMap, falseMap);
}
function opIfFunc(srcObj, loc, fn, parms, trueMap, falseMap) {
if (!isLocValid(loc)) return srcObj;
var val = getObjectAtLoc(srcObj, loc);
return opIf(srcObj, fn(val, parms) === true, trueMap, falseMap);
}
function doMap(srcObj, map) {
// if map is not an array (probably a single map object), make it a single element array
if (!isArray(map)) {
map = [ map ];
}
for (var i = 0, mapLen = map.length; i < mapLen; i++) {
var mapObj = (map)[i];
switch (mapObj.op) {
case "ADD":
log("Op: setting object at", mapObj.loc);
srcObj = opAdd(srcObj, mapObj.loc, mapObj.elemToAdd);
log("Updated:", srcObj);
break;
case "ADDTOARRAY":
log("Op: adding object to array at", mapObj.loc);
srcObj = opAddToArray(srcObj, mapObj.loc, mapObj.elemToAdd);
log("Updated:", srcObj);
break;
case "MOVETOARRAY":
log("Op: moving", mapObj.srcLoc, "into array at", mapObj.destLoc);
srcObj = opMoveToArray(srcObj, mapObj.srcLoc, mapObj.destLoc);
log("Updated:", srcObj);
break;
case "DEL":
log("Op: deleting", mapObj.loc);
srcObj = opDel(srcObj, mapObj.loc);
log("Updated:", srcObj);
break;
case "COPY":
log("Op: copying", mapObj.srcLoc, "to", mapObj.destLoc);
srcObj = opCopy(srcObj, mapObj.srcLoc, mapObj.destLoc);
log("Updated:", srcObj);
break;
case "MOVE":
log("Op: moving", mapObj.srcLoc, "to", mapObj.destLoc);
srcObj = opMove(srcObj, mapObj.srcLoc, mapObj.destLoc);
log("Updated:", srcObj);
break;
case "TOSTRING":
log("making", mapObj.loc, "a string");
srcObj = opToString(srcObj, mapObj.loc);
log("Updated:", srcObj);
break;
case "STRINGIFY":
log("Op: stringifying", mapObj.loc);
srcObj = opStringify(srcObj, mapObj.loc);
log("Updated:", srcObj);
break;
case "MAKEARRAY":
log("Op: making", mapObj.loc, "an array");
srcObj = opMakeArray(srcObj, mapObj.loc);
log("Updated:", srcObj);
break;
case "MAP1":
log("Op: mapping", mapObj.loc);
srcObj = opMap1(srcObj, mapObj.loc, mapObj.map);
log("Updated:", srcObj);
break;
case "MAPEACH":
log("Op: mapping each of", mapObj.loc);
srcObj = opMapEach(srcObj, mapObj.loc, mapObj.map);
log("Updated:", srcObj);
break;
case "FUNC1":
log("Op: calling function on", mapObj.loc);
srcObj = opFunc1(srcObj, mapObj.loc, mapObj.fn, mapObj.parms);
log("Updated:", srcObj);
break;
case "FUNCEACH":
log("Op: calling function on each of", mapObj.loc);
srcObj = opFuncEach(srcObj, mapObj.loc, mapObj.fn, mapObj.parms);
log("Updated:", srcObj);
break;
case "SORTARRAY":
log("Op: sorting the elements of the array at", mapObj.loc);
srcObj = opSortArray(srcObj, mapObj.loc, mapObj.fn);
log("Updated:", srcObj);
break;
case "EXEC":
log("Op: execing function on object");
srcObj = opExec(srcObj, mapObj.fn, mapObj.parms);
log("Updated:", srcObj);
break;
case "LOCSEPARATOR":
log("Op: changing loc separator to", mapObj.sep);
sep = mapObj.sep;
break;
case "IF":
log("Op: if condition");
srcObj = opIf(srcObj, mapObj.condition, mapObj.trueMap, mapObj.falseMap);
log("Updated:", srcObj);
break;
case "IFEXISTS":
log("Op: if exists", mapObj.loc);
srcObj = opIfExists(srcObj, mapObj.loc, mapObj.trueMap, mapObj.falseMap);
log("Updated:", srcObj);
break;
case "IFEQUAL":
log("Op: if loc", mapObj.loc, "equal to", mapObj.val);
srcObj = opIfEqual(srcObj, mapObj.loc, mapObj.val, mapObj.trueMap, mapObj.falseMap);
log("Updated:", srcObj);
break;
case "IFTYPE":
log("Op: if loc", mapObj.loc, "has type of:", mapObj.typeStr);
srcObj = opIfType(srcObj, mapObj.loc, mapObj.typeStr, mapObj.trueMap, mapObj.falseMap);
log("Updated:", srcObj);
break;
case "IFFUNC":
log("Op: if func returns true for loc", mapObj.loc);
srcObj = opIfFunc(srcObj, mapObj.loc, mapObj.fn, mapObj.parms, mapObj.trueMap, mapObj.falseMap);
log("Updated:", srcObj);
break;
}
}
return srcObj;
}
JSMapr.prototype.map = function(srcObj) {
return doMap(srcObj, mapCommands);
};
}());
JSMapr.ADD = function(loc, elemToAdd) {
return { "op": "ADD", "loc": loc, "elemToAdd": elemToAdd };
};
JSMapr.DEL = function(loc) {
return { "op": "DEL", "loc": loc };
};
JSMapr.COPY = function(srcLoc, destLoc) {
return { "op": "COPY", "srcLoc": srcLoc, "destLoc": destLoc };
};
JSMapr.MOVE = function(srcLoc, destLoc) {
return { "op": "MOVE", "srcLoc": srcLoc, "destLoc": destLoc };
};
JSMapr.TOSTRING = function(loc) {
return { "op": "TOSTRING", "loc": loc };
};
JSMapr.STRINGIFY = function(loc) {
return { "op": "STRINGIFY", "loc": loc };
};
JSMapr.MAKEARRAY = function(loc) {
return { "op": "MAKEARRAY", "loc": loc };
};
JSMapr.ADDTOARRAY = function(loc, elemToAdd) {
return { "op": "ADDTOARRAY", "loc": loc, "elemToAdd": elemToAdd };
};
JSMapr.MOVETOARRAY = function(srcLoc, destLoc) {
return { "op": "MOVETOARRAY", "srcLoc": srcLoc, "destLoc": destLoc };
};
JSMapr.MAP1 = function(loc, map) {
return { "op": "MAP1", "loc": loc, "map": map };
};
JSMapr.MAPEACH = function(loc, map) {
return { "op": "MAPEACH", "loc": loc, "map": map };
};
JSMapr.FUNC1 = function(loc, fn, parms) {
return { "op": "FUNC1", "loc": loc, "fn": fn, "parms": parms };
};
JSMapr.FUNCEACH = function(loc, fn, parms) {
return { "op": "FUNCEACH", "loc": loc, "fn": fn, "parms": parms };
};
JSMapr.SORTARRAY = function(loc, fn) {
return { "op": "SORTARRAY", "loc": loc, "fn": fn };
};
JSMapr.EXEC = function(fn, parms) {
return { "op": "EXEC", "fn": fn, "parms": parms };
};
JSMapr.IF = function(condition, trueMap, falseMap) {
return { "op": "IF", "condition": condition, "trueMap": trueMap, "falseMap": falseMap };
};
JSMapr.IFEXISTS = function(loc, trueMap, falseMap) {
return { "op": "IFEXISTS", "loc": loc, "trueMap": trueMap, "falseMap": falseMap };
};
JSMapr.IFEQUAL = function(loc, val, trueMap, falseMap) {
return { "op": "IFEQUAL", "loc": loc, "val": val, "trueMap": trueMap, "falseMap": falseMap };
};
JSMapr.IFTYPE = function(loc, typeStr, trueMap, falseMap) {
return { "op": "IFTYPE", "loc": loc, "typeStr": typeStr, "trueMap": trueMap, "falseMap": falseMap };
};
JSMapr.IFFUNC = function(loc, fn, parms, trueMap, falseMap) {
return { "op": "IFFUNC", "loc": loc, "fn": fn, "parms": parms, "trueMap": trueMap, "falseMap": falseMap };
};
JSMapr.LOCSEPARATOR = function(sep) {
return { "op": "LOCSEPARATOR", "sep": sep };
};