util-helpers 4.23.0 → 4.23.1

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.
Files changed (49) hide show
  1. package/dist/util-helpers.js +457 -503
  2. package/dist/util-helpers.js.map +1 -1
  3. package/dist/util-helpers.min.js +1 -1
  4. package/dist/util-helpers.min.js.map +1 -1
  5. package/esm/VERSION.js +1 -1
  6. package/esm/getImageInfo.js +34 -40
  7. package/esm/index.js +1 -1
  8. package/esm/loadImage.js +32 -34
  9. package/esm/loadImageWithBlob.js +23 -24
  10. package/lib/VERSION.js +1 -1
  11. package/lib/getImageInfo.js +33 -39
  12. package/lib/index.js +1 -1
  13. package/lib/loadImage.js +31 -33
  14. package/lib/loadImageWithBlob.js +23 -24
  15. package/package.json +11 -11
  16. package/types/AsyncMemo.d.ts +2 -2
  17. package/types/ajax.d.ts +2 -2
  18. package/types/blobToDataURL.d.ts +1 -1
  19. package/types/calculateCursorPosition.d.ts +2 -2
  20. package/types/compressImage.d.ts +1 -1
  21. package/types/dataURLToBlob.d.ts +1 -1
  22. package/types/download.d.ts +3 -3
  23. package/types/fileReader.d.ts +1 -1
  24. package/types/gcd.d.ts +1 -1
  25. package/types/getImageInfo.d.ts +11 -10
  26. package/types/index.d.ts +2 -2
  27. package/types/isBankCard.d.ts +1 -1
  28. package/types/isBusinessLicense.d.ts +1 -1
  29. package/types/isChinese.d.ts +3 -3
  30. package/types/isHMCard.d.ts +1 -1
  31. package/types/isIdCard.d.ts +2 -2
  32. package/types/isPassport.d.ts +1 -1
  33. package/types/isPassword.d.ts +1 -1
  34. package/types/isSocialCreditCode.d.ts +1 -1
  35. package/types/isSwiftCode.d.ts +1 -1
  36. package/types/isTWCard.d.ts +1 -1
  37. package/types/isUrl.d.ts +1 -1
  38. package/types/isVehicle.d.ts +1 -1
  39. package/types/lcm.d.ts +1 -1
  40. package/types/loadImage.d.ts +2 -0
  41. package/types/loadImageWithBlob.d.ts +6 -4
  42. package/types/normalizeString.d.ts +1 -1
  43. package/types/numberToChinese.d.ts +1 -1
  44. package/types/parseIdCard.d.ts +1 -1
  45. package/types/safeDate.d.ts +1 -1
  46. package/types/setDataURLPrefix.d.ts +2 -2
  47. package/types/validatePassword.d.ts +1 -1
  48. package/esm/utils/Cache.js +0 -47
  49. package/lib/utils/Cache.js +0 -49
@@ -200,6 +200,11 @@
200
200
  return isNil(value) ? '' : baseToString(value);
201
201
  }
202
202
 
203
+ var defaultTo = function (value, defaultValue) {
204
+ return value == null || value !== value ? defaultValue : value;
205
+ };
206
+ var defaultTo$1 = defaultTo;
207
+
203
208
  var blobExisted = typeof Blob !== stringUndefined;
204
209
  function isBlob(value) {
205
210
  if (blobExisted && value instanceof Blob) {
@@ -1637,81 +1642,406 @@
1637
1642
  return EmitterPro;
1638
1643
  }());
1639
1644
 
