util-helpers 4.21.1 → 4.21.2

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.
@@ -6,6 +6,7 @@
6
6
 
7
7
  var objectProto = Object.prototype;
8
8
  var objectProtoToString = objectProto.toString;
9
+ var objectKeys$1 = Object.keys;
9
10
  var symbolExisted = typeof Symbol !== 'undefined';
10
11
  var symbolProto = symbolExisted ? Symbol.prototype : undefined;
11
12
  var mathMin = Math.min;
@@ -18,6 +19,7 @@
18
19
  var numberTag = '[object Number]';
19
20
  var stringTag = '[object String]';
20
21
  var symbolTag = '[object Symbol]';
22
+ var functionTags = ['Function', 'AsyncFunction', 'GeneratorFunction', 'Proxy'].map(function (item) { return '[object ' + item + ']'; });
21
23
  var blobTag = '[object Blob]';
22
24
 
23
25
  function isArray(value) {
@@ -71,6 +73,54 @@
71
73
  return remainder ? result - remainder : result;
72
74
  }
73
75
 
76
+ function identity(value) {
77
+ return value;
78
+ }
79
+
80
+ function isFunction(value) {
81
+ if (typeof value === 'function') {
82
+ return true;
83
+ }
84
+ var tag = getTag(value);
85
+ return functionTags.some(function (item) { return item === tag; });
86
+ }
87
+
88
+ function isLength(value) {
89
+ return typeof value === 'number' && value > -1 && value % 1 === 0 && value <= MAX_SAFE_INTEGER;
90
+ }
91
+
92
+ function isArrayLike(value) {
93
+ return value != null && isLength(value.length) && !isFunction(value);
94
+ }
95
+
96
+ function keys(object) {
97
+ if (!isObject(object)) {
98
+ return [];
99
+ }
100
+ return objectKeys$1(object);
101
+ }
102
+
103
+ function createForEach(dir) {
104
+ function forEach(collection, iteratee) {
105
+ if (iteratee === void 0) { iteratee = identity; }
106
+ var _keys = !isArrayLike(collection) && keys(collection);
107
+ var len = (_keys || collection).length;
108
+ var i = dir > 0 ? 0 : len - 1;
109
+ while (i >= 0 && i < len) {
110
+ var currentKey = _keys ? _keys[i] : i;
111
+ if (iteratee(collection[currentKey], currentKey, collection) === false) {
112
+ break;
113
+ }
114
+ i += dir;
115
+ }
116
+ return collection;
117
+ }
118
+ return forEach;
119
+ }
120
+
121
+ var forEach = createForEach(1);
122
+ var forEach$1 = forEach;
123
+
74
124
  function isNil(value) {
75
125
  return value == null;
76
126
  }
@@ -578,6 +628,20 @@
578
628
  ***************************************************************************** */
579
629
  /* global Reflect, Promise, SuppressedError, Symbol */
580
630
 
631
+ var extendStatics = function(d, b) {
632
+ extendStatics = Object.setPrototypeOf ||
633
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
634
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
635
+ return extendStatics(d, b);
636
+ };
637
+
638
+ function __extends(d, b) {
639
+ if (typeof b !== "function" && b !== null)
640
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
641
+ extendStatics(d, b);
642
+ function __() { this.constructor = d; }
643
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
644
+ }
581
645
 
582
646
  var __assign = function() {
583
647
  __assign = Object.assign || function __assign(t) {
@@ -1419,13 +1483,137 @@
1419
1483
  });
1420
1484
  }
1421
1485
 
