mol_vary 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/web.test.js CHANGED
@@ -1319,156 +1319,6 @@ var $;
1319
1319
  });
1320
1320
  })($ || ($ = {}));
1321
1321
 
1322
- ;
1323
- "use strict";
1324
- var $;
1325
- (function ($_1) {
1326
- var $$;
1327
- (function ($$) {
1328
- $mol_test({
1329
- "1 byte int"($) {
1330
- $mol_assert_equal($mol_bigint_encode(0n), new Uint8Array(new Int8Array([0]).buffer));
1331
- $mol_assert_equal($mol_bigint_encode(1n), new Uint8Array(new Int8Array([1]).buffer));
1332
- $mol_assert_equal($mol_bigint_encode(-1n), new Uint8Array(new Int8Array([-1]).buffer));
1333
- $mol_assert_equal($mol_bigint_encode(127n), new Uint8Array(new Int8Array([127]).buffer));
1334
- $mol_assert_equal($mol_bigint_encode(-128n), new Uint8Array(new Int8Array([-128]).buffer));
1335
- },
1336
- "2 byte int"($) {
1337
- $mol_assert_equal($mol_bigint_encode(128n), new Uint8Array(new Int16Array([128]).buffer));
1338
- $mol_assert_equal($mol_bigint_encode(-129n), new Uint8Array(new Int16Array([-129]).buffer));
1339
- $mol_assert_equal($mol_bigint_encode(128n * 256n - 1n), new Uint8Array(new Int16Array([128 * 256 - 1]).buffer));
1340
- $mol_assert_equal($mol_bigint_encode(-128n * 256n), new Uint8Array(new Int16Array([-128 * 256]).buffer));
1341
- },
1342
- "3 byte int"($) {
1343
- $mol_assert_equal($mol_bigint_encode(128n * 256n), new Uint8Array(new Int32Array([128 * 256]).buffer).slice(0, 3));
1344
- $mol_assert_equal($mol_bigint_encode(-128n * 256n - 1n), new Uint8Array(new Int32Array([-128 * 256 - 1]).buffer).slice(0, 3));
1345
- $mol_assert_equal($mol_bigint_encode(128n * 256n ** 2n - 1n), new Uint8Array(new Int32Array([128 * 256 ** 2 - 1]).buffer).slice(0, 3));
1346
- $mol_assert_equal($mol_bigint_encode(-128n * 256n ** 2n), new Uint8Array(new Int32Array([-128 * 256 ** 2]).buffer).slice(0, 3));
1347
- },
1348
- "4 byte int"($) {
1349
- $mol_assert_equal($mol_bigint_encode(128n * 256n ** 2n), new Uint8Array(new Int32Array([128 * 256 ** 2]).buffer));
1350
- $mol_assert_equal($mol_bigint_encode(-128n * 256n ** 2n - 1n), new Uint8Array(new Int32Array([-128 * 256 ** 2 - 1]).buffer));
1351
- $mol_assert_equal($mol_bigint_encode(128n * 256n ** 3n - 1n), new Uint8Array(new Int32Array([128 * 256 ** 3 - 1]).buffer));
1352
- $mol_assert_equal($mol_bigint_encode(-128n * 256n ** 3n), new Uint8Array(new Int32Array([-128 * 256 ** 3]).buffer));
1353
- },
1354
- "8 byte int"($) {
1355
- $mol_assert_equal($mol_bigint_encode(128n * 256n ** 7n - 1n), new Uint8Array(new BigInt64Array([128n * 256n ** 7n - 1n]).buffer));
1356
- $mol_assert_equal($mol_bigint_encode(-128n * 256n ** 7n), new Uint8Array(new BigInt64Array([-128n * 256n ** 7n]).buffer));
1357
- },
1358
- });
1359
- })($$ = $_1.$$ || ($_1.$$ = {}));
1360
- })($ || ($ = {}));
1361
-
1362
- ;
1363
- "use strict";
1364
- var $;
1365
- (function ($) {
1366
- $mol_test({
1367
- 'encode empty'() {
1368
- $mol_assert_equal($mol_charset_encode(''), new Uint8Array([]));
1369
- },
1370
- 'encode 1 octet'() {
1371
- $mol_assert_equal($mol_charset_encode('F'), new Uint8Array([0x46]));
1372
- },
1373
- 'encode 2 octet'() {
1374
- $mol_assert_equal($mol_charset_encode('Б'), new Uint8Array([0xd0, 0x91]));
1375
- },
1376
- 'encode 3 octet'() {
1377
- $mol_assert_equal($mol_charset_encode('ह'), new Uint8Array([0xe0, 0xa4, 0xb9]));
1378
- },
1379
- 'encode 4 octet'() {
1380
- $mol_assert_equal($mol_charset_encode('𐍈'), new Uint8Array([0xf0, 0x90, 0x8d, 0x88]));
1381
- },
1382
- 'encode surrogate pair'() {
1383
- $mol_assert_equal($mol_charset_encode('😀'), new Uint8Array([0xf0, 0x9f, 0x98, 0x80]));
1384
- },
1385
- });
1386
- })($ || ($ = {}));
1387
-
1388
- ;
1389
- "use strict";
1390
- var $;
1391
- (function ($) {
1392
- $mol_test({
1393
- 'decode utf8 string'() {
1394
- const str = 'Hello, ΧΨΩЫ';
1395
- const encoded = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 206, 167, 206, 168, 206, 169, 208, 171]);
1396
- $mol_assert_equal($mol_charset_decode(encoded), str);
1397
- $mol_assert_equal($mol_charset_decode(encoded, 'utf8'), str);
1398
- },
1399
- 'decode empty string'() {
1400
- const encoded = new Uint8Array([]);
1401
- $mol_assert_equal($mol_charset_decode(encoded), '');
1402
- },
1403
- });
1404
- })($ || ($ = {}));
1405
-
1406
- ;
1407
- "use strict";
1408
- var $;
1409
- (function ($) {
1410
- function $mol_bigint_decode(buf) {
1411
- if (buf.length === 8)
1412
- return new BigInt64Array(buf.buffer, buf.byteOffset, 1)[0];
1413
- if (buf.length === 4)
1414
- return BigInt(new Int32Array(buf.buffer, buf.byteOffset, 1)[0]);
1415
- if (buf.length === 2)
1416
- return BigInt(new Int16Array(buf.buffer, buf.byteOffset, 1)[0]);
1417
- if (buf.length === 1)
1418
- return BigInt(new Int8Array(buf.buffer, buf.byteOffset, 1)[0]);
1419
- const minus = (buf.at(-1) & 128) ? 255 : 0;
1420
- let result = 0n;
1421
- let offset = 0n;
1422
- for (let i = 0; i < buf.length; i++, offset += 8n) {
1423
- result |= BigInt(buf[i] ^ minus) << offset;
1424
- }
1425
- if (minus)
1426
- result = (result + 1n) * -1n;
1427
- return result;
1428
- }
1429
- $.$mol_bigint_decode = $mol_bigint_decode;
1430
- })($ || ($ = {}));
1431
-
1432
- ;
1433
- "use strict";
1434
- var $;
1435
- (function ($_1) {
1436
- var $$;
1437
- (function ($$) {
1438
- $mol_test({
1439
- "1 byte int"($) {
1440
- $mol_assert_equal($mol_bigint_decode(new Uint8Array), 0n);
1441
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int8Array([1]).buffer)), 1n);
1442
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int8Array([-1]).buffer)), -1n);
1443
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int8Array([127]).buffer)), 127n);
1444
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int8Array([-128]).buffer)), -128n);
1445
- },
1446
- "2 byte int"($) {
1447
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int16Array([128]).buffer)), 128n);
1448
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int16Array([-129]).buffer)), -129n);
1449
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int16Array([128 * 256 - 1]).buffer)), 128n * 256n - 1n);
1450
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int16Array([-128 * 256]).buffer)), -128n * 256n);
1451
- },
1452
- "3 byte int"($) {
1453
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([128 * 256]).buffer).slice(0, 3)), 128n * 256n);
1454
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([-128 * 256 - 1]).buffer).slice(0, 3)), -128n * 256n - 1n);
1455
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([128 * 256 ** 2 - 1]).buffer).slice(0, 3)), 128n * 256n ** 2n - 1n);
1456
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([-128 * 256 ** 2]).buffer).slice(0, 3)), -128n * 256n ** 2n);
1457
- },
1458
- "4 byte int"($) {
1459
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([128 * 256 ** 2]).buffer)), 128n * 256n ** 2n);
1460
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([-128 * 256 ** 2 - 1]).buffer)), -128n * 256n ** 2n - 1n);
1461
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([128 * 256 ** 3 - 1]).buffer)), 128n * 256n ** 3n - 1n);
1462
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new Int32Array([-128 * 256 ** 3]).buffer)), -128n * 256n ** 3n);
1463
- },
1464
- "8 byte int"($) {
1465
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new BigInt64Array([128n * 256n ** 7n - 1n]).buffer)), 128n * 256n ** 7n - 1n);
1466
- $mol_assert_equal($mol_bigint_decode(new Uint8Array(new BigInt64Array([-128n * 256n ** 7n]).buffer)), -128n * 256n ** 7n);
1467
- },
1468
- });
1469
- })($$ = $_1.$$ || ($_1.$$ = {}));
1470
- })($ || ($ = {}));
1471
-
1472
1322
  ;
