util-helpers 4.13.1 → 4.14.1

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 (174) hide show
  1. package/README.md +16 -6
  2. package/dist/util-helpers.js +940 -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 +125 -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 +35 -0
  53. package/esm/transformFieldNames.js +100 -0
  54. package/esm/transformFieldNames.type.js +19 -0
  55. package/esm/treeToList.js +88 -0
  56. package/esm/utils/config.js +4 -4
  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 +134 -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 +42 -0
  131. package/lib/transformFieldNames.js +106 -0
  132. package/lib/transformFieldNames.type.js +5 -0
  133. package/lib/treeToList.js +96 -0
  134. package/lib/utils/config.js +6 -6
  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 +6 -5
  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 +43 -0
  172. package/types/transformFieldNames.d.ts +35 -0
  173. package/types/transformFieldNames.type.d.ts +2 -0
  174. package/types/treeToList.d.ts +20 -0
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _isType = _interopRequireDefault(require("./isType"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
9
  /**
13
10
  * 检查值是否为Symbol
14
11
  *
@@ -28,6 +25,5 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
28
25
  function isSymbol(value) {
29
26
  return (0, _isType["default"])(value, 'Symbol');
30
27
  }
31
-
32
28
  var _default = isSymbol;
33
29
  exports["default"] = _default;
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports["default"] = void 0;
7
7
  var toString = Object.prototype.toString;
8
+
8
9
  /**
9
10
  * 检测值的 `toString` 类型
10
11
  *
@@ -14,10 +15,8 @@ var toString = Object.prototype.toString;
14
15
  * @param {string} typename 类型名称
15
16
  * @returns {boolean} 返回值的 `toString` 类型是否匹配
16
17
  */
17
-
18
18
  function isType(value, typename) {
19
19
  return toString.call(value) === "[object ".concat(typename, "]");
20
20
  }
21
-
22
21
  var _default = isType;
23
22
  exports["default"] = _default;
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  /**
9
8
  * 检查值是否为Undefined
10
9
  *
@@ -27,6 +26,5 @@ exports["default"] = void 0;
27
26
  function isUndefined(value) {
28
27
  return value === void 0;
29
28
  }
30
-
31
29
  var _default = isUndefined;
32
30
  exports["default"] = _default;
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _isType = _interopRequireDefault(require("./isType"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
9
  /**
13
10
  * 检查值是否为WeakMap
14
11
  *
@@ -28,6 +25,5 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
28
25
  function isWeakMap(value) {
29
26
  return (0, _isType["default"])(value, 'WeakMap');
30
27
  }
31
-
32
28
  var _default = isWeakMap;
33
29
  exports["default"] = _default;
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _isType = _interopRequireDefault(require("./isType"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
9
  /**
13
10
  * 检查值是否为WeakSet
14
11
  *
@@ -28,6 +25,5 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
28
25
  function isWeakSet(value) {
29
26
  return (0, _isType["default"])(value, 'WeakSet');
30
27
  }
31
-
32
28
  var _default = isWeakSet;
33
29
  exports["default"] = _default;
@@ -4,15 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  var _devWarn = _interopRequireDefault(require("./utils/devWarn"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
9
  var regNumber = /[\d]/;
13
10
  var regLowerCaseLetter = /[a-z]/;
14
11
  var regUpperCaseLetter = /[A-Z]/;
15
12
  var regAllNumberAndLetter = /[\d|a-z]/gi;
13
+
16
14
  /**
17
15
  * 是否包含数字
18
16
  *
@@ -20,10 +18,10 @@ var regAllNumberAndLetter = /[\d|a-z]/gi;
20
18
  * @param {string} val 检查的值
21
19
  * @returns {boolean} 是否包含数字
22
20
  */
23
-
24
21
  function hasNumber(val) {
25
22
  return regNumber.test(val);
26
23
  }
24
+
27
25
  /**
28
26
  * 是否包含小写字母
29
27
  *
@@ -31,11 +29,10 @@ function hasNumber(val) {
31
29
  * @param {string} val 检测的值
32
30
  * @returns {boolean} 是否包含小写字母
33
31
  */
34
-
35
-
36
32
  function hasLowerCaseLetter(val) {
37
33
  return regLowerCaseLetter.test(val);
38
34
  }
35
+
39
36
  /**
40
37
  * 是否包含大写字母
41
38
  *
@@ -43,11 +40,10 @@ function hasLowerCaseLetter(val) {
43
40
  * @param {string} val 检测的值
44
41
  * @returns {boolean} 是否包含大写字母
45
42
  */
46
-
47
-
48
43
  function hasUpperCaseLetter(val) {
49
44
  return regUpperCaseLetter.test(val);
50
45
  }
46
+
51
47
  /**
52
48
  * 是否为十六进制
53
49
  *
@@ -55,11 +51,10 @@ function hasUpperCaseLetter(val) {
55
51
  * @param {string} val 检测的值
56
52
  * @returns {boolean} 是否为十六进制
57
53
  */
58
-
59
-
60
54
  function hasHex(val) {
61
55
  return val.indexOf('\\x') > -1 || val.indexOf("\\u") > -1;
62
56
  }
57
+
63
58
  /**
64
59
  * 是否包含特殊字符
65
60
  *
@@ -68,35 +63,28 @@ function hasHex(val) {
68
63
  * @param {string} chars 特殊字符
69
64
  * @returns {boolean} 是否包含特殊字符
70
65
  */
71
-
72
-
73
66
  function hasSpecialCharacter(val, chars) {
74
67
  if (!chars || !val) {
75
68
  return false;
76
69
  }
77
-
78
70
  var specialChars = val.replace(regAllNumberAndLetter, '');
79
-
80
71
  if (!specialChars) {
81
72
  return false;
82
73
  }
83
-
84
74
  var regChars = hasHex(chars) ? new RegExp("[".concat(chars, "]")) : null;
85
-
86
75
  if (regChars) {
87
76
  return regChars.test(specialChars);
88
77
  }
89
-
90
78
  var ret = false;
91
79
  specialChars.split('').some(function (charItem) {
92
80
  if (chars.indexOf(charItem) > -1) {
93
81
  ret = true;
94
82
  }
95
-
96
83
  return ret;
97
84
  });
98
85
  return ret;
99
86
  }
87
+
100
88
  /**
101
89
  * 是否包含非法字符
102
90
  *
@@ -105,37 +93,30 @@ function hasSpecialCharacter(val, chars) {
105
93
  * @param {string} chars 特殊字符
106
94
  * @returns {boolean} 是否包含非法字符
107
95
  */
108
-
109
-
110
96
  function hasUnallowableCharacter(val, chars) {
111
97
  if (!val) {
112
98
  return false;
113
99
  }
114
-
115
100
  var specialChars = val.replace(regAllNumberAndLetter, '');
116
-
117
101
  if (!specialChars) {
118
102
  return false;
119
103
  } else if (!chars) {
120
104
  return true;
121
105
  }
122
-
123
106
  var regChars = hasHex(chars) ? new RegExp("[^".concat(chars, "]")) : null;
124
-
125
107
  if (regChars) {
126
108
  return regChars.test(specialChars);
127
109
  }
128
-
129
110
  var ret = false;
130
111
  specialChars.split('').some(function (charItem) {
131
112
  if (chars.indexOf(charItem) === -1) {
132
113
  ret = true;
133
114
  }
134
-
135
115
  return ret;
136
116
  });
137
117
  return ret;
138
118
  }
119
+
139
120
  /**
140
121
  * @typedef {Object} PasswordContaines - 验证密码的包含内容
141
122
  * @property {boolean} number - 包含数字
@@ -224,40 +205,36 @@ function hasUnallowableCharacter(val, chars) {
224
205
  * }
225
206
  * }
226
207
  */
227
-
228
-
229
208
  function validatePassword(value) {
230
209
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
231
210
  var _options$level = options.level,
232
- level = _options$level === void 0 ? 2 : _options$level,
233
- _options$ignoreCase = options.ignoreCase,
234
- ignoreCase = _options$ignoreCase === void 0 ? false : _options$ignoreCase,
235
- _options$special = options.special,
236
- special = _options$special === void 0 ? '\\x21-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E' : _options$special;
211
+ level = _options$level === void 0 ? 2 : _options$level,
212
+ _options$ignoreCase = options.ignoreCase,
213
+ ignoreCase = _options$ignoreCase === void 0 ? false : _options$ignoreCase,
214
+ _options$special = options.special,
215
+ special = _options$special === void 0 ? '\\x21-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E' : _options$special;
237
216
  var valStr = value;
238
-
239
217
  if (typeof value !== 'string') {
240
218
  (0, _devWarn["default"])("[validatePassword] value must be a string.");
241
219
  valStr = '';
242
220
  }
243
-
244
- var currentLevel = 0; // 包含数字
245
-
246
- var containesNumber = hasNumber(valStr); // 包含小写字母
247
-
248
- var containesLowerCaseLetter = hasLowerCaseLetter(valStr); // 包含大写字母
249
-
250
- var containesUpperCaseLetter = hasUpperCaseLetter(valStr); // 包含特殊字符
251
-
252
- var containesSpecialCharacter = hasSpecialCharacter(valStr, special); // 包含非法字符,即含有非数字字母特殊字符以外的其他字符
253
-
221
+ var currentLevel = 0;
222
+
223
+ // 包含数字
224
+ var containesNumber = hasNumber(valStr);
225
+ // 包含小写字母
226
+ var containesLowerCaseLetter = hasLowerCaseLetter(valStr);
227
+ // 包含大写字母
228
+ var containesUpperCaseLetter = hasUpperCaseLetter(valStr);
229
+ // 包含特殊字符
230
+ var containesSpecialCharacter = hasSpecialCharacter(valStr, special);
231
+ // 包含非法字符,即含有非数字字母特殊字符以外的其他字符
254
232
  var containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
255
-
256
233
  if (containesNumber) {
257
234
  currentLevel += 1;
258
- } // 不区分大小写
259
-
235
+ }
260
236
 
237
+ // 不区分大小写
261
238
  if (ignoreCase) {
262
239
  if (containesLowerCaseLetter || containesUpperCaseLetter) {
263
240
  currentLevel += 1;
@@ -267,17 +244,15 @@ function validatePassword(value) {
267
244
  if (containesLowerCaseLetter) {
268
245
  currentLevel += 1;
269
246
  }
270
-
271
247
  if (containesUpperCaseLetter) {
272
248
  currentLevel += 1;
273
249
  }
274
250
  }
275
-
276
251
  if (containesSpecialCharacter) {
277
252
  currentLevel += 1;
278
- } // 验证结果
279
-
253
+ }
280
254
 
255
+ // 验证结果
281
256
  var validated = currentLevel >= level && !containesUnallowableCharacter;
282
257
  return {
283
258
  validated: validated,
@@ -291,6 +266,5 @@ function validatePassword(value) {
291
266
  }
292
267
  };
293
268
  }
294
-
295
269
  var _default = validatePassword;
296
270
  exports["default"] = _default;
package/lib/waitTime.js CHANGED
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
-
8
7
  /**
9
8
  * 等待时间返回 Promise 。常用于异步方法中延时。
10
9
  *
@@ -31,6 +30,5 @@ function waitTime() {
31
30
  setTimeout(resolve, time);
32
31
  });
33
32
  }
34
-
35
33
  var _default = waitTime;
36
34
  exports["default"] = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.13.1",
3
+ "version": "4.14.1",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -61,22 +61,23 @@
61
61
  "@babel/plugin-transform-modules-commonjs": "^7.14.0",
62
62
  "@babel/plugin-transform-runtime": "^7.13.15",
63
63
  "@babel/preset-env": "^7.9.0",
64
+ "@babel/preset-typescript": "^7.18.6",
64
65
  "@commitlint/cli": "^11.0.0",
65
66
  "@commitlint/config-conventional": "^11.0.0",
66
67
  "@rollup/plugin-babel": "^5.3.1",
67
68
  "@rollup/plugin-commonjs": "^19.0.0",
68
69
  "@rollup/plugin-node-resolve": "^13.0.0",
69
70
  "@rollup/plugin-replace": "^5.0.1",
70
- "@types/jest": "^26.0.23",
71
- "babel-jest": "^27.0.2",
72
- "babel-plugin-inline-json": "^2.1.0",
71
+ "@types/jest": "^29.2.4",
72
+ "babel-jest": "^29.3.1",
73
73
  "babel-plugin-minify-replace": "^0.5.0",
74
74
  "cross-env": "^7.0.3",
75
75
  "cz-conventional-changelog": "^3.3.0",
76
76
  "docdash": "^1.2.0",
77
77
  "eslint": "^7.32.0",
78
78
  "husky": "^4.3.6",
79
- "jest": "^27.0.4",
79
+ "jest": "^29.3.1",
80
+ "jest-environment-jsdom": "^29.3.1",
80
81
  "jsdoc": "^3.6.3",
81
82
  "lint-staged": "^11.1.2",
82
83
  "prettier": "^2.3.2",
@@ -6,6 +6,8 @@ export default bytesToSize;
6
6
  * @alias module:Processor.bytesToSize
7
7
  * @since 3.8.0
8
8
  * @param {number} bytes 字节大小
9
+ * @param {Object} [options] 配置项
10
+ * @param {string} [options.spaceMark=' '] 间隔字符
9
11
  * @returns {string} 存储单位值
10
12
  * @example
11
13
  *
@@ -14,5 +16,11 @@ export default bytesToSize;
14
16
  * bytesToSize(3.213243*1024*1024); // 3.21 MB
15
17
  * bytesToSize(1024*1024*1024); // 1 GB
16
18
  *
19
+ * // 自定义间隔符号
20
+ * bytesToSize(0, { spaceMark: '' }); // 0B
21
+ * bytesToSize(1024, { spaceMark: '' }); // 1KB
22
+ *
17
23
  */
18
- declare function bytesToSize(bytes: number): string;
24
+ declare function bytesToSize(bytes: number, options?: {
25
+ spaceMark?: string | undefined;
26
+ } | undefined): string;
@@ -0,0 +1,26 @@
1
+ export default findTreeNode;
2
+ /**
3
+ * 查找树结构数据节点
4
+ *
5
+ * @static
6
+ * @alias module:Other.findTreeNode
7
+ * @since 4.14.0
8
+ * @template {any} T
9
+ * @template {(item: T) => boolean} F
10
+ * @param {T[]} tree 树结构数据
11
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回该节点
12
+ * @param {string} [childrenField='children'] 子级字段名
13
+ * @returns {T|undefined}
14
+ * @example
15
+ * const menus = [{ "id": "1", "name": "首页", "code": "trade", "pid": null }, { "id": "2", "name": "交易管理", "code": "trade", "pid": null, "children": [{ "id": "3", "name": "交易查询", "code": "trade-1", "pid": "2", "children": [{ "id": "4", "name": "交易查询-查询操作", "code": "trade-1-1", "pid": "3" }] }] }, { "id": "5", "name": "权限管理", "code": "authorization", "pid": null, "children": [{ "id": "6", "name": "角色管理", "code": "authorization-1", "pid": "5" }, { "id": "7", "name": "用户管理", "code": "authorization-2", "pid": "5" }] }];
16
+ *
17
+ * findTreeNode(menus, item=>item.id === '2');
18
+ * // {"id":"2","name":"交易管理","code":"trade","pid":null,"children":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","children":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]}
19
+ *
20
+ * findTreeNode(menus, item=>item.id === '7');
21
+ * // {"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}
22
+ *
23
+ * findTreeNode(menus, item=>item.id === 'not found');
24
+ * // undefined
25
+ */
26
+ declare function findTreeNode<T extends unknown, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string | undefined): T | undefined;
@@ -0,0 +1,26 @@
1
+ export default findTreeSelect;
2
+ /**
3
+ * 查找包含当前节点的所有父级节点
4
+ *
5
+ * @static
6
+ * @alias module:Other.findTreeSelect
7
+ * @since 4.14.0
8
+ * @template {any} T
9
+ * @template {(item: T) => boolean} F
10
+ * @param {T[]} tree 树结构数据
11
+ * @param {F} predicate 遍历每一项执行的函数,参数是当前遍历到的节点数据,如果返回 Truthy 将返回包含该节点的所有父级节点
12
+ * @param {string} [childrenField='children'] 子级字段名
13
+ * @returns {T[]}
14
+ * @example
15
+ * const menus = [{ "id": "1", "name": "首页", "code": "trade", "pid": null }, { "id": "2", "name": "交易管理", "code": "trade", "pid": null, "children": [{ "id": "3", "name": "交易查询", "code": "trade-1", "pid": "2", "children": [{ "id": "4", "name": "交易查询-查询操作", "code": "trade-1-1", "pid": "3" }] }] }, { "id": "5", "name": "权限管理", "code": "authorization", "pid": null, "children": [{ "id": "6", "name": "角色管理", "code": "authorization-1", "pid": "5" }, { "id": "7", "name": "用户管理", "code": "authorization-2", "pid": "5" }] }];
16
+ *
17
+ * findTreeSelect(menus, item => item.id === '2');
18
+ * // [{"id":"2","name":"交易管理","code":"trade","pid":null,"children":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","children":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]}]
19
+ *
20
+ * findTreeSelect(menus, item => item.id === '7');
21
+ * // [{"id":"5","name":"权限管理","code":"authorization","pid":null,"children":[{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]
22
+ *
23
+ * findTreeSelect(menus, item => item.id === 'not found');
24
+ * // []
25
+ */
26
+ declare function findTreeSelect<T extends unknown, F extends (item: T) => boolean>(tree: T[], predicate: F, childrenField?: string | undefined): T[];
@@ -7,7 +7,7 @@ export default formatBankCard;
7
7
  * @since 1.1.0
8
8
  * @param {string} bankCardNo 要处理的字符串
9
9
  * @param {Object} [options] 配置项
10
- * @param {string} [options.char=" "] 间隔字符
10
+ * @param {string} [options.spaceMark=' '] 间隔字符
11
11
  * @param {number} [options.length=4] 间隔长度
12
12
  * @returns {string} 格式化的银行卡号
13
13
  * @example
@@ -22,10 +22,10 @@ export default formatBankCard;
22
22
  * formatBankCard('6228********890'); // 6228 **** **** 890
23
23
  *
24
24
  * // 16位银行卡,"-"间隔
25
- * formatBankCard('6228480402564890', {char: '-'}); // 6228-4804-0256-4890
25
+ * formatBankCard('6228480402564890', {spaceMark: '-'}); // 6228-4804-0256-4890
26
26
  *
27
27
  */
28
28
  declare function formatBankCard(bankCardNo?: string, options?: {
29
- char?: string | undefined;
29
+ spaceMark?: string | undefined;
30
30
  length?: number | undefined;
31
31
  } | undefined): string;
@@ -7,16 +7,16 @@ export default formatMobile;
7
7
  * @since 4.5.0
8
8
  * @param {string} mobileNo 手机号码
9
9
  * @param {object} [options] 配置项
10
- * @param {string} [options.char=' '] 间隔字符
10
+ * @param {string} [options.spaceMark=' '] 间隔字符
11
11
  * @returns {string} 格式化后的手机号码
12
12
  * @example
13
13
  *
14
14
  * formatMobile('13345678900'); // 133 4567 8900
15
- * formatMobile('13345678900', { char: '-' }); // 133-4567-8900
15
+ * formatMobile('13345678900', { spaceMark: '-' }); // 133-4567-8900
16
16
  *
17
17
  * // 脱敏手机号码
18
18
  * formatMobile('133****1234'); // 133 **** 1234
19
- * formatMobile('133****1234', { char: '-' }); // 133-****-1234
19
+ * formatMobile('133****1234', { spaceMark: '-' }); // 133-****-1234
20
20
  *
21
21
  * // 手机号码位数不够
22
22
  * formatMobile('133'); // 133
@@ -25,5 +25,5 @@ export default formatMobile;
25
25
  *
26
26
  */
27
27
  declare function formatMobile(mobileNo: string, options?: {
28
- char?: string | undefined;
28
+ spaceMark?: string | undefined;
29
29
  } | undefined): string;
package/types/index.d.ts CHANGED
@@ -33,6 +33,11 @@ export { default as normalizeString } from "./normalizeString";
33
33
  export { default as safeDate } from "./safeDate";
34
34
  export { default as formatMobile } from "./formatMobile";
35
35
  export { default as padZero } from "./padZero";
36
+ export { default as transformFieldNames } from "./transformFieldNames";
37
+ export { default as listToTree } from "./listToTree";
38
+ export { default as treeToList } from "./treeToList";
39
+ export { default as findTreeNode } from "./findTreeNode";
40
+ export { default as findTreeSelect } from "./findTreeSelect";
36
41
  export { default as plus } from "./plus";
37
42
  export { default as minus } from "./minus";
38
43
  export { default as times } from "./times";
@@ -5,6 +5,8 @@ export default isIdCard;
5
5
  * @static
6
6
  * @alias module:Validator.isIdCard
7
7
  * @since 1.1.0
8
+ * @see 参考 {@link https://zh.wikipedia.org/wiki/中华人民共和国公民身份号码|中华人民共和国公民身份号码}
9
+ * @see 参考 {@link https://baike.baidu.com/item/居民身份证号码|居民身份证号码}
8
10
  * @param {*} value 要检测的值
9
11
  * @param {Object} [options] 配置项
10
12
  * @param {boolean} [options.loose=false] 宽松模式,支持15位身份证号码
package/types/isUrl.d.ts CHANGED
@@ -26,6 +26,8 @@ export default isUrl;
26
26
  * isUrl('a.b:8080'); // true
27
27
  * isUrl('p://a.b'); // true
28
28
  * isUrl('p://a.b:8888'); // true
29
+ * isUrl('中文域名.中文后缀'); // true
30
+ * isUrl('中文域名.cn'); // true
29
31
  *
30
32
  */
31
33
  declare function isUrl(value: any): boolean;
@@ -0,0 +1,43 @@
1
+ export default listToTree;
2
+ /**
3
+ * 列表数据转树结构
4
+ *
5
+ * @static
6
+ * @alias module:Processor.listToTree
7
+ * @since 4.14.0
8
+ * @template {Record<string,any>} [T=Record<string,any>]
9
+ * @template {*} [R=T&Record<string,any>]
10
+ * @param {T[]} list 列表数据
11
+ * @param {object} options 配置项
12
+ * @param {string} [options.keyField='id'] 当前数据的键值字段名称
13
+ * @param {string} [options.parentField='pid'] 当前数据的父级字段名称
14
+ * @param {string} [options.childrenField='children'] 子级字段名称
15
+ * @param {'none'|'null'|'array'} [options.emptyChildrenValue='none'] 子级为空时的值,none表示删除该子级,null表示为null,array表示为[]。
16
+ * @param {'spread'|'self'} [options.nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
17
+ * @returns {R[]} 树结构
18
+ * @example
19
+ *
20
+ * const menus = [
21
+ * { id: '1', name: '首页', code: 'trade', pid: null },
22
+ * { id: '2', name: '交易管理', code: 'trade', pid: null },
23
+ * { id: '3', name: '交易查询', code: 'trade-1', pid: '2' },
24
+ * { id: '4', name: '交易查询-查询操作', code: 'trade-1-1', pid: '3' },
25
+ * { id: '5', name: '权限管理', code: 'authorization', pid: null },
26
+ * { id: '6', name: '角色管理', code: 'authorization-1', pid: '5' },
27
+ * { id: '7', name: '用户管理', code: 'authorization-2', pid: '5' }
28
+ * ];
29
+ * listToTree(menus);
30
+ * // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"2","name":"交易管理","code":"trade","pid":null,"children":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","children":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]},{"id":"5","name":"权限管理","code":"authorization","pid":null,"children":[{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]}]
31
+ *
32
+ * // 自定义子级字段名
33
+ * listToTree(basicMenus, { childrenField: 'childs' });
34
+ * // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"2","name":"交易管理","code":"trade","pid":null,"childs":[{"id":"3","name":"交易查询","code":"trade-1","pid":"2","childs":[{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"}]}]},{"id":"5","name":"权限管理","code":"authorization","pid":null,"childs":[{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]}]
35
+ *
36
+ */
37
+ declare function listToTree<T extends Record<string, any> = Record<string, any>, R extends unknown = T & Record<string, any>>(list: T[], options?: {
38
+ keyField?: string | undefined;
39
+ parentField?: string | undefined;
40
+ childrenField?: string | undefined;
41
+ emptyChildrenValue?: "none" | "null" | "array" | undefined;
42
+ nodeAssign?: "spread" | "self" | undefined;
43
+ }): R[];
@@ -0,0 +1,35 @@
1
+ export default transformFieldNames;
2
+ /**
3
+ * 转换字段名,返回一个转换字段后的值,不改变原值。
4
+ *
5
+ * @static
6
+ * @alias module:Processor.transformFieldNames
7
+ * @since 4.14.0
8
+ * @template {*} D
9
+ * @template {Record<string, keyof D>} F
10
+ * @template {string} C
11
+ * @param {D[]} data 对象数组。如果是树结构数据,需要指定第三个参数 childrenFieldName
12
+ * @param {F} fieldNames 字段名映射
13
+ * @param {C} [childrenFieldName] 子级数据字段名
14
+ * @param {'spread'|'self'} [nodeAssign='spread'] 节点赋值方式。spread表示使用展开运算符创建新值,self表示使用自身对象。
15
+ * @returns {import('./transformFieldNames.type.js').TransformFieldNames<D, F, C>}
16
+ * @example
17
+ *
18
+ * const options = [{code: '1', name: 'one'},{code:'2', name:'two'}];
19
+ * const newOptions = transformFieldNames(options, {label: 'name', value: 'code'});
20
+ * // [{value: '1', label: 'one'},{value:'2', label:'two'}]
21
+ *
22
+ * // 嵌套数据,指定子级字段名 children
23
+ * const options2 = [{code: '1', name: 'one'},{code:'2', name:'two', children: [{code:'2-1', name:'two-one', children: [{code: '2-1-1', name:'two-one-one'}]}]}];
24
+ * const newOptions2 = transformFieldNames(options2, {label: 'name', value: 'code'}, 'children');
25
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one', children: [{value: '2-1-1', label:'two-one-one'}]}]}]
26
+ *
27
+ * const options3 = [{code: '1', name: 'one'},{code:'2', name:'two', childs: [{code:'2-1', name:'two-one'}]}];
28
+ * const newOptions3 = transformFieldNames(options3, {label: 'name', value: 'code'}, 'childs');
29
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', childs: [{value: '2-1', label:'two-one'}]}]
30
+ *
31
+ * // 嵌套数据,并替换子集字段名
32
+ * const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
33
+ * // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
34
+ */
35
+ declare function transformFieldNames<D extends unknown, F extends Record<string, keyof D>, C extends string>(data: D[], fieldNames: F, childrenFieldName?: C | undefined, nodeAssign?: "spread" | "self" | undefined): import("./transformFieldNames.type.js").TransformFieldNames<D, F, C>;
@@ -0,0 +1,2 @@
1
+ export type ExchangeFieldNames<D extends unknown, F extends Record<string, keyof D>> = Omit<D, F[keyof F]> & { [P in keyof F]: D[F[P]]; };
2
+ export type TransformFieldNames<D extends unknown, F extends Record<string, keyof D>, C extends string> = (C extends keyof D ? ExchangeFieldNames<Omit<D, C> & Record<C, TransformFieldNames<D, F, C>>, F> : ExchangeFieldNames<D, F>)[];
@@ -0,0 +1,20 @@
1
+ export default treeToList;
2
+ /**
3
+ * 树结构转列表数据
4
+ *
5
+ * @static
6
+ * @alias module:Processor.treeToList
7
+ * @since 4.14.0
8
+ * @template {Record<string,any>} T
9
+ * @template {keyof T} K
10
+ * @template {Omit<T, K>} R
11
+ * @param {T[]} tree 列表数据
12
+ * @param {K} childrenField 子级字段名称
13
+ * @returns {R[]} 列表数据
14
+ * @example
15
+ * const menus = [{ "id": "1", "name": "首页", "code": "trade", "pid": null }, { "id": "2", "name": "交易管理", "code": "trade", "pid": null, "children": [{ "id": "3", "name": "交易查询", "code": "trade-1", "pid": "2", "children": [{ "id": "4", "name": "交易查询-查询操作", "code": "trade-1-1", "pid": "3" }] }] }, { "id": "5", "name": "权限管理", "code": "authorization", "pid": null, "children": [{ "id": "6", "name": "角色管理", "code": "authorization-1", "pid": "5" }, { "id": "7", "name": "用户管理", "code": "authorization-2", "pid": "5" }] }];
16
+ *
17
+ * treeToList(menus, 'children'));
18
+ * // [{"id":"1","name":"首页","code":"trade","pid":null},{"id":"2","name":"交易管理","code":"trade","pid":null},{"id":"3","name":"交易查询","code":"trade-1","pid":"2"},{"id":"4","name":"交易查询-查询操作","code":"trade-1-1","pid":"3"},{"id":"5","name":"权限管理","code":"authorization","pid":null},{"id":"6","name":"角色管理","code":"authorization-1","pid":"5"},{"id":"7","name":"用户管理","code":"authorization-2","pid":"5"}]
19
+ */
20
+ declare function treeToList<T extends Record<string, any>, K extends keyof T, R extends Omit<T, K>>(tree: T[], childrenField: K): R[];