mol_key 0.0.615 → 0.0.617

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.test.js CHANGED
@@ -1699,6 +1699,283 @@ var $;
1699
1699
  ;
1700
1700
  "use strict";
1701
1701
  var $;
1702
+ (function ($) {
1703
+ function $mol_range2(item = index => index, size = () => Number.POSITIVE_INFINITY) {
1704
+ return new Proxy(new $mol_range2_array(), {
1705
+ get(target, field) {
1706
+ if (typeof field === 'string') {
1707
+ if (field === 'length')
1708
+ return size();
1709
+ const index = Number(field);
1710
+ if (index < 0)
1711
+ return undefined;
1712
+ if (index >= size())
1713
+ return undefined;
1714
+ if (index === Math.trunc(index))
1715
+ return item(index);
1716
+ }
1717
+ return target[field];
1718
+ },
1719
+ set(target, field) {
1720
+ return $mol_fail(new TypeError(`Lazy range is read only (trying to set field ${JSON.stringify(field)})`));
1721
+ },
1722
+ ownKeys(target) {
1723
+ return [...Array(size())].map((v, i) => String(i)).concat('length');
1724
+ },
1725
+ getOwnPropertyDescriptor(target, field) {
1726
+ if (field === "length")
1727
+ return {
1728
+ value: size(),
1729
+ writable: true,
1730
+ enumerable: false,
1731
+ configurable: false,
1732
+ };
1733
+ const index = Number(field);
1734
+ if (index === Math.trunc(index))
1735
+ return {
1736
+ get: () => this.get(target, field, this),
1737
+ enumerable: true,
1738
+ configurable: true,
1739
+ };
1740
+ return Object.getOwnPropertyDescriptor(target, field);
1741
+ }
1742
+ });
1743
+ }
1744
+ $.$mol_range2 = $mol_range2;
1745
+ class $mol_range2_array extends Array {
1746
+ concat(...tail) {
1747
+ if (tail.length === 0)
1748
+ return this;
1749
+ if (tail.length > 1) {
1750
+ let list = this;
1751
+ for (let item of tail)
1752
+ list = list.concat(item);
1753
+ return list;
1754
+ }
1755
+ return $mol_range2(index => index < this.length ? this[index] : tail[0][index - this.length], () => this.length + tail[0].length);
1756
+ }
1757
+ filter(check, context) {
1758
+ const filtered = new $mol_range2_array();
1759
+ for (let index = 0; index < this.length; ++index) {
1760
+ const item = this[index];
1761
+ if (check.call(context, item, index, this))
1762
+ filtered.push(item);
1763
+ }
1764
+ return filtered;
1765
+ }
1766
+ forEach(proceed, context) {
1767
+ for (let [key, value] of this.entries())
1768
+ proceed.call(context, value, key, this);
1769
+ }
1770
+ map(proceed, context) {
1771
+ return $mol_range2(index => proceed.call(context, this[index], index, this), () => this.length);
1772
+ }
1773
+ reduce(merge, result) {
1774
+ let index = 0;
1775
+ if (arguments.length === 1) {
1776
+ result = this[index++];
1777
+ }
1778
+ for (; index < this.length; ++index) {
1779
+ result = merge(result, this[index], index, this);
1780
+ }
1781
+ return result;
1782
+ }
1783
+ toReversed() {
1784
+ return $mol_range2(index => this[this.length - 1 - index], () => this.length);
1785
+ }
1786
+ slice(from = 0, to = this.length) {
1787
+ return $mol_range2(index => this[from + index], () => Math.min(to, this.length) - from);
1788
+ }
1789
+ some(check, context) {
1790
+ for (let index = 0; index < this.length; ++index) {
1791
+ if (check.call(context, this[index], index, this))
1792
+ return true;
1793
+ }
1794
+ return false;
1795
+ }
1796
+ every(check, context) {
1797
+ for (let index = 0; index < this.length; ++index) {
1798
+ if (!check.call(context, this[index], index, this))
1799
+ return false;
1800
+ }
1801
+ return true;
1802
+ }
1803
+ reverse() {
1804
+ return $mol_fail(new TypeError(`Mutable reverse is forbidden. Use toReversed instead.`));
1805
+ }
1806
+ sort() {
1807
+ return $mol_fail(new TypeError(`Mutable sort is forbidden. Use toSorted instead.`));
1808
+ }
1809
+ [Symbol.toPrimitive]() {
1810
+ return $mol_guid();
1811
+ }
1812
+ }
1813
+ $.$mol_range2_array = $mol_range2_array;
1814
+ })($ || ($ = {}));
1815
+ //mol/range2/range2.ts
1816
+ ;
1817
+ "use strict";
1818
+ var $;
1819
+ (function ($) {
1820
+ $mol_test({
1821
+ 'lazy calls'() {
1822
+ let calls = 0;
1823
+ const list = $mol_range2(index => (++calls, index), () => 10);
1824
+ $mol_assert_ok(list instanceof Array);
1825
+ $mol_assert_equal(list.length, 10);
1826
+ $mol_assert_equal(list[-1], undefined);
1827
+ $mol_assert_equal(list[0], 0);
1828
+ $mol_assert_equal(list[9], 9);
1829
+ $mol_assert_equal(list[9.5], undefined);
1830
+ $mol_assert_equal(list[10], undefined);
1831
+ $mol_assert_equal(calls, 2);
1832
+ },
1833
+ 'infinity list'() {
1834
+ let calls = 0;
1835
+ const list = $mol_range2(index => (++calls, index));
1836
+ $mol_assert_equal(list.length, Number.POSITIVE_INFINITY);
1837
+ $mol_assert_equal(list[0], 0);
1838
+ $mol_assert_equal(list[4], 4);
1839
+ $mol_assert_equal(list[Number.MAX_SAFE_INTEGER], Number.MAX_SAFE_INTEGER);
1840
+ $mol_assert_equal(list[Number.POSITIVE_INFINITY], undefined);
1841
+ $mol_assert_equal(calls, 3);
1842
+ },
1843
+ 'stringify'() {
1844
+ const list = $mol_range2(i => i, () => 5);
1845
+ $mol_assert_equal(list.toString(), '0,1,2,3,4');
1846
+ $mol_assert_equal(list.join(';'), '0;1;2;3;4');
1847
+ },
1848
+ 'for-of'() {
1849
+ let log = '';
1850
+ for (let i of $mol_range2(i => i + 1, () => 5)) {
1851
+ log += i;
1852
+ }
1853
+ $mol_assert_equal(log, '12345');
1854
+ },
1855
+ 'for-in'() {
1856
+ let log = '';
1857
+ for (let i in $mol_range2(i => i, () => 5)) {
1858
+ log += i;
1859
+ }
1860
+ $mol_assert_equal(log, '01234');
1861
+ },
1862
+ 'forEach'() {
1863
+ let log = '';
1864
+ $mol_range2(i => i, () => 5).forEach(i => log += i);
1865
+ $mol_assert_equal(log, '01234');
1866
+ },
1867
+ 'lazy concat'() {
1868
+ let calls1 = 0;
1869
+ let calls2 = 0;
1870
+ const list = $mol_range2(index => (++calls1, index), () => 5).concat([0, 1, 2, 3, 4], $mol_range2(index => (++calls2, index), () => 5));
1871
+ $mol_assert_ok(list instanceof Array);
1872
+ $mol_assert_equal(list.length, 15);
1873
+ $mol_assert_equal(list[0], 0);
1874
+ $mol_assert_equal(list[4], 4);
1875
+ $mol_assert_equal(list[5], 0);
1876
+ $mol_assert_equal(list[9], 4);
1877
+ $mol_assert_equal(list[10], 0);
1878
+ $mol_assert_equal(list[14], 4);
1879
+ $mol_assert_equal(list[15], undefined);
1880
+ $mol_assert_equal(calls1, 2);
1881
+ $mol_assert_equal(calls2, 2);
1882
+ },
1883
+ 'filter'() {
1884
+ let calls = 0;
1885
+ const list = $mol_range2(index => (++calls, index), () => 10).filter(v => v % 2).slice(0, 3);
1886
+ $mol_assert_ok(list instanceof Array);
1887
+ $mol_assert_equal(list.length, 3);
1888
+ $mol_assert_equal(list[0], 1);
1889
+ $mol_assert_equal(list[2], 5);
1890
+ $mol_assert_equal(list[3], undefined);
1891
+ $mol_assert_equal(calls, 10);
1892
+ },
1893
+ 'reverse'() {
1894
+ let calls = 0;
1895
+ const list = $mol_range2(index => (++calls, index), () => 10).toReversed().slice(0, 3);
1896
+ $mol_assert_ok(list instanceof Array);
1897
+ $mol_assert_equal(list.length, 3);
1898
+ $mol_assert_equal(list[0], 9);
1899
+ $mol_assert_equal(list[2], 7);
1900
+ $mol_assert_equal(list[3], undefined);
1901
+ $mol_assert_equal(calls, 2);
1902
+ },
1903
+ 'reduce'() {
1904
+ let calls = 0;
1905
+ const list = $mol_range2().slice(1, 6);
1906
+ $mol_assert_equal(list.reduce((s, v) => s + v), 15);
1907
+ $mol_assert_equal(list.reduce((s, v) => s + v, 5), 20);
1908
+ },
1909
+ 'lazy map'() {
1910
+ let calls1 = 0;
1911
+ let calls2 = 0;
1912
+ const source = $mol_range2(index => (++calls1, index), () => 5);
1913
+ const target = source.map((item, index, self) => {
1914
+ ++calls2;
1915
+ $mol_assert_equal(source, self);
1916
+ return index + 10;
1917
+ }, () => 5);
1918
+ $mol_assert_ok(target instanceof Array);
1919
+ $mol_assert_equal(target.length, 5);
1920
+ $mol_assert_equal(target[0], 10);
1921
+ $mol_assert_equal(target[4], 14);
1922
+ $mol_assert_equal(target[5], undefined);
1923
+ $mol_assert_equal(calls1, 2);
1924
+ $mol_assert_equal(calls2, 2);
1925
+ },
1926
+ 'lazy slice'() {
1927
+ let calls = 0;
1928
+ const list = $mol_range2(index => (++calls, index), () => 10).slice(3, 7);
1929
+ $mol_assert_ok(list instanceof Array);
1930
+ $mol_assert_equal(list.length, 4);
1931
+ $mol_assert_equal(list[0], 3);
1932
+ $mol_assert_equal(list[3], 6);
1933
+ $mol_assert_equal(list[4], undefined);
1934
+ $mol_assert_equal(calls, 2);
1935
+ },
1936
+ 'lazy some'() {
1937
+ let calls = 0;
1938
+ $mol_assert_ok($mol_range2(index => (++calls, index), () => 5).some(v => v >= 2));
1939
+ $mol_assert_equal(calls, 3);
1940
+ $mol_assert_not($mol_range2(i => i, () => 0).some(v => true));
1941
+ $mol_assert_ok($mol_range2(i => i).some(v => v > 5));
1942
+ },
1943
+ 'lazy every'() {
1944
+ let calls = 0;
1945
+ $mol_assert_not($mol_range2(index => (++calls, index), () => 5).every(v => v < 2));
1946
+ $mol_assert_equal(calls, 3);
1947
+ $mol_assert_ok($mol_range2(i => i, () => 0).every(v => false));
1948
+ $mol_assert_not($mol_range2(i => i).every(v => v < 5));
1949
+ },
1950
+ 'lazyfy'() {
1951
+ let calls = 0;
1952
+ const list = new $mol_range2_array(...[0, 1, 2, 3, 4, 5]).map(i => (++calls, i + 10)).slice(2);
1953
+ $mol_assert_ok(list instanceof Array);
1954
+ $mol_assert_equal(list.length, 4);
1955
+ $mol_assert_equal(calls, 0);
1956
+ $mol_assert_equal(list[0], 12);
1957
+ $mol_assert_equal(list[3], 15);
1958
+ $mol_assert_equal(list[4], undefined);
1959
+ $mol_assert_equal(calls, 2);
1960
+ },
1961
+ 'prevent modification'() {
1962
+ const list = $mol_range2(i => i, () => 5);
1963
+ $mol_assert_fail(() => list.push(4), TypeError);
1964
+ $mol_assert_fail(() => list.pop(), TypeError);
1965
+ $mol_assert_fail(() => list.unshift(4), TypeError);
1966
+ $mol_assert_fail(() => list.shift(), TypeError);
1967
+ $mol_assert_fail(() => list.splice(1, 2), TypeError);
1968
+ $mol_assert_fail(() => list[1] = 2, TypeError);
1969
+ $mol_assert_fail(() => list.reverse(), TypeError);
1970
+ $mol_assert_fail(() => list.sort(), TypeError);
1971
+ $mol_assert_equal(list.toString(), '0,1,2,3,4');
1972
+ }
1973
+ });
1974
+ })($ || ($ = {}));
1975
+ //mol/range2/range2.test.ts
1976
+ ;
1977
+ "use strict";
1978
+ var $;
1702
1979
  (function ($) {
1703
1980
  $.$mol_compare_deep_cache = new WeakMap();
1704
1981
  function $mol_compare_deep(left, right) {
@@ -1744,6 +2021,8 @@ var $;
1744
2021
  result = compare_pojo(left, right);
1745
2022
  else if (!Reflect.getPrototypeOf(left_proto))
1746
2023
  result = compare_pojo(left, right);
2024
+ else if (Symbol.toPrimitive in left)
2025
+ result = compare_primitive(left, right);
1747
2026
  else if (Array.isArray(left))
1748
2027
  result = compare_array(left, right);
1749
2028
  else if (left instanceof Set)
@@ -1754,8 +2033,6 @@ var $;
1754
2033
  result = compare_buffer(left, right);
1755
2034
  else if (Symbol.iterator in left)
1756
2035
  result = compare_iterator(left[Symbol.iterator](), right[Symbol.iterator]());
1757
- else if (Symbol.toPrimitive in left)
1758
- result = compare_primitive(left, right);
1759
2036
  else
1760
2037
  result = false;
1761
2038
  }
@@ -1865,6 +2142,8 @@ var $;
1865
2142
  $mol_assert_ok($mol_compare_deep([1, [2]], [1, [2]]));
1866
2143
  $mol_assert_not($mol_compare_deep([1, 2], [1, 3]));
1867
2144
  $mol_assert_not($mol_compare_deep([1, 2,], [1, 3, undefined]));
2145
+ $mol_assert_not($mol_compare_deep($mol_range2().slice(0, 0), new Array()));
2146
+ $mol_assert_not($mol_compare_deep($mol_range2(), $mol_range2()));
1868
2147
  },
1869
2148
  'Non POJO are different'() {
1870
2149
  class Thing extends Object {