util-helpers 4.22.2 → 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 (58) hide show
  1. package/README.md +3 -0
  2. package/dist/util-helpers.js +594 -560
  3. package/dist/util-helpers.js.map +1 -1
  4. package/dist/util-helpers.min.js +1 -1
  5. package/dist/util-helpers.min.js.map +1 -1
  6. package/esm/VERSION.js +1 -1
  7. package/esm/getImageInfo.js +34 -40
  8. package/esm/index.js +2 -1
  9. package/esm/loadImage.js +32 -34
  10. package/esm/loadImageWithBlob.js +23 -24
  11. package/esm/randomString.js +26 -12
  12. package/esm/transformObjectValue.js +25 -0
  13. package/esm/validatePassword.js +4 -13
  14. package/lib/VERSION.js +1 -1
  15. package/lib/getImageInfo.js +33 -39
  16. package/lib/index.js +3 -1
  17. package/lib/loadImage.js +31 -33
  18. package/lib/loadImageWithBlob.js +23 -24
  19. package/lib/randomString.js +25 -11
  20. package/lib/transformObjectValue.js +27 -0
  21. package/lib/validatePassword.js +4 -13
  22. package/package.json +11 -11
  23. package/types/AsyncMemo.d.ts +16 -2
  24. package/types/ajax.d.ts +2 -2
  25. package/types/blobToDataURL.d.ts +1 -1
  26. package/types/calculateCursorPosition.d.ts +2 -2
  27. package/types/compressImage.d.ts +1 -1
  28. package/types/dataURLToBlob.d.ts +1 -1
  29. package/types/download.d.ts +3 -3
  30. package/types/fileReader.d.ts +1 -1
  31. package/types/gcd.d.ts +1 -1
  32. package/types/getImageInfo.d.ts +11 -10
  33. package/types/index.d.ts +3 -2
  34. package/types/isBankCard.d.ts +1 -1
  35. package/types/isBusinessLicense.d.ts +1 -1
  36. package/types/isChinese.d.ts +3 -3
  37. package/types/isHMCard.d.ts +1 -1
  38. package/types/isIdCard.d.ts +2 -2
  39. package/types/isPassport.d.ts +1 -1
  40. package/types/isPassword.d.ts +1 -1
  41. package/types/isSocialCreditCode.d.ts +1 -1
  42. package/types/isSwiftCode.d.ts +1 -1
  43. package/types/isTWCard.d.ts +1 -1
  44. package/types/isUrl.d.ts +1 -1
  45. package/types/isVehicle.d.ts +1 -1
  46. package/types/lcm.d.ts +1 -1
  47. package/types/loadImage.d.ts +2 -0
  48. package/types/loadImageWithBlob.d.ts +6 -4
  49. package/types/normalizeString.d.ts +1 -1
  50. package/types/numberToChinese.d.ts +1 -1
  51. package/types/parseIdCard.d.ts +1 -1
  52. package/types/randomString.d.ts +7 -3
  53. package/types/safeDate.d.ts +1 -1
  54. package/types/setDataURLPrefix.d.ts +2 -2
  55. package/types/transformObjectValue.d.ts +67 -0
  56. package/types/validatePassword.d.ts +1 -1
  57. package/esm/utils/Cache.js +0 -47
  58. package/lib/utils/Cache.js +0 -49
@@ -9,12 +9,20 @@
9
9
  var stringObject = 'object';
10
10
  var objectProto = Object.prototype;
11
11
  var objectProtoToString = objectProto.toString;
12
+ var objectProtoHasOwnProperty = objectProto.hasOwnProperty;
12
13
  var objectProtoPropertyIsEnumerable = objectProto.propertyIsEnumerable;
13
14
  var objectGetOwnPropertySymbols = Object.getOwnPropertySymbols;
15
+ var objectGetPrototypeOf = Object.getPrototypeOf;
14
16
  var objectKeys$1 = Object.keys;
17
+ var functionProto = Function.prototype;
18
+ var functionProtoToString = functionProto.toString;
15
19
  var symbolExisted = typeof Symbol !== stringUndefined;
16
20
  var symbolProto = symbolExisted ? Symbol.prototype : nativeUndefined$1;
17
21
  var mathMin = Math.min;
22
+ var mathMax = Math.max;
23
+ var mathRandom = Math.random;
24
+ var mathFloor = Math.floor;
25
+ var mathCeil = Math.ceil;
18
26
  var numberIsFinite = Number.isFinite;
19
27
  var globalThisExisted = typeof globalThis === stringObject && globalThis;
20
28
  var globalExisted = typeof global === stringObject && global;
@@ -26,6 +34,7 @@
26
34
  var symbolTag = '[object Symbol]';
27
35
  var functionTags = ['Function', 'AsyncFunction', 'GeneratorFunction', 'Proxy'].map(function (item) { return '[object ' + item + ']'; });
28
36
  var blobTag = '[object Blob]';
37
+ var objectTag = '[object Object]';
29
38
 
30
39
  function isArray(value) {
31
40
  return Array.isArray(value);
@@ -98,6 +107,38 @@
98
107
  return value != null && isLength(value.length) && !isFunction(value);
99
108
  }