1473
1323
  "use strict";
1474
1324
  var $;
@@ -1597,21 +1447,71 @@ var $;
1597
1447
  ;
1598
1448
  "use strict";
1599
1449
  var $;
1600
- (function ($_1) {
1601
- var $$;
1602
- (function ($$) {
1603
- $mol_test({
1604
- "Zero int"($) {
1605
- $mol_assert_equal($mol_bigint_decode($mol_bigint_encode(0n)), 0n);
1606
- },
1607
- "Large positive int"($) {
1608
- $mol_assert_equal($mol_bigint_decode($mol_bigint_encode(12345678901234567890n)), 12345678901234567890n);
1609
- },
1610
- "Large negative int"($) {
1611
- $mol_assert_equal($mol_bigint_decode($mol_bigint_encode(-12345678901234567890n)), -12345678901234567890n);
1612
- },
1613
- });
1614
- })($$ = $_1.$$ || ($_1.$$ = {}));
1450
+ (function ($) {
1451
+ $mol_test({
1452
+ 'encode empty'() {
1453
+ $mol_assert_equal($mol_charset_encode(''), new Uint8Array([]));
1454
+ },
1455
+ 'encode 1 octet'() {
1456
+ $mol_assert_equal($mol_charset_encode('F'), new Uint8Array([0x46]));
1457
+ },
1458
+ 'encode 2 octet'() {
1459
+ $mol_assert_equal($mol_charset_encode('Б'), new Uint8Array([0xd0, 0x91]));
1460
+ },
1461
+ 'encode 3 octet'() {
1462
+ $mol_assert_equal($mol_charset_encode('ह'), new Uint8Array([0xe0, 0xa4, 0xb9]));
1463
+ },
1464
+ 'encode 4 octet'() {
1465
+ $mol_assert_equal($mol_charset_encode('𐍈'), new Uint8Array([0xf0, 0x90, 0x8d, 0x88]));
1466
+ },
1467
+ 'encode surrogate pair'() {
1468
+ $mol_assert_equal($mol_charset_encode('😀'), new Uint8Array([0xf0, 0x9f, 0x98, 0x80]));
1469
+ },
1470
+ });
1471
+ })($ || ($ = {}));
1472
+
1473
+ ;
1474
+ "use strict";
1475
+ var $;
1476
+ (function ($) {
1477
+ $mol_test({
1478
+ 'decode utf8 string'() {
1479
+ const str = 'Hello, ΧΨΩЫ';
1480
+ const encoded = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 206, 167, 206, 168, 206, 169, 208, 171]);
1481
+ $mol_assert_equal($mol_charset_decode(encoded), str);
1482
+ $mol_assert_equal($mol_charset_decode(encoded, 'utf8'), str);
1483
+ },
1484
+ 'decode empty string'() {
1485
+ const encoded = new Uint8Array([]);
1486
+ $mol_assert_equal($mol_charset_decode(encoded), '');
1487
+ },
1488
+ });
1489
+ })($ || ($ = {}));
1490
+
1491
+ ;
1492
+ "use strict";
1493
+ var $;
1494
+ (function ($) {
1495
+ $mol_test({
1496
+ 'encode empty'() {
1497
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([]), 0, 0), ['', 0]);
1498
+ },
1499
+ 'encode 1 octet'() {
1500
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0x46]), 0, 1), ['F', 1]);
1501
+ },
1502
+ 'encode 2 octet'() {
1503
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xd0, 0x91]), 0, 1), ['Б', 2]);
1504
+ },
1505
+ 'encode 3 octet'() {
1506
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xe0, 0xa4, 0xb9]), 0, 1), ['ह', 3]);
1507
+ },
1508
+ 'encode 4 octet'() {
1509
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xf0, 0x90, 0x8d, 0x88]), 0, 1), ['𐍈', 4]);
1510
+ },
1511
+ 'encode surrogate pair'() {
1512
+ $mol_assert_equal($mol_charset_decode_from(new Uint8Array([0xf0, 0x9f, 0x98, 0x80]), 0, 2), ['😀', 4]);
1513
+ },
1514
+ });
1615
1515
  })($ || ($ = {}));
