util-helpers 4.16.1 → 4.17.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 +3 -1
- package/dist/util-helpers.js +1542 -4040
- 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/ajax.js +50 -146
- package/esm/blobToDataURL.js +4 -34
- package/esm/bytesToSize.js +12 -37
- package/esm/calculateCursorPosition.js +30 -52
- package/esm/dataURLToBlob.js +14 -28
- package/esm/divide.js +24 -48
- package/esm/download.js +83 -164
- package/esm/fileReader.js +18 -64
- package/esm/filterTree.js +25 -57
- package/esm/findTreeNode.js +39 -69
- package/esm/findTreeNodes.js +35 -68
- package/esm/findTreeSelect.js +38 -76
- package/esm/formatBankCard.js +13 -43
- package/esm/formatMobile.js +17 -43
- package/esm/formatMoney.js +59 -139
- package/esm/index.js +55 -130
- package/esm/isBankCard.js +22 -57
- package/esm/isBusinessLicense.js +32 -78
- package/esm/isChinese.js +23 -66
- package/esm/isEmail.js +6 -21
- package/esm/isHMCard.js +5 -28
- package/esm/isIPv4.js +5 -22
- package/esm/isIPv6.js +5 -36
- package/esm/isIdCard.js +27 -66
- package/esm/isMobile.js +5 -20
- package/esm/isPassport.js +5 -22
- package/esm/isPassword.js +7 -48
- package/esm/isPostcode.js +5 -20
- package/esm/isPromiseLike.js +3 -18
- package/esm/isQQ.js +5 -20
- package/esm/isSocialCreditCode.js +28 -83
- package/esm/isSwiftCode.js +6 -25
- package/esm/isTWCard.js +11 -37
- package/esm/isTelephone.js +6 -27
- package/esm/isUrl.js +5 -44
- package/esm/isVehicle.js +5 -28
- package/esm/isWX.js +5 -20
- package/esm/listToTree.js +51 -122
- package/esm/minus.js +21 -39
- package/esm/normalizeString.js +8 -29
- package/esm/numberToChinese.js +96 -215
- package/esm/padZero.js +14 -36
- package/esm/parseIdCard.js +64 -132
- package/esm/plus.js +21 -39
- package/esm/randomString.js +13 -45
- package/esm/replaceChar.js +27 -77
- package/esm/round.js +14 -31
- package/esm/safeDate.js +12 -55
- package/esm/setDataURLPrefix.js +6 -29
- package/esm/strlen.js +14 -30
- package/esm/times.js +24 -42
- package/esm/transformFieldNames.js +37 -95
- package/esm/treeToList.js +25 -86
- package/esm/utils/config.js +4 -14
- package/esm/utils/constants.js +4 -4
- package/esm/utils/convertToString.js +4 -9
- package/esm/utils/devWarn.js +11 -11
- package/esm/utils/math.util.js +95 -181
- package/esm/utils/type/isBlob.js +4 -24
- package/esm/utils/type/isNaN.js +4 -19
- package/esm/utils/type/isNil.js +5 -13
- package/esm/utils/type/isNull.js +3 -18
- package/esm/utils/type/isNumber.js +4 -28
- package/esm/utils/type/isObject.js +4 -23
- package/esm/utils/type/isString.js +4 -19
- package/esm/utils/type/isSymbol.js +4 -19
- package/esm/utils/type/isType.js +3 -12
- package/esm/utils/type/isUndefined.js +3 -21
- package/esm/validatePassword.js +86 -237
- package/esm/waitTime.js +7 -26
- package/lib/ajax.js +51 -152
- package/lib/blobToDataURL.js +5 -40
- package/lib/bytesToSize.js +13 -43
- package/lib/calculateCursorPosition.js +31 -58
- package/lib/dataURLToBlob.js +15 -34
- package/lib/divide.js +26 -60
- package/lib/download.js +85 -169
- package/lib/fileReader.js +19 -70
- package/lib/filterTree.js +27 -66
- package/lib/findTreeNode.js +40 -77
- package/lib/findTreeNodes.js +36 -76
- package/lib/findTreeSelect.js +40 -82
- package/lib/formatBankCard.js +15 -50
- package/lib/formatMobile.js +19 -50
- package/lib/formatMoney.js +60 -150
- package/lib/index.js +117 -395
- package/lib/isBankCard.js +24 -64
- package/lib/isBusinessLicense.js +34 -85
- package/lib/isChinese.js +24 -73
- package/lib/isEmail.js +7 -27
- package/lib/isHMCard.js +7 -35
- package/lib/isIPv4.js +7 -29
- package/lib/isIPv6.js +7 -43
- package/lib/isIdCard.js +29 -73
- package/lib/isMobile.js +7 -27
- package/lib/isPassport.js +7 -29
- package/lib/isPassword.js +9 -55
- package/lib/isPostcode.js +7 -27
- package/lib/isPromiseLike.js +4 -24
- package/lib/isQQ.js +7 -27
- package/lib/isSocialCreditCode.js +30 -90
- package/lib/isSwiftCode.js +7 -32
- package/lib/isTWCard.js +13 -44
- package/lib/isTelephone.js +7 -33
- package/lib/isUrl.js +6 -50
- package/lib/isVehicle.js +7 -35
- package/lib/isWX.js +7 -27
- package/lib/listToTree.js +53 -131
- package/lib/minus.js +23 -51
- package/lib/normalizeString.js +10 -36
- package/lib/numberToChinese.js +98 -222
- package/lib/padZero.js +16 -43
- package/lib/parseIdCard.js +65 -138
- package/lib/plus.js +23 -51
- package/lib/randomString.js +14 -51
- package/lib/replaceChar.js +28 -83
- package/lib/round.js +15 -37
- package/lib/safeDate.js +13 -63
- package/lib/setDataURLPrefix.js +7 -35
- package/lib/strlen.js +16 -37
- package/lib/times.js +26 -53
- package/lib/transformFieldNames.js +38 -101
- package/lib/treeToList.js +27 -94
- package/lib/utils/config.js +6 -23
- package/lib/utils/constants.js +3 -11
- package/lib/utils/convertToString.js +6 -16
- package/lib/utils/devWarn.js +13 -17
- package/lib/utils/math.util.js +95 -185
- package/lib/utils/type/isBlob.js +6 -31
- package/lib/utils/type/isNaN.js +6 -26
- package/lib/utils/type/isNil.js +7 -20
- package/lib/utils/type/isNull.js +4 -24
- package/lib/utils/type/isNumber.js +6 -35
- package/lib/utils/type/isObject.js +5 -29
- package/lib/utils/type/isString.js +6 -26
- package/lib/utils/type/isSymbol.js +6 -26
- package/lib/utils/type/isType.js +4 -18
- package/lib/utils/type/isUndefined.js +4 -27
- package/lib/validatePassword.js +87 -244
- package/lib/waitTime.js +8 -32
- package/package.json +30 -34
- package/types/ajax.d.ts +38 -95
- package/types/blobToDataURL.d.ts +2 -2
- package/types/bytesToSize.d.ts +7 -6
- package/types/calculateCursorPosition.d.ts +10 -9
- package/types/dataURLToBlob.d.ts +5 -5
- package/types/divide.d.ts +2 -2
- package/types/download.d.ts +22 -38
- package/types/fileReader.d.ts +35 -2
- package/types/filterTree.d.ts +6 -7
- package/types/findTreeNode.d.ts +5 -7
- package/types/findTreeNodes.d.ts +5 -7
- package/types/findTreeSelect.d.ts +5 -7
- package/types/formatBankCard.d.ts +8 -5
- package/types/formatMobile.d.ts +7 -4
- package/types/formatMoney.d.ts +8 -7
- package/types/index.d.ts +125 -55
- package/types/isBankCard.d.ts +9 -8
- package/types/isBusinessLicense.d.ts +8 -5
- package/types/isChinese.d.ts +9 -8
- package/types/isEmail.d.ts +1 -1
- package/types/isHMCard.d.ts +2 -2
- package/types/isIPv4.d.ts +1 -1
- package/types/isIPv6.d.ts +1 -1
- package/types/isIdCard.d.ts +8 -7
- package/types/isMobile.d.ts +1 -1
- package/types/isPassport.d.ts +2 -2
- package/types/isPassword.d.ts +4 -7
- package/types/isPostcode.d.ts +1 -1
- package/types/isPromiseLike.d.ts +1 -1
- package/types/isQQ.d.ts +1 -1
- package/types/isSocialCreditCode.d.ts +8 -5
- package/types/isSwiftCode.d.ts +2 -2
- package/types/isTWCard.d.ts +6 -5
- package/types/isTelephone.d.ts +1 -1
- package/types/isUrl.d.ts +2 -2
- package/types/isVehicle.d.ts +2 -2
- package/types/isWX.d.ts +1 -1
- package/types/listToTree.d.ts +12 -12
- package/types/minus.d.ts +3 -2
- package/types/normalizeString.d.ts +2 -2
- package/types/numberToChinese.d.ts +13 -12
- package/types/padZero.d.ts +2 -2
- package/types/parseIdCard.d.ts +17 -56
- package/types/plus.d.ts +3 -2
- package/types/randomString.d.ts +2 -2
- package/types/replaceChar.d.ts +15 -11
- package/types/round.d.ts +2 -2
- package/types/safeDate.d.ts +24 -2
- package/types/setDataURLPrefix.d.ts +5 -5
- package/types/strlen.d.ts +1 -1
- package/types/times.d.ts +3 -2
- package/types/transformFieldNames.d.ts +12 -10
- package/types/treeToList.d.ts +1 -1
- package/types/utils/config.d.ts +6 -5
- package/types/utils/constants.d.ts +2 -2
- package/types/utils/convertToString.d.ts +1 -1
- package/types/utils/devWarn.d.ts +1 -1
- package/types/utils/math.util.d.ts +15 -9
- package/types/utils/type/index.d.ts +7 -0
- package/types/utils/type/isArguments.d.ts +1 -1
- package/types/utils/type/isArray.d.ts +1 -1
- package/types/utils/type/isArrayBuffer.d.ts +1 -1
- package/types/utils/type/isBlob.d.ts +1 -1
- package/types/utils/type/isBoolean.d.ts +1 -1
- package/types/utils/type/isDate.d.ts +1 -1
- package/types/utils/type/isError.d.ts +1 -1
- package/types/utils/type/isFunction.d.ts +1 -1
- package/types/utils/type/isMap.d.ts +1 -1
- package/types/utils/type/isNaN.d.ts +1 -1
- package/types/utils/type/isNil.d.ts +1 -1
- package/types/utils/type/isNull.d.ts +1 -1
- package/types/utils/type/isNumber.d.ts +1 -1
- package/types/utils/type/isObject.d.ts +1 -1
- package/types/utils/type/isRegExp.d.ts +1 -1
- package/types/utils/type/isSet.d.ts +1 -1
- package/types/utils/type/isString.d.ts +1 -1
- package/types/utils/type/isSymbol.d.ts +1 -1
- package/types/utils/type/isType.d.ts +1 -1
- package/types/utils/type/isUndefined.d.ts +1 -1
- package/types/utils/type/isWeakMap.d.ts +1 -1
- package/types/utils/type/isWeakSet.d.ts +1 -1
- package/types/validatePassword.d.ts +17 -48
- package/types/waitTime.d.ts +2 -2
- package/esm/interface.doc.js +0 -125
- package/esm/interface.type.js +0 -17
- package/esm/utils/type/index.js +0 -29
- package/esm/utils/type/isArguments.js +0 -22
- package/esm/utils/type/isArray.js +0 -22
- package/esm/utils/type/isArrayBuffer.js +0 -25
- package/esm/utils/type/isBoolean.js +0 -22
- package/esm/utils/type/isDate.js +0 -22
- package/esm/utils/type/isError.js +0 -22
- package/esm/utils/type/isFunction.js +0 -22
- package/esm/utils/type/isMap.js +0 -22
- package/esm/utils/type/isRegExp.js +0 -22
- package/esm/utils/type/isSet.js +0 -22
- package/esm/utils/type/isWeakMap.js +0 -22
- package/esm/utils/type/isWeakSet.js +0 -22
- package/lib/interface.doc.js +0 -127
- package/lib/interface.type.js +0 -5
- package/lib/utils/type/index.js +0 -146
- package/lib/utils/type/isArguments.js +0 -29
- package/lib/utils/type/isArray.js +0 -29
- package/lib/utils/type/isArrayBuffer.js +0 -32
- package/lib/utils/type/isBoolean.js +0 -29
- package/lib/utils/type/isDate.js +0 -29
- package/lib/utils/type/isError.js +0 -29
- package/lib/utils/type/isFunction.js +0 -29
- package/lib/utils/type/isMap.js +0 -29
- package/lib/utils/type/isRegExp.js +0 -29
- package/lib/utils/type/isSet.js +0 -29
- package/lib/utils/type/isWeakMap.js +0 -29
- package/lib/utils/type/isWeakSet.js +0 -29
- package/types/interface.type.d.ts +0 -2
package/lib/bytesToSize.js
CHANGED
|
@@ -1,45 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* @param {number} bytes 字节大小
|
|
14
|
-
* @param {Object} [options] 配置项
|
|
15
|
-
* @param {string} [options.spaceMark=' '] 间隔字符
|
|
16
|
-
* @param {number} [options.precision=2] 精度
|
|
17
|
-
* @returns {string} 存储单位值
|
|
18
|
-
* @example
|
|
19
|
-
*
|
|
20
|
-
* bytesToSize(0); // 0 B
|
|
21
|
-
* bytesToSize(1024); // 1 KB
|
|
22
|
-
* bytesToSize(3.213243*1024*1024); // 3.21 MB
|
|
23
|
-
* bytesToSize(1024*1024*1024); // 1 GB
|
|
24
|
-
*
|
|
25
|
-
* // 自定义间隔符号
|
|
26
|
-
* bytesToSize(0, { spaceMark: '' }); // 0B
|
|
27
|
-
* bytesToSize(1024, { spaceMark: '' }); // 1KB
|
|
28
|
-
*
|
|
29
|
-
*/
|
|
30
|
-
function bytesToSize(bytes) {
|
|
31
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
32
|
-
var _options$spaceMark = options.spaceMark,
|
|
33
|
-
spaceMark = _options$spaceMark === void 0 ? ' ' : _options$spaceMark,
|
|
34
|
-
_options$precision = options.precision,
|
|
35
|
-
precision = _options$precision === void 0 ? 2 : _options$precision;
|
|
36
|
-
var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
|
|
37
|
-
if (numBytes === 0 || isNaN(numBytes)) return "0".concat(spaceMark, "B");
|
|
38
|
-
var k = 1024;
|
|
39
|
-
// 存储单位
|
|
40
|
-
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
41
|
-
var i = Math.floor(Math.log(numBytes) / Math.log(k));
|
|
42
|
-
return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(precision))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
|
|
3
|
+
function bytesToSize(bytes, options) {
|
|
4
|
+
if (options === void 0) { options = {}; }
|
|
5
|
+
var _a = options.spaceMark, spaceMark = _a === void 0 ? ' ' : _a, _b = options.precision, precision = _b === void 0 ? 2 : _b;
|
|
6
|
+
var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
|
|
7
|
+
if (numBytes === 0 || isNaN(numBytes))
|
|
8
|
+
return "0".concat(spaceMark, "B");
|
|
9
|
+
var k = 1024;
|
|
10
|
+
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
11
|
+
var i = Math.floor(Math.log(numBytes) / Math.log(k));
|
|
12
|
+
return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(precision))).concat(spaceMark).concat(sizes[i]) : numBytes + '';
|
|
43
13
|
}
|
|
44
|
-
|
|
45
|
-
exports
|
|
14
|
+
|
|
15
|
+
module.exports = bytesToSize;
|
|
@@ -1,62 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
var _normalizeString = _interopRequireDefault(require("./normalizeString"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
9
|
-
// ref: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
|
|
3
|
+
var normalizeString = require('./normalizeString.js');
|
|
10
4
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
maskReg = _options$maskReg === void 0 ? /\D/g : _options$maskReg,
|
|
35
|
-
type = options.type;
|
|
36
|
-
var realCtrlValue = (0, _normalizeString["default"])(prevCtrlValue);
|
|
37
|
-
var realRawValue = (0, _normalizeString["default"])(rawValue);
|
|
38
|
-
var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
|
|
39
|
-
var editLength = realRawValue.length - realCtrlValue.length;
|
|
40
|
-
var isAddition = editLength > 0;
|
|
41
|
-
var pos = prevPos;
|
|
42
|
-
if (isAddition) {
|
|
43
|
-
var additionStr = realRawValue.substring(pos - editLength, pos);
|
|
44
|
-
var ctrlCharCount = additionStr.replace(maskReg, '').length;
|
|
45
|
-
pos -= editLength - ctrlCharCount;
|
|
46
|
-
var placeholderCharCount = 0;
|
|
47
|
-
while (ctrlCharCount > 0) {
|
|
48
|
-
if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
|
|
49
|
-
placeholderCharCount++;
|
|
50
|
-
} else {
|
|
51
|
-
ctrlCharCount--;
|
|
52
|
-
}
|
|
5
|
+
function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue, options) {
|
|
6
|
+
if (options === void 0) { options = {}; }
|
|
7
|
+
var _a = options.placeholderChar, placeholderChar = _a === void 0 ? ' ' : _a, _b = options.maskReg, maskReg = _b === void 0 ? /\D/g : _b, type = options.type;
|
|
8
|
+
var realCtrlValue = normalizeString(prevCtrlValue);
|
|
9
|
+
var realRawValue = normalizeString(rawValue);
|
|
10
|
+
var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
|
|
11
|
+
var editLength = realRawValue.length - realCtrlValue.length;
|
|
12
|
+
var isAddition = editLength > 0;
|
|
13
|
+
var pos = prevPos;
|
|
14
|
+
if (isAddition) {
|
|
15
|
+
var additionStr = realRawValue.substring(pos - editLength, pos);
|
|
16
|
+
var ctrlCharCount = additionStr.replace(maskReg, '').length;
|
|
17
|
+
pos -= editLength - ctrlCharCount;
|
|
18
|
+
var placeholderCharCount = 0;
|
|
19
|
+
while (ctrlCharCount > 0) {
|
|
20
|
+
if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
|
|
21
|
+
placeholderCharCount++;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
ctrlCharCount--;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
pos += placeholderCharCount;
|
|
53
28
|
}
|
|
54
|
-
pos
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
pos
|
|
58
|
-
}
|
|
59
|
-
return pos;
|
|
29
|
+
if ((type === 'mobile' && (pos === 4 || pos === 9)) || (type === 'bankCard' && pos > 0 && pos % 5 === 0)) {
|
|
30
|
+
pos -= 1;
|
|
31
|
+
}
|
|
32
|
+
return pos;
|
|
60
33
|
}
|
|
61
|
-
|
|
62
|
-
exports
|
|
34
|
+
|
|
35
|
+
module.exports = calculateCursorPosition;
|
package/lib/dataURLToBlob.js
CHANGED
|
@@ -1,36 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* @returns {Blob} Blob 对象
|
|
16
|
-
* @example
|
|
17
|
-
* const dataurl = 'data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=';
|
|
18
|
-
* dataURLToBlob(dataurl); // Blob {size: 32, type: 'text/html'}
|
|
19
|
-
*/
|
|
20
|
-
function dataURLToBlob(dataurl) {
|
|
21
|
-
var parts = dataurl.split(',');
|
|
22
|
-
var meta = parts[0].substring(5).split(';');
|
|
23
|
-
var type = meta[0];
|
|
24
|
-
var decoder = meta.indexOf('base64') !== -1 ? atob : decodeURIComponent;
|
|
25
|
-
var bstr = decoder(parts[1]);
|
|
26
|
-
var n = bstr.length;
|
|
27
|
-
var u8arr = new Uint8Array(n);
|
|
28
|
-
while (n--) {
|
|
29
|
-
u8arr[n] = bstr.charCodeAt(n);
|
|
30
|
-
}
|
|
31
|
-
return new Blob([u8arr], {
|
|
32
|
-
type: type
|
|
33
|
-
});
|
|
3
|
+
function dataURLToBlob(data) {
|
|
4
|
+
var parts = data.split(',');
|
|
5
|
+
var meta = parts[0].substring(5).split(';');
|
|
6
|
+
var type = meta[0];
|
|
7
|
+
var decoder = meta.indexOf('base64') !== -1 ? atob : decodeURIComponent;
|
|
8
|
+
var bstr = decoder(parts[1]);
|
|
9
|
+
var n = bstr.length;
|
|
10
|
+
var u8arr = new Uint8Array(n);
|
|
11
|
+
while (n--) {
|
|
12
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
13
|
+
}
|
|
14
|
+
return new Blob([u8arr], { type: type });
|
|
34
15
|
}
|
|
35
|
-
|
|
36
|
-
exports
|
|
16
|
+
|
|
17
|
+
module.exports = dataURLToBlob;
|
package/lib/divide.js
CHANGED
|
@@ -1,63 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var _math = require("./utils/math.util");
|
|
8
|
-
var _times = _interopRequireDefault(require("./times"));
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
11
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
12
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
13
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
14
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
15
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
16
|
-
/**
|
|
17
|
-
* 精确除法,支持多个数相除,除数默认为 1 。
|
|
18
|
-
*
|
|
19
|
-
* @static
|
|
20
|
-
* @alias module:Math.divide
|
|
21
|
-
* @since 3.1.0
|
|
22
|
-
* @param {...number|string} nums 被除数和除数
|
|
23
|
-
* @returns {number} 商数
|
|
24
|
-
* @example
|
|
25
|
-
*
|
|
26
|
-
* divide(1.21); // 1.21 除数默认为 1 ,即 1.21/1 = 1.21
|
|
27
|
-
* divide(1.21, 1.1); // 1.1
|
|
28
|
-
* divide(1000, 10, 10); // 10
|
|
29
|
-
* divide(1000, 10, 10, 10); // 1
|
|
30
|
-
*
|
|
31
|
-
* divide(); // NaN 如果没有传入参数,被除数默认为 undefined 。 Number(undefined) 转换为 NaN ,NaN/1 = NaN
|
|
32
|
-
* divide(null); // 0 Number(null) 转换为 0 , 0/1 = 0
|
|
33
|
-
* divide('1.5 ', 0.5); // 3 Number('1.5 ') 转换为 1.5 ,1.5/0.5 = 3
|
|
34
|
-
*
|
|
35
|
-
*/
|
|
36
|
-
function divide() {
|
|
37
|
-
for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
38
|
-
nums[_key] = arguments[_key];
|
|
39
|
-
}
|
|
40
|
-
var num1 = nums[0],
|
|
41
|
-
_nums$ = nums[1],
|
|
42
|
-
num2 = _nums$ === void 0 ? 1 : _nums$,
|
|
43
|
-
rest = nums.slice(2);
|
|
44
|
-
if (rest.length > 0) {
|
|
45
|
-
return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
|
|
46
|
-
}
|
|
47
|
-
num1 = (0, _math.transformEffectiveNumber)(num1);
|
|
48
|
-
num2 = (0, _math.transformEffectiveNumber)(num2);
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var math_util = require('./utils/math.util.js');
|
|
5
|
+
var times = require('./times.js');
|
|
6
|
+
var isNaN = require('./utils/type/isNaN.js');
|
|
49
7
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
8
|
+
function divide() {
|
|
9
|
+
var nums = [];
|
|
10
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
11
|
+
nums[_i] = arguments[_i];
|
|
12
|
+
}
|
|
13
|
+
var _a = tslib.__read(nums), num1 = _a[0], _b = _a[1], num2 = _b === void 0 ? 1 : _b, rest = _a.slice(2);
|
|
14
|
+
if (rest.length > 0) {
|
|
15
|
+
return divide.apply(void 0, tslib.__spreadArray([divide(num1, num2)], tslib.__read(rest), false));
|
|
16
|
+
}
|
|
17
|
+
num1 = math_util.transformEffectiveNumber(num1);
|
|
18
|
+
num2 = math_util.transformEffectiveNumber(num2);
|
|
19
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
20
|
+
return Number.NaN;
|
|
21
|
+
}
|
|
22
|
+
var num1Changed = math_util.float2Fixed(num1);
|
|
23
|
+
var num2Changed = math_util.float2Fixed(num2);
|
|
24
|
+
math_util.checkBoundary(num1Changed);
|
|
25
|
+
math_util.checkBoundary(num2Changed);
|
|
26
|
+
return times(num1Changed / num2Changed, math_util.strip(Math.pow(10, math_util.digitLength(num2) - math_util.digitLength(num1))));
|
|
61
27
|
}
|
|
62
|
-
|
|
63
|
-
exports
|
|
28
|
+
|
|
29
|
+
module.exports = divide;
|
package/lib/download.js
CHANGED
|
@@ -1,176 +1,92 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var _ajax = _interopRequireDefault(require("./ajax"));
|
|
10
|
-
var _type = require("./utils/type");
|
|
11
|
-
var _isPromiseLike = _interopRequireDefault(require("./isPromiseLike"));
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
14
|
-
/**
|
|
15
|
-
* 下载文件
|
|
16
|
-
*
|
|
17
|
-
* @param {string} blobUrl blob 地址
|
|
18
|
-
* @param {string} [fileName] 文件名称
|
|
19
|
-
*/
|
|
20
|
-
function saver(blobUrl, fileName) {
|
|
21
|
-
var anchor = document.createElement('a');
|
|
22
|
-
// anchor.href = decodeURIComponent(blobUrl);
|
|
23
|
-
anchor.href = blobUrl;
|
|
24
|
-
anchor.style.display = 'none';
|
|
25
|
-
anchor.setAttribute('download', fileName || '');
|
|
3
|
+
var tslib = require('tslib');
|
|
4
|
+
var dataURLToBlob = require('./dataURLToBlob.js');
|
|
5
|
+
var isUrl = require('./isUrl.js');
|
|
6
|
+
var ajax = require('./ajax.js');
|
|
7
|
+
var isBlob = require('./utils/type/isBlob.js');
|
|
8
|
+
var isPromiseLike = require('./isPromiseLike.js');
|
|
26
9
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
e
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
10
|
+
function saver(blobUrl, fileName) {
|
|
11
|
+
if (fileName === void 0) { fileName = ''; }
|
|
12
|
+
var anchor = document.createElement('a');
|
|
13
|
+
anchor.href = blobUrl;
|
|
14
|
+
anchor.style.display = 'none';
|
|
15
|
+
anchor.setAttribute('download', fileName);
|
|
16
|
+
function handleClick(e) {
|
|
17
|
+
e.stopPropagation();
|
|
18
|
+
anchor.removeEventListener('click', handleClick);
|
|
19
|
+
}
|
|
20
|
+
anchor.addEventListener('click', handleClick);
|
|
21
|
+
document.body.appendChild(anchor);
|
|
22
|
+
anchor.click();
|
|
23
|
+
document.body.removeChild(anchor);
|
|
40
24
|
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @typedef {import('./ajax.js').AjaxOptions} AjaxOptions ajax 配置项
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @callback TransformRequest
|
|
48
|
-
* @param {AjaxOptions} options ajax 配置项
|
|
49
|
-
* @returns {AjaxOptions | Promise<AjaxOptions>}
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @callback TransformResponse
|
|
54
|
-
* @param {Blob} res 响应的Blob对象。如果你通过 transformRequest 修改了 responseType ,该参数将是该类型响应值。
|
|
55
|
-
* @returns {Blob | Promise<Blob>}
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @typedef {Object} DownloadOptions 下载配置项
|
|
60
|
-
* @property {string} [options.fileName] 文件名称
|
|
61
|
-
* @property {string} [options.type] MIME 类型
|
|
62
|
-
* @property {'url'|'text'} [options.dataType] 手动设置数据类型,默认会根据传入的数据判断类型,主要是为了区分 url 和 text 。<br/>如果你要下载的文本是 url ,请设置 'text' ;如果你要下载的 url 是绝对/相对路径,请设置 'url' 。
|
|
63
|
-
* @property {TransformRequest} [options.transformRequest] 请求前触发,XHR 对象或配置调整
|
|
64
|
-
* @property {TransformResponse} [options.transformResponse] 请求成功后触发,在传递给 then/catch 前,允许修改响应数据
|
|
65
|
-
*/
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 下载<br/><br/>
|
|
69
|
-
*
|
|
70
|
-
* <em style="font-weight: bold;">注意:该方法仅适用于浏览器端,兼容 IE10+ 和现代浏览器。</em>
|
|
71
|
-
*
|
|
72
|
-
* @static
|
|
73
|
-
* @alias module:Other.download
|
|
74
|
-
* @since 4.16.0
|
|
75
|
-
* @see {@link https://zh.wikipedia.org/wiki/多用途互聯網郵件擴展|MIME}
|
|
76
|
-
* @see {@link https://9ykc9s.csb.app/|在线示例}
|
|
77
|
-
* @param {string|Blob|ArrayBuffer|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|BigInt64Array|BigUint64Array} data 字符串、blob数据或url地址
|
|
78
|
-
* @param {string|DownloadOptions} [options] 文件名称 或 配置项
|
|
79
|
-
* @returns {Promise<void>}
|
|
80
|
-
* @example
|
|
81
|
-
* // 文本
|
|
82
|
-
* download('hello world', 'text.txt');
|
|
83
|
-
*
|
|
84
|
-
* // 远程文件
|
|
85
|
-
* download('/xxx.jpg', { dataType: 'url' });
|
|
86
|
-
*
|
|
87
|
-
* // blob文件
|
|
88
|
-
* download(new Blob(['hello world']), 'text.txt');
|
|
89
|
-
*
|
|
90
|
-
*/
|
|
91
25
|
function download(data, options) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
26
|
+
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
27
|
+
var config, fileName, type, dataType, transformRequest, transformResponse, payload, asyncTransformRequest, asyncTransformResponse, ajaxOptions, ev, res, currentFileName, url;
|
|
28
|
+
return tslib.__generator(this, function (_a) {
|
|
29
|
+
switch (_a.label) {
|
|
30
|
+
case 0:
|
|
31
|
+
config = typeof options === 'object' ? options : {};
|
|
32
|
+
if (typeof options === 'string') {
|
|
33
|
+
config.fileName = options;
|
|
34
|
+
}
|
|
35
|
+
fileName = config.fileName, type = config.type, dataType = config.dataType, transformRequest = config.transformRequest, transformResponse = config.transformResponse;
|
|
36
|
+
if (!(typeof data === 'string')) return [3, 8];
|
|
37
|
+
if (!(!dataType && /^blob:.*?\/.*/.test(data))) return [3, 1];
|
|
38
|
+
saver(data, fileName);
|
|
39
|
+
return [2, Promise.resolve()];
|
|
40
|
+
case 1:
|
|
41
|
+
if (!(!dataType && /^data:([\w+-]+\/[\w+.-]+)?[,;]/.test(data))) return [3, 2];
|
|
42
|
+
payload = dataURLToBlob(data);
|
|
43
|
+
return [3, 7];
|
|
44
|
+
case 2:
|
|
45
|
+
if (!(dataType === 'url' || (!dataType && isUrl(data)))) return [3, 6];
|
|
46
|
+
asyncTransformRequest = function (opts) {
|
|
47
|
+
var tempOptions = typeof transformRequest === 'function' ? transformRequest(opts) : opts;
|
|
48
|
+
return isPromiseLike(tempOptions) ? tempOptions : Promise.resolve(tempOptions);
|
|
49
|
+
};
|
|
50
|
+
asyncTransformResponse = function (res) {
|
|
51
|
+
var tempRes = typeof transformResponse === 'function' ? transformResponse(res) : res;
|
|
52
|
+
return isPromiseLike(tempRes) ? tempRes : Promise.resolve(tempRes);
|
|
53
|
+
};
|
|
54
|
+
return [4, asyncTransformRequest({ responseType: 'blob' })];
|
|
55
|
+
case 3:
|
|
56
|
+
ajaxOptions = _a.sent();
|
|
57
|
+
return [4, ajax(data, ajaxOptions)];
|
|
58
|
+
case 4:
|
|
59
|
+
ev = _a.sent();
|
|
60
|
+
return [4, asyncTransformResponse(ev.target.response)];
|
|
61
|
+
case 5:
|
|
62
|
+
res = _a.sent();
|
|
63
|
+
currentFileName = fileName || data.split('?')[0].split('#')[0].split('/').pop();
|
|
64
|
+
return [2, download(res, { fileName: currentFileName, type: type || (isBlob(res) ? res.type : undefined) })];
|
|
65
|
+
case 6:
|
|
66
|
+
payload = new Blob([data], { type: type || 'text/plain' });
|
|
67
|
+
_a.label = 7;
|
|
68
|
+
case 7: return [3, 9];
|
|
69
|
+
case 8:
|
|
70
|
+
if (data instanceof Blob) {
|
|
71
|
+
payload = data;
|
|
72
|
+
}
|
|
73
|
+
_a.label = 9;
|
|
74
|
+
case 9:
|
|
75
|
+
if (!payload) {
|
|
76
|
+
payload = new Blob([data], { type: type });
|
|
77
|
+
}
|
|
78
|
+
if (navigator.msSaveBlob) {
|
|
79
|
+
navigator.msSaveBlob(payload, fileName || 'download');
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
url = URL.createObjectURL(payload);
|
|
83
|
+
saver(url, fileName);
|
|
84
|
+
URL.revokeObjectURL(url);
|
|
85
|
+
}
|
|
86
|
+
return [2, Promise.resolve()];
|
|
87
|
+
}
|
|
144
88
|
});
|
|
145
|
-
});
|
|
146
|
-
} else {
|
|
147
|
-
// string
|
|
148
|
-
payload = new Blob([data], {
|
|
149
|
-
type: type || 'text/plain'
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
} else if ((0, _type.isBlob)(data)) {
|
|
153
|
-
// @ts-ignore
|
|
154
|
-
payload = data;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// html、TypedArray
|
|
158
|
-
if (!(payload instanceof Blob)) {
|
|
159
|
-
payload = new Blob([data], {
|
|
160
|
-
type: type
|
|
161
89
|
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// @ts-ignore
|
|
165
|
-
if (navigator.msSaveBlob) {
|
|
166
|
-
// @ts-ignore
|
|
167
|
-
navigator.msSaveBlob(payload, fileName || 'download');
|
|
168
|
-
} else {
|
|
169
|
-
var url = URL.createObjectURL(payload);
|
|
170
|
-
saver(url, fileName);
|
|
171
|
-
URL.revokeObjectURL(url);
|
|
172
|
-
}
|
|
173
|
-
return Promise.resolve();
|
|
174
90
|
}
|
|
175
|
-
|
|
176
|
-
exports
|
|
91
|
+
|
|
92
|
+
module.exports = download;
|