util-helpers 5.2.0 → 5.2.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.
Files changed (85) hide show
  1. package/README.md +18 -17
  2. package/dist/util-helpers.js +78 -73
  3. package/dist/util-helpers.js.map +1 -1
  4. package/dist/util-helpers.min.js +1 -1
  5. package/dist/util-helpers.min.js.map +1 -1
  6. package/esm/VERSION.js +1 -1
  7. package/esm/checkFileType.js +6 -4
  8. package/esm/getFileType.js +2 -2
  9. package/esm/getMimeType.js +2 -2
  10. package/esm/index.js +3 -3
  11. package/esm/utils/file.util.js +7 -4
  12. package/lib/VERSION.js +1 -1
  13. package/lib/checkFileType.js +6 -4
  14. package/lib/getFileType.js +2 -2
  15. package/lib/getMimeType.js +2 -2
  16. package/lib/index.js +6 -6
  17. package/lib/utils/file.util.js +6 -2
  18. package/package.json +1 -1
  19. package/types/AsyncMemo.d.ts +1 -1
  20. package/types/VERSION.d.ts +1 -3
  21. package/types/ajax.d.ts +19 -3
  22. package/types/bytesToSize.d.ts +0 -1
  23. package/types/calculateCursorPosition.d.ts +0 -1
  24. package/types/checkFileType.d.ts +3 -2
  25. package/types/compressImage.d.ts +1 -1
  26. package/types/dataURLToBlob.d.ts +1 -2
  27. package/types/divide.d.ts +0 -1
  28. package/types/download.d.ts +7 -21
  29. package/types/fileReader.d.ts +1 -1
  30. package/types/filterTree.d.ts +0 -1
  31. package/types/findTreeNode.d.ts +0 -1
  32. package/types/findTreeNodes.d.ts +0 -1
  33. package/types/findTreeSelect.d.ts +0 -1
  34. package/types/formatBankCard.d.ts +0 -1
  35. package/types/formatMobile.d.ts +0 -1
  36. package/types/formatMoney.d.ts +0 -1
  37. package/types/gcd.d.ts +0 -1
  38. package/types/getFileBlob.d.ts +1 -2
  39. package/types/getFileType.d.ts +13 -4
  40. package/types/getImageInfo.d.ts +14 -5
  41. package/types/getMimeType.d.ts +41 -4
  42. package/types/index.d.ts +12 -10
  43. package/types/isBankCard.d.ts +0 -1
  44. package/types/isBusinessLicense.d.ts +0 -1
  45. package/types/isChinese.d.ts +0 -1
  46. package/types/isEmail.d.ts +0 -1
  47. package/types/isHMCard.d.ts +0 -1
  48. package/types/isIPv4.d.ts +0 -1
  49. package/types/isIPv6.d.ts +0 -1
  50. package/types/isIdCard.d.ts +0 -1
  51. package/types/isMobile.d.ts +0 -1
  52. package/types/isPassport.d.ts +0 -1
  53. package/types/isPassword.d.ts +0 -1
  54. package/types/isPostcode.d.ts +0 -1
  55. package/types/isQQ.d.ts +0 -1
  56. package/types/isSocialCreditCode.d.ts +0 -1
  57. package/types/isSwiftCode.d.ts +0 -1
  58. package/types/isTWCard.d.ts +0 -1
  59. package/types/isTelephone.d.ts +0 -1
  60. package/types/isUrl.d.ts +0 -1
  61. package/types/isValidNumber.d.ts +0 -1
  62. package/types/isVehicle.d.ts +0 -1
  63. package/types/isWX.d.ts +0 -1
  64. package/types/lcm.d.ts +0 -1
  65. package/types/listToTree.d.ts +0 -1
  66. package/types/loadImage.d.ts +1 -2
  67. package/types/loadImageWithBlob.d.ts +3 -2
  68. package/types/loadScript.d.ts +1 -2
  69. package/types/minus.d.ts +0 -1
  70. package/types/numberToChinese.d.ts +0 -1
  71. package/types/padZero.d.ts +0 -1
  72. package/types/parseIdCard.d.ts +0 -1
  73. package/types/plus.d.ts +0 -1
  74. package/types/replaceChar.d.ts +0 -1
  75. package/types/round.d.ts +0 -1
  76. package/types/setDataURLPrefix.d.ts +0 -1
  77. package/types/strlen.d.ts +0 -1
  78. package/types/times.d.ts +0 -1
  79. package/types/transformFieldNames.d.ts +0 -1
  80. package/types/transformObjectValue.d.ts +0 -1
  81. package/types/treeToList.d.ts +0 -1
  82. package/types/utils/config.d.ts +0 -1
  83. package/types/utils/file.util.d.ts +4 -1
  84. package/types/validatePassword.d.ts +0 -1
  85. package/types/BlobUrl.d.ts +0 -41
