util-helpers 4.16.2 → 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.
Files changed (259) hide show
  1. package/dist/util-helpers.js +1542 -4040
  2. package/dist/util-helpers.js.map +1 -1
  3. package/dist/util-helpers.min.js +1 -1
  4. package/dist/util-helpers.min.js.map +1 -1
  5. package/esm/ajax.js +50 -146
  6. package/esm/blobToDataURL.js +4 -34
  7. package/esm/bytesToSize.js +12 -37
  8. package/esm/calculateCursorPosition.js +30 -52
  9. package/esm/dataURLToBlob.js +14 -28
  10. package/esm/divide.js +24 -48
  11. package/esm/download.js +83 -164
  12. package/esm/fileReader.js +18 -64
  13. package/esm/filterTree.js +25 -55
  14. package/esm/findTreeNode.js +39 -67
  15. package/esm/findTreeNodes.js +35 -66
  16. package/esm/findTreeSelect.js +38 -74
  17. package/esm/formatBankCard.js +13 -43
  18. package/esm/formatMobile.js +17 -43
  19. package/esm/formatMoney.js +59 -139
  20. package/esm/index.js +55 -130
  21. package/esm/isBankCard.js +22 -57
  22. package/esm/isBusinessLicense.js +32 -78
  23. package/esm/isChinese.js +23 -66
  24. package/esm/isEmail.js +6 -21
  25. package/esm/isHMCard.js +5 -28
  26. package/esm/isIPv4.js +5 -22
  27. package/esm/isIPv6.js +5 -36
  28. package/esm/isIdCard.js +27 -66
  29. package/esm/isMobile.js +5 -20
  30. package/esm/isPassport.js +5 -22
  31. package/esm/isPassword.js +7 -48
  32. package/esm/isPostcode.js +5 -20
  33. package/esm/isPromiseLike.js +3 -18
  34. package/esm/isQQ.js +5 -20
  35. package/esm/isSocialCreditCode.js +28 -83
  36. package/esm/isSwiftCode.js +6 -25
  37. package/esm/isTWCard.js +11 -37
  38. package/esm/isTelephone.js +6 -27
  39. package/esm/isUrl.js +5 -44
  40. package/esm/isVehicle.js +5 -28
  41. package/esm/isWX.js +5 -20
  42. package/esm/listToTree.js +51 -120
  43. package/esm/minus.js +21 -39
  44. package/esm/normalizeString.js +8 -29
  45. package/esm/numberToChinese.js +96 -215
  46. package/esm/padZero.js +14 -36
  47. package/esm/parseIdCard.js +64 -129
  48. package/esm/plus.js +21 -39
  49. package/esm/randomString.js +13 -45
  50. package/esm/replaceChar.js +27 -77
  51. package/esm/round.js +14 -31
  52. package/esm/safeDate.js +12 -55
  53. package/esm/setDataURLPrefix.js +6 -29
  54. package/esm/strlen.js +14 -30
  55. package/esm/times.js +24 -42
  56. package/esm/transformFieldNames.js +37 -93
  57. package/esm/treeToList.js +25 -84
  58. package/esm/utils/config.js +4 -14
  59. package/esm/utils/constants.js +4 -4
  60. package/esm/utils/convertToString.js +4 -9
  61. package/esm/utils/devWarn.js +11 -11
  62. package/esm/utils/math.util.js +95 -181
  63. package/esm/utils/type/isBlob.js +4 -24
  64. package/esm/utils/type/isNaN.js +4 -19
  65. package/esm/utils/type/isNil.js +5 -13
  66. package/esm/utils/type/isNull.js +3 -18
  67. package/esm/utils/type/isNumber.js +4 -28
  68. package/esm/utils/type/isObject.js +4 -23
  69. package/esm/utils/type/isString.js +4 -19
  70. package/esm/utils/type/isSymbol.js +4 -19
  71. package/esm/utils/type/isType.js +3 -12
  72. package/esm/utils/type/isUndefined.js +3 -21
  73. package/esm/validatePassword.js +86 -237
  74. package/esm/waitTime.js +7 -26
  75. package/lib/ajax.js +51 -152
  76. package/lib/blobToDataURL.js +5 -40
  77. package/lib/bytesToSize.js +13 -43
  78. package/lib/calculateCursorPosition.js +32 -58
  79. package/lib/dataURLToBlob.js +15 -34
  80. package/lib/divide.js +26 -60
  81. package/lib/download.js +85 -169
  82. package/lib/fileReader.js +19 -70
  83. package/lib/filterTree.js +27 -66
  84. package/lib/findTreeNode.js +40 -77
  85. package/lib/findTreeNodes.js +36 -76
  86. package/lib/findTreeSelect.js +40 -82
  87. package/lib/formatBankCard.js +15 -50
  88. package/lib/formatMobile.js +19 -50
  89. package/lib/formatMoney.js +60 -150
  90. package/lib/index.js +117 -395
  91. package/lib/isBankCard.js +24 -64
  92. package/lib/isBusinessLicense.js +34 -85
  93. package/lib/isChinese.js +24 -73
  94. package/lib/isEmail.js +7 -27
  95. package/lib/isHMCard.js +7 -35
  96. package/lib/isIPv4.js +7 -29
  97. package/lib/isIPv6.js +7 -43
  98. package/lib/isIdCard.js +29 -73
  99. package/lib/isMobile.js +7 -27
  100. package/lib/isPassport.js +7 -29
  101. package/lib/isPassword.js +9 -55
  102. package/lib/isPostcode.js +7 -27
  103. package/lib/isPromiseLike.js +4 -24
  104. package/lib/isQQ.js +7 -27
  105. package/lib/isSocialCreditCode.js +30 -90
  106. package/lib/isSwiftCode.js +7 -32
  107. package/lib/isTWCard.js +13 -44
  108. package/lib/isTelephone.js +7 -33
  109. package/lib/isUrl.js +6 -49
  110. package/lib/isVehicle.js +7 -35
  111. package/lib/isWX.js +7 -27
  112. package/lib/listToTree.js +53 -131
  113. package/lib/minus.js +23 -51
  114. package/lib/normalizeString.js +10 -36
  115. package/lib/numberToChinese.js +98 -222
  116. package/lib/padZero.js +16 -43
  117. package/lib/parseIdCard.js +65 -138
  118. package/lib/plus.js +23 -51
  119. package/lib/randomString.js +14 -51
  120. package/lib/replaceChar.js +28 -83
  121. package/lib/round.js +15 -37
  122. package/lib/safeDate.js +13 -63
  123. package/lib/setDataURLPrefix.js +7 -35
  124. package/lib/strlen.js +16 -37
  125. package/lib/times.js +26 -53
  126. package/lib/transformFieldNames.js +38 -101
  127. package/lib/treeToList.js +27 -94
  128. package/lib/utils/config.js +6 -23
  129. package/lib/utils/constants.js +3 -11
  130. package/lib/utils/convertToString.js +6 -16
  131. package/lib/utils/devWarn.js +13 -17
  132. package/lib/utils/math.util.js +95 -185
  133. package/lib/utils/type/isBlob.js +6 -31
  134. package/lib/utils/type/isNaN.js +6 -26
  135. package/lib/utils/type/isNil.js +7 -20
  136. package/lib/utils/type/isNull.js +4 -24
  137. package/lib/utils/type/isNumber.js +6 -35
  138. package/lib/utils/type/isObject.js +5 -29
  139. package/lib/utils/type/isString.js +6 -26
  140. package/lib/utils/type/isSymbol.js +6 -26
  141. package/lib/utils/type/isType.js +4 -18
  142. package/lib/utils/type/isUndefined.js +4 -27
  143. package/lib/validatePassword.js +87 -244
  144. package/lib/waitTime.js +8 -32
  145. package/package.json +21 -25
  146. package/types/ajax.d.ts +38 -95
  147. package/types/blobToDataURL.d.ts +2 -2
  148. package/types/bytesToSize.d.ts +7 -6
  149. package/types/calculateCursorPosition.d.ts +10 -9
  150. package/types/dataURLToBlob.d.ts +5 -5
  151. package/types/divide.d.ts +2 -2
  152. package/types/download.d.ts +22 -38
  153. package/types/fileReader.d.ts +35 -2
  154. package/types/filterTree.d.ts +6 -7
  155. package/types/findTreeNode.d.ts +5 -7
  156. package/types/findTreeNodes.d.ts +5 -7
  157. package/types/findTreeSelect.d.ts +5 -7
  158. package/types/formatBankCard.d.ts +8 -5
  159. package/types/formatMobile.d.ts +7 -4
  160. package/types/formatMoney.d.ts +8 -7
  161. package/types/index.d.ts +125 -55
  162. package/types/isBankCard.d.ts +9 -8
  163. package/types/isBusinessLicense.d.ts +8 -5
  164. package/types/isChinese.d.ts +9 -8
  165. package/types/isEmail.d.ts +1 -1
  166. package/types/isHMCard.d.ts +2 -2
  167. package/types/isIPv4.d.ts +1 -1
  168. package/types/isIPv6.d.ts +1 -1
  169. package/types/isIdCard.d.ts +8 -7
  170. package/types/isMobile.d.ts +1 -1
  171. package/types/isPassport.d.ts +2 -2
  172. package/types/isPassword.d.ts +4 -7
  173. package/types/isPostcode.d.ts +1 -1
  174. package/types/isPromiseLike.d.ts +1 -1
  175. package/types/isQQ.d.ts +1 -1
  176. package/types/isSocialCreditCode.d.ts +8 -5
  177. package/types/isSwiftCode.d.ts +2 -2
  178. package/types/isTWCard.d.ts +6 -5
  179. package/types/isTelephone.d.ts +1 -1
  180. package/types/isUrl.d.ts +2 -2
  181. package/types/isVehicle.d.ts +2 -2
  182. package/types/isWX.d.ts +1 -1
  183. package/types/listToTree.d.ts +12 -12
  184. package/types/minus.d.ts +3 -2
  185. package/types/normalizeString.d.ts +2 -2
  186. package/types/numberToChinese.d.ts +13 -12
  187. package/types/padZero.d.ts +2 -2
  188. package/types/parseIdCard.d.ts +17 -56
  189. package/types/plus.d.ts +3 -2
  190. package/types/randomString.d.ts +2 -2
  191. package/types/replaceChar.d.ts +15 -11
  192. package/types/round.d.ts +2 -2
  193. package/types/safeDate.d.ts +24 -2
  194. package/types/setDataURLPrefix.d.ts +5 -5
  195. package/types/strlen.d.ts +1 -1
  196. package/types/times.d.ts +3 -2
  197. package/types/transformFieldNames.d.ts +12 -10
  198. package/types/treeToList.d.ts +1 -1
  199. package/types/utils/config.d.ts +6 -5
  200. package/types/utils/constants.d.ts +2 -2
  201. package/types/utils/convertToString.d.ts +1 -1
  202. package/types/utils/devWarn.d.ts +1 -1
  203. package/types/utils/math.util.d.ts +15 -9
  204. package/types/utils/type/index.d.ts +7 -0
  205. package/types/utils/type/isArguments.d.ts +1 -1
  206. package/types/utils/type/isArray.d.ts +1 -1
  207. package/types/utils/type/isArrayBuffer.d.ts +1 -1
  208. package/types/utils/type/isBlob.d.ts +1 -1
  209. package/types/utils/type/isBoolean.d.ts +1 -1
  210. package/types/utils/type/isDate.d.ts +1 -1
  211. package/types/utils/type/isError.d.ts +1 -1
  212. package/types/utils/type/isFunction.d.ts +1 -1
  213. package/types/utils/type/isMap.d.ts +1 -1
  214. package/types/utils/type/isNaN.d.ts +1 -1
  215. package/types/utils/type/isNil.d.ts +1 -1
  216. package/types/utils/type/isNull.d.ts +1 -1
  217. package/types/utils/type/isNumber.d.ts +1 -1
  218. package/types/utils/type/isObject.d.ts +1 -1
  219. package/types/utils/type/isRegExp.d.ts +1 -1
  220. package/types/utils/type/isSet.d.ts +1 -1
  221. package/types/utils/type/isString.d.ts +1 -1
  222. package/types/utils/type/isSymbol.d.ts +1 -1
  223. package/types/utils/type/isType.d.ts +1 -1
  224. package/types/utils/type/isUndefined.d.ts +1 -1
  225. package/types/utils/type/isWeakMap.d.ts +1 -1
  226. package/types/utils/type/isWeakSet.d.ts +1 -1
  227. package/types/validatePassword.d.ts +17 -48
  228. package/types/waitTime.d.ts +2 -2
  229. package/esm/interface.doc.js +0 -125
  230. package/esm/interface.type.js +0 -17
  231. package/esm/utils/type/index.js +0 -29
  232. package/esm/utils/type/isArguments.js +0 -22
  233. package/esm/utils/type/isArray.js +0 -22
  234. package/esm/utils/type/isArrayBuffer.js +0 -25
  235. package/esm/utils/type/isBoolean.js +0 -22
  236. package/esm/utils/type/isDate.js +0 -22
  237. package/esm/utils/type/isError.js +0 -22
  238. package/esm/utils/type/isFunction.js +0 -22
  239. package/esm/utils/type/isMap.js +0 -22
  240. package/esm/utils/type/isRegExp.js +0 -22
  241. package/esm/utils/type/isSet.js +0 -22
  242. package/esm/utils/type/isWeakMap.js +0 -22
  243. package/esm/utils/type/isWeakSet.js +0 -22
  244. package/lib/interface.doc.js +0 -127
  245. package/lib/interface.type.js +0 -5
  246. package/lib/utils/type/index.js +0 -146
  247. package/lib/utils/type/isArguments.js +0 -29
  248. package/lib/utils/type/isArray.js +0 -29
  249. package/lib/utils/type/isArrayBuffer.js +0 -32
  250. package/lib/utils/type/isBoolean.js +0 -29
  251. package/lib/utils/type/isDate.js +0 -29
  252. package/lib/utils/type/isError.js +0 -29
  253. package/lib/utils/type/isFunction.js +0 -29
  254. package/lib/utils/type/isMap.js +0 -29
  255. package/lib/utils/type/isRegExp.js +0 -29
  256. package/lib/utils/type/isSet.js +0 -29
  257. package/lib/utils/type/isWeakMap.js +0 -29
  258. package/lib/utils/type/isWeakSet.js +0 -29
  259. package/types/interface.type.d.ts +0 -2
