util-helpers 4.21.0 → 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) {
@@ -26,11 +28,15 @@
26
28
 
27
29
  function isObject(value) {
28
30
  var type = typeof value;
29
- return value != null && (type === 'object' || type === 'function');
31
+ return type === 'function' || (type === 'object' && !!value);
32
+ }
33
+
34
+ function getTag(value) {
35
+ return objectProtoToString.call(value);
30
36
  }
31
37
 
32
38
  function isSymbol(value) {
33
- return typeof value === 'symbol' || objectProtoToString.call(value) === symbolTag;
39
+ return typeof value === 'symbol' || getTag(value) === symbolTag;
34
40
  }
35
41
 
36
42
  var reIsBinary = /^0b[01]+$/i;
@@ -67,22 +73,74 @@
67
73
  return remainder ? result - remainder : result;
68
74
  }
69
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
+
124
+ function isNil(value) {
125
+ return value == null;
126
+ }
127
+
70
128
  var symbolToString = symbolProto ? symbolProto.toString : undefined;
71
129
  function baseToString(value) {
72
130
  if (typeof value === 'string') {
73
131
  return value;
74
132
  }
75
133
  if (isArray(value)) {
76
- return "".concat(value.map(baseToString));
134
+ return '' + value.map(baseToString);
77
135
  }
78
136
  if (isSymbol(value)) {
79
137
  return symbolToString ? symbolToString.call(value) : '';
80
138
  }
81
139
  var result = '' + value;
82
- return result == '0' && 1 / value === -Infinity ? '-0' : result;
140
+ return result === '0' && 1 / value === -Infinity ? '-0' : result;
83
141
  }
84
142
  function toString(value) {
85
- return value == null ? '' : baseToString(value);
143
+ return isNil(value) ? '' : baseToString(value);
86
144
  }
87
145
 
88
146
  var blobExisted = typeof Blob !== 'undefined';
@@ -90,7 +148,7 @@
90
148
  if (blobExisted && value instanceof Blob) {
91
149
  return true;
92
150
  }
93
- return objectProtoToString.call(value) === blobTag;
151
+ return getTag(value) === blobTag;
94
152
  }
95
153
 
96
154
  var freeGlobalThis = globalThisExisted && globalThis.Object === Object && globalThis;
@@ -104,7 +162,7 @@
104
162
  }
105
163
 
106
164
  function isNumber(value) {
107
- return typeof value === 'number' || objectProtoToString.call(value) === numberTag;
165
+ return typeof value === 'number' || getTag(value) === numberTag;
108
166
  }
109
167
 
110
168
  function isNaN(value) {
@@ -116,7 +174,7 @@
116
174
  }
117
175
 
118
176
  function isString(value) {
119
- return typeof value === 'string' || objectProtoToString.call(value) === stringTag;
177
+ return typeof value === 'string' || getTag(value) === stringTag;
120
178
  }
121
179
 
122
180
  function decimalAdjust(type, value, precision) {
@@ -570,6 +628,20 @@
570
628
  ***************************************************************************** */
571
629
  /* global Reflect, Promise, SuppressedError, Symbol */
572
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
+ }
573
645
 
574
646
  var __assign = function() {
575
647
  __assign = Object.assign || function __assign(t) {
@@ -1411,13 +1483,137 @@
1411
1483
  });
1412
1484
  }
1413
1485
 