package/README.md CHANGED
@@ -51,25 +51,22 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
51
51
  - [plus](https://doly-dev.github.io/util-helpers/module-Math.html#.plus) - 加
52
52
  - [round](https://doly-dev.github.io/util-helpers/module-Math.html#.round) - 四舍五入
53
53
  - [times](https://doly-dev.github.io/util-helpers/module-Math.html#.times) - 乘
54
- - 其他
55
- - [ajax](https://doly-dev.github.io/util-helpers/module-Other.html#.ajax) - 请求
56
- - [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
57
- - [checkFileType](https://doly-dev.github.io/util-helpers/module-Other.html#.checkFileType) - 检查文件类型
58
- - [compressImage](https://doly-dev.github.io/util-helpers/module-Other.html#.compressImage) - 压缩图片
59
- - [download](https://doly-dev.github.io/util-helpers/module-Other.html#.download) - 下载
60
- - [getFileBlob](https://doly-dev.github.io/util-helpers/module-Other.html#.getFileBlob) - 获取文件 Blob
61
- - [getFileType](https://doly-dev.github.io/util-helpers/module-Other.html#.getFileType) - 获取文件类型
62
- - [getImageInfo](https://doly-dev.github.io/util-helpers/module-Other.html#.getImageInfo) - 获取图片信息
63
- - [getMimeType](https://doly-dev.github.io/util-helpers/module-Other.html#.getMimeType) - 获取 MIME 类型
64
- - [loadImage](https://doly-dev.github.io/util-helpers/module-Other.html#.loadImage) - 加载图片
65
- - [loadImageWithBlob](https://doly-dev.github.io/util-helpers/module-Other.html#.loadImageWithBlob) - 加载图片和 blob 对象
66
- - [loadScript](https://doly-dev.github.io/util-helpers/module-Other.html#.loadScript) - 加载 js 文件
67
- - [randomString](https://doly-dev.github.io/util-helpers/module-Other.html#.randomString) - 随机字符串
68
- - [strlen](https://doly-dev.github.io/util-helpers/module-Other.html#.strlen) - 字符长度
54
+ - 浏览器
55
+ - [ajax](https://doly-dev.github.io/util-helpers/module-Browser.html#.ajax) - 请求
56
+ - [checkFileType](https://doly-dev.github.io/util-helpers/module-Browser.html#.checkFileType) - 检查文件类型
57
+ - [compressImage](https://doly-dev.github.io/util-helpers/module-Browser.html#.compressImage) - 压缩图片
58
+ - [dataURLToBlob](https://doly-dev.github.io/util-helpers/module-Browser.html#.dataURLToBlob) - 将 DataURL 转为 Blob 对象
59
+ - [download](https://doly-dev.github.io/util-helpers/module-Browser.html#.download) - 下载
60
+ - [fileReader](https://doly-dev.github.io/util-helpers/module-Browser.html#.fileReader) - 读取 Blob 或 File 对象,转为 Base64/String/ArrayBuffer
61
+ - [getFileBlob](https://doly-dev.github.io/util-helpers/module-Browser.html#.getFileBlob) - 获取文件 Blob
62
+ - [getFileType](https://doly-dev.github.io/util-helpers/module-Browser.html#.getFileType) - 获取文件类型
63
+ - [getImageInfo](https://doly-dev.github.io/util-helpers/module-Browser.html#.getImageInfo) - 获取图片信息
64
+ - [getMimeType](https://doly-dev.github.io/util-helpers/module-Browser.html#.getMimeType) - 获取文件 MIME 类型
65
+ - [loadImage](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadImage) - 加载图片
66
+ - [loadImageWithBlob](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadImageWithBlob) - 加载图片和 blob 对象
67
+ - [loadScript](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadScript) - 加载 js 文件
69
68
  - 数据处理
70
69
  - [bytesToSize](https://doly-dev.github.io/util-helpers/module-Processor.html#.bytesToSize) - 字节转换为存储单位
71
- - [dataURLToBlob](https://doly-dev.github.io/util-helpers/module-Processor.html#.dataURLToBlob) - 将 DataURL 转为 Blob 对象
72
- - [fileReader](https://doly-dev.github.io/util-helpers/module-Processor.html#.fileReader) - 读取 Blob 或 File 对象,转为 Base64/String/ArrayBuffer
73
70
  - [formatBankCard](https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard) - 格式化银行卡
74
71
  - [formatMobile](https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile) - 格式化手机号码
75
72
  - [formatMoney](https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMoney) - 格式化金额
@@ -111,6 +108,10 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
111
108
  - [isVehicle](https://doly-dev.github.io/util-helpers/module-Validator.html#.isVehicle) - 车牌号
112
109
  - [isWX](https://doly-dev.github.io/util-helpers/module-Validator.html#.isWX) - 微信号
113
110
  - [validatePassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.validatePassword) - 验证密码
111
+ - 其他
112
+ - [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
113
+ - [randomString](https://doly-dev.github.io/util-helpers/module-Other.html#.randomString) - 随机字符串
114
+ - [strlen](https://doly-dev.github.io/util-helpers/module-Other.html#.strlen) - 字符长度
114
115
  - Classes
115
116
  - [AsyncMemo](https://doly-dev.github.io/util-helpers/AsyncMemo.html) - 异步缓存类
116
117
 
@@ -1159,40 +1159,6 @@
1159
1159
  };
1160
1160
  }
1161
1161
 
1162
- var FileReaderMethodMap = {
1163
- arrayBuffer: 'readAsArrayBuffer',
1164
- binaryString: 'readAsBinaryString',
1165
- dataURL: 'readAsDataURL',
1166
- text: 'readAsText'
1167
- };
1168
- var fileReader = function (blob, type) {
1169
- if (type === void 0) { type = 'dataURL'; }
1170
- var method = FileReaderMethodMap[type];
1171
- if (!method) {
1172
- method = FileReaderMethodMap.dataURL;
1173
- }
1174
- return new Promise(function (resolve, reject) {
1175
- var reader = new FileReader();
1176
- reader[method](blob);
1177
- reader.onload = function () { return resolve(reader.result); };
1178
- reader.onerror = function (error) { return reject(error); };
1179
- });
1180
- };
1181
-
1182
- function dataURLToBlob(data) {
1183
- var parts = data.split(',');
1184
- var meta = parts[0].substring(5).split(';');
1185
- var type = meta[0];
1186
- var decoder = meta.indexOf('base64') !== -1 ? atob : decodeURIComponent;
1187
- var bstr = decoder(parts[1]);
1188
- var n = bstr.length;
1189
- var u8arr = new Uint8Array(n);
1190
- while (n--) {
1191
- u8arr[n] = bstr.charCodeAt(n);
1192
- }
1193
- return new Blob([u8arr], { type: type });
1194
- }
1195
-
1196
1162
  function setDataURLPrefix(data, mimeType, base64) {
1197
1163
  if (mimeType === void 0) { mimeType = 'image/png'; }
1198
1164
  if (base64 === void 0) { base64 = true; }
@@ -1455,48 +1421,23 @@
1455
1421
  });
1456
1422
  }
1457
1423
 
1458
- function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue, options) {
1459
- if (options === void 0) { options = {}; }
1460
- var _a = options.placeholderChar, placeholderChar = _a === void 0 ? ' ' : _a, _b = options.maskReg, maskReg = _b === void 0 ? /\D/g : _b, type = options.type;
1461
- var realCtrlValue = toString(prevCtrlValue);
1462
- var realRawValue = toString(rawValue);
1463
- var placeholderChars = isArray(placeholderChar) ? placeholderChar : [placeholderChar];
1464
- var editLength = realRawValue.length - realCtrlValue.length;
1465
- var isAddition = editLength > 0;
1466
- var pos = prevPos;
1467
- if (isAddition) {
1468
- var additionStr = realRawValue.substring(pos - editLength, pos);
1469
- var ctrlCharCount = additionStr.replace(maskReg, '').length;
1470
- pos -= editLength - ctrlCharCount;
1471
- var placeholderCharCount = 0;
1472
- while (ctrlCharCount > 0) {
1473
- if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
1474
- placeholderCharCount++;
1475
- }
1476
- else {
1477
- ctrlCharCount--;
1478
- }
1479
- }
1480
- pos += placeholderCharCount;
1481
- }
1482
- if ((type === 'mobile' && (pos === 4 || pos === 9)) || (type === 'bankCard' && pos > 0 && pos % 5 === 0)) {
1483
- pos -= 1;
1484
- }
1485
- return pos;
1424
+ function getFileNameExt(name) {
1425
+ return isString(name) && name.indexOf('.') > 0 ? '.' + nth(name.split('.'), -1) : '';
1486
1426
  }
1487
-
1488
1427
  function testExt(name, ext) {
1489
- return !!name && name.slice(-ext.length) === ext;
1428
+ return !!name && getFileNameExt(name) === ext;
1490
1429
  }
1491
1430
  function isUploadFile(fileObj) {
1492
- if (isObjectLike(fileObj) && isString(fileObj.uid) && isString(fileObj.name)) {
1431
+ if (isObjectLike(fileObj) && isString(fileObj.name)) {
1493
1432
  return true;
1494
1433
  }
1495
1434
  return false;
1496
1435
  }
1497
1436
 
1498
1437
  function checkFileType(file, accept) {
1499
- if (!isFile(file) && !isUploadFile(file)) {
1438
+ var _a, _b;
1439
+ var isFileType = isFile(file);
1440
+ if (!isFileType && !isUploadFile(file)) {
1500
1441
  return false;
1501
1442
  }
1502
1443
  if (!isString(accept)) {
@@ -1508,9 +1449,9 @@
1508
1449
  }
1509
1450
  var ret = false;
1510
1451
  var types = accept.toLowerCase().split(/,(?:\s+)?/);
1511
- var fileName = file.name.toLowerCase();
1512
- var fileType = file.type || '';
1513
- var fileUrl = file.url || '';
1452
+ var fileName = (file.name || (!isFileType && (file.fileName || ((_a = file.originFileObj) === null || _a === void 0 ? void 0 : _a.name))) || '').toLowerCase();
1453
+ var fileType = file.type || (!isFileType && ((_b = file.originFileObj) === null || _b === void 0 ? void 0 : _b.type)) || '';
1454
+ var fileUrl = (!isFileType && file.url) || '';
1514
1455
  types.some(function (type) {
1515
1456
  if (type === '*' || fileType === type || (type.indexOf('.') === 0 && (testExt(fileName, type) || testExt(fileUrl, type)))) {
1516
1457
  ret = true;
@@ -1650,6 +1591,20 @@
1650
1591
  });
1651
1592
  };
1652
1593
 
1594
+ function dataURLToBlob(data) {
1595
+ var parts = data.split(',');
1596
+ var meta = parts[0].substring(5).split(';');
1597
+ var type = meta[0];
1598
+ var decoder = meta.indexOf('base64') !== -1 ? atob : decodeURIComponent;
1599
+ var bstr = decoder(parts[1]);
1600
+ var n = bstr.length;
1601
+ var u8arr = new Uint8Array(n);
1602
+ while (n--) {
1603
+ u8arr[n] = bstr.charCodeAt(n);
1604
+ }
1605
+ return new Blob([u8arr], { type: type });
1606
+ }
1607
+
1653
1608
  function saver(blobUrl, fileName) {
1654
1609
  if (fileName === void 0) { fileName = ''; }
1655
1610
  var anchor = document.createElement('a');
@@ -1732,10 +1687,30 @@
1732
1687
  });
1733
1688
  }
1734
1689
 
1690
+ var FileReaderMethodMap = {
1691
+ arrayBuffer: 'readAsArrayBuffer',
1692
+ binaryString: 'readAsBinaryString',
1693
+ dataURL: 'readAsDataURL',
1694
+ text: 'readAsText'
1695
+ };
1696
+ var fileReader = function (blob, type) {
1697
+ if (type === void 0) { type = 'dataURL'; }
1698
+ var method = FileReaderMethodMap[type];
1699
+ if (!method) {
1700
+ method = FileReaderMethodMap.dataURL;
1701
+ }
1702
+ return new Promise(function (resolve, reject) {
1703
+ var reader = new FileReader();
1704
+ reader[method](blob);
1705
+ reader.onload = function () { return resolve(reader.result); };
1706
+ reader.onerror = function (error) { return reject(error); };
1707
+ });
1708
+ };
1709
+
1735
1710
  var config = {
1736
- image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp',
1711
+ image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp,.svg,.apng,.avif,.ico,.tif,.tiff',
1737
1712
  audio: 'audio/*,.mp3,.wav,.aac,.flac',
1738
- video: 'video/*,.mp4,.webm,.ogg',
1713
+ video: 'video/*,.mp4,.webm,.ogg,.mov',
1739
1714
  pdf: 'application/pdf,.pdf',
1740
1715
  word: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document,.doc,.docx',
1741
1716
  excel: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,.xls,.xlsx'
@@ -1809,7 +1784,7 @@
1809
1784
  ];
1810
1785
  function getMimeType(fileName) {
1811
1786
  var _a;
1812
- var ext = isString(fileName) ? nth(fileName.split('.'), -1) : '';
1787
+ var ext = getFileNameExt(fileName).slice(1);
1813
1788
  return ext ? (_a = mimeTypes.find(function (item) { return item[1].includes(ext); })) === null || _a === void 0 ? void 0 : _a[0] : nativeUndefined;
1814
1789
  }
1815
1790
 
@@ -1849,6 +1824,36 @@
1849
1824
  });
1850
1825
  }
1851
1826
 
1827
+ function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue, options) {
1828
+ if (options === void 0) { options = {}; }
1829
+ var _a = options.placeholderChar, placeholderChar = _a === void 0 ? ' ' : _a, _b = options.maskReg, maskReg = _b === void 0 ? /\D/g : _b, type = options.type;
1830
+ var realCtrlValue = toString(prevCtrlValue);
1831
+ var realRawValue = toString(rawValue);
1832
+ var placeholderChars = isArray(placeholderChar) ? placeholderChar : [placeholderChar];
1833
+ var editLength = realRawValue.length - realCtrlValue.length;
1834
+ var isAddition = editLength > 0;
1835
+ var pos = prevPos;
1836
+ if (isAddition) {
1837
+ var additionStr = realRawValue.substring(pos - editLength, pos);
1838
+ var ctrlCharCount = additionStr.replace(maskReg, '').length;
1839
+ pos -= editLength - ctrlCharCount;
1840
+ var placeholderCharCount = 0;
1841
+ while (ctrlCharCount > 0) {
1842
+ if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
1843
+ placeholderCharCount++;
1844
+ }
1845
+ else {
1846
+ ctrlCharCount--;
1847
+ }
1848
+ }
1849
+ pos += placeholderCharCount;
1850
+ }
1851
+ if ((type === 'mobile' && (pos === 4 || pos === 9)) || (type === 'bankCard' && pos > 0 && pos % 5 === 0)) {
1852
+ pos -= 1;
1853
+ }
1854
+ return pos;
1855
+ }
1856
+
1852
1857
  var letter = 'abcdefghijklmnopqrstuvwxyz';
1853
1858
  var chars = {
1854
1859
  number: '0123456789',
@@ -2124,7 +2129,7 @@
2124
2129
  return internalFindTreeSelect(tree, predicate, childrenField);
2125
2130
  }
2126
2131
 
2127
- var VERSION = "5.2.0";
2132
+ var VERSION = "5.2.2";
2128
2133
 
2129
2134
  /**
2130
2135
  * 事件触发器,支持浏览器端和 node 端。