util-helpers 4.23.1 → 5.0.0

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 (47) hide show
  1. package/dist/util-helpers.js +844 -961
  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/compressImage.js +2 -2
  7. package/esm/formatBankCard.js +3 -5
  8. package/esm/formatMobile.js +4 -6
  9. package/esm/getImageInfo.js +16 -43
  10. package/esm/index.js +0 -8
  11. package/esm/isBusinessLicense.js +1 -2
  12. package/esm/isSocialCreditCode.js +1 -3
  13. package/esm/loadImage.js +20 -45
  14. package/esm/loadImageWithBlob.js +19 -46
  15. package/lib/VERSION.js +1 -1
  16. package/lib/compressImage.js +2 -2
  17. package/lib/formatBankCard.js +3 -5
  18. package/lib/formatMobile.js +4 -6
  19. package/lib/getImageInfo.js +15 -42
  20. package/lib/index.js +1 -9
  21. package/lib/isBusinessLicense.js +1 -2
  22. package/lib/isSocialCreditCode.js +1 -3
  23. package/lib/loadImage.js +18 -43
  24. package/lib/loadImageWithBlob.js +18 -45
  25. package/package.json +1 -1
  26. package/types/compressImage.d.ts +2 -4
  27. package/types/formatBankCard.d.ts +1 -3
  28. package/types/formatMobile.d.ts +0 -2
  29. package/types/getImageInfo.d.ts +10 -12
  30. package/types/index.d.ts +0 -8
  31. package/types/isBusinessLicense.d.ts +0 -2
  32. package/types/isSocialCreditCode.d.ts +0 -2
  33. package/types/loadImage.d.ts +1 -12
  34. package/types/loadImageWithBlob.d.ts +6 -10
  35. package/esm/blobToDataURL.js +0 -7
  36. package/esm/isPromiseLike.js +0 -7
  37. package/esm/normalizeString.js +0 -7
  38. package/esm/waitTime.js +0 -8
  39. package/lib/blobToDataURL.js +0 -9
  40. package/lib/isPromiseLike.js +0 -9
  41. package/lib/normalizeString.js +0 -9
  42. package/lib/waitTime.js +0 -10
  43. package/types/blobToDataURL.d.ts +0 -29
  44. package/types/isPromiseLike.d.ts +0 -19
  45. package/types/normalizeString.d.ts +0 -26
  46. package/types/utils/Cache.d.ts +0 -14
  47. package/types/waitTime.d.ts +0 -24
@@ -200,11 +200,6 @@
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
-
208
203
  var blobExisted = typeof Blob !== stringUndefined;
