util-helpers 5.2.1 → 5.3.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.
Files changed (88) hide show
  1. package/README.md +19 -17
  2. package/dist/util-helpers.js +101 -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/getMimeType.js +2 -2
  9. package/esm/index.js +4 -3
  10. package/esm/injectStyle.js +23 -0
  11. package/esm/loadScript.js +3 -3
  12. package/esm/utils/file.util.js +6 -3
  13. package/lib/VERSION.js +1 -1
  14. package/lib/checkFileType.js +6 -4
  15. package/lib/getMimeType.js +2 -2
  16. package/lib/index.js +8 -6
  17. package/lib/injectStyle.js +25 -0
  18. package/lib/loadScript.js +3 -3
  19. package/lib/utils/file.util.js +5 -1
  20. package/package.json +1 -1
  21. package/types/VERSION.d.ts +1 -2
  22. package/types/ajax.d.ts +2 -3
  23. package/types/bytesToSize.d.ts +0 -1
  24. package/types/calculateCursorPosition.d.ts +0 -1
  25. package/types/checkFileType.d.ts +3 -2
  26. package/types/compressImage.d.ts +1 -1
  27. package/types/dataURLToBlob.d.ts +1 -2
  28. package/types/divide.d.ts +0 -1
  29. package/types/download.d.ts +1 -2
  30. package/types/fileReader.d.ts +1 -1
  31. package/types/filterTree.d.ts +0 -1
  32. package/types/findTreeNode.d.ts +0 -1
  33. package/types/findTreeNodes.d.ts +0 -1
  34. package/types/findTreeSelect.d.ts +0 -1
  35. package/types/formatBankCard.d.ts +0 -1
  36. package/types/formatMobile.d.ts +0 -1
  37. package/types/formatMoney.d.ts +0 -1
  38. package/types/gcd.d.ts +0 -1
  39. package/types/getExtname.d.ts +0 -0
  40. package/types/getFileBlob.d.ts +1 -2
  41. package/types/getFileType.d.ts +1 -3
  42. package/types/getImageInfo.d.ts +1 -2
  43. package/types/getMimeType.d.ts +1 -1
  44. package/types/index.d.ts +13 -10
  45. package/types/injectStyle.d.ts +16 -0
  46. package/types/isBankCard.d.ts +0 -1
  47. package/types/isBusinessLicense.d.ts +0 -1
  48. package/types/isChinese.d.ts +0 -1
  49. package/types/isEmail.d.ts +0 -1
  50. package/types/isHMCard.d.ts +0 -1
  51. package/types/isIPv4.d.ts +0 -1
  52. package/types/isIPv6.d.ts +0 -1
  53. package/types/isIdCard.d.ts +0 -1
  54. package/types/isMobile.d.ts +0 -1
  55. package/types/isPassport.d.ts +0 -1
  56. package/types/isPassword.d.ts +0 -1
  57. package/types/isPostcode.d.ts +0 -1
  58. package/types/isQQ.d.ts +0 -1
  59. package/types/isSocialCreditCode.d.ts +0 -1
  60. package/types/isSwiftCode.d.ts +0 -1
  61. package/types/isTWCard.d.ts +0 -1
  62. package/types/isTelephone.d.ts +0 -1
  63. package/types/isUrl.d.ts +0 -1
  64. package/types/isValidNumber.d.ts +0 -1
  65. package/types/isVehicle.d.ts +0 -1
  66. package/types/isWX.d.ts +0 -1
  67. package/types/lcm.d.ts +0 -1
  68. package/types/listToTree.d.ts +0 -1
  69. package/types/loadImage.d.ts +1 -2
  70. package/types/loadImageWithBlob.d.ts +1 -1
  71. package/types/loadScript.d.ts +1 -2
  72. package/types/minus.d.ts +0 -1
  73. package/types/numberToChinese.d.ts +0 -1
  74. package/types/padZero.d.ts +0 -1
  75. package/types/parseIdCard.d.ts +0 -1
  76. package/types/plus.d.ts +0 -1
  77. package/types/replaceChar.d.ts +0 -1
  78. package/types/round.d.ts +0 -1
  79. package/types/setDataURLPrefix.d.ts +0 -1
  80. package/types/strlen.d.ts +0 -1
  81. package/types/times.d.ts +0 -1
  82. package/types/transformFieldNames.d.ts +0 -1
  83. package/types/transformObjectValue.d.ts +0 -1
  84. package/types/treeToList.d.ts +0 -1
  85. package/types/utils/config.d.ts +0 -1
  86. package/types/utils/file.util.d.ts +4 -0
  87. package/types/validatePassword.d.ts +0 -1
  88. package/types/BlobUrl.d.ts +0 -41
