mol_wire_lib 1.0.1680 → 1.0.1682

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/node.mjs CHANGED
@@ -635,6 +635,14 @@ var $;
635
635
  $.$mol_func_name_from = $mol_func_name_from;
636
636
  })($ || ($ = {}));
637
637
 
638
+ ;
639
+ "use strict";
640
+ var $;
641
+ (function ($) {
642
+ $.$mol_key_handle = Symbol.for('$mol_key_handle');
643
+ $.$mol_key_store = new WeakMap();
644
+ })($ || ($ = {}));
645
+
638
646
  ;
639
647
  "use strict";
640
648
  var $;
@@ -671,6 +679,9 @@ var $;
671
679
  static toJSON() {
672
680
  return this.toString();
673
681
  }
682
+ static [$mol_key_handle]() {
683
+ return this.toString();
684
+ }
674
685
  destructor() { }
675
686
  static destructor() { }
676
687
  [Symbol.dispose]() {
@@ -1764,46 +1775,50 @@ var $;
1764
1775
  "use strict";
1765
1776
  var $;
1766
1777
  (function ($) {
1767
- $.$mol_key_store = new WeakMap();
1768
1778
  const TypedArray = Object.getPrototypeOf(Uint8Array);
1769
1779
  function $mol_key(value) {
1770
- if (typeof value === 'bigint')
1771
- return value.toString() + 'n';
1772
- if (typeof value === 'symbol')
1773
- return value.description;
1774
- if (!value)
1775
- return JSON.stringify(value);
1776
- if (typeof value !== 'object' && typeof value !== 'function')
1777
- return JSON.stringify(value);
1778
- return JSON.stringify(value, (field, value) => {
1780
+ primitives: {
1779
1781
  if (typeof value === 'bigint')
1780
1782
  return value.toString() + 'n';
1781
1783
  if (typeof value === 'symbol')
1782
- return value.description;
1784
+ return `Symbol(${value.description})`;
1783
1785
  if (!value)
1784
- return value;
1786
+ return JSON.stringify(value);
1785
1787
  if (typeof value !== 'object' && typeof value !== 'function')
1786
- return value;
1788
+ return JSON.stringify(value);
1789
+ }
1790
+ caching: {
1791
+ let key = $mol_key_store.get(value);
1792
+ if (key)
1793
+ return key;
1794
+ }
1795
+ objects: {
1796
+ if (value instanceof TypedArray) {
1797
+ return `${value[Symbol.toStringTag]}([${[...value].map(v => $mol_key(v))}])`;
1798
+ }
1787
1799
  if (Array.isArray(value))
1788
- return value;
1789
- const proto = Reflect.getPrototypeOf(value);
1790
- if (!proto)
1791
- return value;
1792
- if (Reflect.getPrototypeOf(proto) === null)
1793
- return value;
1794
- if ('toJSON' in value)
1795
- return value;
1800
+ return `[${value.map(v => $mol_key(v))}]`;
1796
1801
  if (value instanceof RegExp)
1797
1802
  return value.toString();
1798
- if (value instanceof TypedArray)
1799
- return [...value];
1800
- let key = $.$mol_key_store.get(value);
1801
- if (key)
1802
- return key;
1803
- key = $mol_guid();
1804
- $.$mol_key_store.set(value, key);
1803
+ if (value instanceof Date)
1804
+ return `Date(${value.valueOf()})`;
1805
+ }
1806
+ structures: {
1807
+ const proto = Reflect.getPrototypeOf(value);
1808
+ if (!proto || !Reflect.getPrototypeOf(proto)) {
1809
+ return `{${Object.entries(value).map(([k, v]) => JSON.stringify(k) + ':' + $mol_key(v))}}`;
1810
+ }
1811
+ }
1812
+ handlers: {
1813
+ if ($mol_key_handle in value) {
1814
+ return value[$mol_key_handle]();
1815
+ }
1816
+ }
1817
+ containers: {
1818
+ const key = JSON.stringify('#' + $mol_guid());
1819
+ $mol_key_store.set(value, key);
1805
1820
  return key;
1806
- });
1821
+ }
1807
1822
  }
1808
1823
  $.$mol_key = $mol_key;
1809
1824
  })($ || ($ = {}));
package/node.test.js CHANGED
@@ -626,6 +626,14 @@ var $;
626
626
  $.$mol_func_name_from = $mol_func_name_from;
627
627
  })($ || ($ = {}));
628
628
 
629
+ ;
630
+ "use strict";
631
+ var $;
632
+ (function ($) {
633
+ $.$mol_key_handle = Symbol.for('$mol_key_handle');
634
+ $.$mol_key_store = new WeakMap();
635
+ })($ || ($ = {}));
636
+
629
637
  ;
630
638
  "use strict";
631
639
  var $;
@@ -662,6 +670,9 @@ var $;
662
670
  static toJSON() {
663
671
  return this.toString();
664
672
  }
673
+ static [$mol_key_handle]() {
674
+ return this.toString();
675
+ }
665
676
  destructor() { }
666
677
  static destructor() { }
