-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionSuite.js
911 lines (902 loc) · 29.6 KB
/
ActionSuite.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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
"object" != typeof JSON && (JSON = {}),
(function () {
"use strict";
var rx_one = /^[\],:{}\s]*$/,
rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
rx_three =
/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
rx_four = /(?:^|:|,)(?:\s*\[)+/g,
rx_escapable =
/[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
rx_dangerous =
/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
gap,
indent,
meta,
rep;
function f(t) {
return t < 10 ? "0" + t : t;
}
function this_value() {
return this.valueOf();
}
function quote(t) {
return (
(rx_escapable.lastIndex = 0),
rx_escapable.test(t)
? '"' +
t.replace(rx_escapable, function (t) {
var e = meta[t];
return "string" == typeof e
? e
: "\\u" + ("0000" + t.charCodeAt(0).toString(16)).slice(-4);
}) +
'"'
: '"' + t + '"'
);
}
function str(t, e) {
var r,
n,
o,
u,
f,
a = gap,
i = e[t];
switch (
(i &&
"object" == typeof i &&
"function" == typeof i.toJSON &&
(i = i.toJSON(t)),
"function" == typeof rep && (i = rep.call(e, t, i)),
typeof i)
) {
case "string":
return quote(i);
case "number":
return isFinite(i) ? String(i) : "null";
case "boolean":
case "null":
return String(i);
case "object":
if (!i) return "null";
if (
((gap += indent),
(f = []),
"[object Array]" === Object.prototype.toString.apply(i))
) {
for (u = i.length, r = 0; r < u; r += 1) f[r] = str(r, i) || "null";
return (
(o =
0 === f.length
? "[]"
: gap
? "[\n" + gap + f.join(",\n" + gap) + "\n" + a + "]"
: "[" + f.join(",") + "]"),
(gap = a),
o
);
}
if (rep && "object" == typeof rep)
for (u = rep.length, r = 0; r < u; r += 1)
"string" == typeof rep[r] &&
(o = str((n = rep[r]), i)) &&
f.push(quote(n) + (gap ? ": " : ":") + o);
else
for (n in i)
Object.prototype.hasOwnProperty.call(i, n) &&
(o = str(n, i)) &&
f.push(quote(n) + (gap ? ": " : ":") + o);
return (
(o =
0 === f.length
? "{}"
: gap
? "{\n" + gap + f.join(",\n" + gap) + "\n" + a + "}"
: "{" + f.join(",") + "}"),
(gap = a),
o
);
}
}
"function" != typeof Date.prototype.toJSON &&
((Date.prototype.toJSON = function () {
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;
}),
(Boolean.prototype.toJSON = this_value),
(Number.prototype.toJSON = this_value),
(String.prototype.toJSON = this_value)),
"function" != typeof JSON.stringify &&
((meta = {
"\b": "\\b",
"\t": "\\t",
"\n": "\\n",
"\f": "\\f",
"\r": "\\r",
'"': '\\"',
"\\": "\\\\",
}),
(JSON.stringify = function (t, e, r) {
var n;
if (((gap = ""), (indent = ""), "number" == typeof r))
for (n = 0; n < r; n += 1) indent += " ";
else "string" == typeof r && (indent = r);
if (
((rep = e),
e &&
"function" != typeof e &&
("object" != typeof e || "number" != typeof e.length))
)
throw new Error("JSON.stringify");
return str("", { "": t });
})),
"function" != typeof JSON.parse &&
(JSON.parse = function (text, reviver) {
var j;
function walk(t, e) {
var r,
n,
o = t[e];
if (o && "object" == typeof o)
for (r in o)
Object.prototype.hasOwnProperty.call(o, r) &&
(void 0 !== (n = walk(o, r)) ? (o[r] = n) : delete o[r]);
return reviver.call(t, e, o);
}
if (
((text = String(text)),
(rx_dangerous.lastIndex = 0),
rx_dangerous.test(text) &&
(text = text.replace(rx_dangerous, function (t) {
return (
"\\u" + ("0000" + t.charCodeAt(0).toString(16)).slice(-4)
);
})),
rx_one.test(
text
.replace(rx_two, "@")
.replace(rx_three, "]")
.replace(rx_four, "")
))
)
return (
(j = eval("(" + text + ")")),
"function" == typeof reviver ? walk({ "": j }, "") : j
);
throw new SyntaxError("JSON.parse");
});
})();
/**
* Since we need Array-Likes to replicate Document.pageItems acting as an array with
* custom functions like .add(), we can begin each Array-Like as an actual array
* then mimick an Object.assign call so they inherit new prototype methods
*/
function extendPrototype(target) {
var arg, obj, key;
for (arg = 1; arg < arguments.length; ++arg) {
obj = arguments[arg];
for (key in obj) if (obj.hasOwnProperty(key)) target[key] = obj[key];
}
return target;
}
/**
* Any Array-Likes need to have parents explicitly set at time of creation.
* Since a user has these generated in their parents, it shouldn't be an issue
*/
function ActionParameters(arr, parent) {
arr = arr || [];
extendPrototype(arr, {
add: function () {
for (var i = 0; i < arguments.length; i++) {
arguments[i]["parent"] = this;
if (arguments[i].typename && arguments[i].typename == "ActionParameter")
this.push(arguments[i]);
else if (!arguments[i].typename)
this.push(new ActionParameter(arguments[i]));
}
if (this.parent) this.parent["parameterCount"] = this.length;
},
removeAll: function () {
for (var i = this.length; !!i; i--) this.pop();
if (this.parent) this.parent["parameterCount"] = this.length;
},
});
arr.parent = parent;
arr.typename = "ActionParameters";
// Could assign parenting chain here (feels redundant)
return arr;
}
function ActionEvents(arr, parent) {
arr = arr || [];
extendPrototype(arr, {
add: function () {
for (var i = 0; i < arguments.length; i++) {
arguments[i]["parent"] = this;
if (arguments[i].typename && arguments[i].typename == "ActionEvent")
this.push(arguments[i]);
else if (!arguments[i].typename)
this.push(new ActionEvent(arguments[i]));
}
if (this.parent) this.parent["eventCount"] = this.length;
},
removeAll: function () {
for (var i = this.length; !!i; i--) this.pop();
if (this.parent) this.parent["eventCount"] = this.length;
},
});
arr.parent = parent;
arr.typename = "ActionEvents";
// Could assign parenting chain here (feels redundant)
return arr;
}
function Actions(arr, parent, gID) {
arr = arr || [];
extendPrototype(arr, {
add: function () {
for (var i = 0; i < arguments.length; i++) {
arguments[i]["parent"] = this;
if (arguments[i].typename && arguments[i].typename == "Action")
this.push(arguments[i]);
else if (!arguments[i].typename) this.push(new Action(arguments[i]));
}
if (this.parent) this.parent["actionCount"] = this.length;
},
removeAll: function () {
for (var i = this.length; !!i; i--) this.pop();
if (this.parent) this.parent["actionCount"] = this.length;
},
getByName: function (query) {
for (var i = 0; i < this.length; i++)
if (this[i].name && this[i].name == query) return this[i];
return null;
},
});
arr.parent = parent || null;
arr.typename = "ActionCollection";
var self = this;
if (arr.length)
for (var ind = 0; ind < arr.length; ind++)
if (!arr[ind].parent) arr[ind]["parent"] = self;
return arr;
}
function ActionParameter(obj, parent) {
this.typename = "ActionParameter";
this.parent = parent || null;
this.key = null;
this.showInPalette = -1;
this.type = null;
this.value = null;
for (var key in obj) this[key] = obj[key];
}
function ActionEvent(obj, parent) {
this.typename = "ActionEvent";
this.parent = parent || null;
this.useRulersIn1stQuadrant = 0;
this.internalName = null;
this.localizedName = null;
this.isOpen = 0;
this.isOn = 1;
this.hasDialog = 0;
this.parameterCount = 0;
this.parameters = new ActionParameters([], this);
for (var key in obj)
if (key == "parameters")
this.parameters = new ActionParameters(obj.parameters, this);
else if (key == "parameterCount")
this.parameterCount = obj.parameters.length;
else this[key] = obj[key];
}
function Action(obj, parent) {
this.parent = parent || null;
this.typename = "Action";
this.eventCount = 0;
this.events = new ActionEvents([], this);
for (var key in obj)
if (key == "events") this.events = new ActionEvents(obj.events, this);
else if (key == "eventCount") this.eventCount = obj.events.length;
else this[key] = obj[key];
}
Action.prototype.run = function () {
// This should trigger a reload of rawtext and Actions
// If properly parent-chained, it could call the root to regenerate AIA text and reload
if (this.parent && this.parent.parent) {
app.doScript(this.name, this.parent.parent.name, false);
} else {
alert(
"Action is not properly mounted to a parent chain of Self > Collection > Set"
);
}
};
function ActionSet(params) {
var self = this;
this.typename = "ActionSet";
this.name = "";
this.version = 3;
this.isOpen = 0;
this.actionCount = 0;
this.actions = new Actions([], this, 1);
this.rawtext = null;
var data;
try {
if (/string/i.test(typeof params)) {
if (ActionBoyIsJSON(params)) {
data = JSON.parse(params);
// Then we can leverage our JSON > AIA to handle everything, but we aren't validating JSON values
} else if (/^\/version/.test(params)) {
data = ActionBoyConvertAIAToJSON(params);
} else {
// This is an unrecognized format
alert("ActionSet cannot be constructed from type " + typeof params);
}
} else if (/object/i.test(typeof params)) {
if (params instanceof File) {
params.encoding = "UTF8";
params.open("r");
var content = params.read();
params.close();
if (/\.json$/.test(params.name)) data = JSON.parse(content);
else if (/\.aia$/.test(params.name))
data = ActionBoyConvertAIAToJSON(content);
} else {
// This is likely to be a File object or a JSON schema. Let's worry about Files later and assume:
data = params;
}
} else {
alert("Construct Mount Error: Unrecognized format");
}
if (data) {
for (var key in data) {
if (key == "actions") this.actions = new Actions(data.actions, self, 2);
else if (key == "actionCount") this.actionCount = data.actions.length;
else this[key] = data[key];
}
if (
this.actions &&
this.actions.length &&
this.actions.typename == "ActionCollection"
) {
// ? This returns no parent chain
} else {
// But this does? If ActionSet is generated with no actions key?
alert("Something went wrong, ActionSet.actions has no parent");
}
}
} catch (err) {
alert("Construct Error: " + err);
}
}
ActionSet.prototype = {
load: function () {
try {
var contents = this.toAIA();
var tmp = File(Folder.desktop + "/ActionBoytmp.aia");
tmp.open("w");
tmp.write(contents);
tmp.close();
app.loadAction(tmp);
tmp.remove();
return true;
} catch (err) {
return false;
}
},
unload: function () {
try {
var temp = app.unloadAction(this.name, "");
return /undefined/i.test(temp + "");
} catch (err) {
return false;
}
},
toJSON: function () {
var result = this.sanitizeJSON(this.getJSONSchema());
return JSON.stringify(result);
},
sanitizeJSON: function (obj) {
// For whatever reason, my recursive RegExp isn't catching parent key/values during JSON conversion.
// For now I'll manually snip them out:
obj.actions = ActionBoyMap(obj.actions, function (value) {
if (value.hasOwnProperty("parent")) delete value.parent;
return value;
});
// Also noticing that minified versions of this script result in Unicode symbols, rather than full strings.
// Don't want this as it breaks the human readability
return obj;
},
toAIA: function () {
function ActionBoyConvertJSONToAIA(obj) {
function chunkSubstr(str, size) {
var numChunks = Math.ceil(str.length / size);
var chunks = new Array(numChunks);
for (var i = 0, o = 0; i < numChunks; ++i, o += size)
chunks[i] = str.substr(o, size);
return chunks;
}
function chunkSubstr(str, size) {
var numChunks = Math.ceil(str.length / size);
var chunks = new Array(numChunks);
for (var i = 0, o = 0; i < numChunks; ++i, o += size)
chunks[i] = str.substr(o, size);
return chunks;
}
function unfoldPerDepth(obj, str, depth, parent) {
str = str || "";
depth = depth || 0;
parent = parent || null;
var padding = "";
for (var p = 0; p < depth; p++) padding += "\t";
var keylist = [];
for (var key in obj) keylist.push(key);
ActionBoyForEach(keylist, function (key, mainIndex) {
var conditions = hasConditionalFormatting(key, obj[key], depth, obj);
if (conditions.isNone) str += padding + "/" + key + " " + obj[key];
else if (conditions.isReal)
str += padding + "/" + key + " " + obj[key] + ".0";
else if (conditions.isHex) {
var hex = ActionBoyAsciiToHex(obj[key]);
var byteLength = hex.length;
if (byteLength > 64)
hex = ActionBoyMap(chunkSubstr(hex, 64), function (i, nn) {
return (nn ? padding + "\t" : "") + i;
}).join("\r\n");
str +=
padding +
"/" +
key +
" [ " +
byteLength / 2 +
"\r\n" +
padding +
"\t" +
hex +
"\r\n" +
padding +
"]";
} else if (conditions.isDec) {
str +=
padding + "/" + key + " " + ActionBoyAsciiToDecimal(obj[key]);
} else if (conditions.isEnum) {
var enumKey = key.replace(/s$/, "");
if (ActionBoyIsArray(obj[key]))
ActionBoyForEach(obj[key], function (value, index) {
str += padding + "/" + enumKey + "-" + (index + 1) + " {\r\n";
str = unfoldPerDepth(value, str, depth + 1, obj);
str += padding + "}\r\n";
});
}
str += "\r\n";
});
return str;
}
function hasConditionalFormatting(key, value, depth, parent) {
function ActionBoyArrayIncludes(haystack, needle) {
for (var i = 0; i < haystack.length; i++)
if (haystack[i] == needle) return true;
return false;
}
var conditions = {
isEnum: [
function () {
return ActionBoyArrayIncludes(
["actions", "events", "parameters"],
key
);
},
],
isHex: [
function () {
return ActionBoyArrayIncludes(["name"], key);
},
function () {
return ActionBoyArrayIncludes(["localizedName"], key);
},
function () {
return (
key == "value" &&
parent["type"] &&
ActionBoyArrayIncludes(["(ustring)"], parent["type"])
);
},
],
isDec: [
function () {
return depth > 2 && ActionBoyArrayIncludes(["key"], key);
},
],
isReal: [
function () {
return (
key == "value" &&
parent["type"] &&
/real/i.test(parent["type"]) &&
!/\./.test(value)
);
},
],
};
var diagnostic = [];
for (var keyFunc in conditions)
for (var i = 0; i < conditions[keyFunc].length; i++) {
var passCondition = conditions[keyFunc][i](
arguments[0],
arguments[1],
arguments[2],
arguments[3]
);
if (passCondition) diagnostic.push(keyFunc);
}
return {
isEnum: ActionBoyArrayIncludes(diagnostic, "isEnum"),
isHex: ActionBoyArrayIncludes(diagnostic, "isHex"),
isDec: ActionBoyArrayIncludes(diagnostic, "isDec"),
isReal: ActionBoyArrayIncludes(diagnostic, "isReal"),
isNone: !diagnostic.length,
};
}
return (
ActionBoyFilter(unfoldPerDepth(obj).split("\r\n"), function (arg) {
return arg && arg.length;
}).join("\r\n") + "\r\n"
);
}
return ActionBoyConvertJSONToAIA(
this.sanitizeJSON(this.getJSONSchema())
).replace(/\n\t*\n/, /\n/);
// Remove redundant extra newlines inside hex value [] brackets for small int
},
// Noticing that parent is still included in JSON despite being on omission list here
getJSONSchema: function () {
function recurseInto(obj) {
var shouldOmit = new RegExp(
"^(" +
[
"toJSON",
"load",
"unload",
"toJSONString",
"getJSONSchema",
"filter",
"map",
"forEach",
"add",
"removeAll",
"getByName",
"typename",
"rawtext",
"parent",
"toAIA",
"sanitizeJSON",
].join("|") +
")$"
),
key,
temp = {},
keylist = [];
for (key in obj) if (!shouldOmit.test(key)) keylist.push(key);
keylist = sortByPriority(keylist, obj.typename);
for (var kk = 0; kk < keylist.length; kk++) {
if (/^\/([^\s-]*)-\d{1,}/.test(keylist[kk])) {
var enumName = keylist[kk].replace(/-.*/, "");
temp[enumName + "Count"] = temp[enumName + "s"].length;
} else temp[keylist[kk]] = obj[keylist[kk]];
}
return temp;
}
function sortByPriority(keys, typename) {
function indexOf(list, entry) {
var i = 0;
for (i = 0; i < list.length; i++) if (list[i] == entry) return i;
return -1;
}
var lookup = {
ActionSet: ["version", "name", "isOpen", "actionCount", "actions"],
Action: [
"name",
"keyIndex",
"colorIndex",
"isOpen",
"eventCount",
"events",
],
ActionEvent: [
"useRulersIn1stQuadrant",
"internalName",
"localizedName",
"isOpen",
"isOn",
"hasDialog",
"parameterCount",
"parameters",
],
ActionParameter: ["key", "showInPalette", "type", "value"],
};
return keys.sort(function (a, b) {
return indexOf(lookup[typename], a) - indexOf(lookup[typename], b);
});
}
return recurseInto(this);
},
};
function isJSON(str) {
try {
str = JSON.parse(str);
return true;
} catch (err) {
return false;
}
}
// This is fine though sanitize/translate could probably be merged into a single function
function ActionBoyConvertAIAToJSON(rawtext) {
try {
var lines = ActionBoyFilter(
rawtext.split(/(\r\n|\r|\n)/g),
function (entry) {
return entry.replace(/(\r\n|\r|\n)/g, "").length;
}
);
var data = ActionBoyMap(lines, function (v, i, a) {
var child = {
raw: v,
index: i,
depth: /\t/.test(v) ? v.match(/\t/gm).length : 0,
parent: -1,
hasBrackets: /[\]\}]/.test(v),
};
if (child.depth || child.hasBrackets) {
for (var cc = i; !!cc; cc--) {
var lastDepth = /\t/.test(a[cc]) ? a[cc].match(/\t/gm).length : 0;
if (child.hasBrackets) {
var squareMatch = /\]/.test(v) && /\[/.test(a[cc]);
var curlyMatch = /\}/.test(v) && /\{/.test(a[cc]);
if (child.depth !== lastDepth) continue;
else if (curlyMatch || squareMatch) {
child.parent = cc;
break;
} else continue;
} else if (!child.hasBrackets && child.depth > lastDepth) {
child.parent = cc;
break;
} else continue;
}
}
return child;
});
var chain = [];
var rootProps = ActionBoyFilter(data, function (v) {
return v.parent < 0;
});
for (var cc = 0; cc < rootProps.length; cc++)
chain.push(ActionBoyRecurseForSchema(rootProps[cc], data));
var sanitizedSchema = ActionBoySanitizeSchema(chain);
return ActionBoyTranslateSchema(sanitizedSchema);
} catch (err) {
alert("Convert Error: " + err);
}
}
function ActionBoyRecurseForSchema(item, data) {
try {
var temp = item;
temp["children"] = ActionBoyMap(
ActionBoyFilter(data, function (child) {
return child.parent == item.index;
}),
function (child) {
return ActionBoyRecurseForSchema(child, data);
}
);
return temp;
} catch (err) {
alert("Recurse Schema Error: " + err);
}
}
function ActionBoyTranslateSchema(data, depth) {
try {
depth = depth || 0;
var result = {};
if (data && data.length && ActionBoyIsArray(data)) {
for (var i = 0; i < data.length; i++) {
var entry = data[i];
var isEnum = /^\/([^\s-]*)-\d{1,}/;
if (isEnum.test(ActionBoyTrim(entry.raw))) {
if (entry && entry.name) {
var enumName = entry.name.replace(/-.*/, "") + "s";
result[enumName] = result[enumName] || [];
if (entry.children && entry.children.length)
result[enumName].push(
ActionBoyTranslateSchema(entry.children, depth + 1)
);
} else {
console.log("Something isn't iterating correctly:");
result["errorFlags"] = result["errorFlags"] || [];
result.errorFlags.push(entry);
}
} else {
// Otherwise we can simply pass in the value and type to know how to handle each property:
result[entry.name] = ActionBoySanitizeValue(entry.value, entry.type);
}
}
}
return result;
} catch (err) {
alert("Translate Error: " + err);
}
}
function ActionBoySanitizeValue(value, type) {
var temp = ActionBoyTrim(value);
if (/hex/i.test(type)) {
temp = ActionBoyTrim(value.replace(/(\[|\])/gm, ""));
temp = ActionBoyFilter(temp.split(/\t{1,}/gm), function (i, ii) {
return i.length && ii && /^[a-f0-9]*$/.test(i);
}).join("");
temp = ActionBoyHexToAscii(temp);
} else if (/decimal/i.test(type)) temp = ActionBoyDecimalToAscii(value);
else if (!isNaN(Number(value))) {
// This is probably an integer of some kind. Just in case it fits a decimal format:
if (/^\d{10}$/.test(temp)) temp = ActionBoyDecimalToAscii(temp);
else temp = +value; // Otherwise, let's just convert it to a number
} else temp = value; // Chances are this is just a normal string
return temp;
}
// This is the only part of the process I'm very iffy on. It should be better than this.
function ActionBoySanitizeSchema(data) {
try {
var temp = [];
ActionBoyForEach(data, function (rootPropGroup) {
var rootClone = {
depth: rootPropGroup.depth,
raw: rootPropGroup.raw,
index: rootPropGroup.index,
};
// Diagnose and handle any propGroup which may need recursion, we want
// to collapse lines like "/name [ 8 120fda9011290 ]" from 4 lines to 1
if (rootPropGroup["children"] && rootPropGroup["children"].length) {
var isDeep = ActionBoyFilter(rootPropGroup["children"], function (i) {
return i.children && i.children.length;
}).length;
if (!isDeep) {
rootClone["value"] =
rootClone.raw +
ActionBoyMap(rootPropGroup.children, function (i) {
return i.raw;
}).join("");
// Damn. Could I make uglier code if I tried?
var str = ActionBoyTrim(rootClone.value);
rootClone["name"] = /\/([^\s]*)/.exec(str)[1];
str = ActionBoyTrim(str.replace(/^\/([^\s]*)/, ""));
rootClone.value = str;
if (/\{/.test(rootClone.value) && /\}/.test(rootClone.value)) {
rootClone["type"] = "container-B";
str = ActionBoyTrim(str.replace(/^(\{)|(\})$/gm, ""));
rootClone.value = str;
rootClone["children"] = ActionBoyMap(
str.split(/\t{1,}/gm),
function (subProp) {
var subClone = {
index: -1,
depth: rootClone.depth + 1,
name: ActionBoyTrim(subProp)
.replace(/^\//, "")
.replace(/\s.*/, ""),
raw: subProp,
value: ActionBoyTrim(
ActionBoyTrim(subProp)
.replace(/^\//, "")
.replace(/^[\s]*/, "")
),
};
// Starting to see keys assigned within values on JSX,
// something went wrong switching RegExp from string literals
if (new RegExp(subClone.name).test(subClone.value))
subClone.value = ActionBoyTrim(
subClone.value.replace(subClone.name, "")
);
if (subClone.value + "" == "null")
subClone.value = ActionBoyTrim(
ActionBoyTrim(subProp).replace(/^\/[^\s]/, "")
);
return subClone;
}
);
} else if (/\[/.test(rootClone.value) && /\]/.test(rootClone.value))
rootClone["type"] = "hexadecimal";
else rootClone["type"] = "undefined-A";
} else {
rootClone["name"] = ActionBoyTrim(rootClone.raw).replace(/^\//, "");
rootClone.name = rootClone.name.match(/[^-]*-\d{1,}/)[0];
rootClone["type"] = "container-A";
rootClone["children"] = ActionBoySanitizeSchema(
rootPropGroup.children
);
}
temp.push(rootClone);
} else if (/^\t*\/[^\s]*/.test(rootPropGroup.raw)) {
// This is probably a one-line value:
rootClone["type"] = "param";
rootClone["value"] = ActionBoyTrim(rootPropGroup.raw);
if (/\/[^\s]*/.test(rootClone.value))
rootClone["name"] = /\/([^\s]*)/.exec(rootClone.value)[1];
else rootClone["name"] = "undefined-B";
rootClone.value = ActionBoyTrim(
rootClone.value.replace("/" + rootClone.name, "")
);
if (/^\d{10}$/.test(rootClone.value)) rootClone["type"] = "decimal";
temp.push(rootClone);
} else {
// This is almost certainly a closing bracket, empty newline, or format artifact with no real value.
// We don't need this and can ignore it.
}
});
return temp;
} catch (err) {
alert("Sanitize Schema Error: " + err);
}
}
function ActionBoyAsciiToHex(input) {
var output = "";
for (var i = 0; i < input.toString().length; i++)
output += input.toString().charCodeAt(i).toString(16);
return output;
}
function ActionBoyAsciiToDecimal(input) {
return parseInt(ActionBoyAsciiToHex(input), 16);
}
function ActionBoyHexToAscii(input) {
var output = "";
for (var i = 0; i < input.toString().length; i += 2)
output += String.fromCharCode(parseInt(input.toString().substr(i, 2), 16));
return output;
}
function ActionBoyDecimalToAscii(input) {
return ActionBoyHexToAscii(Number(ActionBoyTrim(input + "")).toString(16));
}
function ActionBoyIsJSON(str) {
try {
JSON.parse(str);
return true;
} catch (err) {
return false;
}
}
function ActionBoyTrim(str) {
return str.replace(/^\s*|\s*$/gm, "");
}
function ActionBoyIsArray(arg) {
return Object.prototype.toString.call(arg) === "[object Array]";
}
function ActionBoyMap(array, callback) {
var mappedParam = [];
for (var i = 0; i < array.length; i++)
mappedParam.push(callback(array[i], i, array));
return mappedParam;
}
function ActionBoyObjectKeys(obj) {
var list = [];
for (var key in obj) list.push(key);
return list;
}
function ActionBoyFilter(array, callback, debug) {
debug = debug || false;
var filtered = [];
for (var i = 0; i < array.length; i++)
if (callback(array[i], i, array)) {
if (debug) console.log("Found match?");
filtered.push(array[i]);
}
return filtered;
}
function ActionBoyForEach(array, callback) {
for (var i = 0; i < array.length; i++) callback(array[i], i, array);
}