1640
- var Cache$1 = (function (_super) {
1645
+ // 随机字符串
1646
+ function randomString$1() {
1647
+ return Math.random().toString(16).substring(2, 8);
1648
+ }
1649
+ // 内部自增id
1650
+ var uid = 1;
1651
+ // 返回唯一标识
1652
+ function getUniqueId() {
1653
+ return "".concat(randomString$1(), "_").concat(uid++);
1654
+ }
1655
+ // 是否支持 storage
1656
+ function isStorageSupported(storage) {
1657
+ try {
1658
+ var isSupport = typeof storage === 'object' &&
1659
+ storage !== null &&
1660
+ !!storage.setItem &&
1661
+ !!storage.getItem &&
1662
+ !!storage.removeItem;
1663
+ if (isSupport) {
1664
+ var key = getUniqueId();
1665
+ var value = '1';
1666
+ storage.setItem(key, value);
1667
+ if (storage.getItem(key) !== value) {
1668
+ return false;
1669
+ }
1670
+ storage.removeItem(key);
1671
+ }
1672
+ return isSupport;
1673
+ }
1674
+ catch (e) {
1675
+ console.error("[cache2] ".concat(storage, " is not supported. The default memory cache will be used."));
1676
+ return false;
1677
+ }
1678
+ }
1679
+ function parse(value, reviver) {
1680
+ try {
1681
+ return JSON.parse(value, reviver);
1682
+ }
1683
+ catch (e) {
1684
+ return value;
1685
+ }
1686
+ }
1687
+ function stringify(value, replacer) {
1688
+ return JSON.stringify(value, replacer);
1689
+ }
1690
+ var inWindow = typeof window !== 'undefined' && typeof window === 'object' && window.window === window;
1691
+
1692
+ var cache = {};
1693
+ var MemoryStorage = /** @class */ (function () {
1694
+ function MemoryStorage(scope) {
1695
+ if (scope === void 0) { scope = 'default'; }
1696
+ this.scope = scope;
1697
+ if (!cache[this.scope]) {
1698
+ cache[this.scope] = {};
1699
+ }
1700
+ this.data = cache[this.scope];
1701
+ }
1702
+ MemoryStorage.prototype.getItem = function (key) {
1703
+ return key in this.data ? this.data[key] : null;
1704
+ };
1705
+ MemoryStorage.prototype.setItem = function (key, value) {
1706
+ this.data[key] = value;
1707
+ };
1708
+ MemoryStorage.prototype.removeItem = function (key) {
1709
+ delete this.data[key];
1710
+ };
1711
+ MemoryStorage.prototype.clear = function () {
1712
+ cache[this.scope] = {};
1713
+ this.data = cache[this.scope];
1714
+ };
1715
+ return MemoryStorage;
1716
+ }());
1717
+
1718
+ var Storage = /** @class */ (function () {
1719
+ function Storage(storage, options) {
1720
+ if (options === void 0) { options = {}; }
1721
+ var isSupported = storage ? isStorageSupported(storage) : false;
1722
+ this.options = __assign({ needParsed: isSupported }, options);
1723
+ this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
1724
+ }
1725
+ Storage.prototype.get = function (key) {
1726
+ var data = this.storage.getItem(key);
1727
+ return this.options.needParsed ? parse(data, this.options.reviver) : data;
1728
+ };
1729
+ Storage.prototype.set = function (key, data) {
1730
+ this.storage.setItem(key, this.options.needParsed ? stringify(data, this.options.replacer) : data);
1731
+ };
1732
+ Storage.prototype.del = function (key) {
1733
+ this.storage.removeItem(key);
1734
+ };
1735
+ Storage.prototype.clear = function () {
1736
+ if (typeof this.storage.clear === 'function') {
1737
+ this.storage.clear();
1738
+ }
1739
+ };
1740
+ return Storage;
1741
+ }());
1742
+
1743
+ var defaultPrefix = 'cache2_'; // 命名空间缓存键前缀,默认 cache2_ 。
1744
+ var defaultNamespace = 'default';
1745
+ var Cache = /** @class */ (function (_super) {
1641
1746
  __extends(Cache, _super);
1642
- function Cache(options) {
1747
+ function Cache(namespace, options) {
1643
1748
  var _this = _super.call(this) || this;
1644
- _this.data = [];
1645
- _this.options = __assign({ max: 10 }, options);
1749
+ var ns = defaultNamespace, opts;
1750
+ if (typeof namespace === 'string') {
1751
+ ns = namespace;
1752
+ }
1753
+ else if (typeof namespace === 'object') {
1754
+ opts = namespace;
1755
+ }
1756
+ if (!opts && typeof options === 'object') {
1757
+ opts = options;
1758
+ }
1759
+ _this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: defaultPrefix }, opts);
1760
+ _this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, opts));
1761
+ _this.cacheKey = (_this.options.prefix || '') + (ns || '') || getUniqueId();
1762
+ _this.startCheckperiod();
1646
1763
  return _this;
1647
1764
  }
1648
- Cache.prototype.has = function (k) {
1649
- return !!this.data.find(function (item) { return item.k === k; });
1765
+ // 检查当前键值是否过期,如果过期将会自动删除
1766
+ Cache.prototype._check = function (key, data) {
1767
+ var ret = true;
1768
+ if (data.t !== 0 && data.t < Date.now()) {
1769
+ ret = false;
1770
+ this.del(key);
1771
+ this.emit('expired', key, data.v);
1772
+ }
1773
+ return ret;
1650
1774
  };
1651
- Cache.prototype.get = function (k) {
1652
- var _a;
1653
- return (_a = this.data.find(function (item) { return item.k === k; })) === null || _a === void 0 ? void 0 : _a.v;
1775
+ Cache.prototype._wrap = function (value, ttl) {
1776
+ var now = Date.now();
1777
+ var currentTtl = typeof ttl === 'number' ? ttl : this.options.stdTTL;
1778
+ var livetime = currentTtl > 0 ? now + currentTtl : 0;
1779
+ return {
1780
+ v: value,
1781
+ t: livetime,
1782
+ n: now
1783
+ };
1654
1784
  };
1655
- Cache.prototype.checkLimit = function () {
1656
- var _this = this;
1657
- if (this.options.max !== 0) {
1658
- var limit = this.data.length - this.options.max;
1659
- if (limit >= 0) {
1660
- var delArr = this.data.splice(0, limit + 1);
1661
- forEach$1(delArr, function (item) {
1662
- _this.emit('del', item.v, item.k);
1663
- });
1785
+ Cache.prototype._isLimited = function (len) {
1786
+ return this.options.max > -1 && len >= this.options.max;
1787
+ };
1788
+ Cache.prototype._getReplaceKey = function (keys, cacheValues) {
1789
+ var retkey = keys[0];
1790
+ keys.forEach(function (key) {
1791
+ if (cacheValues[key].t < cacheValues[retkey].t ||
1792
+ (cacheValues[key].t === cacheValues[retkey].t && cacheValues[key].n < cacheValues[retkey].n)) {
1793
+ retkey = key;
1664
1794
  }
1665
- }
1795
+ });
1796
+ return retkey;
1797
+ };
1798
+ Object.defineProperty(Cache.prototype, "cacheValues", {
1799
+ // 获取全部缓存数据,不处理过期数据和排序
1800
+ get: function () {
1801
+ return this.storage.get(this.cacheKey) || {};
1802
+ },
1803
+ enumerable: false,
1804
+ configurable: true
1805
+ });
1806
+ // 设置缓存数据
1807
+ Cache.prototype.setCacheValues = function (values) {
1808
+ this.storage.set(this.cacheKey, values);
1666
1809
  };
1667
- Cache.prototype.set = function (k, v) {
1668
- var newData = { k: k, v: v };
1669
- if (this.has(k)) {
1670
- var index = this.data.findIndex(function (item) { return item.k === k; });
1671
- this.data.splice(index, 1, newData);
1810
+ // 从缓存中获取保存的值。如果未找到或已过期,则返回 undefined 。如果找到该值,则返回该值。
1811
+ Cache.prototype.get = function (key) {
1812
+ var data = this.cacheValues[key];
1813
+ if (data && this._check(key, data)) {
1814
+ return data.v;
1672
1815
  }
1673
- else {
1674
- this.checkLimit();
1675
- this.data.push(newData);
1816
+ return;
1817
+ };
1818
+ // 从缓存中获取多个保存的值。如果未找到或已过期,则返回一个空对象。如果找到该值,它会返回一个具有键值对的对象。
1819
+ Cache.prototype.mget = function (keys) {
1820
+ var _this = this;
1821
+ var ret = {};
1822
+ if (!Array.isArray(keys)) {
1823
+ return ret;
1676
1824
  }
1825
+ var cacheValues = this.cacheValues;
1826
+ keys.forEach(function (key) {
1827
+ var data = cacheValues[key];
1828
+ if (data && _this._check(key, data)) {
1829
+ ret[key] = data.v;
1830
+ }
1831
+ });
1832
+ return ret;
1677
1833
  };
1678
- return Cache;
1679
- }(EmitterPro));
1680
-
1681
- var cache$3 = new Cache$1({ max: 1 });
1682
- cache$3.on('del', function (v) {
1683
- if (v.r) {
1684
- try {
1685
- revokeObjectURL(v.data.image.src);
1834
+ // 从缓存中获取全部保存的值。返回一个具有键值对的对象。
1835
+ Cache.prototype.getAll = function () {
1836
+ var keys = Object.keys(this.cacheValues);
1837
+ return this.mget(keys);
1838
+ };
1839
+ // 设置键值对。设置成功返回 true 。
1840
+ Cache.prototype.set = function (key, value, ttl) {
1841
+ if (this.options.max === 0) {
1842
+ return false;
1686
1843
  }
1687
- catch (_a) {
1844
+ var cacheValues = this.cacheValues;
1845
+ var keys = Object.keys(cacheValues);
1846
+ // 当前不存在该键值,并且数据量超过最大限制
1847
+ if (!cacheValues[key] && this._isLimited(keys.length)) {
1848
+ var validKeys = this.keys();
1849
+ if (this._isLimited(validKeys.length)) {
1850
+ // 如果最大限制策略是替换,将优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理。
1851
+ if (this.options.maxStrategy === 'replaced') {
1852
+ var replaceKey = this._getReplaceKey(validKeys, cacheValues);
1853
+ this.del(replaceKey);
1854
+ }
1855
+ else {
1856
+ // 如果是最大限制策略是不允许添加,返回 false 。
1857
+ return false;
1858
+ }
1859
+ }
1688
1860
  }
1689
- }
1690
- });
1691
- function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
1692
- if (cacheOptions === void 0) { cacheOptions = true; }
1693
- var _cacheOptions = {
1694
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1695
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
1861
+ cacheValues[key] = this._wrap(value, ttl);
1862
+ this.setCacheValues(cacheValues);
1863
+ this.emit('set', key, cacheValues[key].v);
1864
+ return true;
1696
1865
  };
1697
- return new Promise(function (resolve, reject) {
1698
- if (_cacheOptions.useCache && cache$3.has(img)) {
1699
- resolve(cache$3.get(img).data);
1866
+ // 设置多个键值对。全部设置成功返回 true
1867
+ Cache.prototype.mset = function (keyValueSet) {
1868
+ var _this = this;
1869
+ // 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
1870
+ var ret = true;
1871
+ keyValueSet.forEach(function (item) {
1872
+ var itemSetResult = _this.set(item.key, item.value, item.ttl);
1873
+ if (ret && !itemSetResult) {
1874
+ ret = false;
1875
+ }
1876
+ });
1877
+ return ret;
1878
+ };
1879
+ // 删除一个或多个键。返回已删除条目的数量。删除永远不会失败。
1880
+ Cache.prototype.del = function (key) {
1881
+ var _this = this;
1882
+ var cacheValues = this.cacheValues;
1883
+ var count = 0;
1884
+ var keys = Array.isArray(key) ? key : [key];
1885
+ keys.forEach(function (key) {
1886
+ if (cacheValues[key]) {
1887
+ count++;
1888
+ var oldData = cacheValues[key];
1889
+ delete cacheValues[key];
1890
+ _this.emit('del', key, oldData.v);
1891
+ }
1892
+ });
1893
+ if (count > 0) {
1894
+ this.setCacheValues(cacheValues);
1700
1895
  }
1701
- else {
1702
- getFileBlob(img, ajaxOptions)
1703
- .then(function (blob) {
1704
- var url = createObjectURL(blob);
1896
+ return count;
1897
+ };
1898
+ // 删除当前所有缓存。
1899
+ Cache.prototype.clear = function () {
1900
+ this.storage.del(this.cacheKey);
1901
+ };
1902
+ // 返回所有现有键的数组。
1903
+ Cache.prototype.keys = function () {
1904
+ var _this = this;
1905
+ var cacheValues = this.cacheValues;
1906
+ var keys = Object.keys(cacheValues);
1907
+ return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
1908
+ };
1909
+ // 当前缓存是否包含某个键。
1910
+ Cache.prototype.has = function (key) {
1911
+ var data = this.cacheValues[key];
1912
+ return !!(data && this._check(key, data));
1913
+ };
1914
+ // 获取缓存值并从缓存中删除键。
1915
+ Cache.prototype.take = function (key) {
1916
+ var ret;
1917
+ var data = this.cacheValues[key];
1918
+ if (data && this._check(key, data)) {
1919
+ ret = data.v;
1920
+ this.del(key);
1921
+ }
1922
+ return ret;
1923
+ };
1924
+ // 重新定义一个键的 ttl 。如果找到并更新成功,则返回 true 。
1925
+ Cache.prototype.ttl = function (key, ttl) {
1926
+ var cacheValues = this.cacheValues;
1927
+ var data = cacheValues[key];
1928
+ if (data && this._check(key, data)) {
1929
+ cacheValues[key] = this._wrap(data.v, ttl);
1930
+ return true;
1931
+ }
1932
+ return false;
1933
+ };
1934
+ // 获取某个键的 ttl 。
1935
+ // 如果未找到键或已过期,返回 undefined 。
1936
+ // 如果 ttl 为 0 ,返回 0 。
1937
+ // 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
1938
+ Cache.prototype.getTtl = function (key) {
1939
+ var cacheValues = this.cacheValues;
1940
+ var data = cacheValues[key];
1941
+ if (data && this._check(key, data)) {
1942
+ return cacheValues[key].t;
1943
+ }
1944
+ return;
1945
+ };
1946
+ // 获取某个键值的最后修改时间
1947
+ // 如果未找到键或已过期,返回 undefined 。
1948
+ // 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
1949
+ Cache.prototype.getLastModified = function (key) {
1950
+ var cacheValues = this.cacheValues;
1951
+ var data = cacheValues[key];
1952
+ if (data && this._check(key, data)) {
1953
+ return cacheValues[key].n;
1954
+ }
1955
+ return;
1956
+ };
1957
+ // 启动定时校验过期数据
1958
+ Cache.prototype.startCheckperiod = function () {
1959
+ var _this = this;
1960
+ // 触发全部缓存数据是否过期校验
1961
+ this.keys();
1962
+ if (this.options.checkperiod > 0) {
1963
+ clearTimeout(this._checkTimeout);
1964
+ this._checkTimeout = setTimeout(function () {
1965
+ _this.startCheckperiod();
1966
+ }, this.options.checkperiod);
1967
+ }
1968
+ };
1969
+ // 停止定时校验过期数据
1970
+ Cache.prototype.stopCheckperiod = function () {
1971
+ clearTimeout(this._checkTimeout);
1972
+ };
1973
+ return Cache;
1974
+ }(EmitterPro));
1975
+
1976
+ new Storage(inWindow ? window.localStorage : undefined);
1977
+
1978
+ new Storage(inWindow ? window.sessionStorage : undefined);
1979
+
1980
+ var AsyncMemo = (function () {
1981
+ function AsyncMemo(options) {
1982
+ this.promiseCache = {};
1983
+ this.cache = new Cache(uniqueId('uh_async_memo'), options);
1984
+ }
1985
+ AsyncMemo.prototype.run = function (asyncFn, key, options) {
1986
+ var _this = this;
1987
+ if (!key || !isString(key)) {
1988
+ return asyncFn();
1989
+ }
1990
+ var opts = __assign({ persisted: true }, options);
1991
+ if (opts.persisted) {
1992
+ var data = this.cache.get(key);
1993
+ if (data) {
1994
+ return Promise.resolve(data);
1995
+ }
1996
+ }
1997
+ if (!this.promiseCache[key]) {
1998
+ this.promiseCache[key] = asyncFn()
1999
+ .then(function (res) {
2000
+ delete _this.promiseCache[key];
2001
+ _this.cache.set(key, res, opts.ttl);
2002
+ return res;
2003
+ })
2004
+ .catch(function (err) {
2005
+ delete _this.promiseCache[key];
2006
+ return Promise.reject(err);
2007
+ });
2008
+ }
2009
+ return this.promiseCache[key];
2010
+ };
2011
+ return AsyncMemo;
2012
+ }());
2013
+
2014
+ var asyncMemo$2 = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
2015
+ asyncMemo$2.cache.on('del', function (k, v) {
2016
+ try {
2017
+ if (v.r) {
2018
+ revokeObjectURL(v.data.image.src);
2019
+ }
2020
+ }
2021
+ catch (_a) {
2022
+ }
2023
+ });
2024
+ function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
2025
+ if (cacheOptions === void 0) { cacheOptions = true; }
2026
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
2027
+ var _cacheOptions = {
2028
+ useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
2029
+ autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
2030
+ cacheKey: defaultTo$1(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
2031
+ };
2032
+ return asyncMemo$2
2033
+ .run(function () {
2034
+ return new Promise(function (resolve, reject) {
2035
+ getFileBlob(img, ajaxOptions)
2036
+ .then(function (blob) {
2037
+ var url = createObjectURL(blob);
1705
2038
  var image = new Image();
1706
2039
  image.onload = function () {
1707
- var result = { blob: blob, image: image };
1708
- if (_cacheOptions.useCache) {
1709
- cache$3.set(img, {
1710
- data: result,
1711
- r: _cacheOptions.autoRevokeOnDel
1712
- });
1713
- }
1714
- resolve(result);
2040
+ var data = { blob: blob, image: image };
2041
+ resolve({
2042
+ data: data,
2043
+ r: _cacheOptions.autoRevokeOnDel
2044
+ });
1715
2045
  };
1716
2046
  image.onerror = function (err) {
1717
2047
  revokeObjectURL(url);
@@ -1721,8 +2051,9 @@
1721
2051
  image.src = url;
1722
2052
  })
1723
2053
  .catch(reject);
1724
- }
1725
- });
2054
+ });
2055
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
2056
+ .then(function (res) { return res.data; });
1726
2057
  }
1727
2058
 
1728
2059
  function canvasToBlob(canvas, type, quality) {
@@ -1873,14 +2204,14 @@
1873
2204
  });
1874
2205
  }
1875
2206
 
1876
- var cache$2 = new Cache$1({ max: 1 });
1877
- cache$2.on('del', function (v) {
1878
- if (v.r) {
1879
- try {
2207
+ var asyncMemo$1 = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
2208
+ asyncMemo$1.cache.on('del', function (k, v) {
2209
+ try {
2210
+ if (v.r) {
1880
2211
  revokeObjectURL(v.data.image.src);
1881
2212
  }
1882
- catch (_a) {
1883
- }
2213
+ }
2214
+ catch (_a) {
1884
2215
  }
1885
2216
  });
1886
2217
  function calcContrast(w, h) {
@@ -1889,88 +2220,80 @@
1889
2220
  }
1890
2221
  function getImageInfo(img, cacheOptions, ajaxOptions) {
1891
2222
  if (cacheOptions === void 0) { cacheOptions = true; }
2223
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
1892
2224
  var _cacheOptions = {
1893
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1894
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
2225
+ useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
2226
+ autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
2227
+ cacheKey: defaultTo$1(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
1895
2228
  };
1896
- return new Promise(function (resolve, reject) {
1897
- if (_cacheOptions.useCache && cache$2.has(img)) {
1898
- resolve(cache$2.get(img).data);
1899
- }
1900
- else {
1901
- loadImageWithBlob(img, false, ajaxOptions)
1902
- .then(function (_a) {
1903
- var image = _a.image, blob = _a.blob;
1904
- var width = image.width, height = image.height;
1905
- var result = {
1906
- width: width,
1907
- height: height,
1908
- contrast: calcContrast(width, height),
1909
- measure: "".concat(width, " \u00D7 ").concat(height, " px"),
1910
- size: bytesToSize(blob.size),
1911
- bytes: blob.size,
1912
- image: image,
1913
- blob: blob
1914
- };
1915
- if (_cacheOptions.useCache) {
1916
- cache$2.set(img, {
1917
- data: result,
1918
- r: _cacheOptions.autoRevokeOnDel
1919
- });
1920
- }
1921
- resolve(result);
1922
- })
1923
- .catch(reject);
1924
- }
1925
- });
2229
+ return asyncMemo$1
2230
+ .run(function () {
2231
+ return loadImageWithBlob(img, false, ajaxOptions).then(function (_a) {
2232
+ var image = _a.image, blob = _a.blob;
2233
+ var width = image.width, height = image.height;
2234
+ var data = {
2235
+ width: width,
2236
+ height: height,
2237
+ contrast: calcContrast(width, height),
2238
+ measure: "".concat(width, " \u00D7 ").concat(height, " px"),
2239
+ size: bytesToSize(blob.size),
2240
+ bytes: blob.size,
2241
+ image: image,
2242
+ blob: blob
2243
+ };
2244
+ return {
2245
+ data: data,
2246
+ r: _cacheOptions.autoRevokeOnDel
2247
+ };
2248
+ });
2249
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
2250
+ .then(function (res) { return res.data; });
1926
2251
  }
1927
2252
 
1928
- var cache$1 = new Cache$1({ max: 1 });
1929
- cache$1.on('del', function (v) {
1930
- if (v.r) {
1931
- try {
2253
+ var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
2254
+ asyncMemo.cache.on('del', function (k, v) {
2255
+ try {
2256
+ if (v.r) {
1932
2257
  revokeObjectURL(v.data.src);
1933
2258
  }
1934
- catch (_a) {
1935
- }
2259
+ }
2260
+ catch (_a) {
1936
2261
  }
1937
2262
  });
1938
2263
  function loadImage(img, cacheOptions) {
1939
2264
  if (cacheOptions === void 0) { cacheOptions = true; }
2265
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
1940
2266
  var _cacheOptions = {
1941
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1942
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
2267
+ useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
2268
+ autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
2269
+ cacheKey: defaultTo$1(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
1943
2270
  };
1944
- return new Promise(function (resolve, reject) {
1945
- if (_cacheOptions.useCache && cache$1.has(img)) {
1946
- resolve(cache$1.get(img).data);
1947
- }
1948
- else {
1949
- var imgIsBlob_1 = isBlob(img);
1950
- var url_1 = imgIsBlob_1 ? createObjectURL(img) : img;
1951
- var image_1 = new Image();
1952
- if (!imgIsBlob_1) {
1953
- image_1.crossOrigin = 'anonymous';
2271
+ return asyncMemo
2272
+ .run(function () {
2273
+ return new Promise(function (resolve, reject) {
2274
+ var imgIsBlob = isBlob(img);
2275
+ var url = imgIsBlob ? createObjectURL(img) : img;
2276
+ var image = new Image();
2277
+ if (!imgIsBlob) {
2278
+ image.crossOrigin = 'anonymous';
1954
2279
  }
1955
- image_1.onload = function () {
1956
- if (_cacheOptions.useCache) {
1957
- cache$1.set(img, {
1958
- data: image_1,
1959
- r: _cacheOptions.autoRevokeOnDel
1960
- });
1961
- }
1962
- resolve(image_1);
2280
+ image.onload = function () {
2281
+ resolve({
2282
+ data: image,
2283
+ r: _cacheOptions.autoRevokeOnDel
2284
+ });
1963
2285
  };
1964
- image_1.onerror = function (err) {
1965
- if (imgIsBlob_1) {
1966
- revokeObjectURL(url_1);
2286
+ image.onerror = function (err) {
2287
+ if (imgIsBlob) {
2288
+ revokeObjectURL(url);
1967
2289
  }
1968
2290
  console.error("[loadImage] The image load failed, '".concat(img, "'."));
1969
2291
  reject(err);
1970
2292
  };
1971
- image_1.src = url_1;
1972
- }
1973
- });
2293
+ image.src = url;
2294
+ });
2295
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
2296
+ .then(function (res) { return res.data; });
1974
2297
  }
1975
2298
 
1976
2299
  function loadScript(src, options) {
@@ -2024,7 +2347,7 @@
2024
2347
  }
2025
2348
  return prefix;
2026
2349
  }
2027
- var randomString$1 = function (len, pool) {
2350
+ var randomString = function (len, pool) {
2028
2351
  if (len === void 0) { len = 0; }
2029
2352
  var _pool;
2030
2353
  if (typeof pool !== 'string') {
@@ -2322,378 +2645,9 @@
2322
2645
  return internalFindTreeSelect(tree, predicate, childrenField);
2323
2646
  }
2324
2647
 
2325
- var VERSION = "4.23.0";
2326
-
2327
- // 随机字符串
2328
- function randomString() {
2329
- return Math.random().toString(16).substring(2, 8);
2330
- }
2331
- // 内部自增id
2332
- var uid = 1;
2333
- // 返回唯一标识
2334
- function getUniqueId() {
2335
- return "".concat(randomString(), "_").concat(uid++);
2336
- }
2337
- // 是否支持 storage
2338
- function isStorageSupported(storage) {
2339
- try {
2340
- var isSupport = typeof storage === 'object' &&
2341
- storage !== null &&
2342
- !!storage.setItem &&
2343
- !!storage.getItem &&
2344
- !!storage.removeItem;
2345
- if (isSupport) {
2346
- var key = getUniqueId();
2347
- var value = '1';
2348
- storage.setItem(key, value);
2349
- if (storage.getItem(key) !== value) {
2350
- return false;
2351
- }
2352
- storage.removeItem(key);
2353
- }
2354
- return isSupport;
2355
- }
2356
- catch (e) {
2357
- console.error("[cache2] ".concat(storage, " is not supported. The default memory cache will be used."));
2358
- return false;
2359
- }
2360
- }
2361
- function parse(value, reviver) {
2362
- try {
2363
- return JSON.parse(value, reviver);
2364
- }
2365
- catch (e) {
2366
- return value;
2367
- }
2368
- }
2369
- function stringify(value, replacer) {
2370
- return JSON.stringify(value, replacer);
2371
- }
2372
- var inWindow = typeof window !== 'undefined' && typeof window === 'object' && window.window === window;
2373
-
2374
- var cache = {};
2375
- var MemoryStorage = /** @class */ (function () {
2376
- function MemoryStorage(scope) {
2377
- if (scope === void 0) { scope = 'default'; }
2378
- this.scope = scope;
2379
- if (!cache[this.scope]) {
2380
- cache[this.scope] = {};
2381
- }
2382
- this.data = cache[this.scope];
2383
- }
2384
- MemoryStorage.prototype.getItem = function (key) {
2385
- return key in this.data ? this.data[key] : null;
2386
- };
2387
- MemoryStorage.prototype.setItem = function (key, value) {
2388
- this.data[key] = value;
2389
- };
2390
- MemoryStorage.prototype.removeItem = function (key) {
2391
- delete this.data[key];
2392
- };
2393
- MemoryStorage.prototype.clear = function () {
2394
- cache[this.scope] = {};
2395
- this.data = cache[this.scope];
2396
- };
2397
- return MemoryStorage;
2398
- }());
2399
-
2400
- var Storage = /** @class */ (function () {
2401
- function Storage(storage, options) {
2402
- if (options === void 0) { options = {}; }
2403
- var isSupported = storage ? isStorageSupported(storage) : false;
2404
- this.options = __assign({ needParsed: isSupported }, options);
2405
- this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
2406
- }
2407
- Storage.prototype.get = function (key) {
2408
- var data = this.storage.getItem(key);
2409
- return this.options.needParsed ? parse(data, this.options.reviver) : data;
2410
- };
2411
- Storage.prototype.set = function (key, data) {
2412
- this.storage.setItem(key, this.options.needParsed ? stringify(data, this.options.replacer) : data);
2413
- };
2414
- Storage.prototype.del = function (key) {
2415
- this.storage.removeItem(key);
2416
- };
2417
- Storage.prototype.clear = function () {
2418
- if (typeof this.storage.clear === 'function') {
2419
- this.storage.clear();
2420
- }
2421
- };
2422
- return Storage;
2423
- }());
2424
-
2425
- var defaultPrefix = 'cache2_'; // 命名空间缓存键前缀,默认 cache2_ 。
2426
- var defaultNamespace = 'default';
2427
- var Cache = /** @class */ (function (_super) {
2428
- __extends(Cache, _super);
2429
- function Cache(namespace, options) {
2430
- var _this = _super.call(this) || this;
2431
- var ns = defaultNamespace, opts;
2432
- if (typeof namespace === 'string') {
2433
- ns = namespace;
2434
- }
2435
- else if (typeof namespace === 'object') {
2436
- opts = namespace;
2437
- }
2438
- if (!opts && typeof options === 'object') {
2439
- opts = options;
2440
- }
2441
- _this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: defaultPrefix }, opts);
2442
- _this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, opts));
2443
- _this.cacheKey = (_this.options.prefix || '') + (ns || '') || getUniqueId();
2444
- _this.startCheckperiod();
2445
- return _this;
2446
- }
2447
- // 检查当前键值是否过期,如果过期将会自动删除
2448
- Cache.prototype._check = function (key, data) {
2449
- var ret = true;
2450
- if (data.t !== 0 && data.t < Date.now()) {
2451
- ret = false;
2452
- this.del(key);
2453
- this.emit('expired', key, data.v);
2454
- }
2455
- return ret;
2456
- };
2457
- Cache.prototype._wrap = function (value, ttl) {
2458
- var now = Date.now();
2459
- var currentTtl = typeof ttl === 'number' ? ttl : this.options.stdTTL;
2460
- var livetime = currentTtl > 0 ? now + currentTtl : 0;
2461
- return {
2462
- v: value,
2463
- t: livetime,
2464
- n: now
2465
- };
2466
- };
2467
- Cache.prototype._isLimited = function (len) {
2468
- return this.options.max > -1 && len >= this.options.max;
2469
- };
2470
- Cache.prototype._getReplaceKey = function (keys, cacheValues) {
2471
- var retkey = keys[0];
2472
- keys.forEach(function (key) {
2473
- if (cacheValues[key].t < cacheValues[retkey].t ||
2474
- (cacheValues[key].t === cacheValues[retkey].t && cacheValues[key].n < cacheValues[retkey].n)) {
2475
- retkey = key;
2476
- }
2477
- });
2478
- return retkey;
2479
- };
2480
- Object.defineProperty(Cache.prototype, "cacheValues", {
2481
- // 获取全部缓存数据,不处理过期数据和排序
2482
- get: function () {
2483
- return this.storage.get(this.cacheKey) || {};
2484
- },
2485
- enumerable: false,
2486
- configurable: true
2487
- });
2488
- // 设置缓存数据
2489
- Cache.prototype.setCacheValues = function (values) {
2490
- this.storage.set(this.cacheKey, values);
2491
- };
2492
- // 从缓存中获取保存的值。如果未找到或已过期,则返回 undefined 。如果找到该值,则返回该值。
2493
- Cache.prototype.get = function (key) {
2494
- var data = this.cacheValues[key];
2495
- if (data && this._check(key, data)) {
2496
- return data.v;
2497
- }
2498
- return;
2499
- };
2500
- // 从缓存中获取多个保存的值。如果未找到或已过期,则返回一个空对象。如果找到该值,它会返回一个具有键值对的对象。
2501
- Cache.prototype.mget = function (keys) {
2502
- var _this = this;
2503
- var ret = {};
2504
- if (!Array.isArray(keys)) {
2505
- return ret;
2506
- }
2507
- var cacheValues = this.cacheValues;
2508
- keys.forEach(function (key) {
2509
- var data = cacheValues[key];
2510
- if (data && _this._check(key, data)) {
2511
- ret[key] = data.v;
2512
- }
2513
- });
2514
- return ret;
2515
- };
2516
- // 从缓存中获取全部保存的值。返回一个具有键值对的对象。
2517
- Cache.prototype.getAll = function () {
2518
- var keys = Object.keys(this.cacheValues);
2519
- return this.mget(keys);
2520
- };
2521
- // 设置键值对。设置成功返回 true 。
2522
- Cache.prototype.set = function (key, value, ttl) {
2523
- if (this.options.max === 0) {
2524
- return false;
2525
- }
2526
- var cacheValues = this.cacheValues;
2527
- var keys = Object.keys(cacheValues);
2528
- // 当前不存在该键值,并且数据量超过最大限制
2529
- if (!cacheValues[key] && this._isLimited(keys.length)) {
2530
- var validKeys = this.keys();
2531
- if (this._isLimited(validKeys.length)) {
2532
- // 如果最大限制策略是替换,将优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理。
2533
- if (this.options.maxStrategy === 'replaced') {
2534
- var replaceKey = this._getReplaceKey(validKeys, cacheValues);
2535
- this.del(replaceKey);
2536
- }
2537
- else {
2538
- // 如果是最大限制策略是不允许添加,返回 false 。
2539
- return false;
2540
- }
2541
- }
2542
- }
2543
- cacheValues[key] = this._wrap(value, ttl);
2544
- this.setCacheValues(cacheValues);
2545
- this.emit('set', key, cacheValues[key].v);
2546
- return true;
2547
- };
2548
- // 设置多个键值对。全部设置成功返回 true 。
2549
- Cache.prototype.mset = function (keyValueSet) {
2550
- var _this = this;
2551
- // 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
2552
- var ret = true;
2553
- keyValueSet.forEach(function (item) {
2554
- var itemSetResult = _this.set(item.key, item.value, item.ttl);
2555
- if (ret && !itemSetResult) {
2556
- ret = false;
2557
- }
2558
- });
2559
- return ret;
2560
- };
2561
- // 删除一个或多个键。返回已删除条目的数量。删除永远不会失败。
2562
- Cache.prototype.del = function (key) {
2563
- var _this = this;
2564
- var cacheValues = this.cacheValues;
2565
- var count = 0;
2566
- var keys = Array.isArray(key) ? key : [key];
2567
- keys.forEach(function (key) {
2568
- if (cacheValues[key]) {
2569
- count++;
2570
- var oldData = cacheValues[key];
2571
- delete cacheValues[key];
2572
- _this.emit('del', key, oldData.v);
2573
- }
2574
- });
2575
- if (count > 0) {
2576
- this.setCacheValues(cacheValues);
2577
- }
2578
- return count;
2579
- };
2580
- // 删除当前所有缓存。
2581
- Cache.prototype.clear = function () {
2582
- this.storage.del(this.cacheKey);
2583
- };
2584
- // 返回所有现有键的数组。
2585
- Cache.prototype.keys = function () {
2586
- var _this = this;
2587
- var cacheValues = this.cacheValues;
2588
- var keys = Object.keys(cacheValues);
2589
- return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
2590
- };
2591
- // 当前缓存是否包含某个键。
2592
- Cache.prototype.has = function (key) {
2593
- var data = this.cacheValues[key];
2594
- return !!(data && this._check(key, data));
2595
- };
2596
- // 获取缓存值并从缓存中删除键。
2597
- Cache.prototype.take = function (key) {
2598
- var ret;
2599
- var data = this.cacheValues[key];
2600
- if (data && this._check(key, data)) {
2601
- ret = data.v;
2602
- this.del(key);
2603
- }
2604
- return ret;
2605
- };
2606
- // 重新定义一个键的 ttl 。如果找到并更新成功,则返回 true 。
2607
- Cache.prototype.ttl = function (key, ttl) {
2608
- var cacheValues = this.cacheValues;
2609
- var data = cacheValues[key];
2610
- if (data && this._check(key, data)) {
2611
- cacheValues[key] = this._wrap(data.v, ttl);
2612
- return true;
2613
- }
2614
- return false;
2615
- };
2616
- // 获取某个键的 ttl 。
2617
- // 如果未找到键或已过期,返回 undefined 。
2618
- // 如果 ttl 为 0 ,返回 0 。
2619
- // 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
2620
- Cache.prototype.getTtl = function (key) {
2621
- var cacheValues = this.cacheValues;
2622
- var data = cacheValues[key];
2623
- if (data && this._check(key, data)) {
2624
- return cacheValues[key].t;
2625
- }
2626
- return;
2627
- };
2628
- // 获取某个键值的最后修改时间
2629
- // 如果未找到键或已过期,返回 undefined 。
2630
- // 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
2631
- Cache.prototype.getLastModified = function (key) {
2632
- var cacheValues = this.cacheValues;
2633
- var data = cacheValues[key];
2634
- if (data && this._check(key, data)) {
2635
- return cacheValues[key].n;
2636
- }
2637
- return;
2638
- };
2639
- // 启动定时校验过期数据
2640
- Cache.prototype.startCheckperiod = function () {
2641
- var _this = this;
2642
- // 触发全部缓存数据是否过期校验
2643
- this.keys();
2644
- if (this.options.checkperiod > 0) {
2645
- clearTimeout(this._checkTimeout);
2646
- this._checkTimeout = setTimeout(function () {
2647
- _this.startCheckperiod();
2648
- }, this.options.checkperiod);
2649
- }
2650
- };
2651
- // 停止定时校验过期数据
2652
- Cache.prototype.stopCheckperiod = function () {
2653
- clearTimeout(this._checkTimeout);
2654
- };
2655
- return Cache;
2656
- }(EmitterPro));
2657
-
2658
- new Storage(inWindow ? window.localStorage : undefined);
2659
-
2660
- new Storage(inWindow ? window.sessionStorage : undefined);
2661
-
2662
- var AsyncMemo = (function () {
2663
- function AsyncMemo(options) {
2664
- this.promiseCache = {};
2665
- this.cache = new Cache(uniqueId('uh_async_memo'), options);
2666
- }
2667
- AsyncMemo.prototype.run = function (asyncFn, key, options) {
2668
- var _this = this;
2669
- if (!key || !isString(key)) {
2670
- return asyncFn();
2671
- }
2672
- var opts = __assign({ persisted: true }, options);
2673
- if (opts.persisted) {
2674
- var data = this.cache.get(key);
2675
- if (data) {
2676
- return Promise.resolve(data);
2677
- }
2678
- }
2679
- if (!this.promiseCache[key]) {
2680
- this.promiseCache[key] = asyncFn()
2681
- .then(function (res) {
2682
- delete _this.promiseCache[key];
2683
- _this.cache.set(key, res, opts.ttl);
2684
- return res;
2685
- })
2686
- .catch(function (err) {
2687
- delete _this.promiseCache[key];
2688
- return Promise.reject(err);
2689
- });
2690
- }
2691
- return this.promiseCache[key];
2692
- };
2693
- return AsyncMemo;
2694
- }());
2648
+ var VERSION = "4.23.1";
2695
2649
 
2696
- var version = "4.23.0";
2650
+ var version = "4.23.1";
2697
2651
 
2698
2652
  exports.AsyncMemo = AsyncMemo;
2699
2653
  exports.VERSION = VERSION;
@@ -2749,7 +2703,7 @@
2749
2703
  exports.padZero = padZero;
2750
2704
  exports.parseIdCard = parseIdCard;
2751
2705
  exports.plus = plus;
2752
- exports.randomString = randomString$1;
2706
+ exports.randomString = randomString;
2753
2707
  exports.replaceChar = replaceChar;
2754
2708
  exports.round = _round;
2755
2709
  exports.safeDate = safeDate;