1616
1516
 
1617
1517
  ;
@@ -1681,23 +1581,25 @@ var $;
1681
1581
  "vary pack float"($) {
1682
1582
  check(1.5, [fp64, ...new Uint8Array(new Float64Array([1.5]).buffer)]);
1683
1583
  },
1584
+ "vary pack list"($) {
1585
+ check([1, 2, 3], [list | 3, 1, 2, 3]);
1586
+ check([[], [1], [2, 3]], [list | 3, list | 0, list | 1, 1, list | 2, 2, 3]);
1587
+ },
1588
+ "vary pack dedup list"($) {
1589
+ const pair = [1, 2];
1590
+ check([pair, pair], [list | 2, list | 2, 1, 2, link | 0]);
1591
+ const seven = [7];
1592
+ const box = [seven];
1593
+ check([box, box, seven], [list | 3, list | 1, list | 1, 7, link | 1, link | 0]);
1594
+ },
1684
1595
  "vary pack text"($) {
1685
1596
  check('foo', [text | 3, ...str('foo')]);
1686
1597
  const long = 'abcdefghijklmnopqrstuvwxyzЖЫ';
1687
- check(long, [text | l1, 30, ...str(long)]);
1598
+ check(long, [text | l1, 28, ...str(long)]);
1688
1599
  },