667
678
  [Symbol.dispose]() {
@@ -1755,46 +1766,50 @@ var $;
1755
1766
  "use strict";
1756
1767
  var $;
1757
1768
  (function ($) {
1758
- $.$mol_key_store = new WeakMap();
1759
1769
  const TypedArray = Object.getPrototypeOf(Uint8Array);
1760
1770
  function $mol_key(value) {
1761
- if (typeof value === 'bigint')
1762
- return value.toString() + 'n';
1763
- if (typeof value === 'symbol')
1764
- return value.description;
1765
- if (!value)
1766
- return JSON.stringify(value);
1767
- if (typeof value !== 'object' && typeof value !== 'function')
1768
- return JSON.stringify(value);
1769
- return JSON.stringify(value, (field, value) => {
1771
+ primitives: {
1770
1772
  if (typeof value === 'bigint')
1771
1773
  return value.toString() + 'n';
1772
1774
  if (typeof value === 'symbol')
1773
- return value.description;
1775
+ return `Symbol(${value.description})`;
1774
1776
  if (!value)
1775
- return value;
1777
+ return JSON.stringify(value);
1776
1778
  if (typeof value !== 'object' && typeof value !== 'function')
1777
- return value;
1779
+ return JSON.stringify(value);
1780
+ }
1781
+ caching: {
1782
+ let key = $mol_key_store.get(value);
1783
+ if (key)
1784
+ return key;
1785
+ }
1786
+ objects: {
1787
+ if (value instanceof TypedArray) {
1788
+ return `${value[Symbol.toStringTag]}([${[...value].map(v => $mol_key(v))}])`;
1789
+ }
1778
1790
  if (Array.isArray(value))
1779
- return value;
1780
- const proto = Reflect.getPrototypeOf(value);
1781
- if (!proto)
1782
- return value;
1783
- if (Reflect.getPrototypeOf(proto) === null)
1784
- return value;
1785
- if ('toJSON' in value)
1786
- return value;
1791
+ return `[${value.map(v => $mol_key(v))}]`;
1787
1792
  if (value instanceof RegExp)
1788
1793
  return value.toString();
1789
- if (value instanceof TypedArray)
1790
- return [...value];
1791
- let key = $.$mol_key_store.get(value);
1792
- if (key)
1793
- return key;
1794
- key = $mol_guid();
1795
- $.$mol_key_store.set(value, key);
1794
+ if (value instanceof Date)
1795
+ return `Date(${value.valueOf()})`;
1796
+ }
1797
+ structures: {
1798
+ const proto = Reflect.getPrototypeOf(value);
1799
+ if (!proto || !Reflect.getPrototypeOf(proto)) {
1800
+ return `{${Object.entries(value).map(([k, v]) => JSON.stringify(k) + ':' + $mol_key(v))}}`;
1801
+ }
1802
+ }
1803
+ handlers: {
1804
+ if ($mol_key_handle in value) {
1805
+ return value[$mol_key_handle]();
1806
+ }
1807
+ }
1808
+ containers: {
1809
+ const key = JSON.stringify('#' + $mol_guid());
1810
+ $mol_key_store.set(value, key);
1796
1811
  return key;
1797
- });
1812
+ }
1798
1813
  }
1799
1814
  $.$mol_key = $mol_key;
1800
1815
  })($ || ($ = {}));
@@ -4739,9 +4754,9 @@ var $;
4739
4754
  $mol_assert_equal($mol_key({ foo: [false] }), '{"foo":[false]}');
4740
4755
  },
4741
4756
  'Uint8Array'() {
4742
- $mol_assert_equal($mol_key(new Uint8Array([1, 2])), '[1,2]');
4743
- $mol_assert_equal($mol_key([new Uint8Array([1, 2])]), '[[1,2]]');
4744
- $mol_assert_equal($mol_key({ foo: new Uint8Array([1, 2]) }), '{"foo":[1,2]}');
4757
+ $mol_assert_equal($mol_key(new Uint8Array([1, 2])), 'Uint8Array([1,2])');
4758
+ $mol_assert_equal($mol_key([new Uint8Array([1, 2])]), '[Uint8Array([1,2])]');
4759
+ $mol_assert_equal($mol_key({ foo: new Uint8Array([1, 2]) }), '{"foo":Uint8Array([1,2])}');
4745
4760
  },
4746
4761
  'Function'() {
4747
4762
  const func = () => { };
@@ -4761,6 +4776,12 @@ var $;
4761
4776
  $mol_assert_unique($mol_key(foo), $mol_key($mol_jsx("div", null, "bar")));
4762
4777
  },
4763
4778
  'Custom JSON representation'() {
4779
+ class User {
4780
+ toJSON() { return 'jin'; }
4781
+ }
4782
+ $mol_assert_unique([$mol_key(new User)], [$mol_key(new User)]);
4783
+ },
4784
+ 'Custom key handler'() {
4764
4785
  class User {
4765
4786
  name;
4766
4787
  age;
@@ -4768,15 +4789,15 @@ var $;
4768
4789
  this.name = name;
4769
4790
  this.age = age;
4770
4791
  }
4771
- toJSON() { return { name: this.name }; }
4792
+ [$mol_key_handle]() { return `User(${JSON.stringify(this.name)})`; }
4772
4793
  }
4773
- $mol_assert_equal($mol_key(new User('jin', 18)), '{"name":"jin"}');
4794
+ $mol_assert_equal($mol_key([new User('jin', 16)]), $mol_key([new User('jin', 18)]), '[User("jin")]');
4774
4795
  },
4775
4796
  'Special native classes'() {
4776
- $mol_assert_equal($mol_key(new Date('xyz')), 'null');
4777
- $mol_assert_equal($mol_key(new Date('2001-01-02T03:04:05.678Z')), '"2001-01-02T03:04:05.678Z"');
4778
- $mol_assert_equal($mol_key(/./), '"/./"');
4779
- $mol_assert_equal($mol_key(/\./gimsu), '"/\\\\./gimsu"');
4797
+ $mol_assert_equal($mol_key(new Date('xyz')), 'Date(NaN)');
4798
+ $mol_assert_equal($mol_key(new Date(12345)), 'Date(12345)');
4799
+ $mol_assert_equal($mol_key(/./), '/./');
4800
+ $mol_assert_equal($mol_key(/\./gimsu), '/\\./gimsu');
4780
4801
  },
4781
4802
  });
4782
4803
  })($ || ($ = {}));