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.
- package/README.md +16 -6
- package/dist/util-helpers.js +935 -536
- 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/blobToDataURL.js +2 -4
- package/esm/bytesToSize.js +13 -5
- package/esm/calculateCursorPosition.js +7 -12
- package/esm/dataURLToBlob.js +2 -5
- package/esm/divide.js +9 -13
- package/esm/findTreeNode.js +66 -0
- package/esm/findTreeSelect.js +80 -0
- package/esm/formatBankCard.js +11 -11
- package/esm/formatMobile.js +11 -14
- package/esm/formatMoney.js +24 -35
- package/esm/index.js +9 -4
- package/esm/isBankCard.js +9 -13
- package/esm/isBusinessLicense.js +19 -20
- package/esm/isChinese.js +6 -6
- package/esm/isEmail.js +3 -3
- package/esm/isHMCard.js +3 -3
- package/esm/isIPv4.js +3 -3
- package/esm/isIPv6.js +3 -3
- package/esm/isIdCard.js +9 -17
- package/esm/isMobile.js +3 -3
- package/esm/isPassport.js +3 -3
- package/esm/isPassword.js +6 -7
- package/esm/isPostcode.js +3 -3
- package/esm/isPromiseLike.js +0 -2
- package/esm/isQQ.js +3 -3
- package/esm/isSocialCreditCode.js +29 -28
- package/esm/isSwiftCode.js +1 -2
- package/esm/isTWCard.js +6 -7
- package/esm/isTelephone.js +3 -3
- package/esm/isUrl.js +7 -3
- package/esm/isVehicle.js +3 -3
- package/esm/isWX.js +3 -3
- package/esm/listToTree.js +122 -0
- package/esm/minus.js +7 -11
- package/esm/normalizeString.js +1 -3
- package/esm/numberToChinese.js +43 -52
- package/esm/padZero.js +1 -5
- package/esm/parseIdCard.js +27 -23
- package/esm/plus.js +7 -11
- package/esm/randomString.js +2 -8
- package/esm/replaceChar.js +11 -16
- package/esm/round.js +4 -5
- package/esm/safeDate.js +5 -7
- package/esm/setDataURLPrefix.js +0 -1
- package/esm/strlen.js +1 -5
- package/esm/times.js +7 -11
- package/esm/transformFieldNames.doc.js +34 -0
- package/esm/transformFieldNames.js +98 -0
- package/esm/transformFieldNames.type.js +19 -0
- package/esm/treeToList.js +88 -0
- package/esm/utils/config.js +3 -3
- package/esm/utils/constants.js +2 -2
- package/esm/utils/convertToString.js +1 -2
- package/esm/utils/devWarn.js +1 -3
- package/esm/utils/math.util.js +20 -29
- package/esm/utils/type/index.js +1 -1
- package/esm/utils/type/isArguments.js +1 -2
- package/esm/utils/type/isArray.js +1 -2
- package/esm/utils/type/isBoolean.js +1 -2
- package/esm/utils/type/isDate.js +1 -2
- package/esm/utils/type/isError.js +1 -2
- package/esm/utils/type/isFunction.js +1 -2
- package/esm/utils/type/isMap.js +1 -2
- package/esm/utils/type/isNaN.js +1 -2
- package/esm/utils/type/isNil.js +1 -2
- package/esm/utils/type/isNull.js +0 -1
- package/esm/utils/type/isNumber.js +1 -2
- package/esm/utils/type/isObject.js +0 -3
- package/esm/utils/type/isRegExp.js +1 -2
- package/esm/utils/type/isSet.js +1 -2
- package/esm/utils/type/isString.js +1 -2
- package/esm/utils/type/isSymbol.js +1 -2
- package/esm/utils/type/isType.js +1 -2
- package/esm/utils/type/isUndefined.js +0 -1
- package/esm/utils/type/isWeakMap.js +1 -2
- package/esm/utils/type/isWeakSet.js +1 -2
- package/esm/validatePassword.js +27 -50
- package/esm/waitTime.js +0 -1
- package/lib/blobToDataURL.js +2 -5
- package/lib/bytesToSize.js +13 -6
- package/lib/calculateCursorPosition.js +4 -13
- package/lib/dataURLToBlob.js +2 -6
- package/lib/divide.js +8 -22
- package/lib/findTreeNode.js +76 -0
- package/lib/findTreeSelect.js +86 -0
- package/lib/formatBankCard.js +10 -13
- package/lib/formatMobile.js +10 -16
- package/lib/formatMoney.js +25 -47
- package/lib/index.js +115 -126
- package/lib/isBankCard.js +7 -15
- package/lib/isBusinessLicense.js +17 -22
- package/lib/isChinese.js +6 -9
- package/lib/isEmail.js +1 -5
- package/lib/isHMCard.js +1 -5
- package/lib/isIPv4.js +1 -5
- package/lib/isIPv6.js +1 -5
- package/lib/isIdCard.js +7 -19
- package/lib/isMobile.js +1 -5
- package/lib/isPassport.js +1 -5
- package/lib/isPassword.js +5 -9
- package/lib/isPostcode.js +1 -5
- package/lib/isPromiseLike.js +1 -4
- package/lib/isQQ.js +1 -5
- package/lib/isSocialCreditCode.js +27 -30
- package/lib/isSwiftCode.js +1 -5
- package/lib/isTWCard.js +4 -9
- package/lib/isTelephone.js +1 -5
- package/lib/isUrl.js +5 -5
- package/lib/isVehicle.js +1 -5
- package/lib/isWX.js +1 -5
- package/lib/listToTree.js +131 -0
- package/lib/minus.js +6 -20
- package/lib/normalizeString.js +0 -6
- package/lib/numberToChinese.js +41 -55
- package/lib/padZero.js +0 -7
- package/lib/parseIdCard.js +30 -29
- package/lib/plus.js +6 -20
- package/lib/randomString.js +2 -8
- package/lib/replaceChar.js +10 -18
- package/lib/round.js +3 -9
- package/lib/safeDate.js +4 -13
- package/lib/setDataURLPrefix.js +0 -2
- package/lib/strlen.js +0 -7
- package/lib/times.js +6 -18
- package/lib/transformFieldNames.doc.js +41 -0
- package/lib/transformFieldNames.js +104 -0
- package/lib/transformFieldNames.type.js +5 -0
- package/lib/treeToList.js +96 -0
- package/lib/utils/config.js +8 -7
- package/lib/utils/constants.js +2 -2
- package/lib/utils/convertToString.js +0 -4
- package/lib/utils/devWarn.js +0 -4
- package/lib/utils/math.util.js +23 -45
- package/lib/utils/type/index.js +20 -39
- package/lib/utils/type/isArguments.js +0 -4
- package/lib/utils/type/isArray.js +0 -4
- package/lib/utils/type/isBoolean.js +0 -4
- package/lib/utils/type/isDate.js +0 -4
- package/lib/utils/type/isError.js +0 -4
- package/lib/utils/type/isFunction.js +0 -4
- package/lib/utils/type/isMap.js +0 -4
- package/lib/utils/type/isNaN.js +0 -4
- package/lib/utils/type/isNil.js +0 -5
- package/lib/utils/type/isNull.js +0 -2
- package/lib/utils/type/isNumber.js +0 -4
- package/lib/utils/type/isObject.js +1 -5
- package/lib/utils/type/isRegExp.js +0 -4
- package/lib/utils/type/isSet.js +0 -4
- package/lib/utils/type/isString.js +0 -4
- package/lib/utils/type/isSymbol.js +0 -4
- package/lib/utils/type/isType.js +1 -2
- package/lib/utils/type/isUndefined.js +0 -2
- package/lib/utils/type/isWeakMap.js +0 -4
- package/lib/utils/type/isWeakSet.js +0 -4
- package/lib/validatePassword.js +27 -53
- package/lib/waitTime.js +0 -2
- package/package.json +9 -7
- package/types/bytesToSize.d.ts +9 -1
- package/types/findTreeNode.d.ts +26 -0
- package/types/findTreeSelect.d.ts +26 -0
- package/types/formatBankCard.d.ts +3 -3
- package/types/formatMobile.d.ts +4 -4
- package/types/index.d.ts +5 -0
- package/types/isIdCard.d.ts +2 -0
- package/types/isUrl.d.ts +2 -0
- package/types/listToTree.d.ts +41 -0
- package/types/transformFieldNames.d.ts +34 -0
- package/types/transformFieldNames.type.d.ts +2 -0
- package/types/treeToList.d.ts +20 -0
- 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 _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;
|
package/lib/utils/type/isType.js
CHANGED
|
@@ -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;
|
package/lib/validatePassword.js
CHANGED
|
@@ -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
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "4.14.0",
|
|
4
4
|
"description": "一个基于业务场景的工具方法库",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -61,27 +61,29 @@
|
|
|
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
|
-
"@rollup/plugin-json": "^4.1.0",
|
|
69
69
|
"@rollup/plugin-node-resolve": "^13.0.0",
|
|
70
|
-
"@
|
|
71
|
-
"
|
|
72
|
-
"babel-
|
|
70
|
+
"@rollup/plugin-replace": "^5.0.1",
|
|
71
|
+
"@types/jest": "^29.2.4",
|
|
72
|
+
"babel-jest": "^29.3.1",
|
|
73
|
+
"babel-plugin-minify-replace": "^0.5.0",
|
|
73
74
|
"cross-env": "^7.0.3",
|
|
74
75
|
"cz-conventional-changelog": "^3.3.0",
|
|
75
76
|
"docdash": "^1.2.0",
|
|
76
77
|
"eslint": "^7.32.0",
|
|
77
78
|
"husky": "^4.3.6",
|
|
78
|
-
"jest": "^
|
|
79
|
+
"jest": "^29.3.1",
|
|
80
|
+
"jest-environment-jsdom": "^29.3.1",
|
|
79
81
|
"jsdoc": "^3.6.3",
|
|
80
82
|
"lint-staged": "^11.1.2",
|
|
81
83
|
"prettier": "^2.3.2",
|
|
82
84
|
"rollup": "^2.49.0",
|
|
83
85
|
"rollup-plugin-terser": "^7.0.2",
|
|
84
|
-
"typescript": "^4.
|
|
86
|
+
"typescript": "^4.9.3"
|
|
85
87
|
},
|
|
86
88
|
"lint-staged": {
|
|
87
89
|
"**/*.js": "npm run lint-staged:js",
|
package/types/bytesToSize.d.ts
CHANGED
|
@@ -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
|
|
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.
|
|
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', {
|
|
25
|
+
* formatBankCard('6228480402564890', {spaceMark: '-'}); // 6228-4804-0256-4890
|
|
26
26
|
*
|
|
27
27
|
*/
|
|
28
28
|
declare function formatBankCard(bankCardNo?: string, options?: {
|
|
29
|
-
|
|
29
|
+
spaceMark?: string | undefined;
|
|
30
30
|
length?: number | undefined;
|
|
31
31
|
} | undefined): string;
|
package/types/formatMobile.d.ts
CHANGED
|
@@ -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.
|
|
10
|
+
* @param {string} [options.spaceMark=' '] 间隔字符
|
|
11
11
|
* @returns {string} 格式化后的手机号码
|
|
12
12
|
* @example
|
|
13
13
|
*
|
|
14
14
|
* formatMobile('13345678900'); // 133 4567 8900
|
|
15
|
-
* formatMobile('13345678900', {
|
|
15
|
+
* formatMobile('13345678900', { spaceMark: '-' }); // 133-4567-8900
|
|
16
16
|
*
|
|
17
17
|
* // 脱敏手机号码
|
|
18
18
|
* formatMobile('133****1234'); // 133 **** 1234
|
|
19
|
-
* formatMobile('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
|
-
|
|
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";
|
package/types/isIdCard.d.ts
CHANGED
|
@@ -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
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
* @returns {R[]} 树结构
|
|
17
|
+
* @example
|
|
18
|
+
*
|
|
19
|
+
* const menus = [
|
|
20
|
+
* { id: '1', name: '首页', code: 'trade', pid: null },
|
|
21
|
+
* { id: '2', name: '交易管理', code: 'trade', pid: null },
|
|
22
|
+
* { id: '3', name: '交易查询', code: 'trade-1', pid: '2' },
|
|
23
|
+
* { id: '4', name: '交易查询-查询操作', code: 'trade-1-1', pid: '3' },
|
|
24
|
+
* { id: '5', name: '权限管理', code: 'authorization', pid: null },
|
|
25
|
+
* { id: '6', name: '角色管理', code: 'authorization-1', pid: '5' },
|
|
26
|
+
* { id: '7', name: '用户管理', code: 'authorization-2', pid: '5' }
|
|
27
|
+
* ];
|
|
28
|
+
* listToTree(menus);
|
|
29
|
+
* // [{"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"}]}]
|
|
30
|
+
*
|
|
31
|
+
* // 自定义子级字段名
|
|
32
|
+
* listToTree(basicMenus, { childrenField: 'childs' });
|
|
33
|
+
* // [{"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"}]}]
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
declare function listToTree<T extends Record<string, any> = Record<string, any>, R extends unknown = T & Record<string, any>>(list: T[], options?: {
|
|
37
|
+
keyField?: string | undefined;
|
|
38
|
+
parentField?: string | undefined;
|
|
39
|
+
childrenField?: string | undefined;
|
|
40
|
+
emptyChildrenValue?: "none" | "null" | "array" | undefined;
|
|
41
|
+
}): R[];
|
|
@@ -0,0 +1,34 @@
|
|
|
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
|
+
* @returns {import('./transformFieldNames.type.js').TransformFieldNames<D, F, C>}
|
|
15
|
+
* @example
|
|
16
|
+
*
|
|
17
|
+
* const options = [{code: '1', name: 'one'},{code:'2', name:'two'}];
|
|
18
|
+
* const newOptions = transformFieldNames(options, {label: 'name', value: 'code'});
|
|
19
|
+
* // [{value: '1', label: 'one'},{value:'2', label:'two'}]
|
|
20
|
+
*
|
|
21
|
+
* // 嵌套数据,指定子级字段名 children
|
|
22
|
+
* 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'}]}]}];
|
|
23
|
+
* const newOptions2 = transformFieldNames(options2, {label: 'name', value: 'code'}, 'children');
|
|
24
|
+
* // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one', children: [{value: '2-1-1', label:'two-one-one'}]}]}]
|
|
25
|
+
*
|
|
26
|
+
* const options3 = [{code: '1', name: 'one'},{code:'2', name:'two', childs: [{code:'2-1', name:'two-one'}]}];
|
|
27
|
+
* const newOptions3 = transformFieldNames(options3, {label: 'name', value: 'code'}, 'childs');
|
|
28
|
+
* // [{value: '1', label: 'one'},{value:'2', label:'two', childs: [{value: '2-1', label:'two-one'}]}]
|
|
29
|
+
*
|
|
30
|
+
* // 嵌套数据,并替换子集字段名
|
|
31
|
+
* const newOptions4 = transformFieldNames(options3, {label: 'name', value: 'code', children: 'childs'}, 'childs');
|
|
32
|
+
* // [{value: '1', label: 'one'},{value:'2', label:'two', children: [{value: '2-1', label:'two-one'}]}]
|
|
33
|
+
*/
|
|
34
|
+
declare function transformFieldNames<D extends unknown, F extends Record<string, keyof D>, C extends string>(data: D[], fieldNames: F, childrenFieldName?: C | 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[];
|
package/types/utils/config.d.ts
CHANGED