1689
1600
  "vary pack dedup text"($) {
1690
1601
  check(["f", "f"], [list | 2, text | 1, ...str('f'), link | 0]);
1691
1602
  },
1692
- "vary pack list"($) {
1693
- check([1, 2, 3], [list | 3, 1, 2, 3]);
1694
- check([[], [1], [2, 3]], [list | 3, list | 0, list | 1, 1, list | 2, 2, 3]);
1695
- },
1696
- "vary pack dedup list"($) {
1697
- check([[1, 2], [1, 2]], [list | 2, list | 2, 1, 2, link | 0]);
1698
- check([[[1]], [[1]]], [list | 2, list | 1, list | 1, 1, link | 1]);
1699
- check([[[7]], [[7]], [7]], [list | 3, list | 1, list | 1, 7, link | 1, link | 0]);
1700
- },
1701
1603
  "vary pack blob"($) {
1702
1604
  check(new Uint8Array([1, 255]), [blob | 2, uint | l1, 1, 255]);
1703
1605
  check(new Int8Array([-128, 127]), [blob | 2, sint | ~l1, -128, 127]);
@@ -1709,23 +1611,26 @@ var $;
1709
1611
  check(new Float64Array([1.5]), [blob | 8, fp64, ...new Uint8Array(new Float64Array([1.5]).buffer)]);
1710
1612
  },
1711
1613
  "vary pack dedup blob"($) {
1712
- check([new Uint8Array([1, 2]), new Uint8Array([1, 2])], [list | 2, blob | 2, uint | l1, 1, 2, link | 0]);
1614
+ const part = new Uint8Array([1, 2]);
1615
+ check([part, part], [list | 2, blob | 2, uint | l1, 1, 2, link | 0]);
1713
1616
  },