1414
- var cacheImage$2;
1415
- var cacheResult$2;
1416
- function loadImageWithBlob(img, useCache, ajaxOptions) {
1417
- 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
+ };
1418
1614
  return new Promise(function (resolve, reject) {
1419
- if (useCache && cacheImage$2 === img && cacheResult$2) {
1420
- resolve(cacheResult$2);
1615
+ if (_cacheOptions.useCache && cache$2.has(img)) {
1616
+ resolve(cache$2.get(img).data);
1421
1617
  }
1422
1618
  else {
1423
1619
  getFileBlob(img, ajaxOptions)
@@ -1425,11 +1621,12 @@
1425
1621
  var url = createObjectURL(blob);
1426
1622
  var image = new Image();
1427
1623
  image.onload = function () {
1428
- revokeObjectURL(url);
1429
1624
  var result = { blob: blob, image: image };
1430
- if (useCache) {
1431
- cacheImage$2 = img;
1432
- cacheResult$2 = result;
1625
+ if (_cacheOptions.useCache) {
1626
+ cache$2.set(img, {
1627
+ data: result,
1628
+ r: _cacheOptions.autoRevokeOnDel
1629
+ });
1433
1630
  }
1434
1631
  resolve(result);
1435
1632
  };
@@ -1476,7 +1673,11 @@
1476
1673
  var numCanvasHeight = toNumber(typeof canvasHeight === 'function' ? canvasHeight(info, options) : canvasHeight);
1477
1674
  canvas.width = numCanvasWidth || image.width;
1478
1675
  canvas.height = numCanvasHeight || image.height;
1479
- 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 {
1480
1681
  ctx.fillStyle = background;
1481
1682
  ctx.fillRect(0, 0, canvas.width, canvas.height);
1482
1683
  }
@@ -1488,7 +1689,13 @@
1488
1689
  }
1489
1690
  var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
1490
1691
  beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
1491
- ctx.drawImage(image, internalOffset[0] + toNumber(outOffset[0]), internalOffset[1] + toNumber(outOffset[1]), image.width, image.height);
1692
+ var dx = internalOffset[0] + toNumber(outOffset[0]);
1693
+ var dy = internalOffset[1] + toNumber(outOffset[1]);
1694
+ ctx.drawImage(image, dx, dy, image.width, image.height);
1695
+ if (type === 'image/png' && bgIsTransparent) {
1696
+ ctx.globalCompositeOperation = 'destination-in';
1697
+ ctx.drawImage(image, dx, dy, image.width, image.height);
1698
+ }
1492
1699
  afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
1493
1700
  if (format === 'blob') {
1494
1701
  canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
@@ -1583,17 +1790,29 @@
1583
1790
  });
1584
1791
  }
1585
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
+ });
1586
1803
  function calcContrast(w, h) {
1587
1804
  var n = gcd(w, h);
1588
1805
  return "".concat(divide(round(w), n), ":").concat(divide(round(h), n));
1589
1806
  }
1590
- var cacheImage$1;
1591
- var cacheResult$1;
1592
- function getImageInfo(img, useCache, ajaxOptions) {
1593
- 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
+ };
1594
1813
  return new Promise(function (resolve, reject) {
1595
- if (useCache && cacheImage$1 === img && cacheResult$1) {
1596
- resolve(cacheResult$1);
1814
+ if (_cacheOptions.useCache && cache$1.has(img)) {
1815
+ resolve(cache$1.get(img).data);
1597
1816
  }
1598
1817
  else {
1599
1818
  loadImageWithBlob(img, false, ajaxOptions)
@@ -1610,9 +1829,11 @@
1610
1829
  image: image,
1611
1830
  blob: blob
1612
1831
  };
1613
- if (useCache) {
1614
- cacheImage$1 = img;
1615
- cacheResult$1 = result;
1832
+ if (_cacheOptions.useCache) {
1833
+ cache$1.set(img, {
1834
+ data: result,
1835
+ r: _cacheOptions.autoRevokeOnDel
1836
+ });
1616
1837
  }
1617
1838
  resolve(result);
1618
1839
  })
@@ -1621,13 +1842,25 @@
1621
1842
  });
1622
1843
  }
1623
1844
 
1624
- var cacheImage;
1625
- var cacheResult;
1626
- function loadImage(img, useCache) {
1627
- 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
+ };
1628
1861
  return new Promise(function (resolve, reject) {
1629
- if (useCache && cacheImage === img && cacheResult) {
1630
- resolve(cacheResult);
1862
+ if (_cacheOptions.useCache && cache.has(img)) {
1863
+ resolve(cache.get(img).data);
1631
1864
  }
1632
1865
  else {
1633
1866
  var imgIsBlob_1 = isBlob(img);
@@ -1637,12 +1870,11 @@
1637
1870
  image_1.crossOrigin = 'anonymous';
1638
1871
  }
1639
1872
  image_1.onload = function () {
1640
- if (imgIsBlob_1) {
1641
- revokeObjectURL(url_1);
1642
- }
1643
- if (useCache) {
1644
- cacheImage = img;
1645
- cacheResult = image_1;
1873
+ if (_cacheOptions.useCache) {
1874
+ cache.set(img, {
1875
+ data: image_1,
1876
+ r: _cacheOptions.autoRevokeOnDel
1877
+ });
1646
1878
  }
1647
1879
  resolve(image_1);
1648
1880
  };
@@ -1991,9 +2223,9 @@
1991
2223
  return internalFindTreeSelect(tree, predicate, childrenField);
1992
2224
  }
1993
2225
 
1994
- var VERSION = "4.21.0";
2226
+ var VERSION = "4.21.2";
1995
2227
 
1996
- var version = "4.21.0";
2228
+ var version = "4.21.2";
1997
2229
 
1998
2230
  exports.VERSION = VERSION;
1999
2231
  exports.ajax = ajax;