@@ -1,45 +1,15 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
- /**
8
- * 字节转换存储单位,保留2位小数点
9
- *
10
- * @static
11
- * @alias module:Processor.bytesToSize
12
- * @since 3.8.0
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
- var _default = bytesToSize;
45
- exports["default"] = _default;
14
+
15
+ module.exports = bytesToSize;
@@ -1,61 +1,35 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
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
10
- /**
11
- * 计算输入框的值格式化后光标位置
12
- *
13
- * @static
14
- * @alias module:Other.calculateCursorPosition
15
- * @since 4.6.0
16
- * @see {@link https://2950v9.csb.app/|h5示例}
17
- * @see {@link https://33ccy9.csb.app/|react示例}
18
- * @param {number} prevPos 赋值前的光标位置,onChange/onInput的光标位置 e.target.selectionEnd
19
- * @param {string} prevCtrlValue 上一个格式化后的值
20
- * @param {string} rawValue 当前输入原值
21
- * @param {string} ctrlValue 当前格式化后的值
22
- * @param {Object} [options] 配置项
23
- * @param {string[]|string} [options.placeholderChar=' '] 占位符
24
- * @param {RegExp} [options.maskReg=/\D/g] 需要遮盖的字符规则。默认去掉非数字,意味着 ctrlValue 需要去掉非数字。
25
- * @param {'mobile'|'bankCard'} [options.type] 格式化类型,内置手机号码和银行卡号特殊处理
26
- * @returns {number} 格式化后的光标位置
27
- */
28
- function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue) {
29
- var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
30
- var _options$placeholderC = options.placeholderChar,
31
- placeholderChar = _options$placeholderC === void 0 ? ' ' : _options$placeholderC,
32
- _options$maskReg = options.maskReg,
33
- maskReg = _options$maskReg === void 0 ? /\D/g : _options$maskReg,
34
- type = options.type;
35
- var realCtrlValue = (0, _normalizeString["default"])(prevCtrlValue);
36
- var realRawValue = (0, _normalizeString["default"])(rawValue);
37
- var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
38
- var editLength = realRawValue.length - realCtrlValue.length;
39
- var isAddition = editLength > 0;
40
- var pos = prevPos;
41
- if (isAddition) {
42
- var additionStr = realRawValue.substring(pos - editLength, pos);
43
- var ctrlCharCount = additionStr.replace(maskReg, '').length;
44
- pos -= editLength - ctrlCharCount;
45
- var placeholderCharCount = 0;
46
- while (ctrlCharCount > 0) {
47
- if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
48
- placeholderCharCount++;
49
- } else {
50
- ctrlCharCount--;
51
- }
3
+ var normalizeString = require('./normalizeString.js');
4
+
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;
28
+ }
29
+ if ((type === 'mobile' && (pos === 4 || pos === 9)) || (type === 'bankCard' && pos > 0 && pos % 5 === 0)) {
30
+ pos -= 1;
52
31
  }
53
- pos += placeholderCharCount;
54
- }
55
- if (type === 'mobile' && (pos === 4 || pos === 9) || type === 'bankCard' && pos > 0 && pos % 5 === 0) {
56
- pos -= 1;
57
- }
58
- return pos;
32
+ return pos;
59
33
  }
