-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
1020 lines (917 loc) · 28.3 KB
/
app.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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
// If any new function does not support BigInt, use this:
//
// // Check if past max safe integer, 9,007,199,254,740,991 (2^53 – 1) If so,
// // error. This is done by assuming 16 and greater digits risk data truncation, so there
// // are false positives.
// if (decimal.length > 15) {
// //throw new Error("Over limit for decimal base conversion. Conversion will fail. (TODO, use BigInt)");
// }
// Many of these are not in use.
const Base2 = document.getElementById('base2').value;
const Base4 = document.getElementById('base4').value;
const Base4DNA = document.getElementById('base4').value;
const Base8 = document.getElementById('base8').value;
const Base10 = document.getElementById('base10').value;
const Base12 = document.getElementById('base12').value;
const Base16 = document.getElementById('base16').value;
const Base16Lower = document.getElementById('base16_lower').value;
const base20 = document.getElementById('base20').value;
const base32 = document.getElementById('base32').value;
const zBase32 = document.getElementById('zbase32').value;
const base58 = document.getElementById('base58').value;
///////////////////////
// RFC 4648 base64s
///////////////////////
// RFCBase64Unsafe is a URI unsafe base 64 alphabet, the "default" for RFC 4648.
const RFCBase64Unsafe = document.getElementById('base64UriUnsafe').value;
// RFCBase64Uri is RFC 4648 URI safe base 64 alphabet.
const RFCBase64Uri = document.getElementById('base64UriSafe').value;
const base85 = document.getElementById('base85').value;
const base91 = document.getElementById('base91').value;
const base128 = document.getElementById('base128').value;
const base256 = document.getElementById('base256').value;
// Application element variables that remain constant, but have inner
// elements/values that will change or be modified.
// In Elements
var inAlphElem;
var inAlphLenElem;
var inBitsElem;
var inElem;
var inLenElem;
// Out Elements
var outAlphElem;
var outAlphLenElem;
var outBitsElem;
var outElem;
var outLenElem;
// Other elements
var alertDivElement;
var alertMsgElement;
var lpadElem;
var diceSidesElem;
document.addEventListener('DOMContentLoaded', () => {
// console.debug(window.nobleHashes);
// In Elements
inElem = document.getElementById("inputString");
inAlphElem = document.getElementById("inputAlphabet");
inBitsElem = document.getElementById("inputBitsNeeded");
inLenElem = document.getElementById("inputStringLength");
inAlphLenElem = document.getElementById("inputAlphabetLength");
// Out Elements
outElem = document.getElementById("outputString");
outAlphElem = document.getElementById("outputAlphabet");
outBitsElem = document.getElementById("outputBitsNeeded");
outLenElem = document.getElementById('outputStringLength');
outAlphLenElem = document.getElementById('outputAlphabetLength');
// Other Elements
diceSidesElem = document.getElementById('numberOfDiceSides');
lpadElem = document.querySelector("#PadCheckbox");
alertDivElement = document.getElementById('alertErrorDiv');
alertMsgElement = document.getElementById('alertErrorMsg');
document.querySelector("#ConvertBaseBtn").addEventListener('click', Convert);
document.getElementById('alertErrorCloseBtn').addEventListener('click', ClearErrAlert);
document.querySelector("#FlipBtn").addEventListener('click', Flip);
document.querySelector("#ClearBtn").addEventListener('click', Clear);
document.querySelector("#reverseInBtn").addEventListener('click', ReverseIn);
document.querySelector("#reverseOutBtn").addEventListener('click', ReverseOut);
lpadElem.addEventListener('click', Convert);
Collapse(document.querySelector("#extrasHead"), document.querySelector("#toggleSquare"), document.querySelector("#extrasBody"));
// Remove spaces
// https://stackoverflow.com/a/5964427/15147681
document.getElementById('removeSpaceBtn').addEventListener('click', () => {
inElem.value = inElem.value.replace(/\s+/g, '');
Convert();
});
document.getElementById('HashAlgoOptions').addEventListener('click', () => {
DefaultIn("text");
outAlphElem.value = "Hash:" + document.getElementById('HashAlgoOptions').value
Convert();
});
document.querySelector('#SysCnvBtn').addEventListener('click', () => {
outAlphElem.value = "SysCnv";
DefaultIn("Hex");
Convert();
});
document.querySelector('#DNDConvertInBtn').addEventListener('click', () => {
inAlphElem.value = "DND:" + diceSidesElem.value;
DefaultOut("Hex");
Convert();
});
document.querySelector('#DNDConvertOutBtn').addEventListener('click', () => {
outAlphElem.value = "DND:" + diceSidesElem.value;
DefaultIn("Hex")
Convert();
});
document.querySelector('#HexInBtn').addEventListener('click', () => {
DefaultOut("text");
inAlphElem.value = "Hex";
Convert();
});
document.querySelector('#HexOutBtn').addEventListener('click', () => {
DefaultIn("text");
outAlphElem.value = "Hex";
Convert();
});
document.querySelector('#hexInBtn').addEventListener('click', () => {
DefaultOut("text");
inAlphElem.value = "hex";
Convert();
});
document.querySelector('#hexOutBtn').addEventListener('click', () => {
DefaultIn("text");
outAlphElem.value = "hex";
Convert();
});
document.querySelector('#base64InBtn').addEventListener('click', () => {
DefaultOut("Hex");
inAlphElem.value = "base64";
Convert();
});
document.querySelector('#b64utOutBtn').addEventListener('click', () => {
DefaultIn("Hex");
outAlphElem.value = "ub64t";
Convert();
});
document.querySelector('#ub64pOutBtn').addEventListener('click', () => {
DefaultIn("Hex");
outAlphElem.value = "ub64p";
Convert();
});
document.querySelector('#TextInBtn').addEventListener('click', () => {
DefaultOut("Hex");
inAlphElem.value = "Text";
Convert();
});
document.querySelector('#TextOutBtn').addEventListener('click', () => {
DefaultIn("Hex");
outAlphElem.value = "Text";
Convert();
});
document.querySelector('#BytesInBtn').addEventListener('click', () => {
DefaultOut("Hex");
inAlphElem.value = "bytes";
Convert();
});
document.querySelector('#BytesOutBtn').addEventListener('click', () => {
DefaultIn("Hex");
outAlphElem.value = "bytes";
Convert();
});
document.getElementById('majusculeBtn').addEventListener('click', () => {
inAlphElem.value = "text";
outAlphElem.value = "Majuscule";
Convert();
});
document.getElementById('minisculeBtn').addEventListener('click', () => {
inAlphElem.value = "text";
outAlphElem.value = "Miniscule";
Convert();
});
document.getElementById('ridiculeBtn').addEventListener('click', () => {
inAlphElem.value = "text";
outAlphElem.value = "Ridicule";
Convert();
});
// Buttons on "Useful Alphabets"
document.querySelectorAll(".copy").forEach(t => {
t.addEventListener('click', Copy);
});
document.querySelectorAll(".source").forEach(t => {
t.addEventListener('click', Source);
});
document.querySelectorAll(".destination").forEach(t => {
t.addEventListener('click', Destination);
});
//////////////////
// Examples
//////////////////
document.querySelector("#Example64To256Btn").addEventListener('click', () => {
// Out is "Hello World!"
GUIConvert(RFCBase64Uri, "SGVsbG8gV29ybGQh", base256);
});
document.querySelector("#ExampleDNAToJSONBtn").addEventListener('click', () => {
// Out is {"Hello":"World!"}
GUIConvert("ACGT", "CTGTAGAGCAGACGCCCGTACGTACGTTAGAGATGGAGAGCCCTCGTTCTAGCGTACGCAAGACAGAGCTTC", base256);
});
// // TODO when right padding is implemented.
// document.querySelector("#Example256to32Btn").addEventListener('click', () => {
// // Out should be MZXW6YTBOI======
// // Or without replacing the padding characters: MZXW6YTBOIAAAAAA
// // Example from https://tools.ietf.org/html/rfc4648#section-10
// GUIConvert(base256, "foobarĀĀĀĀ", base32);
// Convert();
// });
//////////////////
// URI parameters
//////////////////
/**@type {FormOptions} */
const FormOptions = {
"FormParameters": [{
"name": "inAlph",
"id": "inputAlphabet"
},
{
"name": "in",
"id": "inputString",
},
{
"name": "outAlph",
"id": "outputAlphabet",
},
{
"name": "lpad",
"id": "PadCheckbox",
"type": "bool",
},
{
"name": "extras",
"type": "bool",
"funcTrue": () => ToggleVisible(document.querySelector("#extrasBody")),
}
]
};
URLForm.Populate(URLForm.Init(FormOptions));
//////////////////
// Live Update Conversion
//////////////////
// "change" doesn't work. "input" doesn't seem to cover all cases of user interaction.
inElem.addEventListener('input', Convert);
inAlphElem.addEventListener('input', Convert);
outAlphElem.addEventListener('input', Convert);
// Change on output should only update length.
outElem.addEventListener('input', Update);
// Finally, convert.
Convert();
});
// Helper that sets inAlph if not set.
function DefaultIn(input) {
if (isEmpty(inAlphElem.value)) {
inAlphElem.value = input;
}
}
// Helper that sets outAlph if not set.
function DefaultOut(output) {
if (isEmpty(outAlphElem.value)) {
outAlphElem.value = output;
}
}
// Flips Input and Output's alphabet and text area.
function Flip() {
let inputAlphabet = inAlphElem.value;
let inputString = inElem.value;
let outputAlphabet = outAlphElem.value;
let outputString = outElem.value;
inAlphElem.value = outputAlphabet;
inElem.value = outputString;
outAlphElem.value = inputAlphabet;
outElem.value = inputString;
Update();
}
// Reverses the input area text
function ReverseIn() {
reverse(inElem);
}
// Reverses the output area text
function ReverseOut() {
reverse(outElem);
}
// Reverses the input or output area text.
function reverse(element) {
let output = element.value;
if (isJson(output) && Array.isArray(JSON.parse(output))) {
element.value = "[" + JSON.parse(output).reverse() + "]";
return;
}
element.value = element.value.split("").reverse().join("");
}
// Clears out the Input and Output's alphabets and text areas.
function Clear() {
inAlphElem.value = "";
inElem.value = "";
outAlphElem.value = "";
outElem.value = "";
Update();
}
// GUIConvert populates the GUI and calls Convert();
async function GUIConvert(inAlph, input, outAlph) {
inAlphElem.value = inAlph;
inElem.value = input;
outAlphElem.value = outAlph;
Convert();
}
// Converts the input text area from the input alphabet to the output alphabet.
async function Convert() {
try {
ClearErrAlert();
let outputAlphabet = outAlphElem.value;
let inputAlpha = inAlphElem.value;
let inputString = inElem.value;
// Set padding Setting
if (lpadElem.checked == true) {
LeftPadding = true;
} else {
LeftPadding = false;
}
if (inputAlpha == "" || outputAlphabet == "") {
// console.debug("Empty input alph or output alph.");
Update();
return;
}
/////////////////////
// Input
/////////////////////
// Convert Keywords
if (isKeyword(inputAlpha)) {
inputString = KeywordToHex(inputAlpha, inputString);
inputAlpha = Base16;
}
if (inputString === null || inputString === undefined) { // sanitize null/undefined, "0" is legit.
inputString = "";
}
// console.debug(inputString, inputString.length);
/////////////////////
// Output
/////////////////////
let out = "";
if (!isKeyword(outputAlphabet)) {
out = BaseConvert(inputString, inputAlpha, outputAlphabet);
} else {
// All keywords accept Hex. If coming from keyword, input and inputAlpha
// are already Hex. Otherwise, convert to Hex.
inputString = BaseToHex(inputString, inputAlpha);
inputAlpha = Base16;
// console.debug(inputString);
let keyword = caseInsensitive(outputAlphabet);
if (outputAlphabet.substring(0, 5) == "Hash:") {
keyword = "hash";
var hashAlg = outputAlphabet.substring(5);
} else if (outputAlphabet.substring(0, 4) == "DND:") {
keyword = "dnd";
var diceSides = BigInt(parseInt(outputAlphabet.substring(4)));
}
// console.debug(keyword);
switch (keyword) {
case "syscnv":
out = "Hex: " + inputString +
"\nub64p: " + HexToUb64p(inputString) +
"\nBytes: " + HexToGoBytesString(inputString) +
"\nASCII: " + BaseConvert(inputString, Base16, base128);
break;
case "hash":
out = await HashExtrasHex(hashAlg, inputString);
break;
case "bytes":
out = HexToGoBytesString(inputString);
break;
case "base64":
case "b64":
case "ubase64p":
case "ub64p":
case "base64up":
case "b64up":
out = HexToUb64p(inputString);
break;
case "ubase64t":
case "ub64t":
case "base64ut":
case "b64ut":
out = await ArrayBufferTo64ut(await HexToArrayBuffer(inputString));
break;
case "dnd":
out = baseToDND(inputString, inputAlpha, diceSides);
break;
case "string":
case "text":
out = await HexToS(inputString);
break;
case "Hex":
out = inputString;
break;
case "hex":
out = inputString.toLowerCase();
break;
case "majuscule":
out = inElem.value.toUpperCase(); // Use original string, and not Hex representation.
break;
case "miniscule":
out = inElem.value.toLowerCase();
break;
case "ridicule":
out = RidiculeCasingGUI(inElem.value);
break;
default:
throw new Error("Keyword not supported for Output");
}
}
outElem.value = out;
} catch (error) {
console.debug(error);
alertMsgElement.textContent = error;
alertDivElement.hidden = false;
}
Update(); // Update lengths even on error.
}
/**
* KeywordToHex is for generalizing an input alphabet keyword into a
* Hex string representation.
* Returns the intermediate base representation for the input (Hex).
* @param {String} inAlph String. Input Alphabet/Keyword for switching.
* @param {String} input String. Input to be converted.
* @returns {String} intermediate String. Hex
* @throws {Error} Error. Error when no dice sides specified.
*/
function KeywordToHex(inAlph, input) {
inAlph = caseInsensitive(inAlph);
if (inAlph.substring(0, 4) == "DND:") {
var diceSides = inAlph.substring(4);
if (isEmpty(diceSides)) {
throw new Error("Must specify number of dice sides. Given: " + diceSides);
}
inAlph = "dnd";
}
switch (inAlph) {
case "dnd":
return BaseToHex(dndToDecimal(diceSides), Base10);
case "bytes":
return GoBytesToHex(input);
case "syscnv":
return SysCnvToHex(input);
case "base64":
case "b64":
case "base64up":
case "b64up":
case "ubase64p":
case "ub64p":
case "base64ut":
case "b64ut":
case "ubase64t":
case "ub64t":
return B64ToHex(input);
case "hex":
return input.toUpperCase();
case "Hex":
return input;
case "string":
case "text":
return SToHex(input);
default:
throw new Error('Keyword unsupported.');
}
}
////////////////////
// Alphabet Buttons. (copy, in, out)
////////////////////
// Copies the alphabet from the row.
function Copy() {
let input = this.parentElement.parentElement.querySelector('input');
input.select();
input.setSelectionRange(0, 99999);
document.execCommand("copy");
}
// Updates the Input/Source Alphabet with a given alphabet.
function Source() {
inAlphElem.value = this.parentElement.parentElement.querySelector('input').value;
Update();
}
// updates the Output Alphabet with a given alphabet.
function Destination() {
outAlphElem.value = this.parentElement.parentElement.querySelector('input').value;
Update();
}
// clears out the error message, and hides the alert.
function ClearErrAlert() {
alertDivElement.hidden = true;
alertMsgElement.textContent = "";
}
// Update updates all information presented on the screen.
// Except share link, for the worry that could slow down things too much.
function Update() {
let upObj = bitsBaseLengthGUI(inAlphElem.value, inElem.value);
inBitsElem.textContent = upObj.bits;
inAlphLenElem.textContent = upObj.base;
inLenElem.textContent = upObj.length;
upObj = bitsBaseLengthGUI(outAlphElem.value, outElem.value, outBitsElem, outAlphLenElem, outLenElem);
outBitsElem.textContent = upObj.bits;
outAlphLenElem.textContent = upObj.base;
outLenElem.textContent = upObj.length;
}
// Sets the output string to ridicule format of input string.
function RidiculeCasingGUI(input) {
input = input.toLowerCase();
let ridicule = "";
for (let i = 0; i < input.length; i++) {
if (input[i].toUpperCase() == undefined) { // returns undefined on non-printable, and possibly other chars (i.e. space)
ridicule += input[i];
continue;
}
if (Math.random() < 0.5) {
ridicule += input[i].toUpperCase();
} else {
ridicule += input[i].toLowerCase();
}
}
return ridicule;
}
///////////////////////////////
//// DND Convert
///////////////////////////////
// Returns a new padded array (if needed) from given array.
function rollsPadded(rolls, diceSides) {
let cols = parseInt(diceSides).toString().length;
// Padding
let paddedRolls = [];
for (let x of rolls) {
if (x.toString().length < cols) {
x = x.toString().padStart(cols, "0");
}
paddedRolls.push(x);
}
return paddedRolls;
}
/**
* Returns the decimal representation, as a string, from the DND input. Returns
* string because number can't represent nil, string can with empty quotes, "".
* @param {number} diceSides Number. Number of dice sides (determines base).
* @returns {string} Decimal representation of DND rolls.
*/
function dndToDecimal(diceSides) {
let cols = diceSides.length;
// console.debug(diceSides);
if (diceSides == 0) {
throw new Error("must specify number of dice sides");
}
let rollErrCheck = (roll) => {
roll = parseInt(roll);
// console.debug(typeof roll, typeof diceSides);
if (roll > diceSides) {
throw new Error("cannot have roll higher than dice sides: " + roll);
}
if (roll <= 0) {
throw new Error("cannot have a roll of 0: " + roll);
}
};
if (isEmpty(inElem.value)) {
return "";
}
let rolls = new Array();
// Support space as delimiter.
if (inElem.value.includes(' ')) {
rolls = rollsPadded(inElem.value.split(' ', inElem.value.length), diceSides)
} else {
rolls = inElem.value.match(new RegExp('.{1,' + cols + '}', 'g'));
}
for (let roll of rolls) {
rollErrCheck(roll); // throws
}
return diceRollsToDecimal(BigInt(diceSides), rolls);
}
/**
* Returns the decimal representation from the number of dice sides, and the
* given rolls. Returns String because, unlike number, it handles null, e.g. "",
* gracefully.
* @param {BigInt} diceSides Number. Number of dice sides (determines base).
* @param {Array<number>} rolls Array. Dice rolls.
* @returns {string} String. Decimal representation of DND rolls.
* @throws {error} error Error. Error when roll is higher than dice side.
*/
function diceRollsToDecimal(diceSides, rolls) {
// console.debug(diceSides, rolls);
let sum = 0n;
let x = 0;
for (let i = rolls.length - 1; i >= 0; i--) {
// Minus one to shift dice roles from "human numbers" (starting at 1) to
// computer science numbers ( starting at 0)
let roll = BigInt(parseInt(rolls[x]) - 1);
x++;
if (roll > diceSides) {
throw new Error("cannot have a roll higher than the dice sides: " + roll);
}
// For each column, the column is calculated by the value in the column
// times dice sides raised to the column number.
// console.log("Roll and i", roll, BigInt(i));
let col = diceSides ** BigInt(i);
let n = (roll * col);
// console.log(n);
sum = sum + n;
// console.log("Sum:" + sum);
}
return sum.toString();
}
/**
* Returns an array of the dice rolls, from the input.
* @param {String} input String. Input string to be converted to DND rolls.
* @param {String} inputAlpha String. Alphabet/Base that input is in.
* @param {number} diceSides Number. Number of dice sides (determines base).
* @returns {String} output String. Output based on input alphabet base.
* @throws {error} error Error. Fails when dice sides are not specified, and/or roll is higher than dice sides.
*/
function baseToDND(input, inputAlpha, diceSides) {
// console.debug("baseToDND: ", input, inputAlpha, diceSides);
if (diceSides == 0 || isEmpty(diceSides)) { // handle "undefined"
throw new Error("must specify number of dice sides. Given:" + diceSides);
}
// Handles the zero case
if (isEmpty(input)) {
return "";
}
let decimal = BigInt(BaseConvert(input, inputAlpha, Base10));
// console.log("baseToDND decimal: ", decimal);
let rolls = [];
// Handle unary
if (diceSides === 1) {
return "1".repeat(parseInt(decimal)); // TODO possibly add to BaseConvert
}
// Discovery for max column (radix column power)
for (var power = 1n; true; power++) {
let n = diceSides ** power;
if (n > decimal) {
power--;
break;
}
}
for (let i = power; i >= 0; i--) {
let roll = ~~(decimal / (diceSides ** i)); // bitwise XOR XOR to get decimal.
decimal = decimal % (diceSides ** i);
if (roll > diceSides) {
throw new Error("cannot have a roll higher than dice sides: " + roll);
}
// Do "+1" to convert from computer science numbers (start dice counting at
// 0) to human numbers (start dice counting at 1)
rolls.push(roll + 1n);
}
// console.log(rolls);
let rp = rollsPadded(rolls, diceSides);
let compressed = rp.toString().replaceAll(',', '');
return compressed;
}
///////////////////////
// App Helpers
///////////////////////
// Returns true, if the string is a recognized keyword, or has a recognized
// prefix (e.g. "Hash" from "Hash:SHA-256").
function isKeyword(string) {
// Lowercase Copy. see docs on caseInsensitive() for more.
let l = (' ' + string).slice(1).toLowerCase();
let insensitive = [
"bytes", "syscnv", "hex", "b64", "b64ut", "ub64p", "ub64t", "b64up",
"base64", "base64ut", "ubase64p", "ubase64t", "base64up", "ridicule", "majuscule",
"miniscule", "text", "string"
];
if (insensitive.includes(l) || l.substring(0, 4) == "dnd:" || l.substring(0, 5) == "hash:") {
return true;
}
return false;
}
// Returns the a given string as lower case, if the given string is not case
// sensitive. If it is case sensitive, the original string is returned.
function caseInsensitive(string) {
// console.debug(string);
// JavaScript's implementation of ECMAScript can vary from browser to browser,
// however for Chrome, many string operations (substr, slice, regex, etc.)
// simply retain references to the original string rather than making copies
// of the string. This is a known issue in Chrome (Bug #2869). To force a copy
// of the string, the following code works:
// https://stackoverflow.com/a/31733628/15147681
//
// Makes a lowercase copy of the input string for checking without modification.
let l = (' ' + string).slice(1).toLowerCase();
let sensitive = ["hex"];
// Return unmodified
if (sensitive.includes(l) || l.substring(0, 4) == "dnd:" || l.substring(0, 5) == "hash:") {
return string;
}
return string.toLowerCase();
}
/**
* GuiMeta holds meta data for updating the bits, base, and length elements on
* the GUI.
* @typedef {Object} GuiMeta
* @property {string} bits - Bits in alphabet.
* @property {string} base - Alphabet base.
* @property {string} length - length of string.
*
* Calculates Bits, Base, and Length based on alphabet, including keywords.
* @param {String} alph String. Bytes as a string.
* @param {String} text String.
* @returns {GuiMeta} GuiMeta
*/
function bitsBaseLengthGUI(alph, text) {
var bits = "n/a";
var base = "n/a";
var length = text.length;
alph = caseInsensitive(alph);
if (alph.substring(0, 4) == "DND:") {
let sides = alph.substring(4);
bits = BitPerBase(sides);
base = "Dice " + sides;
alph = "dnd";
}
if (alph.substring(0, 5) == "Hash:") {
base = alph.substring(5);
bits = 4; // Output is Hex, which is 4 bits.
alph = "hash";
}
switch (alph) {
case "dnd":
case "hash":
case "ridicule":
case "majuscule":
case "miniscule":
case "syscnv":
// Make sure base and bits are manually set before.
break; // Do nothing.
case "bytes":
bits = 8;
base = 2;
// Filter out zero case:
// WTF Javascript: "If separator does not occur, the returned array
// contains one element consisting of the entire string."
if ("[]" === text.split(",", 1)[0]) {
length = text.length + " (Bytes: 0)";
} else {
length = text.length + " (Bytes: " + text.split(",").length + ")";
}
break;
case "base64":
case "b64":
case "ubase64p":
case "ub64p":
bits = 6;
base = "u64p";
break;
case "base64up":
case "b64up":
bits = 6;
base = "b64up";
break;
case "base64ut":
case "b64ut":
bits = 6;
base = "b64ut";
break;
case "ubase64t":
case "ub64t":
bits = 6;
base = "ub64t";
break;
case "text":
case "string":
base = "Unicode";
break;
default: // Not a keyword
base = alph.length;
bits = BitPerBase(alph.length);
break;
}
/**@type {GuiMeta} */
return {
"bits": bits,
"base": base,
"length": length
};
}
// TODO
// function BucketPad() {}
// function FullBuckets() {
// inBits = BitPerBase(inAlphElem.value.length);
// outBits = BitPerBase(outAlphElem.value.length);
// inBitsElem.textContent = inBits;
// outBitsElem.textContent = outBits;
// }
///////////////////////
// Helpers
///////////////////////
function lcm_two_numbers(x, y) {
if ((typeof x !== 'number') || (typeof y !== 'number'))
return false;
return (!x || !y) ? 0 : Math.abs((x * y) / gcd_two_numbers(x, y));
}
function gcd_two_numbers(x, y) {
x = Math.abs(x);
y = Math.abs(y);
while (y) {
var t = y;
y = x % y;
x = t;
}
return x;
}
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
/**
* isEmpty is a helper function to determine if thing is empty.
*
* Functions are considered always not empty.
*
* Arrays are checked for the number of elements, and recursively calls isEmpty.
*
* Objects are empty if they have no keys. (Returns len === 0 of object keys.)
*
* NaN returns true. (NaN === NaN is always false, as NaN is never equal to
* anything. NaN is the only JavaScript value unequal to itself.)
*
* Don't use on HTMl elements. For HTML elements, use the !== equality check
* (element !== null).
*
* Cannot use CryptoKey with this function since (len === 0) always.
*
* @param {any} thing Thing you wish was empty.
* @returns {boolean} Boolean.
*/
function isEmpty(thing) {
if (typeof thing === 'function') {
return false;
}
if (Array.isArray(thing)) {
return isEmpty(thing[0]);
}
if (thing === Object(thing)) {
if (Object.keys(thing).length === 0) {
return true;
}
return false;
}
if (!isBool(thing)) {
return true;
}
return false
};
/**
* Helper function to determine boolean.
*
* Javascript, instead of considering everything false except a few key words,
* decided everything is true instead of a few key words. Why? Because
* Javascript. This function inverts that assumption, so that everything can be
* considered false unless true.
*
* @param {any} bool Thing that you wish was a boolean.
* @returns {boolean} An actual boolean.
*/
function isBool(bool) {
if (
bool === false ||
bool === "false" ||
bool === undefined ||
bool === "undefined" ||
bool === "" ||
bool === 0 ||
bool === "0" ||
bool === null ||
bool === "null" ||
bool === "NaN" ||
Number.isNaN(bool) ||
bool === Object(bool) // isObject
) {
return false;
}
return true;
};
/**
* Collapse marks an element as not disabled.
* @param {string|element} clickElement
* @param {string|element} toggleElement
*/
function Collapse(clickElement, toggleElement, visibleElement) {
if (typeof toggleElement == "string") {
var toggleElement = document.getElementById(toggleElement);
}
if (typeof visibleElement == "string") {
var visibleElement = document.getElementById(visibleElement);
}
// console.debug(toggleElement, visibleElement);
clickElement.addEventListener('click', function() {
let hidden = ToggleVisible(visibleElement);
// Icon
if (hidden) {
toggleElement.classList.remove("bi-dash-square");
toggleElement.classList.add("bi-plus-square");
} else {
toggleElement.classList.remove("bi-plus-square");
toggleElement.classList.add("bi-dash-square");
}
});