100
109
 
110
+ var MAX_VALUE = 1.7976931348623157e308;
111
+ function toFinite(value) {
112
+ if (!value) {
113
+ return value === 0 ? value : 0;
114
+ }
115
+ value = toNumber(value);
116
+ if (value === Infinity || value === -Infinity) {
117
+ var sign = value < 0 ? -1 : 1;
118
+ return sign * MAX_VALUE;
119
+ }
120
+ return value === value ? value : 0;
121
+ }
122
+
123
+ function randomInt(lower, upper) {
124
+ if (lower === void 0) { lower = 0; }
125
+ if (upper === void 0) { upper = 1; }
126
+ lower = toFinite(lower);
127
+ upper = toFinite(upper);
128
+ var min = mathCeil(mathMin(lower, upper) || 0);
129
+ var max = mathFloor(mathMax(lower, upper) || 0);
130
+ if (min > max) {
131
+ var temp = min;
132
+ min = max;
133
+ max = temp;
134
+ }
135
+ return mathFloor(min + mathRandom() * (max - min + 1));
136
+ }
137
+
138
+ function isObjectLike(value) {
139
+ return value !== null && typeof value === 'object';
140
+ }
141
+
101
142
  function getSymbols(object) {
102
143
  if (!objectGetOwnPropertySymbols || object === null) {
103
144
  return [];
@@ -159,6 +200,11 @@
159
200
  return isNil(value) ? '' : baseToString(value);
160
201
  }
161
202
 
203
+ var defaultTo = function (value, defaultValue) {
204
+ return value == null || value !== value ? defaultValue : value;
205
+ };
206
+ var defaultTo$1 = defaultTo;
207
+
162
208
  var blobExisted = typeof Blob !== stringUndefined;
163
209
  function isBlob(value) {
164
210
  if (blobExisted && value instanceof Blob) {
@@ -167,6 +213,19 @@
167
213
  return getTag(value) === blobTag;
168
214
  }
169
215
 
216
+ var objectCtorString = functionProtoToString.call(Object);
217
+ function isPlainObject(value) {
218
+ if (!isObjectLike(value) || getTag(value) !== objectTag) {
219
+ return false;
220
+ }
221
+ var proto = objectGetPrototypeOf(Object(value));
222
+ if (proto === null) {
223
+ return true;
224
+ }
225
+ var Ctor = objectProtoHasOwnProperty.call(proto, 'constructor') && proto.constructor;
226
+ return typeof Ctor === 'function' && Ctor instanceof Ctor && functionProtoToString.call(Ctor) === objectCtorString;
227
+ }
228
+
170
229
  var freeGlobalThis = globalThisExisted && globalThis.Object === Object && globalThis;
171
230
  var freeGlobal = globalExisted && global.Object === Object && global;
172
231
  var freeSelf = selfExisted && self.Object === Object && self;
@@ -373,19 +432,10 @@
373
432
  }
374
433
  }
375
434
 
376
- var regNumber = /[\d]/;
435
+ var regNumber = /\d/;
377
436
  var regLowerCaseLetter = /[a-z]/;
378
437
  var regUpperCaseLetter = /[A-Z]/;
379
438
  var regAllNumberAndLetter = /[\d|a-z]/gi;
380
- function hasNumber(val) {
381
- return regNumber.test(val);
382
- }
383
- function hasLowerCaseLetter(val) {
384
- return regLowerCaseLetter.test(val);
385
- }
386
- function hasUpperCaseLetter(val) {
387
- return regUpperCaseLetter.test(val);
388
- }
389
439
  function hasHex(val) {
390
440
  return val.indexOf('\\x') > -1 || val.indexOf('\\u') > -1;
391
441
  }
@@ -443,9 +493,9 @@
443
493
  valStr = '';
444
494
  }
445
495
  var currentLevel = 0;
446
- var containesNumber = hasNumber(valStr);
447
- var containesLowerCaseLetter = hasLowerCaseLetter(valStr);
448
- var containesUpperCaseLetter = hasUpperCaseLetter(valStr);
496
+ var containesNumber = regNumber.test(valStr);
497
+ var containesLowerCaseLetter = regLowerCaseLetter.test(valStr);
498
+ var containesUpperCaseLetter = regUpperCaseLetter.test(valStr);
449
499
  var containesSpecialCharacter = hasSpecialCharacter(valStr, special);
450
500
  var containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
451
501
  if (containesNumber) {
@@ -1221,6 +1271,27 @@
1221
1271
  return str;
1222
1272
  }
1223
1273
 
1274
+ var transformObjectValue = function (data, fn, deep) {
1275
+ if (deep === void 0) { deep = true; }
1276
+ if (isPlainObject(data)) {
1277
+ var result_1 = {};
1278
+ forEach$1(data, function (value, key) {
1279
+ var newValue = deep && (isPlainObject(value) || isArray(value)) ? transformObjectValue(value, fn) : fn(value, key);
1280
+ result_1[key] = newValue;
1281
+ });
1282
+ return result_1;
1283
+ }
1284
+ else if (isArray(data)) {
1285
+ var result_2 = [];
1286
+ forEach$1(data, function (value, index) {
1287
+ var newValue = deep && (isPlainObject(value) || isArray(value)) ? transformObjectValue(value, fn) : fn(value, index);
1288
+ result_2.push(newValue);
1289
+ });
1290
+ return result_2;
1291
+ }
1292
+ return data;
1293
+ };
1294
+
1224
1295
  function times() {
1225
1296
  var nums = [];
1226
1297
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -1571,115 +1642,441 @@
1571
1642
  return EmitterPro;
1572
1643
  }());