60
- var _default = calculateCursorPosition;
61
- exports["default"] = _default;
34
+
35
+ module.exports = calculateCursorPosition;
@@ -1,36 +1,17 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
- /**
8
- * DataURL 转为 Blob 对象
9
- *
10
- * @static
11
- * @alias module:Processor.dataURLToBlob
12
- * @since 4.1.0
13
- * @see {@link https://developer.mozilla.org/zh-CN/docs/Glossary/Base64|Base64}
14
- * @param {string} dataurl data: 协议的URL
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
- var _default = dataURLToBlob;
36
- exports["default"] = _default;
16
+
17
+ module.exports = dataURLToBlob;
package/lib/divide.js CHANGED
@@ -1,63 +1,29 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
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
- // 兼容处理,如果参数包含无效数值时,返回 NaN
51
- // @ts-ignore
52
- if (isNaN(num1) || isNaN(num2)) {
53
- return Number.NaN;
54
- }
55
- var num1Changed = (0, _math.float2Fixed)(num1);
56
- var num2Changed = (0, _math.float2Fixed)(num2);
57
- (0, _math.checkBoundary)(num1Changed);
58
- (0, _math.checkBoundary)(num2Changed);
59
- // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
60
- return (0, _times["default"])(num1Changed / num2Changed, (0, _math.strip)(Math.pow(10, (0, _math.digitLength)(num2) - (0, _math.digitLength)(num1))));
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
- var _default = divide;
63
- exports["default"] = _default;
28
+
29
+ module.exports = divide;
package/lib/download.js CHANGED
@@ -1,176 +1,92 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
- var _dataURLToBlob = _interopRequireDefault(require("./dataURLToBlob"));
8
- var _isUrl = _interopRequireDefault(require("./isUrl"));
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); } // 如果修改文档,请同步修改 interface.doc.js
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
- * 处理点击事件,防止事件冒泡到 body/html 的点击事件。
29
- *
30
- * @param {MouseEvent} e 鼠标事件对象
31
- */
32
- function handleClick(e) {
33
- e.stopPropagation();
34
- anchor.removeEventListener('click', handleClick);
35
- }
36
- anchor.addEventListener('click', handleClick);
37
- document.body.appendChild(anchor);
38
- anchor.click();
39
- document.body.removeChild(anchor);
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
- var config = _typeof(options) === 'object' ? options : {};
93
- if (typeof options === 'string') {
94
- config.fileName = options;
95
- }
96
- var fileName = config.fileName,
97
- type = config.type,
98
- dataType = config.dataType,
99
- transformRequest = config.transformRequest,
100
- transformResponse = config.transformResponse;
101
-
102
- /** @type {Blob|undefined} */
103
- var payload;
104
-
105
- // dataURLs、blob url、url、string
106
- if (typeof data === 'string') {
107
- if (!dataType && /^blob:.*?\/.*/.test(data)) {
108
- // blob url
109
- saver(data, fileName);
110
- return Promise.resolve();
111
- } else if (!dataType && /^data:([\w+-]+\/[\w+.-]+)?[,;]/.test(data)) {
112
- // dataURLs
113
- payload = (0, _dataURLToBlob["default"])(data);
114
- } else if (dataType === 'url' || !dataType && (0, _isUrl["default"])(data)) {
115
- // url
116
- // 包装为异步方法
117
- /** @type {(opts: AjaxOptions)=>Promise<AjaxOptions>} */
118
- var asyncTransformRequest = function asyncTransformRequest(opts) {
119
- // 请求前配置调整
120
- var tempOptions = typeof transformRequest === 'function' ? transformRequest(opts) : opts;
121
- // @ts-ignore
122
- return (0, _isPromiseLike["default"])(tempOptions) ? tempOptions : Promise.resolve(tempOptions);
123
- };
124
- /** @type {(res: Blob)=>Promise<Blob>} */
125
- var asyncTransformResponse = function asyncTransformResponse(res) {
126
- var tempRes = typeof transformResponse === 'function' ? transformResponse(res) : res;
127
- // @ts-ignore
128
- return (0, _isPromiseLike["default"])(tempRes) ? tempRes : Promise.resolve(tempRes);
129
- };
130
-
131
- // 此处如果使用 async/await 语法糖,编译后的umd代码过大
132
- return asyncTransformRequest({
133
- responseType: 'blob'
134
- }).then(function (ajaxOptions) {
135
- return (0, _ajax["default"])(data, ajaxOptions).then(function (e) {
136
- // @ts-ignore
137
- return asyncTransformResponse(e.target.response).then(function (res) {
138
- var currentFileName = fileName || data.split("?")[0].split("#")[0].split("/").pop();
139
- return download(res, {
140
- fileName: currentFileName,
141
- type: type || ((0, _type.isBlob)(res) ? res.type : undefined)
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
- var _default = download;
176
- exports["default"] = _default;
91
+
92
+ module.exports = download;