1714
1617
  "vary pack struct"($) {
1715
- check({ a: 1, b: 2 }, [tupl | 2, list | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
1716
- check({ x: {}, y: { a: 1 } }, [tupl | 2, list | 2, text | 1, ...str('x'), text | 1, ...str('y'), tupl | 0, list | 0, tupl | 1, list | 1, text | 1, ...str('a'), 1]);
1717
- check([{}, { foo: 1 }, { foo: 2 }], [list | 3, tupl | 0, list | 0, tupl | 1, list | 1, text | 3, ...str('foo'), 1, tupl | 1, link | 1, 2]);
1618
+ check({ a: 1, b: 2 }, [tupl | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
1619
+ check({ x: {}, y: { a: 1 } }, [tupl | 2, text | 1, ...str('x'), text | 1, ...str('y'), tupl | 0, tupl | 1, text | 1, ...str('a'), 1]);
1718
1620
  },
1719
- "vary pack struct dedup"($) {
1720
- check({ x: 1, y: { x: 2, y: 3 } }, [tupl | 2, list | 2, text | 1, ...str('x'), text | 1, ...str('y'), 1, tupl | 2, link | 2, 2, 3]);
1721
- check([{ x: 1 }, { x: 1 }], [list | 2, tupl | 1, list | 1, text | 1, ...str('x'), 1, link | 2]);
1722
- check({ x: { x: 1, y: 2 }, y: { x: 1, y: 2 } }, [tupl | 2, list | 2, text | 1, ...str('x'), text | 1, ...str('y'), tupl | 2, link | 2, 1, 2, link | 3]);
1621
+ "vary pack struct full dedup"($) {
1622
+ const item = { x: 1 };
1623
+ check([item, item], [list | 2, tupl | 1, text | 1, ...str('x'), 1, link | 1]);
1723
1624
  },
1724
1625
  "vary pack Map"($) {
1725
- check(new Map([['foo', 1], [2, 'bar']]), [tupl | 2, list | 2, text | 4, ...str('keys'), text | 4, ...str('vals'), list | 2, text | 3, ...str('foo'), 2, list | 2, 1, text | 3, ...str('bar')]);
1626
+ check(new Map([['foo', 1], [2, 'bar']]), [tupl | 2, text | 4, ...str('keys'), text | 4, ...str('vals'), list | 2, text | 3, ...str('foo'), 2, list | 2, 1, text | 3, ...str('bar')]);
1726
1627
  },
1727
1628
  "vary pack Set"($) {
1728
- check(new Set([7, 'foo']), [tupl | 1, list | 1, text | 4, ...str('vals'), list | 2, 7, text | 3, ...str('foo')]);
1629
+ check(new Set([7, 'foo']), [tupl | 1, text | 4, ...str('vals'), list | 2, 7, text | 3, ...str('foo')]);
1630
+ },
1631
+ "vary pack Date"($) {
1632
+ const date = new Date('2025-01-02T03:04:05.678');
1633
+ check(date, [tupl | 1, text | 9, ...str('unix_time'), fp64, ...new Uint8Array(new Float64Array([date.valueOf() / 1000]).buffer)]);
1729
1634
  },
1730
1635
  "vary pack custom class"($) {
1731
1636
  class Foo {
@@ -1740,11 +1645,8 @@ var $;
1740
1645
  return [this.a, this.b].values();
1741
1646
  }
1742
1647
  }
1743
- $mol_vary.type((a = 0, b = 0) => new Foo(a, b), foo => [
1744
- ['a', 'b'],
1745
- [foo.a, foo.b],
1746
- ]);
1747
- check(new Foo(1, 2), [tupl | 2, list | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
1648
+ $mol_vary.type(['a', 'b'], (a = 0, b = 0) => new Foo(a, b), foo => [foo.a, foo.b]);
1649
+ check(new Foo(1, 2), [tupl | 2, text | 1, ...str('a'), text | 1, ...str('b'), 1, 2]);
1748
1650
  },
1749
1651
  });
1750
1652
  })($$ = $_1.$$ || ($_1.$$ = {}));