1573
1644
 
1574
- var Cache$1 = (function (_super) {
1575
- __extends(Cache, _super);
1576
- function Cache(options) {
1577
- var _this = _super.call(this) || this;
1578
- _this.data = [];
1579
- _this.options = __assign({ max: 10 }, options);
1580
- return _this;
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];
1581
1701
  }
1582
- Cache.prototype.has = function (k) {
1583
- return !!this.data.find(function (item) { return item.k === k; });
1702
+ MemoryStorage.prototype.getItem = function (key) {
1703
+ return key in this.data ? this.data[key] : null;
1584
1704
  };
1585
- Cache.prototype.get = function (k) {
1586
- var _a;
1587
- return (_a = this.data.find(function (item) { return item.k === k; })) === null || _a === void 0 ? void 0 : _a.v;
1705
+ MemoryStorage.prototype.setItem = function (key, value) {
1706
+ this.data[key] = value;
1588
1707
  };
1589
- Cache.prototype.checkLimit = function () {
1590
- var _this = this;
1591
- if (this.options.max !== 0) {
1592
- var limit = this.data.length - this.options.max;
1593
- if (limit >= 0) {
1594
- var delArr = this.data.splice(0, limit + 1);
1595
- forEach$1(delArr, function (item) {
1596
- _this.emit('del', item.v, item.k);
1597
- });
1598
- }
1599
- }
1708
+ MemoryStorage.prototype.removeItem = function (key) {
1709
+ delete this.data[key];
1600
1710
  };
1601
- Cache.prototype.set = function (k, v) {
1602
- var newData = { k: k, v: v };
1603
- if (this.has(k)) {
1604
- var index = this.data.findIndex(function (item) { return item.k === k; });
1605
- this.data.splice(index, 1, newData);
1606
- }
1607
- else {
1608
- this.checkLimit();
1609
- this.data.push(newData);
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();
1610
1738
  }
1611
1739
  };
1612
- return Cache;
1613
- }(EmitterPro));
1740
+ return Storage;
1741
+ }());
1614
1742
 