209
204
  function isBlob(value) {
210
205
  if (blobExisted && value instanceof Blob) {
@@ -240,7 +235,7 @@
240
235
  return isNumber(value) && root$1.isNaN(value);
241
236
  }
242
237
 
243
- function isPromiseLike$1(value) {
238
+ function isPromiseLike(value) {
244
239
  return isObject(value) && typeof value.then === 'function';
245
240
  }
246
241
 
@@ -274,13 +269,6 @@
274
269
 
275
270
  function noop() { }
276
271
 
277
- function sleep(ms) {
278
- if (ms === void 0) { ms = 1000; }
279
- return new Promise(function (resolve) {
280
- setTimeout(resolve, ms);
281
- });
282
- }
283
-
284
272
  var idCounter = 0;
285
273
  function uniqueId(prefix) {
286
274
  var id = ++idCounter;
@@ -401,10 +389,8 @@
401
389
  return baseCodeArr[checkCodeIndex];
402
390
  }
403
391
  function isSocialCreditCode(value, options) {
404
- if (options === void 0) { options = {}; }
405
392
  var valueStr = toString(value);
406
- var _a = options.loose, loose = _a === void 0 ? false : _a, _b = options.checkCode, cc = _b === void 0 ? true : _b;
407
- var needCheckCode = !loose && cc;
393
+ var _a = (options || {}).checkCode, needCheckCode = _a === void 0 ? true : _a;
408
394
  var passBaseRule = baseReg$1.test(valueStr);
409
395
  if (!needCheckCode || !passBaseRule) {
410
396
  return passBaseRule;
@@ -632,8 +618,7 @@
632
618
  function isBusinessLicense(value, options) {
633
619
  if (options === void 0) { options = {}; }
634
620
  var valueStr = toString(value);
635
- var _a = options.loose, loose = _a === void 0 ? false : _a, _b = options.checkCode, cc = _b === void 0 ? true : _b;
636
- var needCheckCode = !loose && cc;
621
+ var _a = options.checkCode, needCheckCode = _a === void 0 ? true : _a;
637
622
  var passBaseRule = baseReg.test(valueStr);
638
623
  if (!needCheckCode || !passBaseRule) {
639
624
  return passBaseRule;
@@ -644,10 +629,6 @@
644
629
  return lastCode === String(checkCode);
645
630
  }
646
631
 
647
- function isPromiseLike(obj) {
648
- return isPromiseLike$1(obj);
649
- }
650
-
651
632
  var regHMCard = /^[hm]{1}([0-9]{10}|[0-9]{8})$/i;
652
633
  function isHMCard(value) {
653
634
  var valueStr = toString(value);
@@ -956,13 +937,11 @@
956
937
 
957
938
  function formatBankCard(bankCardNo, options) {
958
939
  if (bankCardNo === void 0) { bankCardNo = ''; }
959
- if (options === void 0) { options = {}; }
960
- var _a = options.char, char = _a === void 0 ? ' ' : _a, _b = options.length, length = _b === void 0 ? 4 : _b;
961
- var realSpaceMark = 'spaceMark' in options ? options.spaceMark : char;
940
+ var _a = options || {}, _b = _a.spaceMark, spaceMark = _b === void 0 ? ' ' : _b, _c = _a.length, length = _c === void 0 ? 4 : _c;
962
941
  var reg = new RegExp("(.{".concat(length, "})"), 'g');
963
- var regChar = new RegExp("".concat(realSpaceMark), 'g');
942
+ var regChar = new RegExp("".concat(spaceMark), 'g');
964
943
  var realValue = toString(bankCardNo).replace(regChar, '');
965
- var str = realValue.replace(reg, "$1".concat(realSpaceMark));
944
+ var str = realValue.replace(reg, "$1".concat(spaceMark));
966
945
  return realValue.length % length === 0 ? str.substring(0, str.length - 1) : str;
967
946
  }
968
947
 
@@ -1202,10 +1181,6 @@
1202
1181
  });
1203
1182
  };
1204
1183
 
1205
- function blobToDataURL(blob) {
1206
- return fileReader(blob);
1207
- }
1208
-
1209
1184
  function dataURLToBlob(data) {
1210
1185
  var parts = data.split(',');
1211
1186
  var meta = parts[0].substring(5).split(';');
@@ -1226,10 +1201,6 @@
1226
1201
  return "data:".concat(mimeType).concat(base64 ? ';base64' : '', ",").concat(data);
1227
1202
  }
1228
1203
 
1229
- function normalizeString(value) {
1230
- return toString(value);
1231
- }
1232
-
1233
1204
  var safeDate = function (value) {
1234
1205
  var args = [];
1235
1206
  for (var _i = 1; _i < arguments.length; _i++) {
@@ -1244,16 +1215,14 @@
1244
1215
 
1245
1216
  function formatMobile(mobileNo, options) {
1246
1217
  if (mobileNo === void 0) { mobileNo = ''; }
1247
- if (options === void 0) { options = {}; }
1248
- var _a = options.char, char = _a === void 0 ? ' ' : _a;
1249
- var realSpaceMark = 'spaceMark' in options ? options.spaceMark : char;
1250
- var regChar = new RegExp(realSpaceMark, 'g');
1218
+ var _a = (options || {}).spaceMark, spaceMark = _a === void 0 ? ' ' : _a;
1219
+ var regChar = new RegExp(spaceMark, 'g');
1251
1220
  var realValue = toString(mobileNo).replace(regChar, '').substring(0, 11);
1252
1221
  if (realValue.length > 7) {
1253
- return realValue.replace(/^(...)(....)/g, "$1".concat(realSpaceMark, "$2").concat(realSpaceMark));
1222
+ return realValue.replace(/^(...)(....)/g, "$1".concat(spaceMark, "$2").concat(spaceMark));
1254
1223
  }
1255
1224
  if (realValue.length > 3) {
1256
- return realValue.replace(/^(...)/g, "$1".concat(realSpaceMark));
1225
+ return realValue.replace(/^(...)/g, "$1".concat(spaceMark));
1257
1226
  }
1258
1227
  return realValue;
1259
1228
  }
@@ -1545,592 +1514,101 @@
1545
1514
  });
1546
1515
  }
1547
1516
 
1548
- var EmitterPro = /** @class */ (function () {
1549
- function EmitterPro() {
1550
- this.handlers = {};
1551
- }
1552
- EmitterPro.prototype.eventNames = function () {
1553
- var _a;
1554
- var symbols = ((_a = Object.getOwnPropertySymbols) === null || _a === void 0 ? void 0 : _a.call(Object, this.handlers)) || [];
1555
- var keys = Object.keys(this.handlers);
1556
- return keys.concat(symbols);
1557
- };
1558
- EmitterPro.prototype.rawListeners = function (eventName) {
1559
- var handler = this.handlers[eventName];
1560
- return handler ? handler.map(function (item) { return item.raw; }) : [];
1561
- };
1562
- EmitterPro.prototype.listeners = function (eventName) {
1563
- var handler = this.handlers[eventName];
1564
- return handler ? handler.map(function (item) { return item.wrap; }) : [];
1565
- };
1566
- EmitterPro.prototype.hasListener = function (eventName, listener) {
1567
- return this.rawListeners(eventName).some(function (item) { return item === listener; });
1568
- };
1569
- EmitterPro.prototype._on = function (eventName, raw, wrap, context, dir) {
1570
- if (context === void 0) { context = null; }
1571
- if (dir === void 0) { dir = 1; }
1572
- var currentListener = { raw: raw, wrap: wrap, context: context };
1573
- if (!this.handlers[eventName]) {
1574
- this.handlers[eventName] = [currentListener];
1575
- }
1576
- else {
1577
- var appendMethod = dir === 1 ? 'push' : 'unshift';
1578
- this.handlers[eventName][appendMethod](currentListener);
1579
- }
1580
- return this;
1581
- };
1582
- EmitterPro.prototype.prependListener = function (eventName, listener, context) {
1583
- return this._on(eventName, listener, listener, context, 0);
1584
- };
1585
- EmitterPro.prototype.on = function (eventName, listener, context) {
1586
- return this._on(eventName, listener, listener, context);
1587
- };
1588
- EmitterPro.prototype._wrapOnce = function (eventName, listener, context) {
1589
- var _this = this;
1590
- if (context === void 0) { context = null; }
1591
- var wrap = (function () {
1592
- var args = [];
1593
- for (var _i = 0; _i < arguments.length; _i++) {
1594
- args[_i] = arguments[_i];
1517
+ function loadImageWithBlob(img, ajaxOptions) {
1518
+ return new Promise(function (resolve, reject) {
1519
+ getFileBlob(img, ajaxOptions)
1520
+ .then(function (blob) {
1521
+ var url = createObjectURL(blob);
1522
+ var image = new Image();
1523
+ image.onload = function () {
1524
+ resolve({ blob: blob, image: image });
1525
+ };
1526
+ image.onerror = function (err) {
1527
+ revokeObjectURL(url);
1528
+ console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
1529
+ reject(err);
1530
+ };
1531
+ image.src = url;
1532
+ })
1533
+ .catch(reject);
1534
+ });
1535
+ }
1536
+
1537
+ function canvasToBlob(canvas, type, quality) {
1538
+ return new Promise(function (resolve) {
1539
+ canvas.toBlob(function (blob) {
1540
+ resolve(blob);
1541
+ }, type, quality);
1542
+ });
1543
+ }
1544
+ var compressImage = function (img, options) {
1545
+ if (options === void 0) { options = {}; }
1546
+ return new Promise(function (resolve, reject) {
1547
+ var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.background, background = _b === void 0 ? '#fff' : _b, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _c = options.format, format = _c === void 0 ? 'blob' : _c, _d = options.type, type = _d === void 0 ? 'image/jpeg' : _d, _e = options.quality, quality = _e === void 0 ? 0.8 : _e, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw, ajaxOptions = options.ajaxOptions;
1548
+ loadImageWithBlob(img, ajaxOptions)
1549
+ .then(function (_a) {
1550
+ var image = _a.image, blob = _a.blob;
1551
+ var numWidth = toNumber(width);
1552
+ var numHeight = toNumber(height);
1553
+ var numQuality = toNumber(quality);
1554
+ if (numWidth) {
1555
+ image.width = numWidth;
1595
1556
  }
1596
- listener.apply(context, args);
1597
- _this.off(eventName, wrap);
1598
- });
1599
- return wrap;
1600
- };
1601
- EmitterPro.prototype.once = function (eventName, listener, context) {
1602
- var wrap = this._wrapOnce(eventName, listener, context);
1603
- return this._on(eventName, listener, wrap, context);
1604
- };
1605
- EmitterPro.prototype.prependOnceListener = function (eventName, listener, context) {
1606
- var wrap = this._wrapOnce(eventName, listener, context);
1607
- return this._on(eventName, listener, wrap, context, 0);
1608
- };
1609
- EmitterPro.prototype.off = function (eventName, listener) {
1610
- var handler = this.handlers[eventName];
1611
- if (handler) {
1612
- if (listener) {
1613
- var index = handler.findIndex(function (item) { return item.wrap === listener || item.raw === listener; });
1614
- if (index !== -1) {
1615
- handler.splice(index, 1);
1616
- }
1557
+ if (numHeight) {
1558
+ image.height = numHeight;
1559
+ }
1560
+ beforeCompress === null || beforeCompress === void 0 ? void 0 : beforeCompress({ image: image, blob: blob }, options);
1561
+ var canvas = document.createElement('canvas');
1562
+ var ctx = canvas.getContext('2d');
1563
+ var info = { image: image, blob: blob, canvas: canvas, context: ctx };
1564
+ var numCanvasWidth = toNumber(typeof canvasWidth === 'function' ? canvasWidth(info, options) : canvasWidth);
1565
+ var numCanvasHeight = toNumber(typeof canvasHeight === 'function' ? canvasHeight(info, options) : canvasHeight);
1566
+ canvas.width = numCanvasWidth || image.width;
1567
+ canvas.height = numCanvasHeight || image.height;
1568
+ var bgIsTransparent = background === 'none' || background === 'transparent';
1569
+ if (bgIsTransparent) {
1570
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
1617
1571
  }
1618
1572
  else {
1619
- delete this.handlers[eventName];
1573
+ ctx.fillStyle = background;
1574
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
1620
1575
  }
1621
- }
1622
- return this;
1623
- };
1624
- EmitterPro.prototype.offAll = function () {
1625
- this.handlers = {};
1626
- return this;
1627
- };
1628
- EmitterPro.prototype.emit = function (eventName) {
1629
- var args = [];
1630
- for (var _i = 1; _i < arguments.length; _i++) {
1631
- args[_i - 1] = arguments[_i];
1632
- }
1633
- var handler = this.handlers[eventName];
1634
- if (handler && handler.length > 0) {
1635
- handler.forEach(function (listener) {
1636
- listener.wrap.apply(listener.context, args);
1637
- });
1638
- return true;
1639
- }
1640
- return false;
1641
- };
1642
- return EmitterPro;
1643
- }());
1644
-
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;
1576
+ var internalOffset = [0, 0];
1577
+ if (rotate !== nativeUndefined) {
1578
+ ctx.translate(image.width / 2, image.height / 2);
1579
+ internalOffset = [-image.width / 2, -image.height / 2];
1580
+ ctx.rotate((rotate * Math.PI) / 180);
1669
1581
  }
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;
1582
+ var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
1583
+ beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
1584
+ var dx = internalOffset[0] + toNumber(outOffset[0]);
1585
+ var dy = internalOffset[1] + toNumber(outOffset[1]);
1586
+ ctx.drawImage(image, dx, dy, image.width, image.height);
1587
+ if (type === 'image/png' && bgIsTransparent) {
1588
+ ctx.globalCompositeOperation = 'destination-in';
1589
+ ctx.drawImage(image, dx, dy, image.width, image.height);
1590
+ }
1591
+ afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
1592
+ if (format === 'blob') {
1593
+ canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
1594
+ }
1595
+ else {
1596
+ resolve(canvas.toDataURL(type, numQuality));
1597
+ }
1598
+ })
1599
+ .catch(reject);
1600
+ });
1601
+ };
1691
1602
 
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) {
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;
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();
1763
- return _this;
1764
- }
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;
1774
- };
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;
1815
- }
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;
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;
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) {
2080
- image.height = numHeight;
2081
- }
2082
- beforeCompress === null || beforeCompress === void 0 ? void 0 : beforeCompress({ image: image, blob: blob }, options);
2083
- var canvas = document.createElement('canvas');
2084
- var ctx = canvas.getContext('2d');
2085
- var info = { image: image, blob: blob, canvas: canvas, context: ctx };
2086
- var numCanvasWidth = toNumber(typeof canvasWidth === 'function' ? canvasWidth(info, options) : canvasWidth);
2087
- var numCanvasHeight = toNumber(typeof canvasHeight === 'function' ? canvasHeight(info, options) : canvasHeight);
2088
- canvas.width = numCanvasWidth || image.width;
2089
- canvas.height = numCanvasHeight || image.height;
2090
- var bgIsTransparent = background === 'none' || background === 'transparent';
2091
- if (bgIsTransparent) {
2092
- ctx.clearRect(0, 0, canvas.width, canvas.height);
2093
- }
2094
- else {
2095
- ctx.fillStyle = background;
2096
- ctx.fillRect(0, 0, canvas.width, canvas.height);
2097
- }
2098
- var internalOffset = [0, 0];
2099
- if (rotate !== nativeUndefined) {
2100
- ctx.translate(image.width / 2, image.height / 2);
2101
- internalOffset = [-image.width / 2, -image.height / 2];
2102
- ctx.rotate((rotate * Math.PI) / 180);
2103
- }
2104
- var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
2105
- beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
2106
- var dx = internalOffset[0] + toNumber(outOffset[0]);
2107
- var dy = internalOffset[1] + toNumber(outOffset[1]);
2108
- ctx.drawImage(image, dx, dy, image.width, image.height);
2109
- if (type === 'image/png' && bgIsTransparent) {
2110
- ctx.globalCompositeOperation = 'destination-in';
2111
- ctx.drawImage(image, dx, dy, image.width, image.height);
2112
- }
2113
- afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
2114
- if (format === 'blob') {
2115
- canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
2116
- }
2117
- else {
2118
- resolve(canvas.toDataURL(type, numQuality));
2119
- }
2120
- })
2121
- .catch(reject);
2122
- });
2123
- };
2124
-
2125
- function saver(blobUrl, fileName) {
2126
- if (fileName === void 0) { fileName = ''; }
2127
- var anchor = document.createElement('a');
2128
- anchor.href = blobUrl;
2129
- anchor.style.display = 'none';
2130
- anchor.setAttribute('download', fileName);
2131
- function handleClick(e) {
2132
- e.stopPropagation();
2133
- anchor.removeEventListener('click', handleClick);
1603
+ function saver(blobUrl, fileName) {
1604
+ if (fileName === void 0) { fileName = ''; }
1605
+ var anchor = document.createElement('a');
1606
+ anchor.href = blobUrl;
1607
+ anchor.style.display = 'none';
1608
+ anchor.setAttribute('download', fileName);
1609
+ function handleClick(e) {
1610
+ e.stopPropagation();
1611
+ anchor.removeEventListener('click', handleClick);
2134
1612
  }
2135
1613
  anchor.addEventListener('click', handleClick);
2136
1614
  document.body.appendChild(anchor);
@@ -2160,11 +1638,11 @@
2160
1638
  if (!(dataType === 'url' || (!dataType && isUrl(data)))) return [3, 6];
2161
1639
  asyncTransformRequest = function (opts) {
2162
1640
  var tempOptions = typeof transformRequest === 'function' ? transformRequest(opts) : opts;
2163
- return isPromiseLike$1(tempOptions) ? tempOptions : Promise.resolve(tempOptions);
1641
+ return isPromiseLike(tempOptions) ? tempOptions : Promise.resolve(tempOptions);
2164
1642
  };
2165
1643
  asyncTransformResponse = function (res) {
2166
1644
  var tempRes = typeof transformResponse === 'function' ? transformResponse(res) : res;
2167
- return isPromiseLike$1(tempRes) ? tempRes : Promise.resolve(tempRes);
1645
+ return isPromiseLike(tempRes) ? tempRes : Promise.resolve(tempRes);
2168
1646
  };
2169
1647
  return [4, asyncTransformRequest({ responseType: 'blob' })];
2170
1648
  case 3:
@@ -2204,96 +1682,47 @@
2204
1682
  });
2205
1683
  }
