-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathgleam_stdlib.mjs
1050 lines (899 loc) Β· 26.3 KB
/
gleam_stdlib.mjs
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
import {
BitArray,
Error,
List,
Ok,
Result,
UtfCodepoint,
stringBits,
toBitArray,
NonEmpty,
CustomType,
} from "./gleam.mjs";
import { DecodeError } from "./gleam/dynamic.mjs";
import { Some, None } from "./gleam/option.mjs";
import { Eq, Gt, Lt } from "./gleam/order.mjs";
import Dict from "./dict.mjs";
const Nil = undefined;
const NOT_FOUND = {};
export function identity(x) {
return x;
}
export function parse_int(value) {
if (/^[-+]?(\d+)$/.test(value)) {
return new Ok(parseInt(value));
} else {
return new Error(Nil);
}
}
export function parse_float(value) {
if (/^[-+]?(\d+)\.(\d+)([eE][-+]?\d+)?$/.test(value)) {
return new Ok(parseFloat(value));
} else {
return new Error(Nil);
}
}
export function to_string(term) {
return term.toString();
}
export function float_to_string(float) {
const string = float.toString().replace("+", "");
if (string.indexOf(".") >= 0) {
return string;
} else {
const index = string.indexOf("e");
if (index >= 0) {
return string.slice(0, index) + ".0" + string.slice(index);
} else {
return string + ".0";
}
}
}
export function int_to_base_string(int, base) {
return int.toString(base).toUpperCase();
}
const int_base_patterns = {
2: /[^0-1]/,
3: /[^0-2]/,
4: /[^0-3]/,
5: /[^0-4]/,
6: /[^0-5]/,
7: /[^0-6]/,
8: /[^0-7]/,
9: /[^0-8]/,
10: /[^0-9]/,
11: /[^0-9a]/,
12: /[^0-9a-b]/,
13: /[^0-9a-c]/,
14: /[^0-9a-d]/,
15: /[^0-9a-e]/,
16: /[^0-9a-f]/,
17: /[^0-9a-g]/,
18: /[^0-9a-h]/,
19: /[^0-9a-i]/,
20: /[^0-9a-j]/,
21: /[^0-9a-k]/,
22: /[^0-9a-l]/,
23: /[^0-9a-m]/,
24: /[^0-9a-n]/,
25: /[^0-9a-o]/,
26: /[^0-9a-p]/,
27: /[^0-9a-q]/,
28: /[^0-9a-r]/,
29: /[^0-9a-s]/,
30: /[^0-9a-t]/,
31: /[^0-9a-u]/,
32: /[^0-9a-v]/,
33: /[^0-9a-w]/,
34: /[^0-9a-x]/,
35: /[^0-9a-y]/,
36: /[^0-9a-z]/,
};
export function int_from_base_string(string, base) {
if (int_base_patterns[base].test(string.replace(/^-/, "").toLowerCase())) {
return new Error(Nil);
}
const result = parseInt(string, base);
if (isNaN(result)) {
return new Error(Nil);
}
return new Ok(result);
}
export function string_replace(string, target, substitute) {
if (typeof string.replaceAll !== "undefined") {
return string.replaceAll(target, substitute);
}
// Fallback for older Node.js versions:
// 1. <https://stackoverflow.com/a/1144788>
// 2. <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping>
// TODO: This fallback could be remove once Node.js 14 is EOL
// aka <https://nodejs.org/en/about/releases/> on or after 2024-04-30
return string.replace(
// $& means the whole matched string
new RegExp(target.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"),
substitute,
);
}
export function string_reverse(string) {
return [...string].reverse().join("");
}
export function string_length(string) {
if (string === "") {
return 0;
}
const iterator = graphemes_iterator(string);
if (iterator) {
let i = 0;
for (const _ of iterator) {
i++;
}
return i;
} else {
return string.match(/./gsu).length;
}
}
export function graphemes(string) {
const iterator = graphemes_iterator(string);
if (iterator) {
return List.fromArray(Array.from(iterator).map((item) => item.segment));
} else {
return List.fromArray(string.match(/./gsu));
}
}
let segmenter = undefined;
function graphemes_iterator(string) {
if (globalThis.Intl && Intl.Segmenter) {
segmenter ||= new Intl.Segmenter();
return segmenter.segment(string)[Symbol.iterator]();
}
}
export function pop_grapheme(string) {
let first;
const iterator = graphemes_iterator(string);
if (iterator) {
first = iterator.next().value?.segment;
} else {
first = string.match(/./su)?.[0];
}
if (first) {
return new Ok([first, string.slice(first.length)]);
} else {
return new Error(Nil);
}
}
export function pop_codeunit(str) {
return [str.charCodeAt(0) | 0, str.slice(1)];
}
export function lowercase(string) {
return string.toLowerCase();
}
export function uppercase(string) {
return string.toUpperCase();
}
export function less_than(a, b) {
return a < b;
}
export function add(a, b) {
return a + b;
}
export function split(xs, pattern) {
return List.fromArray(xs.split(pattern));
}
export function join(xs, separator) {
const iterator = xs[Symbol.iterator]();
let result = iterator.next().value || "";
let current = iterator.next();
while (!current.done) {
result = result + separator + current.value;
current = iterator.next();
}
return result;
}
export function concat(xs) {
let result = "";
for (const x of xs) {
result = result + x;
}
return result;
}
export function length(data) {
return data.length;
}
export function string_slice(string, idx, len) {
if (len <= 0 || idx >= string.length) {
return "";
}
const iterator = graphemes_iterator(string);
if (iterator) {
while (idx-- > 0) {
iterator.next();
}
let result = "";
while (len-- > 0) {
const v = iterator.next().value;
if (v === undefined) {
break;
}
result += v.segment;
}
return result;
} else {
return string
.match(/./gsu)
.slice(idx, idx + len)
.join("");
}
}
export function string_codeunit_slice(str, from, length) {
return str.slice(from, from + length);
}
export function crop_string(string, substring) {
return string.substring(string.indexOf(substring));
}
export function contains_string(haystack, needle) {
return haystack.indexOf(needle) >= 0;
}
export function starts_with(haystack, needle) {
return haystack.startsWith(needle);
}
export function ends_with(haystack, needle) {
return haystack.endsWith(needle);
}
export function split_once(haystack, needle) {
const index = haystack.indexOf(needle);
if (index >= 0) {
const before = haystack.slice(0, index);
const after = haystack.slice(index + needle.length);
return new Ok([before, after]);
} else {
return new Error(Nil);
}
}
const unicode_whitespaces = [
"\u0020", // Space
"\u0009", // Horizontal tab
"\u000A", // Line feed
"\u000B", // Vertical tab
"\u000C", // Form feed
"\u000D", // Carriage return
"\u0085", // Next line
"\u2028", // Line separator
"\u2029", // Paragraph separator
].join("");
const trim_start_regex = new RegExp(`^[${unicode_whitespaces}]*`);
const trim_end_regex = new RegExp(`[${unicode_whitespaces}]*$`);
export function trim_start(string) {
return string.replace(trim_start_regex, "");
}
export function trim_end(string) {
return string.replace(trim_end_regex, "");
}
export function bit_array_from_string(string) {
return toBitArray([stringBits(string)]);
}
export function bit_array_concat(bit_arrays) {
return toBitArray(bit_arrays.toArray().map((b) => b.buffer));
}
export function console_log(term) {
console.log(term);
}
export function console_error(term) {
console.error(term);
}
export function crash(message) {
throw new globalThis.Error(message);
}
export function bit_array_to_string(bit_array) {
try {
const decoder = new TextDecoder("utf-8", { fatal: true });
return new Ok(decoder.decode(bit_array.buffer));
} catch {
return new Error(Nil);
}
}
export function print(string) {
if (typeof process === "object" && process.stdout?.write) {
process.stdout.write(string); // We can write without a trailing newline
} else if (typeof Deno === "object") {
Deno.stdout.writeSync(new TextEncoder().encode(string)); // We can write without a trailing newline
} else {
console.log(string); // We're in a browser. Newlines are mandated
}
}
export function print_error(string) {
if (typeof process === "object" && process.stderr?.write) {
process.stderr.write(string); // We can write without a trailing newline
} else if (typeof Deno === "object") {
Deno.stderr.writeSync(new TextEncoder().encode(string)); // We can write without a trailing newline
} else {
console.error(string); // We're in a browser. Newlines are mandated
}
}
export function print_debug(string) {
if (typeof process === "object" && process.stderr?.write) {
process.stderr.write(string + "\n"); // If we're in Node.js, use `stderr`
} else if (typeof Deno === "object") {
Deno.stderr.writeSync(new TextEncoder().encode(string + "\n")); // If we're in Deno, use `stderr`
} else {
console.log(string); // Otherwise, use `console.log` (so that it doesn't look like an error)
}
}
export function ceiling(float) {
return Math.ceil(float);
}
export function floor(float) {
return Math.floor(float);
}
export function round(float) {
return Math.round(float);
}
export function truncate(float) {
return Math.trunc(float);
}
export function power(base, exponent) {
// It is checked in Gleam that:
// - The base is non-negative and that the exponent is not fractional.
// - The base is non-zero and the exponent is non-negative (otherwise
// the result will essentially be division by zero).
// It can thus be assumed that valid input is passed to the Math.pow
// function and a NaN or Infinity value will not be produced.
return Math.pow(base, exponent);
}
export function random_uniform() {
const random_uniform_result = Math.random();
// With round-to-nearest-even behavior, the ranges claimed for the functions below
// (excluding the one for Math.random() itself) aren't exact.
// If extremely large bounds are chosen (2^53 or higher),
// it's possible in extremely rare cases to calculate the usually-excluded upper bound.
// Note that as numbers in JavaScript are IEEE 754 floating point numbers
// See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random>
// Because of this, we just loop 'until' we get a valid result where 0.0 <= x < 1.0:
if (random_uniform_result === 1.0) {
return random_uniform();
}
return random_uniform_result;
}
export function bit_array_slice(bits, position, length) {
const start = Math.min(position, position + length);
const end = Math.max(position, position + length);
if (start < 0 || end > bits.length) return new Error(Nil);
const byteOffset = bits.buffer.byteOffset + start;
const buffer = new Uint8Array(
bits.buffer.buffer,
byteOffset,
Math.abs(length),
);
return new Ok(new BitArray(buffer));
}
export function bit_array_split_once(bits, pattern) {
try {
const patternEmpty = pattern.buffer.length < 1
const patternLongerThanBits = pattern.buffer.length >= bits.buffer.length
const incorrectArguments = !(bits instanceof BitArray) || !(pattern instanceof BitArray)
if (incorrectArguments || patternEmpty || patternLongerThanBits) {
return new Error(Nil);
}
const n = bits.buffer.length - pattern.buffer.length + 1;
find: for (let i = 0; i < n; i++) {
for (let j = 0; j < pattern.buffer.length; j++) {
if (bits.buffer[i + j] !== pattern.buffer[j]) {
continue find;
}
}
const before = bits.buffer.slice(0, i);
const after = bits.buffer.slice(i + pattern.buffer.length);
return new Ok([new BitArray(before), new BitArray(after)]);
}
return new Error(Nil);
} catch (e) {
return new Error(Nil);
}
}
export function bit_array_split(bits, pattern) {
try {
const patternEmpty = pattern.buffer.length < 1
const incorrectArguments = !(bits instanceof BitArray) || !(pattern instanceof BitArray)
if (incorrectArguments || patternEmpty) {
return new Error(Nil);
}
const bitsShorter = bits.buffer.length < pattern.buffer.length
if (bitsShorter) {
return new Ok(List.fromArray([bits]))
}
const results = [];
let lastIndex = 0;
const n = bits.buffer.length - pattern.buffer.length + 1;
find: for (let i = 0; i < n; i++) {
for (let j = 0; j < pattern.buffer.length; j++) {
if (bits.buffer[i + j] !== pattern.buffer[j]) {
continue find;
}
}
const bitsEqualsPattern = bits.buffer.length === pattern.buffer.length
if (bitsEqualsPattern) {
return new Ok(List.fromArray([]));
}
if (i > lastIndex) {
results.push(new BitArray(bits.buffer.slice(lastIndex, i)));
}
lastIndex = i + pattern.buffer.length;
i = lastIndex - 1;
}
if (lastIndex < bits.buffer.length) {
results.push(new BitArray(bits.buffer.slice(lastIndex)));
}
return new Ok(List.fromArray(results))
} catch (e) {
return new Error(Nil);
}
}
export function codepoint(int) {
return new UtfCodepoint(int);
}
export function string_to_codepoint_integer_list(string) {
return List.fromArray(Array.from(string).map((item) => item.codePointAt(0)));
}
export function utf_codepoint_list_to_string(utf_codepoint_integer_list) {
return utf_codepoint_integer_list
.toArray()
.map((x) => String.fromCodePoint(x.value))
.join("");
}
export function utf_codepoint_to_int(utf_codepoint) {
return utf_codepoint.value;
}
export function new_map() {
return Dict.new();
}
export function map_size(map) {
return map.size;
}
export function map_to_list(map) {
return List.fromArray(map.entries());
}
export function map_remove(key, map) {
return map.delete(key);
}
export function map_get(map, key) {
const value = map.get(key, NOT_FOUND);
if (value === NOT_FOUND) {
return new Error(Nil);
}
return new Ok(value);
}
export function map_insert(key, value, map) {
return map.set(key, value);
}
function unsafe_percent_decode(string) {
return decodeURIComponent(string || "");
}
function unsafe_percent_decode_query(string) {
return decodeURIComponent((string || "").replace("+", " "));
}
export function percent_decode(string) {
try {
return new Ok(unsafe_percent_decode(string));
} catch {
return new Error(Nil);
}
}
export function percent_encode(string) {
return encodeURIComponent(string).replace("%2B", "+");
}
export function parse_query(query) {
try {
const pairs = [];
for (const section of query.split("&")) {
const [key, value] = section.split("=");
if (!key) continue;
const decodedKey = unsafe_percent_decode_query(key);
const decodedValue = unsafe_percent_decode_query(value);
pairs.push([decodedKey, decodedValue]);
}
return new Ok(List.fromArray(pairs));
} catch {
return new Error(Nil);
}
}
const b64EncodeLookup = [
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, 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, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47,
];
let b64TextDecoder;
// Implementation based on https://github.com/mitschabaude/fast-base64/blob/main/js.js
export function encode64(bit_array, padding) {
b64TextDecoder ??= new TextDecoder();
const bytes = bit_array.buffer;
const m = bytes.length;
const k = m % 3;
const n = Math.floor(m / 3) * 4 + (k && k + 1);
const N = Math.ceil(m / 3) * 4;
const encoded = new Uint8Array(N);
for (let i = 0, j = 0; j < m; i += 4, j += 3) {
const y = (bytes[j] << 16) + (bytes[j + 1] << 8) + (bytes[j + 2] | 0);
encoded[i] = b64EncodeLookup[y >> 18];
encoded[i + 1] = b64EncodeLookup[(y >> 12) & 0x3f];
encoded[i + 2] = b64EncodeLookup[(y >> 6) & 0x3f];
encoded[i + 3] = b64EncodeLookup[y & 0x3f];
}
let base64 = b64TextDecoder.decode(new Uint8Array(encoded.buffer, 0, n));
if (padding) {
if (k === 1) {
base64 += "==";
} else if (k === 2) {
base64 += "=";
}
}
return base64;
}
// From https://developer.mozilla.org/en-US/docs/Glossary/Base64
export function decode64(sBase64) {
try {
const binString = atob(sBase64);
const length = binString.length;
const array = new Uint8Array(length);
for (let i = 0; i < length; i++) {
array[i] = binString.charCodeAt(i);
}
return new Ok(new BitArray(array));
} catch {
return new Error(Nil);
}
}
export function classify_dynamic(data) {
if (typeof data === "string") {
return "String";
} else if (typeof data === "boolean") {
return "Bool";
} else if (data instanceof Result) {
return "Result";
} else if (data instanceof List) {
return "List";
} else if (data instanceof BitArray) {
return "BitArray";
} else if (data instanceof Dict) {
return "Dict";
} else if (Number.isInteger(data)) {
return "Int";
} else if (Array.isArray(data)) {
return `Tuple of ${data.length} elements`;
} else if (typeof data === "number") {
return "Float";
} else if (data === null) {
return "Null";
} else if (data === undefined) {
return "Nil";
} else {
const type = typeof data;
return type.charAt(0).toUpperCase() + type.slice(1);
}
}
function decoder_error(expected, got) {
return decoder_error_no_classify(expected, classify_dynamic(got));
}
function decoder_error_no_classify(expected, got) {
return new Error(
List.fromArray([new DecodeError(expected, got, List.fromArray([]))]),
);
}
export function decode_string(data) {
return typeof data === "string"
? new Ok(data)
: decoder_error("String", data);
}
export function decode_int(data) {
return Number.isInteger(data) ? new Ok(data) : decoder_error("Int", data);
}
export function decode_float(data) {
return typeof data === "number" ? new Ok(data) : decoder_error("Float", data);
}
export function decode_bool(data) {
return typeof data === "boolean" ? new Ok(data) : decoder_error("Bool", data);
}
export function decode_bit_array(data) {
if (data instanceof BitArray) {
return new Ok(data);
}
if (data instanceof Uint8Array) {
return new Ok(new BitArray(data));
}
return decoder_error("BitArray", data);
}
export function decode_tuple(data) {
return Array.isArray(data) ? new Ok(data) : decoder_error("Tuple", data);
}
export function decode_tuple2(data) {
return decode_tupleN(data, 2);
}
export function decode_tuple3(data) {
return decode_tupleN(data, 3);
}
export function decode_tuple4(data) {
return decode_tupleN(data, 4);
}
export function decode_tuple5(data) {
return decode_tupleN(data, 5);
}
export function decode_tuple6(data) {
return decode_tupleN(data, 6);
}
function decode_tupleN(data, n) {
if (Array.isArray(data) && data.length == n) {
return new Ok(data);
}
const list = decode_exact_length_list(data, n);
if (list) return new Ok(list);
return decoder_error(`Tuple of ${n} elements`, data);
}
function decode_exact_length_list(data, n) {
if (!(data instanceof List)) return;
const elements = [];
let current = data;
for (let i = 0; i < n; i++) {
if (!(current instanceof NonEmpty)) break;
elements.push(current.head);
current = current.tail;
}
if (elements.length === n && !(current instanceof NonEmpty)) return elements;
}
export function tuple_get(data, index) {
return index >= 0 && data.length > index
? new Ok(data[index])
: new Error(Nil);
}
export function decode_list(data) {
if (Array.isArray(data)) {
return new Ok(List.fromArray(data));
}
return data instanceof List ? new Ok(data) : decoder_error("List", data);
}
export function decode_result(data) {
return data instanceof Result ? new Ok(data) : decoder_error("Result", data);
}
export function decode_map(data) {
if (data instanceof Dict) {
return new Ok(data);
}
if (data instanceof Map || data instanceof WeakMap) {
return new Ok(Dict.fromMap(data));
}
if (data == null) {
return decoder_error("Dict", data);
}
if (typeof data !== "object") {
return decoder_error("Dict", data);
}
const proto = Object.getPrototypeOf(data);
if (proto === Object.prototype || proto === null) {
return new Ok(Dict.fromObject(data));
}
return decoder_error("Dict", data);
}
export function decode_option(data, decoder) {
if (data === null || data === undefined || data instanceof None)
return new Ok(new None());
if (data instanceof Some) data = data[0];
const result = decoder(data);
if (result.isOk()) {
return new Ok(new Some(result[0]));
} else {
return result;
}
}
export function decode_field(value, name) {
const not_a_map_error = () => decoder_error("Dict", value);
if (
value instanceof Dict ||
value instanceof WeakMap ||
value instanceof Map
) {
const entry = map_get(value, name);
return new Ok(entry.isOk() ? new Some(entry[0]) : new None());
} else if (value === null) {
return not_a_map_error();
} else if (Object.getPrototypeOf(value) == Object.prototype) {
return try_get_field(value, name, () => new Ok(new None()));
} else {
return try_get_field(value, name, not_a_map_error);
}
}
function try_get_field(value, field, or_else) {
try {
return field in value ? new Ok(new Some(value[field])) : or_else();
} catch {
return or_else();
}
}
export function byte_size(string) {
return new TextEncoder().encode(string).length;
}
// In Javascript bitwise operations convert numbers to a sequence of 32 bits
// while Erlang uses arbitrary precision.
// To get around this problem and get consistent results use BigInt and then
// downcast the value back to a Number value.
export function bitwise_and(x, y) {
return Number(BigInt(x) & BigInt(y));
}
export function bitwise_not(x) {
return Number(~BigInt(x));
}
export function bitwise_or(x, y) {
return Number(BigInt(x) | BigInt(y));
}
export function bitwise_exclusive_or(x, y) {
return Number(BigInt(x) ^ BigInt(y));
}
export function bitwise_shift_left(x, y) {
return Number(BigInt(x) << BigInt(y));
}
export function bitwise_shift_right(x, y) {
return Number(BigInt(x) >> BigInt(y));
}
export function inspect(v) {
const t = typeof v;
if (v === true) return "True";
if (v === false) return "False";
if (v === null) return "//js(null)";
if (v === undefined) return "Nil";
if (t === "string") return inspectString(v);
if (t === "bigint" || Number.isInteger(v)) return v.toString();
if (t === "number") return float_to_string(v);
if (Array.isArray(v)) return `#(${v.map(inspect).join(", ")})`;
if (v instanceof List) return inspectList(v);
if (v instanceof UtfCodepoint) return inspectUtfCodepoint(v);
if (v instanceof BitArray) return inspectBitArray(v);
if (v instanceof CustomType) return inspectCustomType(v);
if (v instanceof Dict) return inspectDict(v);
if (v instanceof Set) return `//js(Set(${[...v].map(inspect).join(", ")}))`;
if (v instanceof RegExp) return `//js(${v})`;
if (v instanceof Date) return `//js(Date("${v.toISOString()}"))`;
if (v instanceof Function) {
const args = [];
for (const i of Array(v.length).keys())
args.push(String.fromCharCode(i + 97));
return `//fn(${args.join(", ")}) { ... }`;
}
return inspectObject(v);
}
function inspectString(str) {
let new_str = '"';
for (let i = 0; i < str.length; i++) {
let char = str[i];
switch (char) {
case "\n":
new_str += "\\n";
break;
case "\r":
new_str += "\\r";
break;
case "\t":
new_str += "\\t";
break;
case "\f":
new_str += "\\f";
break;
case "\\":
new_str += "\\\\";
break;
case '"':
new_str += '\\"';
break;
default:
if (char < " " || (char > "~" && char < "\u{00A0}")) {
new_str +=
"\\u{" +
char.charCodeAt(0).toString(16).toUpperCase().padStart(4, "0") +
"}";
} else {
new_str += char;
}
}
}
new_str += '"';
return new_str;
}
function inspectDict(map) {
let body = "dict.from_list([";
let first = true;
map.forEach((value, key) => {
if (!first) body = body + ", ";
body = body + "#(" + inspect(key) + ", " + inspect(value) + ")";
first = false;
});
return body + "])";
}
function inspectObject(v) {
const name = Object.getPrototypeOf(v)?.constructor?.name || "Object";
const props = [];
for (const k of Object.keys(v)) {
props.push(`${inspect(k)}: ${inspect(v[k])}`);
}
const body = props.length ? " " + props.join(", ") + " " : "";
const head = name === "Object" ? "" : name + " ";
return `//js(${head}{${body}})`;
}
function inspectCustomType(record) {
const props = Object.keys(record)
.map((label) => {
const value = inspect(record[label]);
return isNaN(parseInt(label)) ? `${label}: ${value}` : value;
})
.join(", ");
return props
? `${record.constructor.name}(${props})`
: record.constructor.name;
}
export function inspectList(list) {
return `[${list.toArray().map(inspect).join(", ")}]`;
}
export function inspectBitArray(bits) {
return `<<${Array.from(bits.buffer).join(", ")}>>`;
}
export function inspectUtfCodepoint(codepoint) {
return `//utfcodepoint(${String.fromCodePoint(codepoint.value)})`;
}
export function base16_encode(bit_array) {
let result = "";
for (const byte of bit_array.buffer) {
result += byte.toString(16).padStart(2, "0").toUpperCase();
}
return result;
}
export function base16_decode(string) {
const bytes = new Uint8Array(string.length / 2);
for (let i = 0; i < string.length; i += 2) {
const a = parseInt(string[i], 16);
const b = parseInt(string[i + 1], 16);
if (isNaN(a) || isNaN(b)) return new Error(Nil);
bytes[i / 2] = a * 16 + b;
}
return new Ok(new BitArray(bytes));
}
export function bit_array_inspect(bits, acc) {
return `${acc}${[...bits.buffer].join(", ")}`;