util-helpers 4.13.0 → 4.14.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 (175) hide show
  1. package/README.md +16 -6
  2. package/dist/util-helpers.js +935 -536
  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/blobToDataURL.js +2 -4
  7. package/esm/bytesToSize.js +13 -5
  8. package/esm/calculateCursorPosition.js +7 -12
  9. package/esm/dataURLToBlob.js +2 -5
  10. package/esm/divide.js +9 -13
  11. package/esm/findTreeNode.js +66 -0
  12. package/esm/findTreeSelect.js +80 -0
  13. package/esm/formatBankCard.js +11 -11
  14. package/esm/formatMobile.js +11 -14
  15. package/esm/formatMoney.js +24 -35
  16. package/esm/index.js +9 -4
  17. package/esm/isBankCard.js +9 -13
  18. package/esm/isBusinessLicense.js +19 -20
  19. package/esm/isChinese.js +6 -6
  20. package/esm/isEmail.js +3 -3
  21. package/esm/isHMCard.js +3 -3
  22. package/esm/isIPv4.js +3 -3
  23. package/esm/isIPv6.js +3 -3
  24. package/esm/isIdCard.js +9 -17
  25. package/esm/isMobile.js +3 -3
  26. package/esm/isPassport.js +3 -3
  27. package/esm/isPassword.js +6 -7
  28. package/esm/isPostcode.js +3 -3
  29. package/esm/isPromiseLike.js +0 -2
  30. package/esm/isQQ.js +3 -3
  31. package/esm/isSocialCreditCode.js +29 -28
  32. package/esm/isSwiftCode.js +1 -2
  33. package/esm/isTWCard.js +6 -7
  34. package/esm/isTelephone.js +3 -3
  35. package/esm/isUrl.js +7 -3
  36. package/esm/isVehicle.js +3 -3
  37. package/esm/isWX.js +3 -3
  38. package/esm/listToTree.js +122 -0
  39. package/esm/minus.js +7 -11
  40. package/esm/normalizeString.js +1 -3
  41. package/esm/numberToChinese.js +43 -52
  42. package/esm/padZero.js +1 -5
  43. package/esm/parseIdCard.js +27 -23
  44. package/esm/plus.js +7 -11
  45. package/esm/randomString.js +2 -8
  46. package/esm/replaceChar.js +11 -16
  47. package/esm/round.js +4 -5
  48. package/esm/safeDate.js +5 -7
  49. package/esm/setDataURLPrefix.js +0 -1
  50. package/esm/strlen.js +1 -5
  51. package/esm/times.js +7 -11
  52. package/esm/transformFieldNames.doc.js +34 -0
  53. package/esm/transformFieldNames.js +98 -0
  54. package/esm/transformFieldNames.type.js +19 -0
  55. package/esm/treeToList.js +88 -0
  56. package/esm/utils/config.js +3 -3
  57. package/esm/utils/constants.js +2 -2
  58. package/esm/utils/convertToString.js +1 -2
  59. package/esm/utils/devWarn.js +1 -3
  60. package/esm/utils/math.util.js +20 -29
  61. package/esm/utils/type/index.js +1 -1
  62. package/esm/utils/type/isArguments.js +1 -2
  63. package/esm/utils/type/isArray.js +1 -2
  64. package/esm/utils/type/isBoolean.js +1 -2
  65. package/esm/utils/type/isDate.js +1 -2
  66. package/esm/utils/type/isError.js +1 -2
  67. package/esm/utils/type/isFunction.js +1 -2
  68. package/esm/utils/type/isMap.js +1 -2
  69. package/esm/utils/type/isNaN.js +1 -2
  70. package/esm/utils/type/isNil.js +1 -2
  71. package/esm/utils/type/isNull.js +0 -1
  72. package/esm/utils/type/isNumber.js +1 -2
  73. package/esm/utils/type/isObject.js +0 -3
  74. package/esm/utils/type/isRegExp.js +1 -2
  75. package/esm/utils/type/isSet.js +1 -2
  76. package/esm/utils/type/isString.js +1 -2
  77. package/esm/utils/type/isSymbol.js +1 -2
  78. package/esm/utils/type/isType.js +1 -2
  79. package/esm/utils/type/isUndefined.js +0 -1
  80. package/esm/utils/type/isWeakMap.js +1 -2
  81. package/esm/utils/type/isWeakSet.js +1 -2
  82. package/esm/validatePassword.js +27 -50
  83. package/esm/waitTime.js +0 -1
  84. package/lib/blobToDataURL.js +2 -5
  85. package/lib/bytesToSize.js +13 -6
  86. package/lib/calculateCursorPosition.js +4 -13
  87. package/lib/dataURLToBlob.js +2 -6
  88. package/lib/divide.js +8 -22
  89. package/lib/findTreeNode.js +76 -0
  90. package/lib/findTreeSelect.js +86 -0
  91. package/lib/formatBankCard.js +10 -13
  92. package/lib/formatMobile.js +10 -16
  93. package/lib/formatMoney.js +25 -47
  94. package/lib/index.js +115 -126
  95. package/lib/isBankCard.js +7 -15
  96. package/lib/isBusinessLicense.js +17 -22
  97. package/lib/isChinese.js +6 -9
  98. package/lib/isEmail.js +1 -5
  99. package/lib/isHMCard.js +1 -5
  100. package/lib/isIPv4.js +1 -5
  101. package/lib/isIPv6.js +1 -5
  102. package/lib/isIdCard.js +7 -19
  103. package/lib/isMobile.js +1 -5
  104. package/lib/isPassport.js +1 -5
  105. package/lib/isPassword.js +5 -9
  106. package/lib/isPostcode.js +1 -5
  107. package/lib/isPromiseLike.js +1 -4
  108. package/lib/isQQ.js +1 -5
  109. package/lib/isSocialCreditCode.js +27 -30
  110. package/lib/isSwiftCode.js +1 -5
  111. package/lib/isTWCard.js +4 -9
  112. package/lib/isTelephone.js +1 -5
  113. package/lib/isUrl.js +5 -5
  114. package/lib/isVehicle.js +1 -5
  115. package/lib/isWX.js +1 -5
  116. package/lib/listToTree.js +131 -0
  117. package/lib/minus.js +6 -20
  118. package/lib/normalizeString.js +0 -6
  119. package/lib/numberToChinese.js +41 -55
  120. package/lib/padZero.js +0 -7
  121. package/lib/parseIdCard.js +30 -29
  122. package/lib/plus.js +6 -20
  123. package/lib/randomString.js +2 -8
  124. package/lib/replaceChar.js +10 -18
  125. package/lib/round.js +3 -9
  126. package/lib/safeDate.js +4 -13
  127. package/lib/setDataURLPrefix.js +0 -2
  128. package/lib/strlen.js +0 -7
  129. package/lib/times.js +6 -18
  130. package/lib/transformFieldNames.doc.js +41 -0
  131. package/lib/transformFieldNames.js +104 -0
  132. package/lib/transformFieldNames.type.js +5 -0
  133. package/lib/treeToList.js +96 -0
  134. package/lib/utils/config.js +8 -7
  135. package/lib/utils/constants.js +2 -2
  136. package/lib/utils/convertToString.js +0 -4
  137. package/lib/utils/devWarn.js +0 -4
  138. package/lib/utils/math.util.js +23 -45
  139. package/lib/utils/type/index.js +20 -39
  140. package/lib/utils/type/isArguments.js +0 -4
  141. package/lib/utils/type/isArray.js +0 -4
  142. package/lib/utils/type/isBoolean.js +0 -4
  143. package/lib/utils/type/isDate.js +0 -4
  144. package/lib/utils/type/isError.js +0 -4
  145. package/lib/utils/type/isFunction.js +0 -4
  146. package/lib/utils/type/isMap.js +0 -4
  147. package/lib/utils/type/isNaN.js +0 -4
  148. package/lib/utils/type/isNil.js +0 -5
  149. package/lib/utils/type/isNull.js +0 -2
  150. package/lib/utils/type/isNumber.js +0 -4
  151. package/lib/utils/type/isObject.js +1 -5
  152. package/lib/utils/type/isRegExp.js +0 -4
  153. package/lib/utils/type/isSet.js +0 -4
  154. package/lib/utils/type/isString.js +0 -4
  155. package/lib/utils/type/isSymbol.js +0 -4
  156. package/lib/utils/type/isType.js +1 -2
  157. package/lib/utils/type/isUndefined.js +0 -2
  158. package/lib/utils/type/isWeakMap.js +0 -4
  159. package/lib/utils/type/isWeakSet.js +0 -4
  160. package/lib/validatePassword.js +27 -53
  161. package/lib/waitTime.js +0 -2
  162. package/package.json +9 -7
  163. package/types/bytesToSize.d.ts +9 -1
  164. package/types/findTreeNode.d.ts +26 -0
  165. package/types/findTreeSelect.d.ts +26 -0
  166. package/types/formatBankCard.d.ts +3 -3
  167. package/types/formatMobile.d.ts +4 -4
  168. package/types/index.d.ts +5 -0
  169. package/types/isIdCard.d.ts +2 -0
  170. package/types/isUrl.d.ts +2 -0
  171. package/types/listToTree.d.ts +41 -0
  172. package/types/transformFieldNames.d.ts +34 -0
  173. package/types/transformFieldNames.type.d.ts +2 -0
  174. package/types/treeToList.d.ts +20 -0
  175. package/types/utils/config.d.ts +1 -2
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _normalizeString = _interopRequireDefault(require("./normalizeString"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
9
  /**
13
10
  * 格式化手机号码
14
11
  *
@@ -17,16 +14,16 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
17
14
  * @since 4.5.0
18
15
  * @param {string} mobileNo 手机号码
19
16
  * @param {object} [options] 配置项
20
- * @param {string} [options.char=' '] 间隔字符
17
+ * @param {string} [options.spaceMark=' '] 间隔字符
21
18
  * @returns {string} 格式化后的手机号码
22
19
  * @example
23
20
  *
24
21
  * formatMobile('13345678900'); // 133 4567 8900
25
- * formatMobile('13345678900', { char: '-' }); // 133-4567-8900
22
+ * formatMobile('13345678900', { spaceMark: '-' }); // 133-4567-8900
26
23
  *
27
24
  * // 脱敏手机号码
28
25
  * formatMobile('133****1234'); // 133 **** 1234
29
- * formatMobile('133****1234', { char: '-' }); // 133-****-1234
26
+ * formatMobile('133****1234', { spaceMark: '-' }); // 133-****-1234
30
27
  *
31
28
  * // 手机号码位数不够
32
29
  * formatMobile('133'); // 133
@@ -36,23 +33,20 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
36
33
  */
37
34
  function formatMobile(mobileNo) {
38
35
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
39
-
36
+ // @ts-ignore
37
+ // TODO 下个版本废弃 char
40
38
  var _options$char = options["char"],
41
- _char = _options$char === void 0 ? ' ' : _options$char;
42
-
43
- var regChar = new RegExp(_char, 'g');
39
+ _char = _options$char === void 0 ? ' ' : _options$char;
40
+ var realSpaceMark = 'spaceMark' in options ? options.spaceMark : _char;
41
+ var regChar = new RegExp(realSpaceMark, 'g');
44
42
  var realValue = (0, _normalizeString["default"])(mobileNo).replace(regChar, '').substring(0, 11);
45
-
46
43
  if (realValue.length > 7) {
47
- return realValue.replace(/^(...)(....)/g, "$1".concat(_char, "$2").concat(_char));
44
+ return realValue.replace(/^(...)(....)/g, "$1".concat(realSpaceMark, "$2").concat(realSpaceMark));
48
45
  }
49
-
50
46
  if (realValue.length > 3) {
51
- return realValue.replace(/^(...)/g, "$1".concat(_char));
47
+ return realValue.replace(/^(...)/g, "$1".concat(realSpaceMark));
52
48
  }
53
-
54
49
  return realValue;
55
50
  }
56
-
57
51
  var _default = formatMobile;
58
52
  exports["default"] = _default;
@@ -4,28 +4,18 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _math = require("./utils/math.util");
9
-
10
8
  var _isNaN = _interopRequireDefault(require("./utils/type/isNaN"));
11
-
12
9
  var _devWarn = _interopRequireDefault(require("./utils/devWarn"));
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
-
16
11
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
17
-
18
12
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
19
-
20
13
  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); }