1615
- var cache$3 = new Cache$1({ max: 1 });
1616
- cache$3.on('del', function (v) {
1617
- if (v.r) {
1618
- try {
1619
- revokeObjectURL(v.data.image.src);
1743
+ var defaultPrefix = 'cache2_'; // 命名空间缓存键前缀,默认 cache2_
1744
+ var defaultNamespace = 'default';
1745
+ var Cache = /** @class */ (function (_super) {
1746
+ __extends(Cache, _super);
1747
+ function Cache(namespace, options) {
1748
+ var _this = _super.call(this) || this;
1749
+ var ns = defaultNamespace, opts;
1750
+ if (typeof namespace === 'string') {
1751
+ ns = namespace;
1752
+ }
1753
+ else if (typeof namespace === 'object') {
1754
+ opts = namespace;
1620
1755
  }
1621
- catch (_a) {
1756
+ if (!opts && typeof options === 'object') {
1757
+ opts = options;
1622
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();
1763
+ return _this;
1623
1764
  }
1624
- });
1625
- function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
1626
- if (cacheOptions === void 0) { cacheOptions = true; }
1627
- var _cacheOptions = {
1628
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1629
- autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
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;
1630
1774
  };
1631
- return new Promise(function (resolve, reject) {
1632
- if (_cacheOptions.useCache && cache$3.has(img)) {
1633
- resolve(cache$3.get(img).data);
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
+ };
1784
+ };
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;
1794
+ }
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);
1809
+ };
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;
1634
1815
  }
1635
- else {
1636
- getFileBlob(img, ajaxOptions)
1637
- .then(function (blob) {
1638
- var url = createObjectURL(blob);
1639
- var image = new Image();
1640
- image.onload = function () {
1641
- var result = { blob: blob, image: image };
1642
- if (_cacheOptions.useCache) {
1643
- cache$3.set(img, {
1644
- data: result,
1645
- r: _cacheOptions.autoRevokeOnDel
1646
- });
1647
- }
1648
- resolve(result);
1649
- };
1650
- image.onerror = function (err) {
1651
- revokeObjectURL(url);
1652
- console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
1653
- reject(err);
1654
- };
1655
- image.src = url;
1656
- })
1657
- .catch(reject);
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;
1658
1824
  }
1659
- });
1660
- }
1661
-
1662
- function canvasToBlob(canvas, type, quality) {
1663
- return new Promise(function (resolve) {
1664
- canvas.toBlob(function (blob) {
1665
- resolve(blob);
1666
- }, type, quality);
1667
- });
1668
- }
1669
- var compressImage = function (img, options) {
1670
- if (options === void 0) { options = {}; }
1671
- return new Promise(function (resolve, reject) {
1672
- var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.cacheImage, cacheImage = _b === void 0 ? true : _b, _c = options.background, background = _c === void 0 ? '#fff' : _c, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _d = options.format, format = _d === void 0 ? 'blob' : _d, _e = options.type, type = _e === void 0 ? 'image/jpeg' : _e, _f = options.quality, quality = _f === void 0 ? 0.8 : _f, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw, ajaxOptions = options.ajaxOptions;
1673
- loadImageWithBlob(img, cacheImage, ajaxOptions)
1674
- .then(function (_a) {
1675
- var image = _a.image, blob = _a.blob;
1676
- var numWidth = toNumber(width);
1677
- var numHeight = toNumber(height);
1678
- var numQuality = toNumber(quality);
1679
- if (numWidth) {
1680
- image.width = numWidth;
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;
1681
1830
  }
1682
- if (numHeight) {
1831
+ });
1832
+ return ret;
1833
+ };
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;
1843
+ }
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
+ }
1860
+ }
1861
+ cacheValues[key] = this._wrap(value, ttl);
1862
+ this.setCacheValues(cacheValues);
1863
+ this.emit('set', key, cacheValues[key].v);
1864
+ return true;
1865
+ };
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);
1895
+ }
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);
2038
+ var image = new Image();
2039
+ image.onload = function () {
2040
+ var data = { blob: blob, image: image };
2041
+ resolve({
2042
+ data: data,
2043
+ r: _cacheOptions.autoRevokeOnDel
2044
+ });
2045
+ };
2046
+ image.onerror = function (err) {
2047
+ revokeObjectURL(url);
2048
+ console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
2049
+ reject(err);
2050
+ };
2051
+ image.src = url;
2052
+ })
2053
+ .catch(reject);
2054
+ });
2055
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
2056
+ .then(function (res) { return res.data; });
2057
+ }
2058
+
2059
+ function canvasToBlob(canvas, type, quality) {
2060
+ return new Promise(function (resolve) {
2061
+ canvas.toBlob(function (blob) {
2062
+ resolve(blob);
2063
+ }, type, quality);
2064
+ });
2065
+ }
2066
+ var compressImage = function (img, options) {
2067
+ if (options === void 0) { options = {}; }
2068
+ return new Promise(function (resolve, reject) {
2069
+ var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.cacheImage, cacheImage = _b === void 0 ? true : _b, _c = options.background, background = _c === void 0 ? '#fff' : _c, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _d = options.format, format = _d === void 0 ? 'blob' : _d, _e = options.type, type = _e === void 0 ? 'image/jpeg' : _e, _f = options.quality, quality = _f === void 0 ? 0.8 : _f, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw, ajaxOptions = options.ajaxOptions;
2070
+ loadImageWithBlob(img, cacheImage, ajaxOptions)
2071
+ .then(function (_a) {
2072
+ var image = _a.image, blob = _a.blob;
2073
+ var numWidth = toNumber(width);
2074
+ var numHeight = toNumber(height);
2075
+ var numQuality = toNumber(quality);
2076
+ if (numWidth) {
2077
+ image.width = numWidth;
2078
+ }
2079
+ if (numHeight) {
1683
2080
  image.height = numHeight;
1684
2081
  }
1685
2082
  beforeCompress === null || beforeCompress === void 0 ? void 0 : beforeCompress({ image: image, blob: blob }, options);
@@ -1807,14 +2204,14 @@
1807
2204
  });
1808
2205
  }
1809
2206
 
1810
- var cache$2 = new Cache$1({ max: 1 });
1811
- cache$2.on('del', function (v) {
1812
- if (v.r) {
1813
- 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) {
1814
2211
  revokeObjectURL(v.data.image.src);
1815
2212
  }
1816
- catch (_a) {
1817
- }
2213
+ }
2214
+ catch (_a) {
1818
2215
  }
1819
2216
  });
1820
2217
  function calcContrast(w, h) {
@@ -1823,88 +2220,80 @@
1823
2220
  }
1824
2221
  function getImageInfo(img, cacheOptions, ajaxOptions) {
1825
2222
  if (cacheOptions === void 0) { cacheOptions = true; }
2223
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
1826
2224
  var _cacheOptions = {
1827
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1828
- 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)
1829
2228
  };
1830
- return new Promise(function (resolve, reject) {
1831
- if (_cacheOptions.useCache && cache$2.has(img)) {
1832
- resolve(cache$2.get(img).data);
1833
- }
1834
- else {
1835
- loadImageWithBlob(img, false, ajaxOptions)
1836
- .then(function (_a) {
1837
- var image = _a.image, blob = _a.blob;
1838
- var width = image.width, height = image.height;
1839
- var result = {
1840
- width: width,
1841
- height: height,
1842
- contrast: calcContrast(width, height),
1843
- measure: "".concat(width, " \u00D7 ").concat(height, " px"),
1844
- size: bytesToSize(blob.size),
1845
- bytes: blob.size,
1846
- image: image,
1847
- blob: blob
1848
- };
1849
- if (_cacheOptions.useCache) {
1850
- cache$2.set(img, {
1851
- data: result,
1852
- r: _cacheOptions.autoRevokeOnDel
1853
- });
1854
- }
1855
- resolve(result);
1856
- })
1857
- .catch(reject);
1858
- }
1859
- });
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; });
1860
2251
  }
