util-helpers 5.2.1 → 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.
- package/README.md +18 -17
- package/dist/util-helpers.js +75 -70
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/VERSION.js +1 -1
- package/esm/checkFileType.js +6 -4
- package/esm/getMimeType.js +2 -2
- package/esm/index.js +3 -3
- package/esm/utils/file.util.js +6 -3
- package/lib/VERSION.js +1 -1
- package/lib/checkFileType.js +6 -4
- package/lib/getMimeType.js +2 -2
- package/lib/index.js +6 -6
- package/lib/utils/file.util.js +5 -1
- package/package.json +1 -1
- package/types/VERSION.d.ts +1 -2
- package/types/ajax.d.ts +2 -3
- package/types/bytesToSize.d.ts +0 -1
- package/types/calculateCursorPosition.d.ts +0 -1
- package/types/checkFileType.d.ts +3 -2
- package/types/compressImage.d.ts +1 -1
- package/types/dataURLToBlob.d.ts +1 -2
- package/types/divide.d.ts +0 -1
- package/types/download.d.ts +1 -2
- package/types/fileReader.d.ts +1 -1
- package/types/filterTree.d.ts +0 -1
- package/types/findTreeNode.d.ts +0 -1
- package/types/findTreeNodes.d.ts +0 -1
- package/types/findTreeSelect.d.ts +0 -1
- package/types/formatBankCard.d.ts +0 -1
- package/types/formatMobile.d.ts +0 -1
- package/types/formatMoney.d.ts +0 -1
- package/types/gcd.d.ts +0 -1
- package/types/getFileBlob.d.ts +1 -2
- package/types/getFileType.d.ts +1 -3
- package/types/getImageInfo.d.ts +1 -2
- package/types/getMimeType.d.ts +1 -1
- package/types/index.d.ts +12 -10
- package/types/isBankCard.d.ts +0 -1
- package/types/isBusinessLicense.d.ts +0 -1
- package/types/isChinese.d.ts +0 -1
- package/types/isEmail.d.ts +0 -1
- package/types/isHMCard.d.ts +0 -1
- package/types/isIPv4.d.ts +0 -1
- package/types/isIPv6.d.ts +0 -1
- package/types/isIdCard.d.ts +0 -1
- package/types/isMobile.d.ts +0 -1
- package/types/isPassport.d.ts +0 -1
- package/types/isPassword.d.ts +0 -1
- package/types/isPostcode.d.ts +0 -1
- package/types/isQQ.d.ts +0 -1
- package/types/isSocialCreditCode.d.ts +0 -1
- package/types/isSwiftCode.d.ts +0 -1
- package/types/isTWCard.d.ts +0 -1
- package/types/isTelephone.d.ts +0 -1
- package/types/isUrl.d.ts +0 -1
- package/types/isValidNumber.d.ts +0 -1
- package/types/isVehicle.d.ts +0 -1
- package/types/isWX.d.ts +0 -1
- package/types/lcm.d.ts +0 -1
- package/types/listToTree.d.ts +0 -1
- package/types/loadImage.d.ts +1 -2
- package/types/loadImageWithBlob.d.ts +1 -1
- package/types/loadScript.d.ts +1 -2
- package/types/minus.d.ts +0 -1
- package/types/numberToChinese.d.ts +0 -1
- package/types/padZero.d.ts +0 -1
- package/types/parseIdCard.d.ts +0 -1
- package/types/plus.d.ts +0 -1
- package/types/replaceChar.d.ts +0 -1
- package/types/round.d.ts +0 -1
- package/types/setDataURLPrefix.d.ts +0 -1
- package/types/strlen.d.ts +0 -1
- package/types/times.d.ts +0 -1
- package/types/transformFieldNames.d.ts +0 -1
- package/types/transformObjectValue.d.ts +0 -1
- package/types/treeToList.d.ts +0 -1
- package/types/utils/config.d.ts +0 -1
- package/types/utils/file.util.d.ts +4 -0
- package/types/validatePassword.d.ts +0 -1
- 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-
|
|
56
|
-
- [
|
|
57
|
-
- [
|
|
58
|
-
- [
|
|
59
|
-
- [download](https://doly-dev.github.io/util-helpers/module-
|
|
60
|
-
- [
|
|
61
|
-
- [
|
|
62
|
-
- [
|
|
63
|
-
- [
|
|
64
|
-
- [
|
|
65
|
-
- [
|
|
66
|
-
- [
|
|
67
|
-
- [
|
|
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
|
|
package/dist/util-helpers.js
CHANGED
|
@@ -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
|
|
1459
|
-
|
|
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
|
|
1428
|
+
return !!name && getFileNameExt(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
|
-
|
|
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,7 +1784,7 @@
|
|
|
1809
1784
|
];
|
|
1810
1785
|
function getMimeType(fileName) {
|
|
1811
1786
|
var _a;
|
|
1812
|
-
var ext =
|
|
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.
|
|
2132
|
+
var VERSION = "5.2.2";
|
|
2128
2133
|
|
|
2129
2134
|
/**
|
|
2130
2135
|
* 事件触发器,支持浏览器端和 node 端。
|