21
-
22
14
  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; }
23
-
24
- function _iterableToArrayLimit(arr, i) { var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
25
-
15
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
26
16
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
27
-
28
17
  var reg = /^[+-]?\d*\.?\d*$/;
18
+
29
19
  /**
30
20
  * 检查数字或数字字符串
31
21
  *
@@ -33,21 +23,20 @@ var reg = /^[+-]?\d*\.?\d*$/;
33
23
  * @param {string} num
34
24
  * @returns 是否为数字
35
25
  */
36
-
37
26
  function checkNumber(num) {
38
27
  if (!(reg.test(num) || (0, _math.isScientificNumber)(num)) || (0, _isNaN["default"])(num) || typeof num !== 'number' && typeof num !== 'string' || num === '') {
39
28
  (0, _devWarn["default"])("".concat(num, " invalid parameter."));
40
29
  return false;
41
- } // 数字超限如果不是是字符串,可能有异常
42
- // 如 1111111111111111111111 // => 1.1111111111111111e+21
43
-
30
+ }
44
31
 
32
+ // 数字超限如果不是是字符串,可能有异常
33
+ // 如 1111111111111111111111 // => 1.1111111111111111e+21
45
34
  if (typeof num === 'number') {
46
35
  (0, _math.checkBoundary)(num);
47
36
  }
48
-
49
37
  return true;
50
38
  }
