util-helpers 4.19.4 → 4.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -47,6 +47,8 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
47
47
  - [times](https://doly-dev.github.io/util-helpers/module-Math.html#.times) - 乘
48
48
  - [divide](https://doly-dev.github.io/util-helpers/module-Math.html#.divide) - 除
49
49
  - [round](https://doly-dev.github.io/util-helpers/module-Math.html#.round) - 四舍五入
50
+ - [gcd](https://doly-dev.github.io/util-helpers/module-Math.html#.gcd) - 最大公约数
51
+ - [lcm](https://doly-dev.github.io/util-helpers/module-Math.html#.lcm) - 最小公倍数
50
52
  - 数据处理
51
53
  - [bytesToSize](https://doly-dev.github.io/util-helpers/module-Processor.html#.bytesToSize) - 字节转换为存储单位
52
54
  - [dataURLToBlob](https://doly-dev.github.io/util-helpers/module-Processor.html#.dataURLToBlob) - 将 DataURL 转为 Blob 对象
@@ -94,7 +96,11 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
94
96
  - 其他
95
97
  - [ajax](https://doly-dev.github.io/util-helpers/module-Other.html#.ajax) - 请求
96
98
  - [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
99
+ - [compressImage](https://doly-dev.github.io/util-helpers/module-Other.html#.compressImage) - 压缩图片
97
100
  - [download](https://doly-dev.github.io/util-helpers/module-Other.html#.download) - 下载
101
+ - [getImageInfo](https://doly-dev.github.io/util-helpers/module-Other.html#.getImageInfo) - 获取图片信息
102
+ - [loadImage](https://doly-dev.github.io/util-helpers/module-Other.html#.loadImage) - 加载图片
103
+ - [loadImageWithBlob](https://doly-dev.github.io/util-helpers/module-Other.html#.loadImageWithBlob) - 加载图片和 blob 对象
98
104
  - [loadScript](https://doly-dev.github.io/util-helpers/module-Other.html#.loadScript) - 加载 js 文件
99
105
  - [randomString](https://doly-dev.github.io/util-helpers/module-Other.html#.randomString) - 随机字符串
100
106
  - [strlen](https://doly-dev.github.io/util-helpers/module-Other.html#.strlen) - 字符长度
@@ -39,6 +39,17 @@
39
39
  }
40
40
  return '';
41
41
  }
42
+ function wrapTags(tags) {
43
+ return tags.map(function (item) { return '[object ' + item + ']'; });
44
+ }
45
+ var numberTag = '[object Number]';
46
+ var stringTag = '[object String]';
47
+ var symbolTag = '[object Symbol]';
48
+ var argumentsTag = '[object Arguments]';
49
+ wrapTags(['Float32Array', 'Float64Array', 'Int8Array', 'Int16Array', 'Int32Array', 'Uint8Array', 'Uint8ClampedArray', 'Uint16Array', 'Uint32Array', 'BigInt64Array', 'BigUint64Array']);
50
+ wrapTags(['Function', 'AsyncFunction', 'GeneratorFunction', 'Proxy']);
51
+ var blobTag = '[object Blob]';
52
+ var fileTag = '[object Blob]';
42
53
  var objectTag = '[object Object]';
43
54
  var dataViewTag = '[object DataView]';
44
55
  var mapTag = '[object Map]';
@@ -115,20 +126,16 @@
115
126
  }
116
127
  var getTag$1 = getTag;
117
128
 
118
- function isType(value, type) {
119
- var nativeTypeString = getTag$1(value);
120
- if (typeof type === 'string') {
121
- return nativeTypeString === '[object ' + type + ']';
122
- }
123
- return type.some(function (item) { return nativeTypeString === '[object ' + item + ']'; });
129
+ function checkType(value, tag) {
130
+ return getTag$1(value) === tag;
124
131
  }
125
-
126
- function isObjectLike(value) {
127
- return value != null && typeof value === 'object';
132
+ function checkTypes(value, tags) {
133
+ var tag = getTag$1(value);
134
+ return tags.some(function (item) { return tag === item; });
128
135
  }
129
136
 
130
137
  function isSymbol(value) {
131
- return typeof value === 'symbol' || (isObjectLike(value) && isType(value, 'Symbol'));
138
+ return typeof value === 'symbol' || checkType(value, symbolTag);
132
139
  }
133
140
 
134
141
  var reIsBinary = /^0b[01]+$/i;
@@ -165,8 +172,7 @@
165
172
  return remainder ? result - remainder : result;
166
173
  }
167
174
 
168
- var argType = 'Arguments';
169
- isType((function () { return arguments; })(), argType);
175
+ checkType((function () { return arguments; })(), argumentsTag);
170
176
  var numberIsFinite = Number.isFinite;
171
177
 
172
178
  function baseToString(value) {
@@ -187,7 +193,7 @@
187
193
  }
188
194
 
189
195
  function isBlob(value) {
190
- return (blobExisted && value instanceof Blob) || isType(value, ['Blob', 'File']);
196
+ return (blobExisted && value instanceof Blob) || checkTypes(value, [blobTag, fileTag]);
191
197
  }
192
198
 
193
199
  function isFinite$1(value) {
@@ -195,7 +201,7 @@
195
201
  }
196
202
 
197
203
  function isNumber(value) {
198
- return typeof value === 'number' || (isObjectLike(value) && isType(value, 'Number'));
204
+ return typeof value === 'number' || checkType(value, numberTag);
199
205
  }
200
206
 
201
207
  function isNaN$1(value) {
@@ -207,7 +213,7 @@
207
213
  }
208
214
 
209
215
  function isString(value) {
210
- return typeof value === 'string' || (isObjectLike(value) && isType(value, 'String'));
216
+ return typeof value === 'string' || checkType(value, stringTag);
211
217
  }
212
218
 
213
219
  function decimalAdjust(type, value, precision) {
@@ -1327,6 +1333,58 @@
1327
1333
  return round(num, precision);
1328
1334
  }
1329
1335
 
1336
+ function gcd() {
1337
+ var nums = [];
1338
+ for (var _i = 0; _i < arguments.length; _i++) {
1339
+ nums[_i] = arguments[_i];
1340
+ }
1341
+ var num1 = nums[0];
1342
+ var num2 = nums[1] === void 0 ? 0 : nums[1];
1343
+ var rest = nums.slice(2);
1344
+ if (rest.length > 0) {
1345
+ return gcd.apply(void 0, [gcd(num1, num2)].concat(rest));
1346
+ }
1347
+ num1 = Math.abs(round(num1));
1348
+ num2 = Math.abs(round(num2));
1349
+ if (isNaN$1(num1) || isNaN$1(num2)) {
1350
+ return Number.NaN;
1351
+ }
1352
+ if (num1 === 0 && num2 === 0) {
1353
+ return 0;
1354
+ }
1355
+ if (num1 === 0) {
1356
+ return num2;
1357
+ }
1358
+ if (num2 === 0) {
1359
+ return num1;
1360
+ }
1361
+ var temp = num2;
1362
+ if (num1 < num2) {
1363
+ temp = num1;
1364
+ num1 = num2;
1365
+ num2 = temp;
1366
+ }
1367
+ while (temp) {
1368
+ temp = num1 % num2;
1369
+ num1 = num2;
1370
+ num2 = temp;
1371
+ }
1372
+ return toNumber(num1);
1373
+ }
1374
+
1375
+ function lcm() {
1376
+ var nums = [];
1377
+ for (var _i = 0; _i < arguments.length; _i++) {
1378
+ nums[_i] = arguments[_i];
1379
+ }
1380
+ var args = nums.map(function (item) { return Math.abs(round(item)); });
1381
+ if (args.length === 1) {
1382
+ args = args.concat([1]);
1383
+ }
1384
+ var product = args.indexOf(0) > -1 ? 0 : times.apply(void 0, __spreadArray([], __read(args), false));
1385
+ return divide(product, gcd.apply(void 0, __spreadArray([], __read(args), false)));
1386
+ }
1387
+
1330
1388
  function ajax(url, options) {
1331
1389
  var _a = options || {}, _b = _a.method, method = _b === void 0 ? 'get' : _b, _c = _a.data, data = _c === void 0 ? null : _c, timeout = _a.timeout, headers = _a.headers, _d = _a.withCredentials, withCredentials = _d === void 0 ? false : _d, _e = _a.async, async = _e === void 0 ? true : _e, _f = _a.user, user = _f === void 0 ? null : _f, _g = _a.password, password = _g === void 0 ? null : _g, responseType = _a.responseType, onReadyStateChange = _a.onReadyStateChange, onLoadStart = _a.onLoadStart, onProgress = _a.onProgress, onAbort = _a.onAbort, onTimeout = _a.onTimeout, onError = _a.onError, onLoad = _a.onLoad, onLoadEnd = _a.onLoadEnd;
1332
1390
  return new Promise(function (resolve, reject) {
@@ -1409,6 +1467,117 @@
1409
1467
  return pos;
1410
1468
  }
1411
1469
 
1470
+ var SuccessResponseStatus = [200, 304];
1471
+ function getBlob(img) {
1472
+ return new Promise(function (resolve, reject) {
1473
+ if (isBlob(img)) {
1474
+ resolve(img);
1475
+ }
1476
+ else {
1477
+ ajax(img, { responseType: 'blob' })
1478
+ .then(function (ev) {
1479
+ var responseStatus = ev.target.status;
1480
+ if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
1481
+ resolve(ev.target.response);
1482
+ }
1483
+ else {
1484
+ reject(new Error("[loadImageWithBlob] The image does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(img, "'.")));
1485
+ }
1486
+ })
1487
+ .catch(reject);
1488
+ }
1489
+ });
1490
+ }
1491
+ var cacheImage$2;
1492
+ var cacheResult$2;
1493
+ function loadImageWithBlob(img, useCache) {
1494
+ if (useCache === void 0) { useCache = true; }
1495
+ return new Promise(function (resolve, reject) {
1496
+ if (useCache && cacheImage$2 === img && cacheResult$2) {
1497
+ resolve(cacheResult$2);
1498
+ }
1499
+ else {
1500
+ getBlob(img)
1501
+ .then(function (blob) {
1502
+ var url = URL.createObjectURL(blob);
1503
+ var image = new Image();
1504
+ image.onload = function () {
1505
+ URL.revokeObjectURL(url);
1506
+ var result = { blob: blob, image: image };
1507
+ if (useCache) {
1508
+ cacheImage$2 = img;
1509
+ cacheResult$2 = result;
1510
+ }
1511
+ resolve(result);
1512
+ };
1513
+ image.onerror = function (err) {
1514
+ URL.revokeObjectURL(url);
1515
+ console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
1516
+ reject(err);
1517
+ };
1518
+ image.src = url;
1519
+ })
1520
+ .catch(reject);
1521
+ }
1522
+ });
1523
+ }
1524
+
1525
+ function canvasToBlob(canvas, type, quality) {
1526
+ return new Promise(function (resolve) {
1527
+ canvas.toBlob(function (blob) {
1528
+ resolve(blob);
1529
+ }, type, quality);
1530
+ });
1531
+ }
1532
+ function compressImage(img, options) {
1533
+ if (options === void 0) { options = {}; }
1534
+ return new Promise(function (resolve, reject) {
1535
+ 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;
1536
+ loadImageWithBlob(img, cacheImage)
1537
+ .then(function (_a) {
1538
+ var image = _a.image, blob = _a.blob;
1539
+ var numWidth = toNumber(width);
1540
+ var numHeight = toNumber(height);
1541
+ var numQuality = toNumber(quality);
1542
+ if (numWidth) {
1543
+ image.width = numWidth;
1544
+ }
1545
+ if (numHeight) {
1546
+ image.height = numHeight;
1547
+ }
1548
+ beforeCompress === null || beforeCompress === void 0 ? void 0 : beforeCompress({ image: image, blob: blob }, options);
1549
+ var canvas = document.createElement('canvas');
1550
+ var ctx = canvas.getContext('2d');
1551
+ var info = { image: image, blob: blob, canvas: canvas, context: ctx };
1552
+ var numCanvasWidth = toNumber(typeof canvasWidth === 'function' ? canvasWidth(info, options) : canvasWidth);
1553
+ var numCanvasHeight = toNumber(typeof canvasHeight === 'function' ? canvasHeight(info, options) : canvasHeight);
1554
+ canvas.width = numCanvasWidth || image.width;
1555
+ canvas.height = numCanvasHeight || image.height;
1556
+ if (background && background !== 'none') {
1557
+ ctx.fillStyle = background;
1558
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
1559
+ }
1560
+ var internalOffset = [0, 0];
1561
+ if (rotate !== undefined) {
1562
+ ctx.translate(image.width / 2, image.height / 2);
1563
+ internalOffset = [-image.width / 2, -image.height / 2];
1564
+ ctx.rotate((rotate * Math.PI) / 180);
1565
+ }
1566
+ var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
1567
+ beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
1568
+ ctx.drawImage(image, internalOffset[0] + toNumber(outOffset[0]), internalOffset[1] + toNumber(outOffset[1]), image.width, image.height);
1569
+ afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
1570
+ if (format === 'blob') {
1571
+ canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
1572
+ }
1573
+ else {
1574
+ resolve(canvas.toDataURL(type, numQuality));
1575
+ }
1576
+ })
1577
+ .catch(reject);
1578
+ });
1579
+ }
1580
+
1412
1581
  function saver(blobUrl, fileName) {
1413
1582
  if (fileName === void 0) { fileName = ''; }
1414
1583
  var anchor = document.createElement('a');
@@ -1491,6 +1660,81 @@
1491
1660
  });
1492
1661
  }
1493
1662
 
1663
+ function calcContrast(w, h) {
1664
+ var n = gcd(w, h);
1665
+ return "".concat(divide(round(w), n), ":").concat(divide(round(h), n));
1666
+ }
1667
+ var cacheImage$1;
1668
+ var cacheResult$1;
1669
+ function getImageInfo(img, useCache) {
1670
+ if (useCache === void 0) { useCache = true; }
1671
+ return new Promise(function (resolve, reject) {
1672
+ if (useCache && cacheImage$1 === img && cacheResult$1) {
1673
+ resolve(cacheResult$1);
1674
+ }
1675
+ else {
1676
+ loadImageWithBlob(img, false)
1677
+ .then(function (_a) {
1678
+ var image = _a.image, blob = _a.blob;
1679
+ var width = image.width, height = image.height;
1680
+ var result = {
1681
+ width: width,
1682
+ height: height,
1683
+ contrast: calcContrast(width, height),
1684
+ measure: "".concat(width, " \u00D7 ").concat(height, " px"),
1685
+ size: bytesToSize(blob.size),
1686
+ bytes: blob.size,
1687
+ image: image,
1688
+ blob: blob
1689
+ };
1690
+ if (useCache) {
1691
+ cacheImage$1 = img;
1692
+ cacheResult$1 = result;
1693
+ }
1694
+ resolve(result);
1695
+ })
1696
+ .catch(reject);
1697
+ }
1698
+ });
1699
+ }
1700
+
1701
+ var cacheImage;
1702
+ var cacheResult;
1703
+ function loadImage(img, useCache) {
1704
+ if (useCache === void 0) { useCache = true; }
1705
+ return new Promise(function (resolve, reject) {
1706
+ if (useCache && cacheImage === img && cacheResult) {
1707
+ resolve(cacheResult);
1708
+ }
1709
+ else {
1710
+ var imgIsBlob_1 = isBlob(img);
1711
+ var url_1 = imgIsBlob_1 ? URL.createObjectURL(img) : img;
1712
+ var image_1 = new Image();
1713
+ if (!imgIsBlob_1) {
1714
+ image_1.crossOrigin = 'anonymous';
1715
+ }
1716
+ image_1.onload = function () {
1717
+ if (imgIsBlob_1) {
1718
+ URL.revokeObjectURL(url_1);
1719
+ }
1720
+ if (useCache) {
1721
+ cacheImage = img;
1722
+ cacheResult = image_1;
1723
+ }
1724
+ resolve(image_1);
1725
+ };
1726
+ image_1.onerror = function (err) {
1727
+ if (imgIsBlob_1) {
1728
+ URL.revokeObjectURL(url_1);
1729
+ }
1730
+ console.error("[loadImage] The image load failed, '".concat(img, "'."));
1731
+ reject(err);
1732
+ };
1733
+ image_1.src = url_1;
1734
+ }
1735
+ });
1736
+ }
1737
+
1494
1738
  function loadScript(src, options) {
1495
1739
  return new Promise(function (resolve, reject) {
1496
1740
  var head = document.head;
@@ -1824,15 +2068,16 @@
1824
2068
  return internalFindTreeSelect(tree, predicate, childrenField);
1825
2069
  }
1826
2070
 
1827
- var VERSION = "4.19.4";
2071
+ var VERSION = "4.20.0";
1828
2072
 
1829
- var version = "4.19.4";
2073
+ var version = "4.20.0";
1830
2074
 
1831
2075
  exports.VERSION = VERSION;
1832
2076
  exports.ajax = ajax;
1833
2077
  exports.blobToDataURL = blobToDataURL;
1834
2078
  exports.bytesToSize = bytesToSize;
1835
2079
  exports.calculateCursorPosition = calculateCursorPosition;
2080
+ exports.compressImage = compressImage;
1836
2081
  exports.dataURLToBlob = dataURLToBlob;
1837
2082
  exports.divide = divide;
1838
2083
  exports.download = download;
@@ -1844,6 +2089,8 @@
1844
2089
  exports.formatBankCard = formatBankCard;
1845
2090
  exports.formatMobile = formatMobile;
1846
2091
  exports.formatMoney = formatMoney;
2092
+ exports.gcd = gcd;
2093
+ exports.getImageInfo = getImageInfo;
1847
2094
  exports.isBankCard = isBankCard;
1848
2095
  exports.isBusinessLicense = isBusinessLicense;
1849
2096
  exports.isChinese = isChinese;
@@ -1866,7 +2113,10 @@
1866
2113
  exports.isValidNumber = isValidNumber;
1867
2114
  exports.isVehicle = isVehicle;
1868
2115
  exports.isWX = isWX;
2116
+ exports.lcm = lcm;
1869
2117
  exports.listToTree = listToTree;
2118
+ exports.loadImage = loadImage;
2119
+ exports.loadImageWithBlob = loadImageWithBlob;
1870
2120
  exports.loadScript = loadScript;
1871
2121
  exports.minus = minus;
1872
2122
  exports.normalizeString = normalizeString;