util-helpers 4.23.1 → 5.0.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.
- package/dist/util-helpers.js +836 -958
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/VERSION.js +1 -1
- package/esm/compressImage.js +2 -2
- package/esm/formatBankCard.js +3 -5
- package/esm/formatMobile.js +4 -6
- package/esm/getImageInfo.js +16 -43
- package/esm/index.js +0 -8
- package/esm/isBusinessLicense.js +1 -2
- package/esm/isSocialCreditCode.js +1 -3
- package/esm/loadImage.js +20 -45
- package/esm/loadImageWithBlob.js +19 -46
- package/lib/VERSION.js +1 -1
- package/lib/compressImage.js +2 -2
- package/lib/formatBankCard.js +3 -5
- package/lib/formatMobile.js +4 -6
- package/lib/getImageInfo.js +15 -42
- package/lib/index.js +1 -9
- package/lib/isBusinessLicense.js +1 -2
- package/lib/isSocialCreditCode.js +1 -3
- package/lib/loadImage.js +18 -43
- package/lib/loadImageWithBlob.js +18 -45
- package/package.json +2 -2
- package/types/compressImage.d.ts +2 -4
- package/types/formatBankCard.d.ts +1 -3
- package/types/formatMobile.d.ts +0 -2
- package/types/getImageInfo.d.ts +10 -12
- package/types/index.d.ts +0 -8
- package/types/isBusinessLicense.d.ts +0 -2
- package/types/isSocialCreditCode.d.ts +0 -2
- package/types/loadImage.d.ts +1 -12
- package/types/loadImageWithBlob.d.ts +6 -10
- package/esm/blobToDataURL.js +0 -7
- package/esm/isPromiseLike.js +0 -7
- package/esm/normalizeString.js +0 -7
- package/esm/waitTime.js +0 -8
- package/lib/blobToDataURL.js +0 -9
- package/lib/isPromiseLike.js +0 -9
- package/lib/normalizeString.js +0 -9
- package/lib/waitTime.js +0 -10
- package/types/blobToDataURL.d.ts +0 -29
- package/types/isPromiseLike.d.ts +0 -19
- package/types/normalizeString.d.ts +0 -26
- package/types/utils/Cache.d.ts +0 -14
- package/types/waitTime.d.ts +0 -24
package/dist/util-helpers.js
CHANGED
|
@@ -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
|
|
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
|
|
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.
|
|
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
|
-
|
|
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(
|
|
942
|
+
var regChar = new RegExp("".concat(spaceMark), 'g');
|
|
964
943
|
var realValue = toString(bankCardNo).replace(regChar, '');
|
|
965
|
-
var str = realValue.replace(reg, "$1".concat(
|
|
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
|
-
|
|
1248
|
-
var
|
|
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(
|
|
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(
|
|
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
|
-
|
|
1549
|
-
function
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
};
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
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
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
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
|
-
|
|
1573
|
+
ctx.fillStyle = background;
|
|
1574
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
1620
1575
|
}
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
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
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
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
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
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
|
|
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
|
|
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,
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
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
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
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
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
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') {
|
|
@@ -2359,300 +1788,753 @@
|
|
|
2359
1788
|
else {
|
|
2360
1789
|
_pool = pool;
|
|
2361
1790
|
}
|
|
2362
|
-
return internalRandomString(toNumber(len), _pool);
|
|
2363
|
-
};
|
|
2364
|
-
|
|
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++;
|
|
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
|
+
}
|
|
2372
1983
|
}
|
|
2373
|
-
|
|
2374
|
-
|
|
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);
|
|
2375
1989
|
}
|
|
1990
|
+
finally { if (e_1) throw e_1.error; }
|
|
2376
1991
|
}
|
|
2377
|
-
return
|
|
1992
|
+
return node;
|
|
2378
1993
|
}
|
|
2379
1994
|
|
|
2380
|
-
function
|
|
2381
|
-
|
|
2382
|
-
|
|
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
|
+
}
|
|
2021
|
+
}
|
|
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);
|
|
2027
|
+
}
|
|
2028
|
+
finally { if (e_1) throw e_1.error; }
|
|
2029
|
+
}
|
|
2030
|
+
return nodes;
|
|
2383
2031
|
}
|
|
2384
2032
|
|
|
2385
|
-
function
|
|
2386
|
-
|
|
2387
|
-
if (
|
|
2388
|
-
|
|
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
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
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
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
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
|
-
|
|
2419
|
-
}
|
|
2055
|
+
path.pop();
|
|
2056
|
+
}
|
|
2420
2057
|
}
|
|
2421
|
-
|
|
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
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2072
|
+
var VERSION = "5.0.1";
|
|
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
|
|
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
|
-
|
|
2436
|
-
|
|
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
|
|
2441
|
-
|
|
2442
|
-
|
|
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
|
-
|
|
2449
|
-
|
|
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
|
-
|
|
2212
|
+
}
|
|
2213
|
+
function stringify(value, replacer) {
|
|
2214
|
+
return JSON.stringify(value, replacer);
|
|
2476
2215
|
}
|
|
2477
2216
|
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2217
|
+
var cache = {};
|
|
2218
|
+
var MemoryStorage = /** @class */ (function () {
|
|
2219
|
+
function MemoryStorage(scope) {
|
|
2220
|
+
if (scope === void 0) { scope = 'default'; }
|
|
2221
|
+
this.scope = scope;
|
|
2222
|
+
if (!cache[this.scope]) {
|
|
2223
|
+
cache[this.scope] = {};
|
|
2224
|
+
}
|
|
2225
|
+
this.data = cache[this.scope];
|
|
2482
2226
|
}
|
|
2483
|
-
function
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2227
|
+
MemoryStorage.prototype.getItem = function (key) {
|
|
2228
|
+
return key in this.data ? this.data[key] : null;
|
|
2229
|
+
};
|
|
2230
|
+
MemoryStorage.prototype.setItem = function (key, value) {
|
|
2231
|
+
this.data[key] = value;
|
|
2232
|
+
};
|
|
2233
|
+
MemoryStorage.prototype.removeItem = function (key) {
|
|
2234
|
+
delete this.data[key];
|
|
2235
|
+
};
|
|
2236
|
+
MemoryStorage.prototype.clear = function () {
|
|
2237
|
+
cache[this.scope] = {};
|
|
2238
|
+
this.data = cache[this.scope];
|
|
2239
|
+
};
|
|
2240
|
+
return MemoryStorage;
|
|
2241
|
+
}());
|
|
2242
|
+
|
|
2243
|
+
var Storage = /** @class */ (function () {
|
|
2244
|
+
function Storage(storage, options) {
|
|
2245
|
+
if (options === void 0) { options = {}; }
|
|
2246
|
+
var isSupported = storage ? isStorageSupported(storage) : false;
|
|
2247
|
+
this.options = __assign({ needParsed: isSupported }, options);
|
|
2248
|
+
this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
|
|
2499
2249
|
}
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2250
|
+
Storage.prototype.get = function (key) {
|
|
2251
|
+
var data = this.storage.getItem(key);
|
|
2252
|
+
return this.options.needParsed ? parse(data, this.options.reviver) : data;
|
|
2253
|
+
};
|
|
2254
|
+
Storage.prototype.set = function (key, data) {
|
|
2255
|
+
this.storage.setItem(key, this.options.needParsed ? stringify(data, this.options.replacer) : data);
|
|
2256
|
+
};
|
|
2257
|
+
Storage.prototype.del = function (key) {
|
|
2258
|
+
this.storage.removeItem(key);
|
|
2259
|
+
};
|
|
2260
|
+
Storage.prototype.clear = function () {
|
|
2261
|
+
if (typeof this.storage.clear === 'function') {
|
|
2262
|
+
this.storage.clear();
|
|
2263
|
+
}
|
|
2264
|
+
};
|
|
2265
|
+
return Storage;
|
|
2266
|
+
}());
|
|
2503
2267
|
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2268
|
+
var defaultPrefix = 'cache2_'; // 命名空间缓存键前缀,默认 cache2_ 。
|
|
2269
|
+
var defaultNamespace = 'default';
|
|
2270
|
+
var Cache = /** @class */ (function (_super) {
|
|
2271
|
+
__extends(Cache, _super);
|
|
2272
|
+
function Cache(namespace, options) {
|
|
2273
|
+
var _this = _super.call(this) || this;
|
|
2274
|
+
var ns = defaultNamespace, opts;
|
|
2275
|
+
if (typeof namespace === 'string') {
|
|
2276
|
+
ns = namespace;
|
|
2277
|
+
}
|
|
2278
|
+
else if (typeof namespace === 'object') {
|
|
2279
|
+
opts = namespace;
|
|
2280
|
+
}
|
|
2281
|
+
if (!opts && typeof options === 'object') {
|
|
2282
|
+
opts = options;
|
|
2283
|
+
}
|
|
2284
|
+
_this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: defaultPrefix }, opts);
|
|
2285
|
+
_this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, opts));
|
|
2286
|
+
_this.cacheKey = (_this.options.prefix || '') + (ns || '') || getUniqueId();
|
|
2287
|
+
_this.startCheckperiod();
|
|
2288
|
+
return _this;
|
|
2510
2289
|
}
|
|
2511
|
-
|
|
2512
|
-
|
|
2513
|
-
|
|
2514
|
-
|
|
2290
|
+
// 检查当前键值是否过期,如果过期将会自动删除
|
|
2291
|
+
Cache.prototype._check = function (key, data) {
|
|
2292
|
+
var ret = true;
|
|
2293
|
+
if (data.t !== 0 && data.t < Date.now()) {
|
|
2294
|
+
ret = false;
|
|
2295
|
+
this.del(key);
|
|
2296
|
+
this.emit('expired', key, data.v);
|
|
2515
2297
|
}
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2298
|
+
return ret;
|
|
2299
|
+
};
|
|
2300
|
+
Cache.prototype._wrap = function (value, ttl) {
|
|
2301
|
+
var now = Date.now();
|
|
2302
|
+
var currentTtl = typeof ttl === 'number' ? ttl : this.options.stdTTL;
|
|
2303
|
+
var livetime = currentTtl > 0 ? now + currentTtl : 0;
|
|
2304
|
+
return {
|
|
2305
|
+
v: value,
|
|
2306
|
+
t: livetime,
|
|
2307
|
+
n: now
|
|
2308
|
+
};
|
|
2309
|
+
};
|
|
2310
|
+
Cache.prototype._isLimited = function (len) {
|
|
2311
|
+
return this.options.max > -1 && len >= this.options.max;
|
|
2312
|
+
};
|
|
2313
|
+
Cache.prototype._getReplaceKey = function (keys, cacheValues) {
|
|
2314
|
+
var retkey = keys[0];
|
|
2315
|
+
keys.forEach(function (key) {
|
|
2316
|
+
if (cacheValues[key].t < cacheValues[retkey].t ||
|
|
2317
|
+
(cacheValues[key].t === cacheValues[retkey].t && cacheValues[key].n < cacheValues[retkey].n)) {
|
|
2318
|
+
retkey = key;
|
|
2319
|
+
}
|
|
2320
|
+
});
|
|
2321
|
+
return retkey;
|
|
2322
|
+
};
|
|
2323
|
+
Object.defineProperty(Cache.prototype, "cacheValues", {
|
|
2324
|
+
// 获取全部缓存数据,不处理过期数据和排序
|
|
2325
|
+
get: function () {
|
|
2326
|
+
return this.storage.get(this.cacheKey) || {};
|
|
2327
|
+
},
|
|
2328
|
+
enumerable: false,
|
|
2329
|
+
configurable: true
|
|
2330
|
+
});
|
|
2331
|
+
// 设置缓存数据
|
|
2332
|
+
Cache.prototype.setCacheValues = function (values) {
|
|
2333
|
+
this.storage.set(this.cacheKey, values);
|
|
2334
|
+
};
|
|
2335
|
+
// 从缓存中获取保存的值。如果未找到或已过期,则返回 undefined 。如果找到该值,则返回该值。
|
|
2336
|
+
Cache.prototype.get = function (key) {
|
|
2337
|
+
var data = this.cacheValues[key];
|
|
2338
|
+
if (data && this._check(key, data)) {
|
|
2339
|
+
return data.v;
|
|
2340
|
+
}
|
|
2341
|
+
return;
|
|
2342
|
+
};
|
|
2343
|
+
// 从缓存中获取多个保存的值。如果未找到或已过期,则返回一个空对象。如果找到该值,它会返回一个具有键值对的对象。
|
|
2344
|
+
Cache.prototype.mget = function (keys) {
|
|
2345
|
+
var _this = this;
|
|
2346
|
+
var ret = {};
|
|
2347
|
+
if (!Array.isArray(keys)) {
|
|
2348
|
+
return ret;
|
|
2349
|
+
}
|
|
2350
|
+
var cacheValues = this.cacheValues;
|
|
2351
|
+
keys.forEach(function (key) {
|
|
2352
|
+
var data = cacheValues[key];
|
|
2353
|
+
if (data && _this._check(key, data)) {
|
|
2354
|
+
ret[key] = data.v;
|
|
2355
|
+
}
|
|
2356
|
+
});
|
|
2357
|
+
return ret;
|
|
2358
|
+
};
|
|
2359
|
+
// 从缓存中获取全部保存的值。返回一个具有键值对的对象。
|
|
2360
|
+
Cache.prototype.getAll = function () {
|
|
2361
|
+
var keys = Object.keys(this.cacheValues);
|
|
2362
|
+
return this.mget(keys);
|
|
2363
|
+
};
|
|
2364
|
+
// 设置键值对。设置成功返回 true 。
|
|
2365
|
+
Cache.prototype.set = function (key, value, ttl) {
|
|
2366
|
+
if (this.options.max === 0) {
|
|
2367
|
+
return false;
|
|
2368
|
+
}
|
|
2369
|
+
var cacheValues = this.cacheValues;
|
|
2370
|
+
var keys = Object.keys(cacheValues);
|
|
2371
|
+
// 当前不存在该键值,并且数据量超过最大限制
|
|
2372
|
+
if (!cacheValues[key] && this._isLimited(keys.length)) {
|
|
2373
|
+
var validKeys = this.keys();
|
|
2374
|
+
if (this._isLimited(validKeys.length)) {
|
|
2375
|
+
// 如果最大限制策略是替换,将优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理。
|
|
2376
|
+
if (this.options.maxStrategy === 'replaced') {
|
|
2377
|
+
var replaceKey = this._getReplaceKey(validKeys, cacheValues);
|
|
2378
|
+
this.del(replaceKey);
|
|
2379
|
+
}
|
|
2380
|
+
else {
|
|
2381
|
+
// 如果是最大限制策略是不允许添加,返回 false 。
|
|
2382
|
+
return false;
|
|
2521
2383
|
}
|
|
2522
2384
|
}
|
|
2523
|
-
result.push(newItem);
|
|
2524
2385
|
}
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
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
|
-
}
|
|
2386
|
+
cacheValues[key] = this._wrap(value, ttl);
|
|
2387
|
+
this.setCacheValues(cacheValues);
|
|
2388
|
+
this.emit('set', key, cacheValues[key].v);
|
|
2389
|
+
return true;
|
|
2390
|
+
};
|
|
2391
|
+
// 设置多个键值对。全部设置成功返回 true 。
|
|
2392
|
+
Cache.prototype.mset = function (keyValueSet) {
|
|
2393
|
+
var _this = this;
|
|
2394
|
+
// 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
|
|
2395
|
+
var ret = true;
|
|
2396
|
+
keyValueSet.forEach(function (item) {
|
|
2397
|
+
var itemSetResult = _this.set(item.key, item.value, item.ttl);
|
|
2398
|
+
if (ret && !itemSetResult) {
|
|
2399
|
+
ret = false;
|
|
2555
2400
|
}
|
|
2556
|
-
|
|
2557
|
-
|
|
2401
|
+
});
|
|
2402
|
+
return ret;
|
|
2403
|
+
};
|
|
2404
|
+
// 删除一个或多个键。返回已删除条目的数量。删除永远不会失败。
|
|
2405
|
+
Cache.prototype.del = function (key) {
|
|
2406
|
+
var _this = this;
|
|
2407
|
+
var cacheValues = this.cacheValues;
|
|
2408
|
+
var count = 0;
|
|
2409
|
+
var keys = Array.isArray(key) ? key : [key];
|
|
2410
|
+
keys.forEach(function (key) {
|
|
2411
|
+
if (cacheValues[key]) {
|
|
2412
|
+
count++;
|
|
2413
|
+
var oldData = cacheValues[key];
|
|
2414
|
+
delete cacheValues[key];
|
|
2415
|
+
_this.emit('del', key, oldData.v);
|
|
2558
2416
|
}
|
|
2417
|
+
});
|
|
2418
|
+
if (count > 0) {
|
|
2419
|
+
this.setCacheValues(cacheValues);
|
|
2559
2420
|
}
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2421
|
+
return count;
|
|
2422
|
+
};
|
|
2423
|
+
// 删除当前所有缓存。
|
|
2424
|
+
Cache.prototype.clear = function () {
|
|
2425
|
+
this.storage.del(this.cacheKey);
|
|
2426
|
+
};
|
|
2427
|
+
// 返回所有现有键的数组。
|
|
2428
|
+
Cache.prototype.keys = function () {
|
|
2429
|
+
var _this = this;
|
|
2430
|
+
var cacheValues = this.cacheValues;
|
|
2431
|
+
var keys = Object.keys(cacheValues);
|
|
2432
|
+
return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
|
|
2433
|
+
};
|
|
2434
|
+
// 当前缓存是否包含某个键。
|
|
2435
|
+
Cache.prototype.has = function (key) {
|
|
2436
|
+
var data = this.cacheValues[key];
|
|
2437
|
+
return !!(data && this._check(key, data));
|
|
2438
|
+
};
|
|
2439
|
+
// 获取缓存值并从缓存中删除键。
|
|
2440
|
+
Cache.prototype.take = function (key) {
|
|
2441
|
+
var ret;
|
|
2442
|
+
var data = this.cacheValues[key];
|
|
2443
|
+
if (data && this._check(key, data)) {
|
|
2444
|
+
ret = data.v;
|
|
2445
|
+
this.del(key);
|
|
2565
2446
|
}
|
|
2566
|
-
|
|
2567
|
-
}
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
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
|
-
}
|
|
2447
|
+
return ret;
|
|
2448
|
+
};
|
|
2449
|
+
// 重新定义一个键的 ttl 。如果找到并更新成功,则返回 true 。
|
|
2450
|
+
Cache.prototype.ttl = function (key, ttl) {
|
|
2451
|
+
var cacheValues = this.cacheValues;
|
|
2452
|
+
var data = cacheValues[key];
|
|
2453
|
+
if (data && this._check(key, data)) {
|
|
2454
|
+
cacheValues[key] = this._wrap(data.v, ttl);
|
|
2455
|
+
return true;
|
|
2597
2456
|
}
|
|
2598
|
-
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2457
|
+
return false;
|
|
2458
|
+
};
|
|
2459
|
+
// 获取某个键的 ttl 。
|
|
2460
|
+
// 如果未找到键或已过期,返回 undefined 。
|
|
2461
|
+
// 如果 ttl 为 0 ,返回 0 。
|
|
2462
|
+
// 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
|
|
2463
|
+
Cache.prototype.getTtl = function (key) {
|
|
2464
|
+
var cacheValues = this.cacheValues;
|
|
2465
|
+
var data = cacheValues[key];
|
|
2466
|
+
if (data && this._check(key, data)) {
|
|
2467
|
+
return cacheValues[key].t;
|
|
2603
2468
|
}
|
|
2604
|
-
|
|
2605
|
-
}
|
|
2606
|
-
|
|
2607
|
-
|
|
2469
|
+
return;
|
|
2470
|
+
};
|
|
2471
|
+
// 获取某个键值的最后修改时间
|
|
2472
|
+
// 如果未找到键或已过期,返回 undefined 。
|
|
2473
|
+
// 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
|
|
2474
|
+
Cache.prototype.getLastModified = function (key) {
|
|
2475
|
+
var cacheValues = this.cacheValues;
|
|
2476
|
+
var data = cacheValues[key];
|
|
2477
|
+
if (data && this._check(key, data)) {
|
|
2478
|
+
return cacheValues[key].n;
|
|
2479
|
+
}
|
|
2480
|
+
return;
|
|
2481
|
+
};
|
|
2482
|
+
// 启动定时校验过期数据
|
|
2483
|
+
Cache.prototype.startCheckperiod = function () {
|
|
2484
|
+
var _this = this;
|
|
2485
|
+
// 触发全部缓存数据是否过期校验
|
|
2486
|
+
this.keys();
|
|
2487
|
+
if (this.options.checkperiod > 0) {
|
|
2488
|
+
clearTimeout(this._checkTimeout);
|
|
2489
|
+
this._checkTimeout = setTimeout(function () {
|
|
2490
|
+
_this.startCheckperiod();
|
|
2491
|
+
}, this.options.checkperiod);
|
|
2492
|
+
}
|
|
2493
|
+
};
|
|
2494
|
+
// 停止定时校验过期数据
|
|
2495
|
+
Cache.prototype.stopCheckperiod = function () {
|
|
2496
|
+
clearTimeout(this._checkTimeout);
|
|
2497
|
+
};
|
|
2498
|
+
return Cache;
|
|
2499
|
+
}(EmitterPro));
|
|
2608
2500
|
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
return [];
|
|
2501
|
+
var AsyncMemo = (function () {
|
|
2502
|
+
function AsyncMemo(options) {
|
|
2503
|
+
this.promiseCache = {};
|
|
2504
|
+
this.cache = new Cache(uniqueId('uh_async_memo'), options);
|
|
2614
2505
|
}
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
var findChildren = internalFindTreeSelect(childs, predicate, childrenField, path);
|
|
2626
|
-
if (findChildren.length > 0) {
|
|
2627
|
-
return findChildren;
|
|
2628
|
-
}
|
|
2629
|
-
}
|
|
2506
|
+
AsyncMemo.prototype.run = function (asyncFn, key, options) {
|
|
2507
|
+
var _this = this;
|
|
2508
|
+
if (!key || !isString(key)) {
|
|
2509
|
+
return asyncFn();
|
|
2510
|
+
}
|
|
2511
|
+
var opts = __assign({ persisted: true }, options);
|
|
2512
|
+
if (opts.persisted) {
|
|
2513
|
+
var data = this.cache.get(key);
|
|
2514
|
+
if (data) {
|
|
2515
|
+
return Promise.resolve(data);
|
|
2630
2516
|
}
|
|
2631
|
-
path.pop();
|
|
2632
2517
|
}
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2518
|
+
if (!this.promiseCache[key]) {
|
|
2519
|
+
this.promiseCache[key] = asyncFn()
|
|
2520
|
+
.then(function (res) {
|
|
2521
|
+
delete _this.promiseCache[key];
|
|
2522
|
+
_this.cache.set(key, res, opts.ttl);
|
|
2523
|
+
return res;
|
|
2524
|
+
})
|
|
2525
|
+
.catch(function (err) {
|
|
2526
|
+
delete _this.promiseCache[key];
|
|
2527
|
+
return Promise.reject(err);
|
|
2528
|
+
});
|
|
2638
2529
|
}
|
|
2639
|
-
|
|
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";
|
|
2530
|
+
return this.promiseCache[key];
|
|
2531
|
+
};
|
|
2532
|
+
return AsyncMemo;
|
|
2533
|
+
}());
|
|
2651
2534
|
|
|
2652
2535
|
exports.AsyncMemo = AsyncMemo;
|
|
2653
2536
|
exports.VERSION = VERSION;
|
|
2654
2537
|
exports.ajax = ajax;
|
|
2655
|
-
exports.blobToDataURL = blobToDataURL;
|
|
2656
2538
|
exports.bytesToSize = bytesToSize;
|
|
2657
2539
|
exports.calculateCursorPosition = calculateCursorPosition;
|
|
2658
2540
|
exports.compressImage = compressImage;
|
|
@@ -2682,7 +2564,6 @@
|
|
|
2682
2564
|
exports.isPassport = isPassport;
|
|
2683
2565
|
exports.isPassword = isPassword;
|
|
2684
2566
|
exports.isPostcode = isPostcode;
|
|
2685
|
-
exports.isPromiseLike = isPromiseLike;
|
|
2686
2567
|
exports.isQQ = isQQ;
|
|
2687
2568
|
exports.isSocialCreditCode = isSocialCreditCode;
|
|
2688
2569
|
exports.isSwiftCode = isSwiftCode;
|
|
@@ -2698,12 +2579,11 @@
|
|
|
2698
2579
|
exports.loadImageWithBlob = loadImageWithBlob;
|
|
2699
2580
|
exports.loadScript = loadScript;
|
|
2700
2581
|
exports.minus = minus;
|
|
2701
|
-
exports.normalizeString = normalizeString;
|
|
2702
2582
|
exports.numberToChinese = numberToChinese;
|
|
2703
2583
|
exports.padZero = padZero;
|
|
2704
2584
|
exports.parseIdCard = parseIdCard;
|
|
2705
2585
|
exports.plus = plus;
|
|
2706
|
-
exports.randomString = randomString;
|
|
2586
|
+
exports.randomString = randomString$1;
|
|
2707
2587
|
exports.replaceChar = replaceChar;
|
|
2708
2588
|
exports.round = _round;
|
|
2709
2589
|
exports.safeDate = safeDate;
|
|
@@ -2715,8 +2595,6 @@
|
|
|
2715
2595
|
exports.transformObjectValue = transformObjectValue;
|
|
2716
2596
|
exports.treeToList = treeToList;
|
|
2717
2597
|
exports.validatePassword = validatePassword;
|
|
2718
|
-
exports.version = version;
|
|
2719
|
-
exports.waitTime = waitTime;
|
|
2720
2598
|
|
|
2721
2599
|
}));
|
|
2722
2600
|
//# sourceMappingURL=util-helpers.js.map
|