39
+
51
40
  /**
52
41
  * 格式化整数部分
53
42
  *
@@ -56,22 +45,18 @@ function checkNumber(num) {
56
45
  * @param {string} thousand 千分位符号
57
46
  * @returns 格式化后的值
58
47
  */
59
-
60
-
61
48
  function formatInt(intStr, thousand) {
62
49
  var txt = '';
63
50
  intStr = (0, _math.trimLeftZero)(intStr);
64
51
  intStr = intStr[0] === '+' ? intStr.substring(1) : intStr; // 去掉+符号
65
-
66
52
  var negativeSymbol = Number(intStr) < 0 ? '-' : '';
67
53
  var reArr = negativeSymbol ? intStr.substring(1).split('').reverse() : intStr.split('').reverse();
68
-
69
54
  for (var i = 0; i < reArr.length; i++) {
70
55
  txt += reArr[i] + ((i + 1) % 3 === 0 && i + 1 !== reArr.length ? thousand : '');
71
56
  }
72
-
73
57
  return negativeSymbol + txt.split('').reverse().join('');
74
58
  }
59
+
75
60
  /**
76
61
  * 格式化小数部分,如果使用 toFixed,超大额数字会自动被截断
77
62
  *
@@ -81,25 +66,21 @@ function formatInt(intStr, thousand) {
81
66
  * @param {string} decimal 小数点符号
82
67
  * @returns 格式化后的值
83
68
  */
