···
1
1
import {
2
2
-
BitString,
2
2
+
BitArray,
3
3
CustomType,
4
4
-
Empty,
5
4
Error,
6
5
List,
7
7
-
NonEmpty,
8
6
Ok,
9
7
UtfCodepoint,
10
8
codepointBits,
11
9
divideFloat,
12
10
divideInt,
13
13
-
inspect,
14
11
isEqual,
15
12
stringBits,
16
13
toBitArray,
···
29
26
console.log("");
30
27
console.assert(false, message);
31
28
failures++;
29
29
+
}
30
30
+
31
31
+
function inspect(a) {
32
32
+
if (typeof a === "object" && a !== null && typeof a.inspect === "function") {
33
33
+
return a.inspect();
34
34
+
} else {
35
35
+
return JSON.stringify(a);
36
36
+
}
32
37
}
33
38
34
39
function assertEqual(a, b) {
···
128
133
assertNotEqual(List.fromArray([1]), List.fromArray([]));
129
134
assertNotEqual(List.fromArray([]), List.fromArray([1]));
130
135
136
136
+
assertEqual(new BitArray(new Uint8Array([])), new BitArray(new Uint8Array([])));
131
137
assertEqual(
132
132
-
new BitString(new Uint8Array([])),
133
133
-
new BitString(new Uint8Array([]))
134
134
-
);
135
135
-
assertEqual(
136
136
-
new BitString(new Uint8Array([1, 2, 3])),
137
137
-
new BitString(new Uint8Array([1, 2, 3]))
138
138
+
new BitArray(new Uint8Array([1, 2, 3])),
139
139
+
new BitArray(new Uint8Array([1, 2, 3]))
138
140
);
139
141
assertNotEqual(
140
140
-
new BitString(new Uint8Array([1, 2])),
141
141
-
new BitString(new Uint8Array([1, 2, 3]))
142
142
+
new BitArray(new Uint8Array([1, 2])),
143
143
+
new BitArray(new Uint8Array([1, 2, 3]))
142
144
);
143
145
144
146
assertEqual(new UtfCodepoint(128013), new UtfCodepoint(128013));
···
146
148
147
149
// toBitArray
148
150
149
149
-
assertEqual(new BitString(new Uint8Array([])), toBitArray([]));
151
151
+
assertEqual(new BitArray(new Uint8Array([])), toBitArray([]));
150
152
151
153
assertEqual(
152
152
-
new BitString(new Uint8Array([97, 98, 99])),
154
154
+
new BitArray(new Uint8Array([97, 98, 99])),
153
155
toBitArray([stringBits("abc")])
154
156
);
155
157
156
158
assertEqual(
157
157
-
new BitString(new Uint8Array([97])),
159
159
+
new BitArray(new Uint8Array([97])),
158
160
toBitArray([codepointBits(new UtfCodepoint(97))])
159
161
);
160
162
161
163
assertEqual(
162
162
-
new BitString(new Uint8Array([240, 159, 144, 141])),
164
164
+
new BitArray(new Uint8Array([240, 159, 144, 141])),
163
165
toBitArray([codepointBits(new UtfCodepoint(128013))])
164
166
);
165
167
···
329
331
assertEqual(hasEqualsField, { ...hasEqualsField });
330
332
assertNotEqual(hasEqualsField, hasEqualsField2);
331
333
332
332
-
// Inspecting Gleam values
333
333
-
334
334
-
assertEqual(inspect(true), "True");
335
335
-
assertEqual(inspect(false), "False");
336
336
-
assertEqual(inspect(undefined), "Nil");
337
337
-
338
338
-
assertEqual(inspect(0), "0");
339
339
-
assertEqual(inspect(1), "1");
340
340
-
assertEqual(inspect(2), "2");
341
341
-
assertEqual(inspect(-1), "-1");
342
342
-
assertEqual(inspect(-2), "-2");
343
343
-
344
344
-
assertEqual(inspect(0.23), "0.23");
345
345
-
assertEqual(inspect(1.23), "1.23");
346
346
-
assertEqual(inspect(2.23), "2.23");
347
347
-
assertEqual(inspect(-1.23), "-1.23");
348
348
-
assertEqual(inspect(-2.23), "-2.23");
349
349
-
350
350
-
assertEqual(inspect(new Ok(1)), "Ok(1)");
351
351
-
assertEqual(inspect(new Ok(true)), "Ok(True)");
352
352
-
assertEqual(inspect(new Ok(false)), "Ok(False)");
353
353
-
assertEqual(inspect(new Ok(undefined)), "Ok(Nil)");
354
354
-
355
355
-
assertEqual(inspect(new Error(2)), "Error(2)");
356
356
-
assertEqual(inspect(new Error(true)), "Error(True)");
357
357
-
assertEqual(inspect(new Error(false)), "Error(False)");
358
358
-
assertEqual(inspect(new Error(undefined)), "Error(Nil)");
359
359
-
334
334
+
assertEqual(new BitArray(new Uint8Array([1, 2, 3])).byteAt(0), 1);
335
335
+
assertEqual(new BitArray(new Uint8Array([1, 2, 3])).byteAt(2), 3);
360
336
assertEqual(
361
361
-
inspect(new ExampleRecordImpl(undefined, 1, 2.1)),
362
362
-
"ExampleRecordImpl(Nil, detail: 1, boop: 2.1)"
363
363
-
);
364
364
-
assertEqual(
365
365
-
inspect(new ExampleRecordImpl(new Ok(1), 1, 2.1)),
366
366
-
"ExampleRecordImpl(Ok(1), detail: 1, boop: 2.1)"
367
367
-
);
368
368
-
369
369
-
assertEqual(inspect([]), "#()");
370
370
-
assertEqual(inspect([1, 2, 3]), "#(1, 2, 3)");
371
371
-
assertEqual(inspect([new Ok(1), new Ok(2)]), "#(Ok(1), Ok(2))");
372
372
-
373
373
-
assertEqual(inspect(List.fromArray([])), "[]");
374
374
-
assertEqual(inspect(List.fromArray([1, 2, 3])), "[1, 2, 3]");
375
375
-
assertEqual(inspect(List.fromArray([new Ok(1), new Ok(2)])), "[Ok(1), Ok(2)]");
376
376
-
377
377
-
assertEqual(inspect(new BitString(new Uint8Array([]))), "<<>>");
378
378
-
assertEqual(inspect(new BitString(new Uint8Array([1, 2, 3]))), "<<1, 2, 3>>");
379
379
-
380
380
-
assertEqual(new BitString(new Uint8Array([1, 2, 3])).byteAt(0), 1);
381
381
-
assertEqual(new BitString(new Uint8Array([1, 2, 3])).byteAt(2), 3);
382
382
-
assertEqual(
383
383
-
new BitString(new Uint8Array([63, 240, 0, 0, 0, 0, 0, 0])).floatAt(0),
337
337
+
new BitArray(new Uint8Array([63, 240, 0, 0, 0, 0, 0, 0])).floatAt(0),
384
338
1.0
385
339
);
386
386
-
assertEqual(new BitString(new Uint8Array([1, 2, 3])).intFromSlice(0, 1), 1);
387
387
-
assertEqual(new BitString(new Uint8Array([1, 2, 3])).intFromSlice(0, 2), 258);
388
388
-
assertEqual(
389
389
-
new BitString(new Uint8Array([1, 2, 3])).sliceAfter(1),
390
390
-
new BitString(new Uint8Array([2, 3]))
391
391
-
);
392
392
-
393
393
-
assertEqual(inspect(new UtfCodepoint(128013)), "//utfcodepoint(🐍)");
394
394
-
395
395
-
assertEqual(
396
396
-
inspect(() => undefined),
397
397
-
"//fn() { ... }"
398
398
-
);
399
399
-
400
400
-
assertEqual(
401
401
-
inspect((a) => undefined),
402
402
-
"//fn(a) { ... }"
403
403
-
);
404
404
-
405
405
-
assertEqual(
406
406
-
inspect((x, y) => undefined),
407
407
-
"//fn(a, b) { ... }"
408
408
-
);
409
409
-
410
410
-
assertEqual(
411
411
-
inspect((x, y, z) => undefined),
412
412
-
"//fn(a, b, c) { ... }"
413
413
-
);
414
414
-
415
415
-
// Inspecting JavaScript values
416
416
-
417
417
-
assertEqual(inspect(null), "//js(null)");
418
418
-
assertEqual(inspect({}), "//js({})");
419
419
-
assertEqual(inspect({ a: 1 }), '//js({ "a": 1 })');
420
420
-
assertEqual(inspect({ a: 1, b: 2 }), '//js({ "a": 1, "b": 2 })');
421
421
-
assertEqual(inspect({ a: 1, b: new Ok(1) }), '//js({ "a": 1, "b": Ok(1) })');
422
422
-
assertEqual(
423
423
-
inspect(new globalThis.Error("Oh no")),
424
424
-
'//js(Error { "message": "Oh no" })'
425
425
-
);
340
340
+
assertEqual(new BitArray(new Uint8Array([1, 2, 3])).intFromSlice(0, 1), 1);
341
341
+
assertEqual(new BitArray(new Uint8Array([1, 2, 3])).intFromSlice(0, 2), 258);
426
342
assertEqual(
427
427
-
inspect(
428
428
-
(() => {
429
429
-
let error = new globalThis.Error("Oh no");
430
430
-
error.other = new Ok(1);
431
431
-
return error;
432
432
-
})()
433
433
-
),
434
434
-
'//js(Error { "message": "Oh no", "other": Ok(1) })'
343
343
+
new BitArray(new Uint8Array([1, 2, 3])).sliceAfter(1),
344
344
+
new BitArray(new Uint8Array([2, 3]))
435
345
);
436
346
437
437
-
// Generic JS objects
438
438
-
assertEqual(inspect(Promise.resolve(1)), "//js(Promise {})");
439
439
-
440
440
-
// Inspecting Dates
441
441
-
assertEqual(
442
442
-
inspect(new Date("1991-01-05")),
443
443
-
'//js(Date("1991-01-05T00:00:00.000Z"))'
444
444
-
);
445
445
-
446
446
-
// Inspecting RegExps
447
447
-
assertEqual(inspect(/1[23]/g), "//js(/1[23]/g)");
448
448
-
449
449
-
// Inspecting Maps
450
450
-
assertEqual(
451
451
-
inspect(
452
452
-
new Map([
453
453
-
[1, 2],
454
454
-
[3, new Ok([1, 2])],
455
455
-
])
456
456
-
),
457
457
-
"//js(Map { 1: 2, 3: Ok(#(1, 2)) })"
458
458
-
);
459
459
-
460
460
-
// Inspecting Sets
461
461
-
assertEqual(
462
462
-
inspect(new Set([1, 2, new Ok([1, 2])])),
463
463
-
"//js(Set(1, 2, Ok(#(1, 2))))"
464
464
-
);
465
465
-
466
466
-
// Inspecting objects that have the null prototype
467
467
-
let nullPrototypeObject = Object.create(null);
468
468
-
assertEqual(inspect(nullPrototypeObject), "//js({})");
469
469
-
nullPrototypeObject.one = 1;
470
470
-
nullPrototypeObject.two = 2;
471
471
-
assertEqual(inspect(nullPrototypeObject), '//js({ "one": 1, "two": 2 })');
472
472
-
473
347
// Result.isOk
474
348
475
349
assertEqual(new Ok(1).isOk(), true);
···
502
376
assertEqual([...toList([])], []);
503
377
assertEqual([...toList([1, 2, 3])], [1, 2, 3]);
504
378
505
505
-
// BitString.length
379
379
+
// BitArray.length
506
380
507
507
-
assertEqual(new BitString(new Uint8Array([])).length, 0);
508
508
-
assertEqual(new BitString(new Uint8Array([1, 2])).length, 2);
509
509
-
assertEqual(new BitString(new Uint8Array([1, 2, 3, 4])).length, 4);
381
381
+
assertEqual(new BitArray(new Uint8Array([])).length, 0);
382
382
+
assertEqual(new BitArray(new Uint8Array([1, 2])).length, 2);
383
383
+
assertEqual(new BitArray(new Uint8Array([1, 2, 3, 4])).length, 4);
510
384
511
385
//
512
386
// Division
···
562
436
new ExampleRecordImpl(6, 5, 4)
563
437
);
564
438
565
565
-
// Test BitString can only be constructed from Uint8Array, not ArrayBuffer
566
566
-
const bs1 = new BitString(new Uint8Array(new ArrayBuffer(8)));
439
439
+
// Test BitArray can only be constructed from Uint8Array, not ArrayBuffer
440
440
+
const bs1 = new BitArray(new Uint8Array(new ArrayBuffer(8)));
567
441
assertThrows("Should only construct BitArray from Uint8Array", () => {
568
568
-
const bs = new BitString(new ArrayBuffer(8));
442
442
+
const bs = new BitArray(new ArrayBuffer(8));
569
443
});
570
444
571
445
//