1422
- var cacheImage$2;
1423
- var cacheResult$2;
1424
- function loadImageWithBlob(img, useCache, ajaxOptions) {
1425
- if (useCache === void 0) { useCache = true; }
1486
+ var EmitterPro = /** @class */ (function () {
1487
+ function EmitterPro() {
1488
+ this.handler = {};
1489
+ }
1490
+ EmitterPro.prototype.eventNames = function () {
1491
+ return Object.keys(this.handler);
1492
+ };
1493
+ EmitterPro.prototype.listeners = function (eventName) {
1494
+ return this.handler[eventName] || [];
1495
+ };
1496
+ EmitterPro.prototype.hasListener = function (eventName, listener) {
1497
+ return this.listeners(eventName).some(function (item) { return item === listener; });
1498
+ };
1499
+ EmitterPro.prototype.on = function (eventName, listener) {
1500
+ if (!this.handler[eventName]) {
1501
+ this.handler[eventName] = [listener];
1502
+ }
1503
+ else {
1504
+ // 不允许添加相同的方法
1505
+ if (!this.hasListener(eventName, listener)) {
1506
+ this.handler[eventName].push(listener);
1507
+ }
1508
+ }
1509
+ return this;
1510
+ };
1511
+ EmitterPro.prototype.off = function (eventName, listener) {
1512
+ if (this.handler[eventName]) {
1513
+ if (typeof listener === 'function') {
1514
+ this.handler[eventName] = this.handler[eventName].filter(function (item) { return item !== listener; });
1515
+ }
1516
+ else {
1517
+ delete this.handler[eventName];
1518
+ }
1519
+ }
1520
+ return this;
1521
+ };
1522
+ EmitterPro.prototype.emit = function (eventName) {
1523
+ var args = [];
1524
+ for (var _i = 1; _i < arguments.length; _i++) {
1525
+ args[_i - 1] = arguments[_i];
1526
+ }
1527
+ var listeners = this.listeners(eventName);
1528
+ if (listeners.length > 0) {
1529
+ listeners.forEach(function (listener) {
1530
+ // eslint-disable-next-line prefer-spread
1531
+ listener.apply(void 0, args);
1532
+ });
1533
+ return true;
1534
+ }
1535
+ return false;
1536
+ };
1537
+ EmitterPro.prototype.once = function (eventName, listener) {
1538
+ var _this = this;
1539
+ var wrap = function () {
1540
+ var args = [];
1541
+ for (var _i = 0; _i < arguments.length; _i++) {
1542
+ args[_i] = arguments[_i];
1543
+ }
1544
+ // eslint-disable-next-line prefer-spread
1545
+ listener.apply(void 0, args);
1546
+ _this.off(eventName, wrap);
1547
+ };
1548
+ return this.on(eventName, wrap);
1549
+ };
1550
+ EmitterPro.prototype.offAll = function () {
1551
+ this.handler = {};
1552
+ return this;
1553
+ };
1554
+ return EmitterPro;
1555
+ }());
1556
+
1557
+ var Cache = (function (_super) {
1558
+ __extends(Cache, _super);
1559
+ function Cache(options) {
1560
+ var _this = _super.call(this) || this;
1561
+ _this.data = [];
1562
+ _this.options = __assign({ max: 10 }, options);
1563
+ return _this;
1564
+ }
1565
+ Cache.prototype.has = function (k) {
1566
+ return !!this.data.find(function (item) { return item.k === k; });
1567
+ };
1568
+ Cache.prototype.get = function (k) {
1569
+ var _a;
1570
+ return (_a = this.data.find(function (item) { return item.k === k; })) === null || _a === void 0 ? void 0 : _a.v;
1571
+ };
1572
+ Cache.prototype.checkLimit = function () {
1573
+ var _this = this;
1574
+ if (this.options.max !== 0) {
1575
+ var limit = this.data.length - this.options.max;
1576
+ if (limit >= 0) {
1577
+ var delArr = this.data.splice(0, limit + 1);
1578
+ forEach$1(delArr, function (item) {
1579
+ _this.emit('del', item.v, item.k);
1580
+ });
1581
+ }
1582
+ }
1583
+ };
1584
+ Cache.prototype.set = function (k, v) {
1585
+ var newData = { k: k, v: v };
1586
+ if (this.has(k)) {
1587
+ var index = this.data.findIndex(function (item) { return item.k === k; });
1588
+ this.data.splice(index, 1, newData);
1589
+ }
1590
+ else {
1591
+ this.checkLimit();
1592
+ this.data.push(newData);
1593
+ }
1594
+ };
1595
+ return Cache;
1596
+ }(EmitterPro));
1597
+
1598
+ var cache$2 = new Cache({ max: 1 });
1599
+ cache$2.on('del', function (v) {
1600
+ if (v.r) {
1601
+ try {
1602
+ revokeObjectURL(v.data.image.src);
1603
+ }
1604
+ catch (_a) {
1605
+ }
1606
+ }
1607
+ });
1608
+ function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
1609
+ if (cacheOptions === void 0) { cacheOptions = true; }
1610
+ var _cacheOptions = {
1611
+ useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1612
+ autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
1613
+ };
1426
1614
  return new Promise(function (resolve, reject) {
1427
- if (useCache && cacheImage$2 === img && cacheResult$2) {
1428
- resolve(cacheResult$2);
1615
+ if (_cacheOptions.useCache && cache$2.has(img)) {
1616
+ resolve(cache$2.get(img).data);
1429
1617
  }
1430
1618
  else {
1431
1619
  getFileBlob(img, ajaxOptions)
@@ -1434,9 +1622,11 @@
1434
1622
  var image = new Image();
1435
1623
  image.onload = function () {
1436
1624
  var result = { blob: blob, image: image };
1437
- if (useCache) {
1438
- cacheImage$2 = img;
1439
- cacheResult$2 = result;
1625
+ if (_cacheOptions.useCache) {
1626
+ cache$2.set(img, {
1627
+ data: result,
1628
+ r: _cacheOptions.autoRevokeOnDel
1629
+ });
1440
1630
  }
1441
1631
  resolve(result);
1442
1632
  };
@@ -1483,7 +1673,11 @@
1483
1673
  var numCanvasHeight = toNumber(typeof canvasHeight === 'function' ? canvasHeight(info, options) : canvasHeight);
1484
1674
  canvas.width = numCanvasWidth || image.width;
1485
1675
  canvas.height = numCanvasHeight || image.height;
1486
- if (background && background !== 'none') {
1676
+ var bgIsTransparent = background === 'none' || background === 'transparent';
1677
+ if (bgIsTransparent) {
1678
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
1679
+ }
1680
+ else {
1487
1681
  ctx.fillStyle = background;
1488
1682
  ctx.fillRect(0, 0, canvas.width, canvas.height);
1489
1683
  }
@@ -1498,7 +1692,7 @@
1498
1692
  var dx = internalOffset[0] + toNumber(outOffset[0]);
1499
1693
  var dy = internalOffset[1] + toNumber(outOffset[1]);
1500
1694
  ctx.drawImage(image, dx, dy, image.width, image.height);
1501
- if (type === 'image/png') {
1695
+ if (type === 'image/png' && bgIsTransparent) {
1502
1696
  ctx.globalCompositeOperation = 'destination-in';
1503
1697
  ctx.drawImage(image, dx, dy, image.width, image.height);
1504
1698
  }
@@ -1596,17 +1790,29 @@
1596
1790
  });
1597
1791
  }
1598
1792
 
1793
+ var cache$1 = new Cache({ max: 1 });
1794
+ cache$1.on('del', function (v) {
1795
+ if (v.r) {
1796
+ try {
1797
+ revokeObjectURL(v.data.image.src);
1798
+ }
1799
+ catch (_a) {
1800
+ }
1801
+ }
1802
+ });
1599
1803
  function calcContrast(w, h) {
1600
1804
  var n = gcd(w, h);
1601
1805
  return "".concat(divide(round(w), n), ":").concat(divide(round(h), n));
1602
1806
  }
1603
- var cacheImage$1;
1604
- var cacheResult$1;
1605
- function getImageInfo(img, useCache, ajaxOptions) {
1606
- if (useCache === void 0) { useCache = true; }
1807
+ function getImageInfo(img, cacheOptions, ajaxOptions) {
1808
+ if (cacheOptions === void 0) { cacheOptions = true; }
1809
+ var _cacheOptions = {
1810
+ useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1811
+ autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
1812
+ };
1607
1813
  return new Promise(function (resolve, reject) {
1608
- if (useCache && cacheImage$1 === img && cacheResult$1) {
1609
- resolve(cacheResult$1);
1814
+ if (_cacheOptions.useCache && cache$1.has(img)) {
1815
+ resolve(cache$1.get(img).data);
1610
1816
  }
1611
1817
  else {
1612
1818
  loadImageWithBlob(img, false, ajaxOptions)
@@ -1623,9 +1829,11 @@
1623
1829
  image: image,
1624
1830
  blob: blob
1625
1831
  };
1626
- if (useCache) {
1627
- cacheImage$1 = img;
1628
- cacheResult$1 = result;
1832
+ if (_cacheOptions.useCache) {
1833
+ cache$1.set(img, {
1834
+ data: result,
1835
+ r: _cacheOptions.autoRevokeOnDel
1836
+ });
1629
1837
  }
1630
1838
  resolve(result);
1631
1839
  })
@@ -1634,13 +1842,25 @@
1634
1842
  });
1635
1843
  }
1636
1844
 
1637
- var cacheImage;
1638
- var cacheResult;
1639
- function loadImage(img, useCache) {
1640
- if (useCache === void 0) { useCache = true; }
1845
+ var cache = new Cache({ max: 1 });
1846
+ cache.on('del', function (v) {
1847
+ if (v.r) {
1848
+ try {
1849
+ revokeObjectURL(v.data.src);
1850
+ }
1851
+ catch (_a) {
1852
+ }
1853
+ }
1854
+ });
1855
+ function loadImage(img, cacheOptions) {
1856
+ if (cacheOptions === void 0) { cacheOptions = true; }
1857
+ var _cacheOptions = {
1858
+ useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
1859
+ autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
1860
+ };
1641
1861
  return new Promise(function (resolve, reject) {
1642
- if (useCache && cacheImage === img && cacheResult) {
1643
- resolve(cacheResult);
1862
+ if (_cacheOptions.useCache && cache.has(img)) {
1863
+ resolve(cache.get(img).data);
1644
1864
  }
1645
1865
  else {
1646
1866
  var imgIsBlob_1 = isBlob(img);
@@ -1650,9 +1870,11 @@
1650
1870
  image_1.crossOrigin = 'anonymous';
1651
1871
  }
1652
1872
  image_1.onload = function () {
1653
- if (useCache) {
1654
- cacheImage = img;
1655
- cacheResult = image_1;
1873
+ if (_cacheOptions.useCache) {
1874
+ cache.set(img, {
1875
+ data: image_1,
1876
+ r: _cacheOptions.autoRevokeOnDel
1877
+ });
1656
1878
  }
1657
1879
  resolve(image_1);
1658
1880
  };
@@ -2001,9 +2223,9 @@
2001
2223
  return internalFindTreeSelect(tree, predicate, childrenField);
2002
2224
  }
2003
2225
 
2004
- var VERSION = "4.21.1";
2226
+ var VERSION = "4.21.2";
2005
2227
 
2006
- var version = "4.21.1";
2228
+ var version = "4.21.2";
2007
2229
 
2008
2230
  exports.VERSION = VERSION;
2009
2231
  exports.ajax = ajax;