84
-
85
-
86
69
  function formatDec(decStr, precision, decimal) {
87
70
  if (precision === 0) {
88
71
  return '';
89
72
  }
90
-
91
73
  var zero = 0;
92
74
  var ret = '';
93
-
94
75
  if (decStr && Number(decStr) > 0) {
95
76
  var tmpNum = parseFloat('0.' + decStr);
96
77
  ret = tmpNum.toFixed(precision).substring(2);
97
78
  } else {
98
79
  ret = zero.toFixed(precision).substring(2);
99
80
  }
100
-
101
81
  return decimal + ret;
102
82
  }
83
+
103
84
  /**
104
85
  * 格式化金额
105
86
  *
@@ -137,43 +118,40 @@ function formatDec(decStr, precision, decimal) {
137
118
  * formatMoney(1000.00, { decimal: '&' }); // 1,000&00
138
119
  *
139
120
  */
140
-
141
-
142
121
  var formatMoney = function formatMoney(num) {
143
122
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
144
123
  var _options$precision = options.precision,
145
- precision = _options$precision === void 0 ? 2 : _options$precision,
146
- symbol = options.symbol,
147
- _options$thousand = options.thousand,
148
- thousand = _options$thousand === void 0 ? ',' : _options$thousand,
149
- _options$decimal = options.decimal,
150
- decimal = _options$decimal === void 0 ? '.' : _options$decimal; // 数字参数不正确,返回空字符串
124
+ precision = _options$precision === void 0 ? 2 : _options$precision,
125
+ symbol = options.symbol,
126
+ _options$thousand = options.thousand,
127
+ thousand = _options$thousand === void 0 ? ',' : _options$thousand,
128
+ _options$decimal = options.decimal,
129
+ decimal = _options$decimal === void 0 ? '.' : _options$decimal;
130
+
131
+ // 数字参数不正确,返回空字符串
151
132
  // @ts-ignore
152
-
153
133
  if (!checkNumber(num)) {
154
134
  return '';
155
- } // 参数规整化
156
-
135
+ }
157
136
 
137
+ // 参数规整化
158
138
  if (typeof precision !== 'number' || (0, _isNaN["default"])(precision) || precision < 0) {
159
139
  precision = 2;
160
140
  } else if (precision > 10) {
161
141
  precision = 10;
162
142
  }
163
-
164
143
  symbol = typeof symbol === 'string' ? symbol : '';
165
144
  thousand = typeof thousand === 'string' ? thousand : ',';
166
- decimal = typeof decimal === 'string' ? decimal : '.'; // 转换数字字符串,支持科学记数法
167
-
168
- var strNum = (0, _math.scientificToNumber)(num) + ''; // 整数和小数部分
145
+ decimal = typeof decimal === 'string' ? decimal : '.';
169
146
 
147
+ // 转换数字字符串,支持科学记数法
148
+ var strNum = (0, _math.scientificToNumber)(num) + '';
149
+ // 整数和小数部分
170
150
  var _strNum$split = strNum.split('.'),
171
- _strNum$split2 = _slicedToArray(_strNum$split, 2),
172
- intStr = _strNum$split2[0],
173
- decStr = _strNum$split2[1];
174
-
151
+ _strNum$split2 = _slicedToArray(_strNum$split, 2),
152
+ intStr = _strNum$split2[0],
153
+ decStr = _strNum$split2[1];
175
154
  return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
176
155
  };
177
-
178
156
  var _default = formatMoney;
179
157
  exports["default"] = _default;