2206
1684
 
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) {
2211
- revokeObjectURL(v.data.image.src);
2212
- }
2213
- }
2214
- catch (_a) {
2215
- }
2216
- });
2217
1685
  function calcContrast(w, h) {
2218
1686
  var n = gcd(w, h);
2219
1687
  return "".concat(divide(round(w), n), ":").concat(divide(round(h), n));
2220
1688
  }
2221
- function getImageInfo(img, cacheOptions, ajaxOptions) {
2222
- if (cacheOptions === void 0) { cacheOptions = true; }
2223
- var cacheOptionsIsObject = typeof cacheOptions === 'object';
2224
- var _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)
2228
- };
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; });
1689
+ function getImageInfo(img, ajaxOptions) {
1690
+ return loadImageWithBlob(img, ajaxOptions).then(function (_a) {
1691
+ var image = _a.image, blob = _a.blob;
1692
+ var width = image.width, height = image.height;
1693
+ return {
1694
+ width: width,
1695
+ height: height,
1696
+ contrast: calcContrast(width, height),
1697
+ measure: "".concat(width, " \u00D7 ").concat(height, " px"),
1698
+ size: bytesToSize(blob.size),
1699
+ bytes: blob.size,
1700
+ image: image,
1701
+ blob: blob
1702
+ };
1703
+ });
2251
1704
  }
2252
1705
 
2253
- var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
2254
- asyncMemo.cache.on('del', function (k, v) {
2255
- try {
2256
- if (v.r) {
2257
- revokeObjectURL(v.data.src);
2258
- }
2259
- }
2260
- catch (_a) {
2261
- }
2262
- });
2263
- function loadImage(img, cacheOptions) {
2264
- if (cacheOptions === void 0) { cacheOptions = true; }
2265
- var cacheOptionsIsObject = typeof cacheOptions === 'object';
2266
- var _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)
2270
- };
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';
1706
+ function loadImage(img) {
1707
+ return new Promise(function (resolve, reject) {
1708
+ var imgIsBlob = isBlob(img);
1709
+ var url = imgIsBlob ? createObjectURL(img) : img;
1710
+ var image = new Image();
1711
+ if (!imgIsBlob) {
1712
+ image.crossOrigin = 'anonymous';
1713
+ }
1714
+ image.onload = function () {
1715
+ resolve(image);
1716
+ };
1717
+ image.onerror = function (err) {
1718
+ if (imgIsBlob) {
1719
+ revokeObjectURL(url);
2279
1720
  }
2280
- image.onload = function () {
2281
- resolve({
2282
- data: image,
2283
- r: _cacheOptions.autoRevokeOnDel
2284
- });
2285
- };
2286
- image.onerror = function (err) {
2287
- if (imgIsBlob) {
2288
- revokeObjectURL(url);
2289
- }
2290
- console.error("[loadImage] The image load failed, '".concat(img, "'."));
2291
- reject(err);
2292
- };
2293
- image.src = url;
2294
- });
2295
- }, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
2296
- .then(function (res) { return res.data; });
1721
+ console.error("[loadImage] The image load failed, '".concat(img, "'."));
1722
+ reject(err);
1723
+ };
1724
+ image.src = url;
1725
+ });
2297
1726
  }
2298
1727
 
2299
1728
  function loadScript(src, options) {
@@ -2347,7 +1776,7 @@
2347
1776
  }
2348
1777
  return prefix;
2349
1778
  }