1861
2252
 
1862
- var cache$1 = new Cache$1({ max: 1 });
1863
- cache$1.on('del', function (v) {
1864
- if (v.r) {
1865
- try {
2253
+ var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
2254
+ asyncMemo.cache.on('del', function (k, v) {
2255
+ try {
2256
+ if (v.r) {
1866
2257
  revokeObjectURL(v.data.src);
1867
2258
  }
1868
- catch (_a) {
1869
- }
2259
+ }
2260
+ catch (_a) {
1870
2261
  }
1871
2262
  });
1872
2263
  function loadImage(img, cacheOptions) {
1873
2264
  if (cacheOptions === void 0) { cacheOptions = true; }
2265
+ var cacheOptionsIsObject = typeof cacheOptions === 'object';
1874
2266
  var _cacheOptions = {
1875
- useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1876
- 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)
1877
2270
  };
1878
- return new Promise(function (resolve, reject) {
1879
- if (_cacheOptions.useCache && cache$1.has(img)) {
1880
- resolve(cache$1.get(img).data);
1881
- }
1882
- else {
1883
- var imgIsBlob_1 = isBlob(img);
1884
- var url_1 = imgIsBlob_1 ? createObjectURL(img) : img;
1885
- var image_1 = new Image();
1886
- if (!imgIsBlob_1) {
1887
- 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';
1888
2279
  }
1889
- image_1.onload = function () {
1890
- if (_cacheOptions.useCache) {
1891
- cache$1.set(img, {
1892
- data: image_1,
1893
- r: _cacheOptions.autoRevokeOnDel
1894
- });
1895
- }
1896
- resolve(image_1);
2280
+ image.onload = function () {
2281
+ resolve({
2282
+ data: image,
2283
+ r: _cacheOptions.autoRevokeOnDel
2284
+ });
1897
2285
  };
1898
- image_1.onerror = function (err) {
1899
- if (imgIsBlob_1) {
1900
- revokeObjectURL(url_1);
2286
+ image.onerror = function (err) {
2287
+ if (imgIsBlob) {
2288
+ revokeObjectURL(url);
1901
2289
  }
1902
2290
  console.error("[loadImage] The image load failed, '".concat(img, "'."));
1903
2291
  reject(err);
1904
2292
  };
1905
- image_1.src = url_1;
1906
- }
1907
- });
2293
+ image.src = url;
2294
+ });
2295
+ }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
2296
+ .then(function (res) { return res.data; });
1908
2297
  }
1909
2298
 
1910
2299
  function loadScript(src, options) {
@@ -1943,22 +2332,35 @@
1943
2332
  });
1944
2333
  }
1945
2334
 
1946
- var numberChars = '0123456789';
1947
- var letterChars = 'abcdefghijklmnopqrstuvwxyz';
1948
- var defaultChars = numberChars + letterChars + letterChars.toUpperCase();
1949
- function internalRandomString(len, optionalChars, prefix) {
2335
+ var letter = 'abcdefghijklmnopqrstuvwxyz';
2336
+ var chars = {
2337
+ number: '0123456789',
2338
+ lower: letter,
2339
+ upper: letter.toUpperCase()
2340
+ };
2341
+ var allChars = chars.number + chars.lower + chars.upper;
2342
+ function internalRandomString(len, pool, prefix) {
1950
2343
  if (prefix === void 0) { prefix = ''; }
1951
2344
  while (len-- > 0) {
1952
- var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
1953
- return internalRandomString(len, optionalChars, prefix + r);
2345
+ var r = pool[randomInt(0, pool.length - 1)];
2346
+ return internalRandomString(len, pool, prefix + r);
1954
2347
  }
1955
2348
  return prefix;
1956
2349
  }
1957
- function randomString$1(len, optionalChars) {
2350
+ var randomString = function (len, pool) {
1958
2351
  if (len === void 0) { len = 0; }
1959
- var realChars = typeof optionalChars === 'string' && optionalChars ? optionalChars : defaultChars;
1960
- return internalRandomString(toNumber(len), realChars);
1961
- }
2352
+ var _pool;
2353
+ if (typeof pool !== 'string') {
2354
+ _pool = allChars;
2355
+ }
2356
+ else if (chars[pool]) {
2357
+ _pool = chars[pool];
2358
+ }
2359
+ else {
2360
+ _pool = pool;
2361
+ }
2362
+ return internalRandomString(toNumber(len), _pool);
2363
+ };
1962
2364
 
1963
2365
  function strlen(str) {
1964
2366
  var realStr = toString(str);
@@ -2243,378 +2645,9 @@
2243
2645
  return internalFindTreeSelect(tree, predicate, childrenField);
2244
2646
  }
2245
2647
 
2246
- var VERSION = "4.22.2";
2247
-
2248
- // 随机字符串
2249
- function randomString() {
2250
- return Math.random().toString(16).substring(2, 8);
2251
- }
2252
- // 内部自增id
2253
- var uid = 1;
2254
- // 返回唯一标识
2255
- function getUniqueId() {
2256
- return "".concat(randomString(), "_").concat(uid++);
2257
- }
2258
- // 是否支持 storage
2259
- function isStorageSupported(storage) {
2260
- try {
2261
- var isSupport = typeof storage === 'object' &&
2262
- storage !== null &&
2263
- !!storage.setItem &&
2264
- !!storage.getItem &&
2265
- !!storage.removeItem;
2266
- if (isSupport) {
2267
- var key = getUniqueId();
2268
- var value = '1';
2269
- storage.setItem(key, value);
2270
- if (storage.getItem(key) !== value) {
2271
- return false;
2272
- }
2273
- storage.removeItem(key);
2274
- }
2275
- return isSupport;
2276
- }
2277
- catch (e) {
2278
- console.error("[cache2] ".concat(storage, " is not supported. The default memory cache will be used."));
2279
- return false;
2280
- }
2281
- }
2282
- function parse(value, reviver) {
2283
- try {
2284
- return JSON.parse(value, reviver);
2285
- }
2286
- catch (e) {
2287
- return value;
2288
- }
2289
- }
2290
- function stringify(value, replacer) {
2291
- return JSON.stringify(value, replacer);
2292
- }
2293
- var inWindow = typeof window !== 'undefined' && typeof window === 'object' && window.window === window;
2294
-
2295
- var cache = {};
2296
- var MemoryStorage = /** @class */ (function () {
2297
- function MemoryStorage(scope) {
2298
- if (scope === void 0) { scope = 'default'; }
2299
- this.scope = scope;
2300
- if (!cache[this.scope]) {
2301
- cache[this.scope] = {};
2302
- }
2303
- this.data = cache[this.scope];
2304
- }
2305
- MemoryStorage.prototype.getItem = function (key) {
2306
- return key in this.data ? this.data[key] : null;
2307
- };
2308
- MemoryStorage.prototype.setItem = function (key, value) {
2309
- this.data[key] = value;
2310
- };
2311
- MemoryStorage.prototype.removeItem = function (key) {
2312
- delete this.data[key];
2313
- };
2314
- MemoryStorage.prototype.clear = function () {
2315
- cache[this.scope] = {};
2316
- this.data = cache[this.scope];
2317
- };
2318
- return MemoryStorage;
2319
- }());
2320
-
2321
- var Storage = /** @class */ (function () {
2322
- function Storage(storage, options) {
2323
- if (options === void 0) { options = {}; }
2324
- var isSupported = storage ? isStorageSupported(storage) : false;
2325
- this.options = __assign({ needParsed: isSupported }, options);
2326
- this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
2327
- }
2328
- Storage.prototype.get = function (key) {
2329
- var data = this.storage.getItem(key);
2330
- return this.options.needParsed ? parse(data, this.options.reviver) : data;
2331
- };
2332
- Storage.prototype.set = function (key, data) {
2333
- this.storage.setItem(key, this.options.needParsed ? stringify(data, this.options.replacer) : data);
2334
- };
2335
- Storage.prototype.del = function (key) {
2336
- this.storage.removeItem(key);
2337
- };
2338
- Storage.prototype.clear = function () {
2339
- if (typeof this.storage.clear === 'function') {
2340
- this.storage.clear();
2341
- }
2342
- };
2343
- return Storage;
2344
- }());
2345
-
2346
- var defaultPrefix = 'cache2_'; // 命名空间缓存键前缀,默认 cache2_ 。
2347
- var defaultNamespace = 'default';
2348
- var Cache = /** @class */ (function (_super) {
2349
- __extends(Cache, _super);
2350
- function Cache(namespace, options) {
2351
- var _this = _super.call(this) || this;
2352
- var ns = defaultNamespace, opts;
2353
- if (typeof namespace === 'string') {
2354
- ns = namespace;
2355
- }
2356
- else if (typeof namespace === 'object') {
2357
- opts = namespace;
2358
- }
2359
- if (!opts && typeof options === 'object') {
2360
- opts = options;
2361
- }
2362
- _this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: defaultPrefix }, opts);
2363
- _this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, opts));
2364
- _this.cacheKey = (_this.options.prefix || '') + (ns || '') || getUniqueId();
2365
- _this.startCheckperiod();
2366
- return _this;
2367
- }
2368
- // 检查当前键值是否过期,如果过期将会自动删除
2369
- Cache.prototype._check = function (key, data) {
2370
- var ret = true;
2371
- if (data.t !== 0 && data.t < Date.now()) {
2372
- ret = false;
2373
- this.del(key);
2374
- this.emit('expired', key, data.v);
2375
- }
2376
- return ret;
2377
- };
2378
- Cache.prototype._wrap = function (value, ttl) {
2379
- var now = Date.now();
2380
- var currentTtl = typeof ttl === 'number' ? ttl : this.options.stdTTL;
2381
- var livetime = currentTtl > 0 ? now + currentTtl : 0;
2382
- return {
2383
- v: value,
2384
- t: livetime,
2385
- n: now
2386
- };
2387
- };
2388
- Cache.prototype._isLimited = function (len) {
2389
- return this.options.max > -1 && len >= this.options.max;
2390
- };
2391
- Cache.prototype._getReplaceKey = function (keys, cacheValues) {
2392
- var retkey = keys[0];
2393
- keys.forEach(function (key) {
2394
- if (cacheValues[key].t < cacheValues[retkey].t ||
2395
- (cacheValues[key].t === cacheValues[retkey].t && cacheValues[key].n < cacheValues[retkey].n)) {
2396
- retkey = key;
2397
- }
2398
- });
2399
- return retkey;
2400
- };
2401
- Object.defineProperty(Cache.prototype, "cacheValues", {
2402
- // 获取全部缓存数据,不处理过期数据和排序
2403
- get: function () {
2404
- return this.storage.get(this.cacheKey) || {};
2405
- },
2406
- enumerable: false,
2407
- configurable: true
2408
- });
2409
- // 设置缓存数据
2410
- Cache.prototype.setCacheValues = function (values) {
2411
- this.storage.set(this.cacheKey, values);
2412
- };
2413
- // 从缓存中获取保存的值。如果未找到或已过期,则返回 undefined 。如果找到该值,则返回该值。
2414
- Cache.prototype.get = function (key) {
2415
- var data = this.cacheValues[key];
2416
- if (data && this._check(key, data)) {
2417
- return data.v;
2418
- }
2419
- return;
2420
- };
2421
- // 从缓存中获取多个保存的值。如果未找到或已过期,则返回一个空对象。如果找到该值,它会返回一个具有键值对的对象。
2422
- Cache.prototype.mget = function (keys) {
2423
- var _this = this;
2424
- var ret = {};
2425
- if (!Array.isArray(keys)) {
2426
- return ret;
2427
- }
2428
- var cacheValues = this.cacheValues;
2429
- keys.forEach(function (key) {
2430
- var data = cacheValues[key];
2431
- if (data && _this._check(key, data)) {
2432
- ret[key] = data.v;
2433
- }
2434
- });
2435
- return ret;
2436
- };
2437
- // 从缓存中获取全部保存的值。返回一个具有键值对的对象。
2438
- Cache.prototype.getAll = function () {
2439
- var keys = Object.keys(this.cacheValues);
2440
- return this.mget(keys);
2441
- };
2442
- // 设置键值对。设置成功返回 true 。
2443
- Cache.prototype.set = function (key, value, ttl) {
2444
- if (this.options.max === 0) {
2445
- return false;
2446
- }
2447
- var cacheValues = this.cacheValues;
2448
- var keys = Object.keys(cacheValues);
2449
- // 当前不存在该键值,并且数据量超过最大限制
2450
- if (!cacheValues[key] && this._isLimited(keys.length)) {
2451
- var validKeys = this.keys();
2452
- if (this._isLimited(validKeys.length)) {
2453
- // 如果最大限制策略是替换,将优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理。
2454
- if (this.options.maxStrategy === 'replaced') {
2455
- var replaceKey = this._getReplaceKey(validKeys, cacheValues);
2456
- this.del(replaceKey);
2457
- }
2458
- else {
2459
- // 如果是最大限制策略是不允许添加,返回 false 。
2460
- return false;
2461
- }
2462
- }
2463
- }
2464
- cacheValues[key] = this._wrap(value, ttl);
2465
- this.setCacheValues(cacheValues);
2466
- this.emit('set', key, cacheValues[key].v);
2467
- return true;
2468
- };
2469
- // 设置多个键值对。全部设置成功返回 true 。
2470
- Cache.prototype.mset = function (keyValueSet) {
2471
- var _this = this;
2472
- // 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
2473
- var ret = true;
2474
- keyValueSet.forEach(function (item) {
2475
- var itemSetResult = _this.set(item.key, item.value, item.ttl);
2476
- if (ret && !itemSetResult) {
2477
- ret = false;
2478
- }
2479
- });
2480
- return ret;
2481
- };
2482
- // 删除一个或多个键。返回已删除条目的数量。删除永远不会失败。
2483
- Cache.prototype.del = function (key) {
2484
- var _this = this;
2485
- var cacheValues = this.cacheValues;
2486
- var count = 0;
2487
- var keys = Array.isArray(key) ? key : [key];
2488
- keys.forEach(function (key) {
2489
- if (cacheValues[key]) {
2490
- count++;
2491
- var oldData = cacheValues[key];
2492
- delete cacheValues[key];
2493
- _this.emit('del', key, oldData.v);
2494
- }
2495
- });
2496
- if (count > 0) {
2497
- this.setCacheValues(cacheValues);
2498
- }
2499
- return count;
2500
- };
2501
- // 删除当前所有缓存。
2502
- Cache.prototype.clear = function () {
2503
- this.storage.del(this.cacheKey);
2504
- };
2505
- // 返回所有现有键的数组。
2506
- Cache.prototype.keys = function () {
2507
- var _this = this;
2508
- var cacheValues = this.cacheValues;
2509
- var keys = Object.keys(cacheValues);
2510
- return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
2511
- };
2512
- // 当前缓存是否包含某个键。
2513
- Cache.prototype.has = function (key) {
2514
- var data = this.cacheValues[key];
2515
- return !!(data && this._check(key, data));
2516
- };
2517
- // 获取缓存值并从缓存中删除键。
2518
- Cache.prototype.take = function (key) {
2519
- var ret;
2520
- var data = this.cacheValues[key];
2521
- if (data && this._check(key, data)) {
2522
- ret = data.v;
2523
- this.del(key);
2524
- }
2525
- return ret;
2526
- };
2527
- // 重新定义一个键的 ttl 。如果找到并更新成功,则返回 true 。
2528
- Cache.prototype.ttl = function (key, ttl) {
2529
- var cacheValues = this.cacheValues;
2530
- var data = cacheValues[key];
2531
- if (data && this._check(key, data)) {
2532
- cacheValues[key] = this._wrap(data.v, ttl);
2533
- return true;
2534
- }
2535
- return false;
2536
- };
2537
- // 获取某个键的 ttl 。
2538
- // 如果未找到键或已过期,返回 undefined 。
2539
- // 如果 ttl 为 0 ,返回 0 。
2540
- // 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
2541
- Cache.prototype.getTtl = function (key) {
2542
- var cacheValues = this.cacheValues;
2543
- var data = cacheValues[key];
2544
- if (data && this._check(key, data)) {
2545
- return cacheValues[key].t;
2546
- }
2547
- return;
2548
- };
2549
- // 获取某个键值的最后修改时间
2550
- // 如果未找到键或已过期,返回 undefined 。
2551
- // 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
2552
- Cache.prototype.getLastModified = function (key) {
2553
- var cacheValues = this.cacheValues;
2554
- var data = cacheValues[key];
2555
- if (data && this._check(key, data)) {
2556
- return cacheValues[key].n;
2557
- }
2558
- return;
2559
- };
2560
- // 启动定时校验过期数据
2561
- Cache.prototype.startCheckperiod = function () {
2562
- var _this = this;
2563
- // 触发全部缓存数据是否过期校验
2564
- this.keys();
2565
- if (this.options.checkperiod > 0) {
2566
- clearTimeout(this._checkTimeout);
2567
- this._checkTimeout = setTimeout(function () {
2568
- _this.startCheckperiod();
2569
- }, this.options.checkperiod);
2570
- }
2571
- };
2572
- // 停止定时校验过期数据
2573
- Cache.prototype.stopCheckperiod = function () {
2574
- clearTimeout(this._checkTimeout);
2575
- };
2576
- return Cache;
2577
- }(EmitterPro));
2578
-
2579
- new Storage(inWindow ? window.localStorage : undefined);
2580
-
2581
- new Storage(inWindow ? window.sessionStorage : undefined);
2582
-
2583
- var AsyncMemo = (function () {
2584
- function AsyncMemo(options) {
2585
- this.promiseCache = {};
2586
- this.cache = new Cache(uniqueId('uh_async_memo'), options);
2587
- }
2588
- AsyncMemo.prototype.run = function (asyncFn, key, options) {
2589
- var _this = this;
2590
- if (!key || !isString(key)) {
2591
- return asyncFn();
2592
- }
2593
- var opts = __assign({ persisted: true }, options);
2594
- if (opts.persisted) {
2595
- var data = this.cache.get(key);
2596
- if (data) {
2597
- return Promise.resolve(data);
2598
- }
2599
- }
2600
- if (!this.promiseCache[key]) {
2601
- this.promiseCache[key] = asyncFn()
2602
- .then(function (res) {
2603
- delete _this.promiseCache[key];
2604
- _this.cache.set(key, res, opts.ttl);
2605
- return res;
2606
- })
2607
- .catch(function (err) {
2608
- delete _this.promiseCache[key];
2609
- return Promise.reject(err);
2610
- });
2611
- }
2612
- return this.promiseCache[key];
2613
- };
2614
- return AsyncMemo;
2615
- }());
2648
+ var VERSION = "4.23.1";
2616
2649
 
2617
- var version = "4.22.2";
2650
+ var version = "4.23.1";
2618
2651
 
2619
2652
  exports.AsyncMemo = AsyncMemo;
2620
2653
  exports.VERSION = VERSION;
@@ -2670,7 +2703,7 @@
2670
2703
  exports.padZero = padZero;
2671
2704
  exports.parseIdCard = parseIdCard;
2672
2705
  exports.plus = plus;
2673
- exports.randomString = randomString$1;
2706
+ exports.randomString = randomString;
2674
2707
  exports.replaceChar = replaceChar;
2675
2708
  exports.round = _round;
2676
2709
  exports.safeDate = safeDate;
@@ -2679,6 +2712,7 @@
2679
2712
  exports.strlen = strlen;
2680
2713
  exports.times = times;
2681
2714
  exports.transformFieldNames = transformFieldNames;
2715
+ exports.transformObjectValue = transformObjectValue;
2682
2716
  exports.treeToList = treeToList;
2683
2717
  exports.validatePassword = validatePassword;
2684
2718
  exports.version = version;