package/README.md CHANGED
@@ -51,25 +51,23 @@ 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
+ - [injectStyle](https://doly-dev.github.io/util-helpers/module-Browser.html#.injectStyle) - 注入样式
66
+ - [loadImage](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadImage) - 加载图片
67
+ - [loadImageWithBlob](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadImageWithBlob) - 加载图片和 blob 对象
68
+ - [loadScript](https://doly-dev.github.io/util-helpers/module-Browser.html#.loadScript) - 加载 js 文件
69
69
  - 数据处理
70
70
  - [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
71
  - [formatBankCard](https://doly-dev.github.io/util-helpers/module-Processor.html#.formatBankCard) - 格式化银行卡
74
72
  - [formatMobile](https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMobile) - 格式化手机号码
75
73
  - [formatMoney](https://doly-dev.github.io/util-helpers/module-Processor.html#.formatMoney) - 格式化金额
@@ -111,6 +109,10 @@ formatBankCard('6228480402564890018', { spaceMark: '-' }); // 6228-4804-0256-489
111
109
  - [isVehicle](https://doly-dev.github.io/util-helpers/module-Validator.html#.isVehicle) - 车牌号
112
110
  - [isWX](https://doly-dev.github.io/util-helpers/module-Validator.html#.isWX) - 微信号
113
111
  - [validatePassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.validatePassword) - 验证密码
112
+ - 其他
113
+ - [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
114
+ - [randomString](https://doly-dev.github.io/util-helpers/module-Other.html#.randomString) - 随机字符串
115
+ - [strlen](https://doly-dev.github.io/util-helpers/module-Other.html#.strlen) - 字符长度
114
116
  - Classes
115
117
  - [AsyncMemo](https://doly-dev.github.io/util-helpers/AsyncMemo.html) - 异步缓存类
116
118
 
@@ -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,38 +1421,11 @@
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 getExtname(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 && getExtname(name) === ext;
1490
1429
  }
1491
1430
  function isUploadFile(fileObj) {
1492
1431
  if (isObjectLike(fileObj) && isString(fileObj.name)) {
@@ -1496,7 +1435,9 @@
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,6 +1687,26 @@
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
1711
  image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp,.svg,.apng,.avif,.ico,.tif,.tiff',
1737
1712
  audio: 'audio/*,.mp3,.wav,.aac,.flac',
@@ -1809,13 +1784,35 @@
1809
1784
  ];
1810
1785
  function getMimeType(fileName) {
1811
1786
  var _a;
1812
- var ext = isString(fileName) && fileName.indexOf('.') > 0 ? nth(fileName.split('.'), -1) : '';
1787
+ var ext = getExtname(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
 
1791
+ function injectStyle(css, options) {
1792
+ var _a = options || {}, _b = _a.container, container = _b === void 0 ? document.head || document.getElementsByTagName('head')[0] || document.body : _b, _c = _a.insertAt, insertAt = _c === void 0 ? 'top' : _c;
1793
+ var style = document.createElement('style');
1794
+ if (style.styleSheet) {
1795
+ style.styleSheet.cssText = css;
1796
+ }
1797
+ else {
1798
+ style.appendChild(document.createTextNode(css));
1799
+ }
1800
+ var atTop = insertAt === 'top';
1801
+ if (atTop && container.prepend) {
1802
+ container.prepend(style);
1803
+ }
1804
+ else if (atTop && container.firstChild) {
1805
+ container.insertBefore(style, container.firstChild);
1806
+ }
1807
+ else {
1808
+ container.appendChild(style);
1809
+ }
1810
+ return style;
1811
+ }
1812
+
1816
1813
  function loadScript(src, options) {
1817
1814
  return new Promise(function (resolve, reject) {
1818
- var head = document.head;
1815
+ var container = document.head || document.getElementsByTagName('head')[0] || document.body;
1819
1816
  var script = document.createElement('script');
1820
1817
  var _a = options || {}, attrs = _a.attrs, _b = _a.destroyOnError, destroyOnError = _b === void 0 ? true : _b, restOptions = __rest(_a, ["attrs", "destroyOnError"]);
1821
1818
  var props = __assign(__assign({ async: true, type: 'text/javascript' }, restOptions), { src: src });
@@ -1841,14 +1838,44 @@
1841
1838
  this.onerror = this.onload = null;
1842
1839
  (_a = props.onerror) === null || _a === void 0 ? void 0 : _a.call(this, ev);
1843
1840
  if (destroyOnError) {
1844
- head.removeChild(script);
1841
+ container.removeChild(script);
1845
1842
  }
1846
1843
  reject(new URIError('Failed to load ' + this.src));
1847
1844
  };
1848
- head.appendChild(script);
1845
+ container.appendChild(script);
1849
1846
  });
1850
1847
  }
1851
1848
 
1849
+ function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue, options) {
1850
+ if (options === void 0) { options = {}; }
1851
+ var _a = options.placeholderChar, placeholderChar = _a === void 0 ? ' ' : _a, _b = options.maskReg, maskReg = _b === void 0 ? /\D/g : _b, type = options.type;
1852
+ var realCtrlValue = toString(prevCtrlValue);
1853
+ var realRawValue = toString(rawValue);
1854
+ var placeholderChars = isArray(placeholderChar) ? placeholderChar : [placeholderChar];
1855
+ var editLength = realRawValue.length - realCtrlValue.length;
1856
+ var isAddition = editLength > 0;
1857
+ var pos = prevPos;
1858
+ if (isAddition) {
1859
+ var additionStr = realRawValue.substring(pos - editLength, pos);
1860
+ var ctrlCharCount = additionStr.replace(maskReg, '').length;
1861
+ pos -= editLength - ctrlCharCount;
1862
+ var placeholderCharCount = 0;
1863
+ while (ctrlCharCount > 0) {
1864
+ if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
1865
+ placeholderCharCount++;
1866
+ }
1867
+ else {
1868
+ ctrlCharCount--;
1869
+ }
1870
+ }
1871
+ pos += placeholderCharCount;
1872
+ }
1873
+ if ((type === 'mobile' && (pos === 4 || pos === 9)) || (type === 'bankCard' && pos > 0 && pos % 5 === 0)) {
1874
+ pos -= 1;
1875
+ }
1876
+ return pos;
1877
+ }
1878
+
1852
1879
  var letter = 'abcdefghijklmnopqrstuvwxyz';
1853
1880
  var chars = {
1854
1881
  number: '0123456789',
@@ -2124,7 +2151,7 @@
2124
2151
  return internalFindTreeSelect(tree, predicate, childrenField);
2125
2152
  }
2126
2153
 
2127
- var VERSION = "5.2.1";
2154
+ var VERSION = "5.3.0";
2128
2155
 
2129
2156
  /**
2130
2157
  * 事件触发器,支持浏览器端和 node 端。
@@ -3170,6 +3197,7 @@
3170
3197
  exports.getFileType = getFileType;
3171
3198
  exports.getImageInfo = getImageInfo;
3172
3199
  exports.getMimeType = getMimeType;
3200
+ exports.injectStyle = injectStyle;
3173
3201
  exports.isBankCard = isBankCard;
3174
3202
  exports.isBusinessLicense = isBusinessLicense;
3175
3203
  exports.isChinese = isChinese;