2350
- var randomString = function (len, pool) {
1779
+ var randomString$1 = function (len, pool) {
2351
1780
  if (len === void 0) { len = 0; }
2352
1781
  var _pool;
2353
1782
  if (typeof pool !== 'string') {
@@ -2356,303 +1785,761 @@
2356
1785
  else if (chars[pool]) {
2357
1786
  _pool = chars[pool];
2358
1787
  }
2359
- else {
2360
- _pool = pool;
1788
+ else {
1789
+ _pool = pool;
1790
+ }
1791
+ return internalRandomString(toNumber(len), _pool);
1792
+ };
1793
+
1794
+ function strlen(str) {
1795
+ var realStr = toString(str);
1796
+ var len = 0;
1797
+ for (var i = 0; i < realStr.length; i++) {
1798
+ var c = realStr.charCodeAt(i);
1799
+ if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
1800
+ len++;
1801
+ }
1802
+ else {
1803
+ len += 2;
1804
+ }
1805
+ }
1806
+ return len;
1807
+ }
1808
+
1809
+ function transformFieldNames(data, fieldNames, childrenField, nodeAssign) {
1810
+ if (nodeAssign === void 0) { nodeAssign = 'spread'; }
1811
+ if (!isArray(data)) {
1812
+ return data;
1813
+ }
1814
+ if (data.length <= 0) {
1815
+ return [];
1816
+ }
1817
+ function recusion(arr) {
1818
+ return arr.map(function (item) {
1819
+ if (!isObject(item)) {
1820
+ return item;
1821
+ }
1822
+ var newItem = nodeAssign === 'spread' ? __assign({}, item) : item;
1823
+ var delKeys = [];
1824
+ if (childrenField && isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
1825
+ newItem[childrenField] = recusion(newItem[childrenField].slice());
1826
+ }
1827
+ var newKeys = objectKeys(fieldNames);
1828
+ newKeys.forEach(function (newKey) {
1829
+ var oldKey = fieldNames[newKey];
1830
+ if (oldKey in newItem) {
1831
+ newItem[newKey] = newItem[oldKey];
1832
+ if (newKeys.indexOf(oldKey) === -1) {
1833
+ delKeys.push(oldKey);
1834
+ }
1835
+ }
1836
+ });
1837
+ if (delKeys.length > 0) {
1838
+ delKeys.forEach(function (delKey) {
1839
+ delete newItem[delKey];
1840
+ });
1841
+ }
1842
+ return newItem;
1843
+ });
1844
+ }
1845
+ return recusion(data.slice());
1846
+ }
1847
+
1848
+ function processEmptyChildren(arr, options) {
1849
+ var _a = options.childrenField, childrenField = _a === void 0 ? 'children' : _a, _b = options.emptyChildrenValue, emptyChildrenValue = _b === void 0 ? 'none' : _b;
1850
+ arr.forEach(function (item) {
1851
+ if (item[childrenField].length <= 0) {
1852
+ if (emptyChildrenValue === 'null') {
1853
+ item[childrenField] = null;
1854
+ }
1855
+ else {
1856
+ delete item[childrenField];
1857
+ }
1858
+ }
1859
+ else {
1860
+ processEmptyChildren(item[childrenField], options);
1861
+ }
1862
+ });
1863
+ }
1864
+ function listToTree(list, options) {
1865
+ if (options === void 0) { options = {}; }
1866
+ var _a = options.keyField, keyField = _a === void 0 ? 'id' : _a, _b = options.parentField, parentField = _b === void 0 ? 'pid' : _b, _c = options.childrenField, childrenField = _c === void 0 ? 'children' : _c, _d = options.emptyChildrenValue, emptyChildrenValue = _d === void 0 ? 'none' : _d, _e = options.nodeAssign, nodeAssign = _e === void 0 ? 'spread' : _e;
1867
+ var tree = [];
1868
+ var record = {};
1869
+ if (!isArray(list)) {
1870
+ return tree;
1871
+ }
1872
+ list.forEach(function (item) {
1873
+ if (isObject(item)) {
1874
+ var newItem = nodeAssign === 'spread' ? __assign({}, item) : item;
1875
+ var id = newItem[keyField];
1876
+ var pid = newItem[parentField];
1877
+ if (record[id]) {
1878
+ newItem[childrenField] = record[id];
1879
+ }
1880
+ else {
1881
+ newItem[childrenField] = record[id] = [];
1882
+ }
1883
+ if (pid) {
1884
+ if (!record[pid]) {
1885
+ record[pid] = [newItem];
1886
+ }
1887
+ else {
1888
+ record[pid].push(newItem);
1889
+ }
1890
+ }
1891
+ else {
1892
+ tree.push(newItem);
1893
+ }
1894
+ }
1895
+ });
1896
+ if (emptyChildrenValue !== 'array') {
1897
+ processEmptyChildren(tree, options);
1898
+ }
1899
+ return tree;
1900
+ }
1901
+
1902
+ function treeToList(tree, childrenField) {
1903
+ var list = [];
1904
+ if (!isArray(tree)) {
1905
+ return list;
1906
+ }
1907
+ function recusion(arr) {
1908
+ arr.forEach(function (item) {
1909
+ if (isObject(item)) {
1910
+ var newItem = __assign({}, item);
1911
+ list.push(newItem);
1912
+ if (newItem[childrenField]) {
1913
+ if (isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
1914
+ recusion(newItem[childrenField]);
1915
+ }
1916
+ delete newItem[childrenField];
1917
+ }
1918
+ }
1919
+ else {
1920
+ list.push(item);
1921
+ }
1922
+ });
1923
+ }
1924
+ recusion(tree);
1925
+ return list;
1926
+ }
1927
+
1928
+ function filterTree(tree, predicate, childrenField, nodeAssign) {
1929
+ if (childrenField === void 0) { childrenField = 'children'; }
1930
+ if (nodeAssign === void 0) { nodeAssign = 'spread'; }
1931
+ var result = [];
1932
+ if (!isArray(tree)) {
1933
+ return result;
1934
+ }
1935
+ tree.forEach(function (item) {
1936
+ var newItem = item;
1937
+ if (isObject(item)) {
1938
+ newItem = nodeAssign === 'spread' ? __assign({}, item) : item;
1939
+ }
1940
+ if (predicate(newItem)) {
1941
+ if (isObject(newItem)) {
1942
+ var childs = newItem[childrenField];
1943
+ if (isArray(childs) && childs.length > 0) {
1944
+ newItem[childrenField] = filterTree(childs, predicate, childrenField, nodeAssign);
1945
+ }
1946
+ }
1947
+ result.push(newItem);
1948
+ }
1949
+ });
1950
+ return result;
1951
+ }
1952
+
1953
+ function findTreeNode(tree, predicate, childrenField) {
1954
+ var e_1, _a;
1955
+ if (childrenField === void 0) { childrenField = 'children'; }
1956
+ var stack = [];
1957
+ var node;
1958
+ if (!isArray(tree)) {
1959
+ return node;
1960
+ }
1961
+ try {
1962
+ for (var tree_1 = __values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
1963
+ var item = tree_1_1.value;
1964
+ stack.push(item);
1965
+ while (stack.length) {
1966
+ var temp = stack.pop();
1967
+ if (predicate(temp)) {
1968
+ node = temp;
1969
+ break;
1970
+ }
1971
+ if (isObject(temp)) {
1972
+ var childs = temp[childrenField];
1973
+ if (isArray(childs) && childs.length > 0) {
1974
+ childs.forEach(function (c) {
1975
+ stack.push(c);
1976
+ });
1977
+ }
1978
+ }
1979
+ }
1980
+ if (node) {
1981
+ break;
1982
+ }
1983
+ }
1984
+ }
1985
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
1986
+ finally {
1987
+ try {
1988
+ if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
1989
+ }
1990
+ finally { if (e_1) throw e_1.error; }
2361
1991
  }
2362
- return internalRandomString(toNumber(len), _pool);
2363
- };
1992
+ return node;
1993
+ }
2364
1994
 
2365
- function strlen(str) {
2366
- var realStr = toString(str);
2367
- var len = 0;
2368
- for (var i = 0; i < realStr.length; i++) {
2369
- var c = realStr.charCodeAt(i);
2370
- if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
2371
- len++;
1995
+ function findTreeNodes(tree, predicate, childrenField) {
1996
+ var e_1, _a;
1997
+ if (childrenField === void 0) { childrenField = 'children'; }
1998
+ var stack = [];
1999
+ var nodes = [];
2000
+ if (!isArray(tree)) {
2001
+ return nodes;
2002
+ }
2003
+ try {
2004
+ for (var tree_1 = __values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
2005
+ var item = tree_1_1.value;
2006
+ stack.push(item);
2007
+ while (stack.length) {
2008
+ var temp = stack.pop();
2009
+ if (predicate(temp)) {
2010
+ nodes.push(temp);
2011
+ }
2012
+ if (isObject(temp)) {
2013
+ var childs = temp[childrenField];
2014
+ if (isArray(childs) && childs.length > 0) {
2015
+ childs.forEach(function (c) {
2016
+ stack.push(c);
2017
+ });
2018
+ }
2019
+ }
2020
+ }
2372
2021
  }
2373
- else {
2374
- len += 2;
2022
+ }
2023
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2024
+ finally {
2025
+ try {
2026
+ if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
2375
2027
  }
2028
+ finally { if (e_1) throw e_1.error; }
2376
2029
  }
2377
- return len;
2378
- }
2379
-
2380
- function waitTime(time) {
2381
- if (time === void 0) { time = 1000; }
2382
- return sleep(time);
2030
+ return nodes;
2383
2031
  }
2384
2032
 
2385
- function transformFieldNames(data, fieldNames, childrenField, nodeAssign) {
2386
- if (nodeAssign === void 0) { nodeAssign = 'spread'; }
2387
- if (!isArray(data)) {
2388
- return data;
2389
- }
2390
- if (data.length <= 0) {
2033
+ function internalFindTreeSelect(tree, predicate, childrenField, path) {
2034
+ var e_1, _a;
2035
+ if (path === void 0) { path = []; }
2036
+ if (!isArray(tree)) {
2391
2037
  return [];
2392
2038
  }
2393
- function recusion(arr) {
2394
- return arr.map(function (item) {
2395
- if (!isObject(item)) {
2396
- return item;
2397
- }
2398
- var newItem = nodeAssign === 'spread' ? __assign({}, item) : item;
2399
- var delKeys = [];
2400
- if (childrenField && isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
2401
- newItem[childrenField] = recusion(newItem[childrenField].slice());
2039
+ try {
2040
+ for (var tree_1 = __values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
2041
+ var item = tree_1_1.value;
2042
+ path.push(item);
2043
+ if (predicate(item)) {
2044
+ return path;
2402
2045
  }
2403
- var newKeys = objectKeys(fieldNames);
2404
- newKeys.forEach(function (newKey) {
2405
- var oldKey = fieldNames[newKey];
2406
- if (oldKey in newItem) {
2407
- newItem[newKey] = newItem[oldKey];
2408
- if (newKeys.indexOf(oldKey) === -1) {
2409
- delKeys.push(oldKey);
2046
+ if (isObject(item)) {
2047
+ var childs = item[childrenField];
2048
+ if (isArray(childs) && childs.length > 0) {
2049
+ var findChildren = internalFindTreeSelect(childs, predicate, childrenField, path);
2050
+ if (findChildren.length > 0) {
2051
+ return findChildren;
2410
2052
  }
2411
2053
  }
2412
- });
2413
- if (delKeys.length > 0) {
2414
- delKeys.forEach(function (delKey) {
2415
- delete newItem[delKey];
2416
- });
2417
2054
  }
2418
- return newItem;
2419
- });
2055
+ path.pop();
2056
+ }
2420
2057
  }
2421
- return recusion(data.slice());
2058
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
2059
+ finally {
2060
+ try {
2061
+ if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
2062
+ }
2063
+ finally { if (e_1) throw e_1.error; }
2064
+ }
2065
+ return [];
2066
+ }
2067
+ function findTreeSelect(tree, predicate, childrenField) {
2068
+ if (childrenField === void 0) { childrenField = 'children'; }
2069
+ return internalFindTreeSelect(tree, predicate, childrenField);
2422
2070
  }
2423
2071
 
2424
- function processEmptyChildren(arr, options) {
2425
- var _a = options.childrenField, childrenField = _a === void 0 ? 'children' : _a, _b = options.emptyChildrenValue, emptyChildrenValue = _b === void 0 ? 'none' : _b;
2426
- arr.forEach(function (item) {
2427
- if (item[childrenField].length <= 0) {
2428
- if (emptyChildrenValue === 'null') {
2429
- item[childrenField] = null;
2072
+ var VERSION = "5.0.0";
2073
+
2074
+ var EmitterPro = /** @class */ (function () {
2075
+ function EmitterPro() {
2076
+ this.handlers = {};
2077
+ }
2078
+ EmitterPro.prototype.eventNames = function () {
2079
+ var _a;
2080
+ var symbols = ((_a = Object.getOwnPropertySymbols) === null || _a === void 0 ? void 0 : _a.call(Object, this.handlers)) || [];
2081
+ var keys = Object.keys(this.handlers);
2082
+ return keys.concat(symbols);
2083
+ };
2084
+ EmitterPro.prototype.rawListeners = function (eventName) {
2085
+ var handler = this.handlers[eventName];
2086
+ return handler ? handler.map(function (item) { return item.raw; }) : [];
2087
+ };
2088
+ EmitterPro.prototype.listeners = function (eventName) {
2089
+ var handler = this.handlers[eventName];
2090
+ return handler ? handler.map(function (item) { return item.wrap; }) : [];
2091
+ };
2092
+ EmitterPro.prototype.hasListener = function (eventName, listener) {
2093
+ return this.rawListeners(eventName).some(function (item) { return item === listener; });
2094
+ };
2095
+ EmitterPro.prototype._on = function (eventName, raw, wrap, context, dir) {
2096
+ if (context === void 0) { context = null; }
2097
+ if (dir === void 0) { dir = 1; }
2098
+ var currentListener = { raw: raw, wrap: wrap, context: context };
2099
+ if (!this.handlers[eventName]) {
2100
+ this.handlers[eventName] = [currentListener];
2101
+ }
2102
+ else {
2103
+ var appendMethod = dir === 1 ? 'push' : 'unshift';
2104
+ this.handlers[eventName][appendMethod](currentListener);
2105
+ }
2106
+ return this;
2107
+ };
2108
+ EmitterPro.prototype.prependListener = function (eventName, listener, context) {
2109
+ return this._on(eventName, listener, listener, context, 0);
2110
+ };
2111
+ EmitterPro.prototype.on = function (eventName, listener, context) {
2112
+ return this._on(eventName, listener, listener, context);
2113
+ };
2114
+ EmitterPro.prototype._wrapOnce = function (eventName, listener, context) {
2115
+ var _this = this;
2116
+ if (context === void 0) { context = null; }
2117
+ var wrap = (function () {
2118
+ var args = [];
2119
+ for (var _i = 0; _i < arguments.length; _i++) {
2120
+ args[_i] = arguments[_i];
2121
+ }
2122
+ listener.apply(context, args);
2123
+ _this.off(eventName, wrap);
2124
+ });
2125
+ return wrap;
2126
+ };
2127
+ EmitterPro.prototype.once = function (eventName, listener, context) {
2128
+ var wrap = this._wrapOnce(eventName, listener, context);
2129
+ return this._on(eventName, listener, wrap, context);
2130
+ };
2131
+ EmitterPro.prototype.prependOnceListener = function (eventName, listener, context) {
2132
+ var wrap = this._wrapOnce(eventName, listener, context);
2133
+ return this._on(eventName, listener, wrap, context, 0);
2134
+ };
2135
+ EmitterPro.prototype.off = function (eventName, listener) {
2136
+ var handler = this.handlers[eventName];
2137
+ if (handler) {
2138
+ if (listener) {
2139
+ var index = handler.findIndex(function (item) { return item.wrap === listener || item.raw === listener; });
2140
+ if (index !== -1) {
2141
+ handler.splice(index, 1);
2142
+ }
2430
2143
  }
2431
2144
  else {
2432
- delete item[childrenField];
2145
+ delete this.handlers[eventName];
2146
+ }
2147
+ }
2148
+ return this;
2149
+ };
2150
+ EmitterPro.prototype.offAll = function () {
2151
+ this.handlers = {};
2152
+ return this;
2153
+ };
2154
+ EmitterPro.prototype.emit = function (eventName) {
2155
+ var args = [];
2156
+ for (var _i = 1; _i < arguments.length; _i++) {
2157
+ args[_i - 1] = arguments[_i];
2158
+ }
2159
+ var handler = this.handlers[eventName];
2160
+ if (handler && handler.length > 0) {
2161
+ handler.forEach(function (listener) {
2162
+ listener.wrap.apply(listener.context, args);
2163
+ });
2164
+ return true;
2165
+ }
2166
+ return false;
2167
+ };
2168
+ return EmitterPro;
2169
+ }());
2170
+
2171
+ // 随机字符串
2172
+ function randomString() {
2173
+ return Math.random().toString(16).substring(2, 8);
2174
+ }
2175
+ // 内部自增id
2176
+ var uid = 1;
2177
+ // 返回唯一标识
2178
+ function getUniqueId() {
2179
+ return "".concat(randomString(), "_").concat(uid++);
2180
+ }
2181
+ // 是否支持 storage
2182
+ function isStorageSupported(storage) {
2183
+ try {
2184
+ var isSupport = typeof storage === 'object' &&
2185
+ storage !== null &&
2186
+ !!storage.setItem &&
2187
+ !!storage.getItem &&
2188
+ !!storage.removeItem;
2189
+ if (isSupport) {
2190
+ var key = getUniqueId();
2191
+ var value = '1';
2192
+ storage.setItem(key, value);
2193
+ if (storage.getItem(key) !== value) {
2194
+ return false;
2433
2195
  }
2196
+ storage.removeItem(key);
2434
2197
  }
2435
- else {
2436
- processEmptyChildren(item[childrenField], options);
2437
- }
2438
- });
2198
+ return isSupport;
2199
+ }
2200
+ catch (e) {
2201
+ console.error("[cache2] ".concat(storage, " is not supported. The default memory cache will be used."));
2202
+ return false;
2203
+ }
2439
2204
  }
2440
- function listToTree(list, options) {
2441
- if (options === void 0) { options = {}; }
2442
- var _a = options.keyField, keyField = _a === void 0 ? 'id' : _a, _b = options.parentField, parentField = _b === void 0 ? 'pid' : _b, _c = options.childrenField, childrenField = _c === void 0 ? 'children' : _c, _d = options.emptyChildrenValue, emptyChildrenValue = _d === void 0 ? 'none' : _d, _e = options.nodeAssign, nodeAssign = _e === void 0 ? 'spread' : _e;
2443
- var tree = [];
2444
- var record = {};
2445
- if (!isArray(list)) {
2446
- return tree;
2205
+ function parse(value, reviver) {
2206
+ try {
2207
+ return JSON.parse(value, reviver);
2447
2208
  }
2448
- list.forEach(function (item) {
2449
- if (isObject(item)) {
2450
- var newItem = nodeAssign === 'spread' ? __assign({}, item) : item;
2451
- var id = newItem[keyField];
2452
- var pid = newItem[parentField];
2453
- if (record[id]) {
2454
- newItem[childrenField] = record[id];
2455
- }
2456
- else {
2457
- newItem[childrenField] = record[id] = [];
2458
- }
2459
- if (pid) {
2460
- if (!record[pid]) {
2461
- record[pid] = [newItem];
2462
- }
2463
- else {
2464
- record[pid].push(newItem);
2465
- }
2466
- }
2467
- else {
2468
- tree.push(newItem);
2469
- }
2470
- }
2471
- });
2472
- if (emptyChildrenValue !== 'array') {
2473
- processEmptyChildren(tree, options);
2209
+ catch (e) {
2210
+ return value;
2474
2211
  }
2475
- return tree;
2476
2212
  }
2213
+ function stringify(value, replacer) {
2214
+ return JSON.stringify(value, replacer);
2215
+ }
2216
+ var inWindow = typeof window !== 'undefined' && typeof window === 'object' && window.window === window;
2477
2217
 
2478
- function treeToList(tree, childrenField) {
2479
- var list = [];
2480
- if (!isArray(tree)) {
2481
- return list;
2218
+ var cache = {};
2219
+ var MemoryStorage = /** @class */ (function () {
2220
+ function MemoryStorage(scope) {
2221
+ if (scope === void 0) { scope = 'default'; }
2222
+ this.scope = scope;
2223
+ if (!cache[this.scope]) {
2224
+ cache[this.scope] = {};
2225
+ }
2226
+ this.data = cache[this.scope];
2482
2227
  }
2483
- function recusion(arr) {
2484
- arr.forEach(function (item) {
2485
- if (isObject(item)) {
2486
- var newItem = __assign({}, item);
2487
- list.push(newItem);
2488
- if (newItem[childrenField]) {
2489
- if (isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
2490
- recusion(newItem[childrenField]);
2491
- }
2492
- delete newItem[childrenField];
2493
- }
2494
- }
2495
- else {
2496
- list.push(item);
2497
- }
2498
- });
2228
+ MemoryStorage.prototype.getItem = function (key) {
2229
+ return key in this.data ? this.data[key] : null;
2230
+ };
2231
+ MemoryStorage.prototype.setItem = function (key, value) {
2232
+ this.data[key] = value;
2233
+ };
2234
+ MemoryStorage.prototype.removeItem = function (key) {
2235
+ delete this.data[key];
2236
+ };
2237
+ MemoryStorage.prototype.clear = function () {
2238
+ cache[this.scope] = {};
2239
+ this.data = cache[this.scope];
2240
+ };
2241
+ return MemoryStorage;
2242
+ }());
2243
+
2244
+ var Storage = /** @class */ (function () {
2245
+ function Storage(storage, options) {
2246
+ if (options === void 0) { options = {}; }
2247
+ var isSupported = storage ? isStorageSupported(storage) : false;
2248
+ this.options = __assign({ needParsed: isSupported }, options);
2249
+ this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
2499
2250
  }
2500
- recusion(tree);
2501
- return list;
2502
- }
2251
+ Storage.prototype.get = function (key) {
2252
+ var data = this.storage.getItem(key);
2253
+ return this.options.needParsed ? parse(data, this.options.reviver) : data;
2254
+ };
2255
+ Storage.prototype.set = function (key, data) {
2256
+ this.storage.setItem(key, this.options.needParsed ? stringify(data, this.options.replacer) : data);
2257
+ };
2258
+ Storage.prototype.del = function (key) {
2259
+ this.storage.removeItem(key);
2260
+ };
2261
+ Storage.prototype.clear = function () {
2262
+ if (typeof this.storage.clear === 'function') {
2263
+ this.storage.clear();
2264
+ }
2265
+ };
2266
+ return Storage;
2267
+ }());
2503
2268
 
2504
- function filterTree(tree, predicate, childrenField, nodeAssign) {
2505
- if (childrenField === void 0) { childrenField = 'children'; }
2506
- if (nodeAssign === void 0) { nodeAssign = 'spread'; }
2507
- var result = [];
2508
- if (!isArray(tree)) {
2509
- return result;
2269
+ var defaultPrefix = 'cache2_'; // 命名空间缓存键前缀,默认 cache2_ 。
2270
+ var defaultNamespace = 'default';
2271
+ var Cache = /** @class */ (function (_super) {
2272
+ __extends(Cache, _super);
2273
+ function Cache(namespace, options) {
2274
+ var _this = _super.call(this) || this;
2275
+ var ns = defaultNamespace, opts;
2276
+ if (typeof namespace === 'string') {
2277
+ ns = namespace;
2278
+ }
2279
+ else if (typeof namespace === 'object') {
2280
+ opts = namespace;
2281
+ }
2282
+ if (!opts && typeof options === 'object') {
2283
+ opts = options;
2284
+ }
2285
+ _this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: defaultPrefix }, opts);
2286
+ _this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, opts));
2287
+ _this.cacheKey = (_this.options.prefix || '') + (ns || '') || getUniqueId();
2288
+ _this.startCheckperiod();
2289
+ return _this;
2510
2290
  }
2511
- tree.forEach(function (item) {
2512
- var newItem = item;
2513
- if (isObject(item)) {
2514
- newItem = nodeAssign === 'spread' ? __assign({}, item) : item;
2291
+ // 检查当前键值是否过期,如果过期将会自动删除
2292
+ Cache.prototype._check = function (key, data) {
2293
+ var ret = true;
2294
+ if (data.t !== 0 && data.t < Date.now()) {
2295
+ ret = false;
2296
+ this.del(key);
2297
+ this.emit('expired', key, data.v);
2515
2298
  }
2516
- if (predicate(newItem)) {
2517
- if (isObject(newItem)) {
2518
- var childs = newItem[childrenField];
2519
- if (isArray(childs) && childs.length > 0) {
2520
- newItem[childrenField] = filterTree(childs, predicate, childrenField, nodeAssign);
2299
+ return ret;
2300
+ };
2301
+ Cache.prototype._wrap = function (value, ttl) {
2302
+ var now = Date.now();
2303
+ var currentTtl = typeof ttl === 'number' ? ttl : this.options.stdTTL;
2304
+ var livetime = currentTtl > 0 ? now + currentTtl : 0;
2305
+ return {
2306
+ v: value,
2307
+ t: livetime,
2308
+ n: now
2309
+ };
2310
+ };
2311
+ Cache.prototype._isLimited = function (len) {
2312
+ return this.options.max > -1 && len >= this.options.max;
2313
+ };
2314
+ Cache.prototype._getReplaceKey = function (keys, cacheValues) {
2315
+ var retkey = keys[0];
2316
+ keys.forEach(function (key) {
2317
+ if (cacheValues[key].t < cacheValues[retkey].t ||
2318
+ (cacheValues[key].t === cacheValues[retkey].t && cacheValues[key].n < cacheValues[retkey].n)) {
2319
+ retkey = key;
2320
+ }
2321
+ });
2322
+ return retkey;
2323
+ };
2324
+ Object.defineProperty(Cache.prototype, "cacheValues", {
2325
+ // 获取全部缓存数据,不处理过期数据和排序
2326
+ get: function () {
2327
+ return this.storage.get(this.cacheKey) || {};
2328
+ },
2329
+ enumerable: false,
2330
+ configurable: true
2331
+ });
2332
+ // 设置缓存数据
2333
+ Cache.prototype.setCacheValues = function (values) {
2334
+ this.storage.set(this.cacheKey, values);
2335
+ };
2336
+ // 从缓存中获取保存的值。如果未找到或已过期,则返回 undefined 。如果找到该值,则返回该值。
2337
+ Cache.prototype.get = function (key) {
2338
+ var data = this.cacheValues[key];
2339
+ if (data && this._check(key, data)) {
2340
+ return data.v;
2341
+ }
2342
+ return;
2343
+ };
2344
+ // 从缓存中获取多个保存的值。如果未找到或已过期,则返回一个空对象。如果找到该值,它会返回一个具有键值对的对象。
2345
+ Cache.prototype.mget = function (keys) {
2346
+ var _this = this;
2347
+ var ret = {};
2348
+ if (!Array.isArray(keys)) {
2349
+ return ret;
2350
+ }
2351
+ var cacheValues = this.cacheValues;
2352
+ keys.forEach(function (key) {
2353
+ var data = cacheValues[key];
2354
+ if (data && _this._check(key, data)) {
2355
+ ret[key] = data.v;
2356
+ }
2357
+ });
2358
+ return ret;
2359
+ };
2360
+ // 从缓存中获取全部保存的值。返回一个具有键值对的对象。
2361
+ Cache.prototype.getAll = function () {
2362
+ var keys = Object.keys(this.cacheValues);
2363
+ return this.mget(keys);
2364
+ };
2365
+ // 设置键值对。设置成功返回 true 。
2366
+ Cache.prototype.set = function (key, value, ttl) {
2367
+ if (this.options.max === 0) {
2368
+ return false;
2369
+ }
2370
+ var cacheValues = this.cacheValues;
2371
+ var keys = Object.keys(cacheValues);
2372
+ // 当前不存在该键值,并且数据量超过最大限制
2373
+ if (!cacheValues[key] && this._isLimited(keys.length)) {
2374
+ var validKeys = this.keys();
2375
+ if (this._isLimited(validKeys.length)) {
2376
+ // 如果最大限制策略是替换,将优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理。
2377
+ if (this.options.maxStrategy === 'replaced') {
2378
+ var replaceKey = this._getReplaceKey(validKeys, cacheValues);
2379
+ this.del(replaceKey);
2380
+ }
2381
+ else {
2382
+ // 如果是最大限制策略是不允许添加,返回 false 。
2383
+ return false;
2521
2384
  }
2522
2385
  }
2523
- result.push(newItem);
2524
2386
  }
2525
- });
2526
- return result;
2527
- }
2528
-
2529
- function findTreeNode(tree, predicate, childrenField) {
2530
- var e_1, _a;
2531
- if (childrenField === void 0) { childrenField = 'children'; }
2532
- var stack = [];
2533
- var node;
2534
- if (!isArray(tree)) {
2535
- return node;
2536
- }
2537
- try {
2538
- for (var tree_1 = __values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
2539
- var item = tree_1_1.value;
2540
- stack.push(item);
2541
- while (stack.length) {
2542
- var temp = stack.pop();
2543
- if (predicate(temp)) {
2544
- node = temp;
2545
- break;
2546
- }
2547
- if (isObject(temp)) {
2548
- var childs = temp[childrenField];
2549
- if (isArray(childs) && childs.length > 0) {
2550
- childs.forEach(function (c) {
2551
- stack.push(c);
2552
- });
2553
- }
2554
- }
2387
+ cacheValues[key] = this._wrap(value, ttl);
2388
+ this.setCacheValues(cacheValues);
2389
+ this.emit('set', key, cacheValues[key].v);
2390
+ return true;
2391
+ };
2392
+ // 设置多个键值对。全部设置成功返回 true 。
2393
+ Cache.prototype.mset = function (keyValueSet) {
2394
+ var _this = this;
2395
+ // 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
2396
+ var ret = true;
2397
+ keyValueSet.forEach(function (item) {
2398
+ var itemSetResult = _this.set(item.key, item.value, item.ttl);
2399
+ if (ret && !itemSetResult) {
2400
+ ret = false;
2555
2401
  }
2556
- if (node) {
2557
- break;
2402
+ });
2403
+ return ret;
2404
+ };
2405
+ // 删除一个或多个键。返回已删除条目的数量。删除永远不会失败。
2406
+ Cache.prototype.del = function (key) {
2407
+ var _this = this;
2408
+ var cacheValues = this.cacheValues;
2409
+ var count = 0;
2410
+ var keys = Array.isArray(key) ? key : [key];
2411
+ keys.forEach(function (key) {
2412
+ if (cacheValues[key]) {
2413
+ count++;
2414
+ var oldData = cacheValues[key];
2415
+ delete cacheValues[key];
2416
+ _this.emit('del', key, oldData.v);
2558
2417
  }
2418
+ });
2419
+ if (count > 0) {
2420
+ this.setCacheValues(cacheValues);
2559
2421
  }
2560
- }
2561
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2562
- finally {
2563
- try {
2564
- if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
2422
+ return count;
2423
+ };
2424
+ // 删除当前所有缓存。
2425
+ Cache.prototype.clear = function () {
2426
+ this.storage.del(this.cacheKey);
2427
+ };
2428
+ // 返回所有现有键的数组。
2429
+ Cache.prototype.keys = function () {
2430
+ var _this = this;
2431
+ var cacheValues = this.cacheValues;
2432
+ var keys = Object.keys(cacheValues);
2433
+ return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
2434
+ };
2435
+ // 当前缓存是否包含某个键。
2436
+ Cache.prototype.has = function (key) {
2437
+ var data = this.cacheValues[key];
2438
+ return !!(data && this._check(key, data));
2439
+ };
2440
+ // 获取缓存值并从缓存中删除键。
2441
+ Cache.prototype.take = function (key) {
2442
+ var ret;
2443
+ var data = this.cacheValues[key];
2444
+ if (data && this._check(key, data)) {
2445
+ ret = data.v;
2446
+ this.del(key);
2565
2447
  }
2566
- finally { if (e_1) throw e_1.error; }
2567
- }
2568
- return node;
2569
- }
2570
-
2571
- function findTreeNodes(tree, predicate, childrenField) {
2572
- var e_1, _a;
2573
- if (childrenField === void 0) { childrenField = 'children'; }
2574
- var stack = [];
2575
- var nodes = [];
2576
- if (!isArray(tree)) {
2577
- return nodes;
2578
- }
2579
- try {
2580
- for (var tree_1 = __values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
2581
- var item = tree_1_1.value;
2582
- stack.push(item);
2583
- while (stack.length) {
2584
- var temp = stack.pop();
2585
- if (predicate(temp)) {
2586
- nodes.push(temp);
2587
- }
2588
- if (isObject(temp)) {
2589
- var childs = temp[childrenField];
2590
- if (isArray(childs) && childs.length > 0) {
2591
- childs.forEach(function (c) {
2592
- stack.push(c);
2593
- });
2594
- }
2595
- }
2596
- }
2448
+ return ret;
2449
+ };
2450
+ // 重新定义一个键的 ttl 。如果找到并更新成功,则返回 true 。
2451
+ Cache.prototype.ttl = function (key, ttl) {
2452
+ var cacheValues = this.cacheValues;
2453
+ var data = cacheValues[key];
2454
+ if (data && this._check(key, data)) {
2455
+ cacheValues[key] = this._wrap(data.v, ttl);
2456
+ return true;
2597
2457
  }
2598
- }
2599
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2600
- finally {
2601
- try {
2602
- if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
2458
+ return false;
2459
+ };
2460
+ // 获取某个键的 ttl 。
2461
+ // 如果未找到键或已过期,返回 undefined 。
2462
+ // 如果 ttl 0 ,返回 0
2463
+ // 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
2464
+ Cache.prototype.getTtl = function (key) {
2465
+ var cacheValues = this.cacheValues;
2466
+ var data = cacheValues[key];
2467
+ if (data && this._check(key, data)) {
2468
+ return cacheValues[key].t;
2603
2469
  }
2604
- finally { if (e_1) throw e_1.error; }
2605
- }
2606
- return nodes;
2607
- }
2470
+ return;
2471
+ };
2472
+ // 获取某个键值的最后修改时间
2473
+ // 如果未找到键或已过期,返回 undefined 。
2474
+ // 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
2475
+ Cache.prototype.getLastModified = function (key) {
2476
+ var cacheValues = this.cacheValues;
2477
+ var data = cacheValues[key];
2478
+ if (data && this._check(key, data)) {
2479
+ return cacheValues[key].n;
2480
+ }
2481
+ return;
2482
+ };
2483
+ // 启动定时校验过期数据
2484
+ Cache.prototype.startCheckperiod = function () {
2485
+ var _this = this;
2486
+ // 触发全部缓存数据是否过期校验
2487
+ this.keys();
2488
+ if (this.options.checkperiod > 0) {
2489
+ clearTimeout(this._checkTimeout);
2490
+ this._checkTimeout = setTimeout(function () {
2491
+ _this.startCheckperiod();
2492
+ }, this.options.checkperiod);
2493
+ }
2494
+ };
2495
+ // 停止定时校验过期数据
2496
+ Cache.prototype.stopCheckperiod = function () {
2497
+ clearTimeout(this._checkTimeout);
2498
+ };
2499
+ return Cache;
2500
+ }(EmitterPro));
2608
2501
 
2609
- function internalFindTreeSelect(tree, predicate, childrenField, path) {
2610
- var e_1, _a;
2611
- if (path === void 0) { path = []; }
2612
- if (!isArray(tree)) {
2613
- return [];
2502
+ new Storage(inWindow ? window.localStorage : undefined);
2503
+
2504
+ new Storage(inWindow ? window.sessionStorage : undefined);
2505
+
2506
+ var AsyncMemo = (function () {
2507
+ function AsyncMemo(options) {
2508
+ this.promiseCache = {};
2509
+ this.cache = new Cache(uniqueId('uh_async_memo'), options);
2614
2510
  }
2615
- try {
2616
- for (var tree_1 = __values(tree), tree_1_1 = tree_1.next(); !tree_1_1.done; tree_1_1 = tree_1.next()) {
2617
- var item = tree_1_1.value;
2618
- path.push(item);
2619
- if (predicate(item)) {
2620
- return path;
2621
- }
2622
- if (isObject(item)) {
2623
- var childs = item[childrenField];
2624
- if (isArray(childs) && childs.length > 0) {
2625
- var findChildren = internalFindTreeSelect(childs, predicate, childrenField, path);
2626
- if (findChildren.length > 0) {
2627
- return findChildren;
2628
- }
2629
- }
2511
+ AsyncMemo.prototype.run = function (asyncFn, key, options) {
2512
+ var _this = this;
2513
+ if (!key || !isString(key)) {
2514
+ return asyncFn();
2515
+ }
2516
+ var opts = __assign({ persisted: true }, options);
2517
+ if (opts.persisted) {
2518
+ var data = this.cache.get(key);
2519
+ if (data) {
2520
+ return Promise.resolve(data);
2630
2521
  }
2631
- path.pop();
2632
2522
  }
2633
- }
2634
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
2635
- finally {
2636
- try {
2637
- if (tree_1_1 && !tree_1_1.done && (_a = tree_1.return)) _a.call(tree_1);
2523
+ if (!this.promiseCache[key]) {
2524
+ this.promiseCache[key] = asyncFn()
2525
+ .then(function (res) {
2526
+ delete _this.promiseCache[key];
2527
+ _this.cache.set(key, res, opts.ttl);
2528
+ return res;
2529
+ })
2530
+ .catch(function (err) {
2531
+ delete _this.promiseCache[key];
2532
+ return Promise.reject(err);
2533
+ });
2638
2534
  }
2639
- finally { if (e_1) throw e_1.error; }
2640
- }
2641
- return [];
2642
- }
2643
- function findTreeSelect(tree, predicate, childrenField) {
2644
- if (childrenField === void 0) { childrenField = 'children'; }
2645
- return internalFindTreeSelect(tree, predicate, childrenField);
2646
- }
2647
-
2648
- var VERSION = "4.23.1";
2649
-
2650
- var version = "4.23.1";
2535
+ return this.promiseCache[key];
2536
+ };
2537
+ return AsyncMemo;
2538
+ }());
2651
2539
 
2652
2540
  exports.AsyncMemo = AsyncMemo;
2653
2541
  exports.VERSION = VERSION;
2654
2542
  exports.ajax = ajax;
2655
- exports.blobToDataURL = blobToDataURL;
2656
2543
  exports.bytesToSize = bytesToSize;
2657
2544
  exports.calculateCursorPosition = calculateCursorPosition;
2658
2545
  exports.compressImage = compressImage;
@@ -2682,7 +2569,6 @@
2682
2569
  exports.isPassport = isPassport;
2683
2570
  exports.isPassword = isPassword;
2684
2571
  exports.isPostcode = isPostcode;
2685
- exports.isPromiseLike = isPromiseLike;
2686
2572
  exports.isQQ = isQQ;
2687
2573
  exports.isSocialCreditCode = isSocialCreditCode;
2688
2574
  exports.isSwiftCode = isSwiftCode;
@@ -2698,12 +2584,11 @@
2698
2584
  exports.loadImageWithBlob = loadImageWithBlob;
2699
2585
  exports.loadScript = loadScript;
2700
2586
  exports.minus = minus;
2701
- exports.normalizeString = normalizeString;
2702
2587
  exports.numberToChinese = numberToChinese;
2703
2588
  exports.padZero = padZero;
2704
2589
  exports.parseIdCard = parseIdCard;
2705
2590
  exports.plus = plus;
2706
- exports.randomString = randomString;
2591
+ exports.randomString = randomString$1;
2707
2592
  exports.replaceChar = replaceChar;
2708
2593
  exports.round = _round;
2709
2594
  exports.safeDate = safeDate;
@@ -2715,8 +2600,6 @@
2715
2600
  exports.transformObjectValue = transformObjectValue;
2716
2601
  exports.treeToList = treeToList;
2717
2602
  exports.validatePassword = validatePassword;
2718
- exports.version = version;
2719
- exports.waitTime = waitTime;
2720
2603
 
2721
2604
  }));
2722
2605
  //# sourceMappingURL=util-helpers.js.map