util-helpers 4.10.3 → 4.11.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/dist/util-helpers.js +171 -133
- 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/bytesToSize.js +4 -3
- package/esm/formatMoney.js +7 -10
- package/esm/isBusinessLicense.js +2 -2
- package/esm/isChinese.js +24 -11
- package/esm/numberToChinese.js +30 -36
- package/esm/parseIdCard.js +9 -24
- package/esm/replaceChar.js +8 -7
- package/esm/round.js +8 -0
- package/esm/utils/devWarn.js +16 -0
- package/esm/utils/math.util.js +52 -29
- package/esm/validatePassword.js +25 -20
- package/lib/bytesToSize.js +4 -3
- package/lib/formatMoney.js +7 -10
- package/lib/isBusinessLicense.js +2 -2
- package/lib/isChinese.js +24 -11
- package/lib/numberToChinese.js +32 -36
- package/lib/parseIdCard.js +9 -24
- package/lib/replaceChar.js +11 -8
- package/lib/round.js +9 -0
- package/lib/utils/devWarn.js +24 -0
- package/lib/utils/math.util.js +54 -29
- package/lib/validatePassword.js +27 -20
- package/package.json +3 -2
- package/types/isChinese.d.ts +13 -3
- package/types/replaceChar.d.ts +1 -1
- package/types/utils/devWarn.d.ts +7 -0
- package/types/utils/math.util.d.ts +2 -2
- package/types/validatePassword.d.ts +3 -3
package/esm/formatMoney.js
CHANGED
|
@@ -2,7 +2,7 @@ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
|
2
2
|
import { checkBoundary, scientificToNumber, isScientificNumber } from './utils/math.util';
|
|
3
3
|
import isNaN from './utils/type/isNaN';
|
|
4
4
|
import { trimLeftZero } from './utils/math.util';
|
|
5
|
-
import
|
|
5
|
+
import devWarn from './utils/devWarn';
|
|
6
6
|
var reg = /^[+-]?\d*\.?\d*$/;
|
|
7
7
|
/**
|
|
8
8
|
* 检查数字或数字字符串
|
|
@@ -14,10 +14,7 @@ var reg = /^[+-]?\d*\.?\d*$/;
|
|
|
14
14
|
|
|
15
15
|
function checkNumber(num) {
|
|
16
16
|
if (!(reg.test(num) || isScientificNumber(num)) || isNaN(num) || typeof num !== 'number' && typeof num !== 'string' || num === '') {
|
|
17
|
-
|
|
18
|
-
console.warn("".concat(num, " invalid parameter."));
|
|
19
|
-
}
|
|
20
|
-
|
|
17
|
+
devWarn("".concat(num, " invalid parameter."));
|
|
21
18
|
return false;
|
|
22
19
|
} // 数字超限如果不是是字符串,可能有异常
|
|
23
20
|
// 如 1111111111111111111111 // => 1.1111111111111111e+21
|
|
@@ -153,12 +150,12 @@ var formatMoney = function formatMoney(num) {
|
|
|
153
150
|
thousand = typeof thousand === 'string' ? thousand : ',';
|
|
154
151
|
decimal = typeof decimal === 'string' ? decimal : '.'; // 转换数字字符串,支持科学记数法
|
|
155
152
|
|
|
156
|
-
var
|
|
153
|
+
var strNum = scientificToNumber(num) + ''; // 整数和小数部分
|
|
157
154
|
|
|
158
|
-
var
|
|
159
|
-
|
|
160
|
-
intStr =
|
|
161
|
-
decStr =
|
|
155
|
+
var _strNum$split = strNum.split('.'),
|
|
156
|
+
_strNum$split2 = _slicedToArray(_strNum$split, 2),
|
|
157
|
+
intStr = _strNum$split2[0],
|
|
158
|
+
decStr = _strNum$split2[1];
|
|
162
159
|
|
|
163
160
|
return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
|
|
164
161
|
};
|
package/esm/isBusinessLicense.js
CHANGED
package/esm/isChinese.js
CHANGED
|
@@ -18,14 +18,12 @@ var chineseDictionary = {
|
|
|
18
18
|
chineseExtendF: "[\uD873\uDEB0-\uD87A\uDFE0]"
|
|
19
19
|
};
|
|
20
20
|
var looseChineseRegExp = chineseDictionary.chineseBasic + '+';
|
|
21
|
-
var chineseRegExp = '^' + chineseDictionary.chineseBasic + '+$';
|
|
21
|
+
var chineseRegExp = '^' + chineseDictionary.chineseBasic + '+$';
|
|
22
|
+
var chineseWithExtend = '(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')';
|
|
23
|
+
var looseChineseExtendRegExp = chineseWithExtend + '+';
|
|
24
|
+
var chineseExtendRegExp = '^' + chineseWithExtend + '+$'; // eslint-disable-next-line no-prototype-builtins
|
|
22
25
|
|
|
23
26
|
var supportRegExpUnicode = RegExp.prototype.hasOwnProperty('unicode');
|
|
24
|
-
|
|
25
|
-
if (supportRegExpUnicode) {
|
|
26
|
-
looseChineseRegExp = '(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')+';
|
|
27
|
-
chineseRegExp = '^(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')+$';
|
|
28
|
-
}
|
|
29
27
|
/**
|
|
30
28
|
* 检测值是否为中文
|
|
31
29
|
*
|
|
@@ -36,6 +34,7 @@ if (supportRegExpUnicode) {
|
|
|
36
34
|
* @param {*} value 要检测的值
|
|
37
35
|
* @param {Object} [options] 配置项
|
|
38
36
|
* @param {boolean} [options.loose=false] 宽松模式。如果为true,只要包含中文即为true
|
|
37
|
+
* @param {boolean} [options.useExtend=false] 使用统一表意文字扩展A-F。注意:如果不支持 `RegExp.prototype.unicode`,扩展字符集将自动不生效,如IE浏览器。
|
|
39
38
|
* @returns {boolean} 值是否为中文
|
|
40
39
|
* @example
|
|
41
40
|
*
|
|
@@ -46,22 +45,36 @@ if (supportRegExpUnicode) {
|
|
|
46
45
|
* // => false
|
|
47
46
|
*
|
|
48
47
|
* // 宽松模式,只要包含中文即为true
|
|
49
|
-
* isChinese('林A', {loose: true});
|
|
48
|
+
* isChinese('林A', { loose: true });
|
|
50
49
|
* // => true
|
|
51
50
|
*
|
|
52
|
-
* isChinese('A林A', {loose: true});
|
|
51
|
+
* isChinese('A林A', { loose: true });
|
|
53
52
|
* // => true
|
|
54
53
|
*
|
|
54
|
+
* isChinese('𠮷');
|
|
55
|
+
* // => false
|
|
56
|
+
*
|
|
57
|
+
* // 使用中文扩展字符集,需要浏览器支持 RegExp.prototype.unicode 才生效。
|
|
58
|
+
* isChinese('𠮷', { useExtend: true });
|
|
59
|
+
* // => true
|
|
60
|
+
* isChinese('𠮷aa', { useExtend: true, loose: true });
|
|
61
|
+
* // => true
|
|
55
62
|
*/
|
|
56
63
|
|
|
57
|
-
|
|
58
64
|
function isChinese(value) {
|
|
59
65
|
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
60
66
|
_ref$loose = _ref.loose,
|
|
61
|
-
loose = _ref$loose === void 0 ? false : _ref$loose
|
|
67
|
+
loose = _ref$loose === void 0 ? false : _ref$loose,
|
|
68
|
+
_ref$useExtend = _ref.useExtend,
|
|
69
|
+
useExtend = _ref$useExtend === void 0 ? false : _ref$useExtend;
|
|
62
70
|
|
|
63
71
|
var valueStr = normalizeString(value);
|
|
64
|
-
var
|
|
72
|
+
var basicRegExp = loose ? looseChineseRegExp : chineseRegExp;
|
|
73
|
+
var extendRegExp = loose ? looseChineseExtendRegExp : chineseExtendRegExp;
|
|
74
|
+
var hasExtend = useExtend && supportRegExpUnicode;
|
|
75
|
+
var resultRegExp = hasExtend ? extendRegExp : basicRegExp;
|
|
76
|
+
var flag = hasExtend ? 'u' : undefined;
|
|
77
|
+
var reg = new RegExp(resultRegExp, flag);
|
|
65
78
|
return reg.test(valueStr);
|
|
66
79
|
}
|
|
67
80
|
|
package/esm/numberToChinese.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { checkBoundary } from './utils/math.util';
|
|
2
|
-
import
|
|
2
|
+
import devWarn from './utils/devWarn'; // 简体
|
|
3
3
|
|
|
4
4
|
var chnNumberChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
|
|
5
5
|
var chnUnitChar = ['', '十', '百', '千']; // 繁体
|
|
@@ -34,11 +34,11 @@ var unitSection;
|
|
|
34
34
|
*/
|
|
35
35
|
|
|
36
36
|
function sectionToChinese(section) {
|
|
37
|
-
var str = ''
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
unitPos = 0;
|
|
37
|
+
var str = '';
|
|
38
|
+
var chnstr = '';
|
|
39
|
+
var zero = false; //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
|
|
40
|
+
|
|
41
|
+
var unitPos = 0;
|
|
42
42
|
|
|
43
43
|
while (section > 0) {
|
|
44
44
|
// 对数字取余10,得到的数即为个位数
|
|
@@ -74,18 +74,18 @@ function sectionToChinese(section) {
|
|
|
74
74
|
|
|
75
75
|
|
|
76
76
|
function convertInteger(num) {
|
|
77
|
-
|
|
77
|
+
var numInt = Math.floor(num);
|
|
78
78
|
var unitPos = 0;
|
|
79
|
-
var strIns = ''
|
|
80
|
-
|
|
79
|
+
var strIns = '';
|
|
80
|
+
var chnStr = '';
|
|
81
81
|
var needZero = false;
|
|
82
82
|
|
|
83
|
-
if (
|
|
83
|
+
if (numInt === 0) {
|
|
84
84
|
return numberChar[0];
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
while (
|
|
88
|
-
var section =
|
|
87
|
+
while (numInt > 0) {
|
|
88
|
+
var section = numInt % 10000;
|
|
89
89
|
|
|
90
90
|
if (needZero) {
|
|
91
91
|
chnStr = numberChar[0] + chnStr;
|
|
@@ -95,7 +95,7 @@ function convertInteger(num) {
|
|
|
95
95
|
strIns += section !== 0 ? unitSection[unitPos] : unitSection[0];
|
|
96
96
|
chnStr = strIns + chnStr;
|
|
97
97
|
needZero = section < 1000 && section > 0;
|
|
98
|
-
|
|
98
|
+
numInt = Math.floor(numInt / 10000);
|
|
99
99
|
unitPos++;
|
|
100
100
|
}
|
|
101
101
|
|
|
@@ -110,12 +110,12 @@ function convertInteger(num) {
|
|
|
110
110
|
|
|
111
111
|
|
|
112
112
|
function convertDecimal(num) {
|
|
113
|
-
var
|
|
114
|
-
var index =
|
|
113
|
+
var strNum = num + '';
|
|
114
|
+
var index = strNum.indexOf('.');
|
|
115
115
|
var ret = '';
|
|
116
116
|
|
|
117
117
|
if (index > -1) {
|
|
118
|
-
var decimalStr =
|
|
118
|
+
var decimalStr = strNum.slice(index + 1);
|
|
119
119
|
ret = mapNumberChar(parseInt(decimalStr));
|
|
120
120
|
}
|
|
121
121
|
|
|
@@ -131,11 +131,11 @@ function convertDecimal(num) {
|
|
|
131
131
|
|
|
132
132
|
|
|
133
133
|
function mapNumberChar(num) {
|
|
134
|
-
var
|
|
134
|
+
var strNum = num + '';
|
|
135
135
|
var ret = '';
|
|
136
136
|
|
|
137
|
-
for (var i = 0, len =
|
|
138
|
-
ret += numberChar[parseInt(
|
|
137
|
+
for (var i = 0, len = strNum.length; i < len; i++) {
|
|
138
|
+
ret += numberChar[parseInt(strNum[i])];
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
return ret;
|
|
@@ -200,19 +200,11 @@ function numberToChinese(num) {
|
|
|
200
200
|
_ref$negative = _ref.negative,
|
|
201
201
|
negative = _ref$negative === void 0 ? '负' : _ref$negative,
|
|
202
202
|
_ref$unitConfig = _ref.unitConfig,
|
|
203
|
-
unitConfig = _ref$unitConfig === void 0 ? {
|
|
204
|
-
w: '万',
|
|
205
|
-
// '萬'
|
|
206
|
-
y: '亿' // '億'
|
|
207
|
-
|
|
208
|
-
} : _ref$unitConfig;
|
|
203
|
+
unitConfig = _ref$unitConfig === void 0 ? {} : _ref$unitConfig;
|
|
209
204
|
|
|
210
205
|
// 非数字 或 NaN 不处理
|
|
211
206
|
if (typeof num !== 'number' || isNaN(num)) {
|
|
212
|
-
|
|
213
|
-
console.warn("\u53C2\u6570\u9519\u8BEF ".concat(num, "\uFF0C\u8BF7\u4F20\u5165\u6570\u5B57"));
|
|
214
|
-
}
|
|
215
|
-
|
|
207
|
+
devWarn("\u53C2\u6570\u9519\u8BEF ".concat(num, "\uFF0C\u8BF7\u4F20\u5165\u6570\u5B57"));
|
|
216
208
|
return '';
|
|
217
209
|
} // 超过安全数字提示
|
|
218
210
|
|
|
@@ -228,9 +220,10 @@ function numberToChinese(num) {
|
|
|
228
220
|
} // 设置节点计数单位,万、亿、万亿
|
|
229
221
|
|
|
230
222
|
|
|
231
|
-
var
|
|
232
|
-
var
|
|
233
|
-
|
|
223
|
+
var unitWan = (unitConfig === null || unitConfig === void 0 ? void 0 : unitConfig.w) || '万';
|
|
224
|
+
var unitYi = (unitConfig === null || unitConfig === void 0 ? void 0 : unitConfig.y) || '亿';
|
|
225
|
+
var unitWanYi = unitWan + unitYi;
|
|
226
|
+
unitSection = ['', unitWan, unitYi, unitWanYi]; // 设置0
|
|
234
227
|
|
|
235
228
|
if (zero) {
|
|
236
229
|
numberChar[0] = zero;
|
|
@@ -239,16 +232,17 @@ function numberToChinese(num) {
|
|
|
239
232
|
|
|
240
233
|
var preStr = num < 0 ? negative : ''; // 整数和小数
|
|
241
234
|
|
|
242
|
-
var chnInteger, chnDecimal;
|
|
235
|
+
var chnInteger, chnDecimal;
|
|
236
|
+
var numAbs = Math.abs(num); // 处理整数
|
|
243
237
|
|
|
244
238
|
if (unit) {
|
|
245
|
-
chnInteger = convertInteger(
|
|
239
|
+
chnInteger = convertInteger(numAbs);
|
|
246
240
|
} else {
|
|
247
|
-
chnInteger = mapNumberChar(Math.floor(
|
|
241
|
+
chnInteger = mapNumberChar(Math.floor(numAbs));
|
|
248
242
|
} // 处理小数
|
|
249
243
|
|
|
250
244
|
|
|
251
|
-
chnDecimal = convertDecimal(
|
|
245
|
+
chnDecimal = convertDecimal(numAbs);
|
|
252
246
|
return chnDecimal ? "".concat(preStr).concat(chnInteger).concat(decimal).concat(chnDecimal) : "".concat(preStr).concat(chnInteger);
|
|
253
247
|
}
|
|
254
248
|
|
package/esm/parseIdCard.js
CHANGED
|
@@ -108,33 +108,18 @@ function parseIdCard(id) {
|
|
|
108
108
|
* @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }}
|
|
109
109
|
*
|
|
110
110
|
*/
|
|
111
|
+
// @ts-ignore
|
|
111
112
|
|
|
112
113
|
|
|
113
|
-
var origin = {
|
|
114
|
-
province:
|
|
115
|
-
city:
|
|
116
|
-
area:
|
|
117
|
-
year:
|
|
118
|
-
month:
|
|
119
|
-
day:
|
|
120
|
-
gender:
|
|
114
|
+
var origin = (info === null || info === void 0 ? void 0 : info.groups) || {
|
|
115
|
+
province: info[1],
|
|
116
|
+
city: info[2],
|
|
117
|
+
area: info[3],
|
|
118
|
+
year: info[4],
|
|
119
|
+
month: info[5],
|
|
120
|
+
day: info[6],
|
|
121
|
+
gender: info[7]
|
|
121
122
|
};
|
|
122
|
-
|
|
123
|
-
if (info.groups) {
|
|
124
|
-
// @ts-ignore
|
|
125
|
-
origin = info.groups;
|
|
126
|
-
} else {
|
|
127
|
-
origin = {
|
|
128
|
-
province: info[1],
|
|
129
|
-
city: info[2],
|
|
130
|
-
area: info[3],
|
|
131
|
-
year: info[4],
|
|
132
|
-
month: info[5],
|
|
133
|
-
day: info[6],
|
|
134
|
-
gender: info[7]
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
123
|
var province = Provinces.find(function (item) {
|
|
139
124
|
return item[0] === origin.province;
|
|
140
125
|
});
|
package/esm/replaceChar.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import normalizeString from './normalizeString';
|
|
1
2
|
/**
|
|
2
3
|
* 替换字符,应用场景如:脱敏
|
|
3
4
|
*
|
|
@@ -44,9 +45,8 @@
|
|
|
44
45
|
* // => 林**
|
|
45
46
|
*
|
|
46
47
|
*/
|
|
47
|
-
function replaceChar() {
|
|
48
|
-
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
49
48
|
|
|
49
|
+
function replaceChar(str) {
|
|
50
50
|
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
51
51
|
_ref$start = _ref.start,
|
|
52
52
|
start = _ref$start === void 0 ? 3 : _ref$start,
|
|
@@ -57,20 +57,21 @@ function replaceChar() {
|
|
|
57
57
|
repeat = _ref.repeat,
|
|
58
58
|
exclude = _ref.exclude;
|
|
59
59
|
|
|
60
|
-
var
|
|
60
|
+
var realStr = normalizeString(str);
|
|
61
|
+
var strLen = realStr.length; // 开始位置超过str长度
|
|
61
62
|
|
|
62
63
|
if (Math.abs(start) >= strLen) {
|
|
63
|
-
return
|
|
64
|
+
return realStr;
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
start = start >= 0 ? start : strLen + start;
|
|
67
68
|
end = end >= 0 ? end : strLen + end; // 开始位置大于结束位置
|
|
68
69
|
|
|
69
70
|
if (start >= end) {
|
|
70
|
-
return
|
|
71
|
+
return realStr;
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
var middleStr =
|
|
74
|
+
var middleStr = realStr.substring(start, end);
|
|
74
75
|
|
|
75
76
|
if (exclude) {
|
|
76
77
|
var reg = new RegExp("[^".concat(exclude, "]"), 'g');
|
|
@@ -80,7 +81,7 @@ function replaceChar() {
|
|
|
80
81
|
middleStr = _char.repeat(repeat);
|
|
81
82
|
}
|
|
82
83
|
|
|
83
|
-
return
|
|
84
|
+
return realStr.substring(0, start) + middleStr + realStr.substring(end);
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
export default replaceChar;
|
package/esm/round.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import divide from './divide';
|
|
2
2
|
import times from './times';
|
|
3
|
+
import { isNumber, isString, isNaN } from './utils/type';
|
|
3
4
|
/**
|
|
4
5
|
* 四舍五入,支持设置精度
|
|
5
6
|
*
|
|
@@ -23,6 +24,13 @@ import times from './times';
|
|
|
23
24
|
|
|
24
25
|
function round(num) {
|
|
25
26
|
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
27
|
+
|
|
28
|
+
// 兼容处理,如果参数为非数字或字符串时,直接返回
|
|
29
|
+
if ((!isNumber(num) || isNaN(num)) && !isString(num)) {
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
return num;
|
|
32
|
+
}
|
|
33
|
+
|
|
26
34
|
var base = Math.pow(10, precision);
|
|
27
35
|
return divide(Math.round(times(num, base)), base);
|
|
28
36
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { config } from './config';
|
|
2
|
+
/**
|
|
3
|
+
* 打印警告信息
|
|
4
|
+
*
|
|
5
|
+
* @param {any[]} args 打印的信息
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
function devWarn() {
|
|
9
|
+
if (!config.disableWarning) {
|
|
10
|
+
var _console;
|
|
11
|
+
|
|
12
|
+
(_console = console).warn.apply(_console, arguments);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default devWarn;
|
package/esm/utils/math.util.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
|
|
6
6
|
*/
|
|
7
7
|
import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from './constants';
|
|
8
|
-
import
|
|
8
|
+
import devWarn from './devWarn';
|
|
9
9
|
/**
|
|
10
10
|
* 是否为科学计数法数字
|
|
11
11
|
*
|
|
@@ -52,8 +52,10 @@ export function digitLength(num) {
|
|
|
52
52
|
*/
|
|
53
53
|
|
|
54
54
|
export function float2Fixed(num) {
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
var strNum = String(num);
|
|
56
|
+
|
|
57
|
+
if (!isScientificNumber(strNum)) {
|
|
58
|
+
return Number(strNum.replace('.', ''));
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
var dLen = digitLength(num);
|
|
@@ -67,9 +69,7 @@ export function float2Fixed(num) {
|
|
|
67
69
|
|
|
68
70
|
export function checkBoundary(num) {
|
|
69
71
|
if (+num > MAX_SAFE_INTEGER || +num < MIN_SAFE_INTEGER) {
|
|
70
|
-
|
|
71
|
-
console.warn("".concat(num, " is beyond boundary when transfer to integer, the results may not be accurate"));
|
|
72
|
-
}
|
|
72
|
+
devWarn("".concat(num, " is beyond boundary when transfer to integer, the results may not be accurate"));
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
@@ -99,39 +99,62 @@ export function trimLeftZero(num) {
|
|
|
99
99
|
* 2.小数点前边是0,小数点后十分位(包含十分位)之后连续零的个数大于等于6个
|
|
100
100
|
*
|
|
101
101
|
* @param {string | number} num 科学计数法数字
|
|
102
|
-
* @returns {string} 转换后的数字字符串
|
|
102
|
+
* @returns {string | number} 转换后的数字字符串
|
|
103
103
|
*/
|
|
104
104
|
|
|
105
105
|
export function scientificToNumber(num) {
|
|
106
|
-
|
|
107
|
-
var zero = '0';
|
|
108
|
-
var parts = String(num).toLowerCase().split('e');
|
|
109
|
-
var e = parts.pop(); // 存储指数
|
|
110
|
-
// @ts-ignore
|
|
106
|
+
var strNum = String(num);
|
|
111
107
|
|
|
112
|
-
|
|
113
|
-
|
|
108
|
+
if (!isScientificNumber(strNum)) {
|
|
109
|
+
return num;
|
|
110
|
+
}
|
|
111
|
+
/** @type string */
|
|
114
112
|
|
|
115
|
-
var sign = e / l; //判断正负
|
|
116
113
|
|
|
117
|
-
|
|
118
|
-
|
|
114
|
+
var ret;
|
|
115
|
+
var zero = '0';
|
|
116
|
+
var parts = strNum.toLowerCase().split('e');
|
|
117
|
+
var e = parts.pop(); // 存储指数
|
|
118
|
+
// @ts-ignore
|
|
119
119
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
var l = Math.abs(e); // 取绝对值,l-1就是0的个数
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
|
|
123
|
+
var sign = e / l; //判断正负
|
|
124
|
+
|
|
125
|
+
var coeff_array = parts[0].split('.'); // 将系数按照小数点拆分
|
|
126
|
+
// 如果是小数
|
|
127
|
+
|
|
128
|
+
if (sign === -1) {
|
|
129
|
+
// 整数部分
|
|
130
|
+
var intVal = trimLeftZero(coeff_array[0]); // 整数部分大于科学计数后面部分
|
|
131
|
+
// 如: 10e-1, 10.2e-1
|
|
125
132
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
133
|
+
if (intVal.length > l) {
|
|
134
|
+
var thanLen = intVal.length - l;
|
|
135
|
+
var dec = coeff_array[1] || '';
|
|
136
|
+
ret = intVal.slice(0, thanLen); // 处理 10e-1, 100e-1
|
|
137
|
+
|
|
138
|
+
if (intVal.slice(thanLen) !== '0' || dec) {
|
|
139
|
+
ret += '.' + intVal.slice(thanLen) + dec;
|
|
131
140
|
}
|
|
141
|
+
} else {
|
|
142
|
+
// 整数部分小于等于科学计数后面部分
|
|
143
|
+
// 如: 1e-1, 0.2e-1, 1.2e-2, 1.2e-1
|
|
144
|
+
ret = zero + '.' + new Array(l - intVal.length + 1).join(zero) + coeff_array.join('');
|
|
132
145
|
}
|
|
133
|
-
}
|
|
146
|
+
} else {
|
|
147
|
+
// 小数部分
|
|
148
|
+
var _dec = coeff_array[1] || ''; // 如果是整数,将整数除第一位之外的非零数字计入位数,相应的减少0的个数
|
|
134
149
|
|
|
135
150
|
|
|
136
|
-
|
|
151
|
+
if (l - _dec.length < 0) {
|
|
152
|
+
ret = trimLeftZero(coeff_array[0] + _dec.substring(0, l)) + '.' + _dec.substring(l);
|
|
153
|
+
} else {
|
|
154
|
+
// 拼接字符串,如果是整数,不需要拼接小数点
|
|
155
|
+
ret = coeff_array.join('') + new Array(l - _dec.length + 1).join(zero);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return trimLeftZero(ret);
|
|
137
160
|
}
|
package/esm/validatePassword.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import devWarn from './utils/devWarn';
|
|
2
2
|
var regNumber = /[\d]/;
|
|
3
3
|
var regLowerCaseLetter = /[a-z]/;
|
|
4
4
|
var regUpperCaseLetter = /[A-Z]/;
|
|
@@ -55,19 +55,22 @@ function hasHex(val) {
|
|
|
55
55
|
*
|
|
56
56
|
* @private
|
|
57
57
|
* @param {string} val 检测的值
|
|
58
|
-
* @param {string}
|
|
58
|
+
* @param {string} chars 特殊字符
|
|
59
59
|
* @returns {boolean} 是否包含特殊字符
|
|
60
60
|
*/
|
|
61
61
|
|
|
62
62
|
|
|
63
|
-
function hasSpecialCharacter(val) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (!chars) {
|
|
63
|
+
function hasSpecialCharacter(val, chars) {
|
|
64
|
+
if (!chars || !val) {
|
|
67
65
|
return false;
|
|
68
66
|
}
|
|
69
67
|
|
|
70
68
|
var specialChars = val.replace(regAllNumberAndLetter, '');
|
|
69
|
+
|
|
70
|
+
if (!specialChars) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
71
74
|
var regChars = hasHex(chars) ? new RegExp("[".concat(chars, "]")) : null;
|
|
72
75
|
|
|
73
76
|
if (regChars) {
|
|
@@ -89,16 +92,21 @@ function hasSpecialCharacter(val) {
|
|
|
89
92
|
*
|
|
90
93
|
* @private
|
|
91
94
|
* @param {string} val 检测的值
|
|
92
|
-
* @param {string} chars
|
|
95
|
+
* @param {string} chars 特殊字符
|
|
93
96
|
* @returns {boolean} 是否包含非法字符
|
|
94
97
|
*/
|
|
95
98
|
|
|
96
99
|
|
|
97
|
-
function hasUnallowableCharacter(val) {
|
|
98
|
-
|
|
100
|
+
function hasUnallowableCharacter(val, chars) {
|
|
101
|
+
if (!val) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
99
105
|
var specialChars = val.replace(regAllNumberAndLetter, '');
|
|
100
106
|
|
|
101
|
-
if (!
|
|
107
|
+
if (!specialChars) {
|
|
108
|
+
return false;
|
|
109
|
+
} else if (!chars) {
|
|
102
110
|
return true;
|
|
103
111
|
}
|
|
104
112
|
|
|
@@ -163,7 +171,7 @@ function hasUnallowableCharacter(val) {
|
|
|
163
171
|
* }
|
|
164
172
|
* }
|
|
165
173
|
*
|
|
166
|
-
* validatePassword('a12345678', {level: 3});
|
|
174
|
+
* validatePassword('a12345678', { level: 3 });
|
|
167
175
|
* // =>
|
|
168
176
|
* {
|
|
169
177
|
* validated: false,
|
|
@@ -177,7 +185,7 @@ function hasUnallowableCharacter(val) {
|
|
|
177
185
|
* }
|
|
178
186
|
* }
|
|
179
187
|
*
|
|
180
|
-
* validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true});
|
|
188
|
+
* validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true });
|
|
181
189
|
* // =>
|
|
182
190
|
* {
|
|
183
191
|
* validated: false,
|
|
@@ -192,7 +200,7 @@ function hasUnallowableCharacter(val) {
|
|
|
192
200
|
* }
|
|
193
201
|
*
|
|
194
202
|
* // 自定义特殊字符
|
|
195
|
-
* validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true, special: '_一二三'});
|
|
203
|
+
* validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true, special: '_一二三' });
|
|
196
204
|
* // =>
|
|
197
205
|
* {
|
|
198
206
|
* validated: true,
|
|
@@ -220,10 +228,7 @@ function validatePassword(value) {
|
|
|
220
228
|
var valStr = value;
|
|
221
229
|
|
|
222
230
|
if (typeof value !== 'string') {
|
|
223
|
-
|
|
224
|
-
console.warn("[validatePassword] value must be a string.");
|
|
225
|
-
}
|
|
226
|
-
|
|
231
|
+
devWarn("[validatePassword] value must be a string.");
|
|
227
232
|
valStr = '';
|
|
228
233
|
}
|
|
229
234
|
|
|
@@ -235,16 +240,16 @@ function validatePassword(value) {
|
|
|
235
240
|
|
|
236
241
|
var containesUpperCaseLetter = hasUpperCaseLetter(valStr); // 包含特殊字符
|
|
237
242
|
|
|
238
|
-
var containesSpecialCharacter = hasSpecialCharacter(valStr, special); //
|
|
243
|
+
var containesSpecialCharacter = hasSpecialCharacter(valStr, special); // 包含非法字符,即含有非数字字母特殊字符以外的其他字符
|
|
239
244
|
|
|
240
245
|
var containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
|
|
241
246
|
|
|
242
247
|
if (containesNumber) {
|
|
243
248
|
currentLevel += 1;
|
|
244
|
-
}
|
|
249
|
+
} // 不区分大小写
|
|
250
|
+
|
|
245
251
|
|
|
246
252
|
if (ignoreCase) {
|
|
247
|
-
// 不区分大小写
|
|
248
253
|
if (containesLowerCaseLetter || containesUpperCaseLetter) {
|
|
249
254
|
currentLevel += 1;
|
|
250
255
|
}
|
package/lib/bytesToSize.js
CHANGED
|
@@ -28,12 +28,13 @@ exports["default"] = void 0;
|
|
|
28
28
|
* // => 1 GB
|
|
29
29
|
*/
|
|
30
30
|
function bytesToSize(bytes) {
|
|
31
|
-
|
|
31
|
+
var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
|
|
32
|
+
if (numBytes === 0 || isNaN(numBytes)) return '0 B';
|
|
32
33
|
var k = 1024; // 存储单位
|
|
33
34
|
|
|
34
35
|
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
35
|
-
var i = Math.floor(Math.log(
|
|
36
|
-
return sizes[i] ? "".concat(Number((
|
|
36
|
+
var i = Math.floor(Math.log(numBytes) / Math.log(k));
|
|
37
|
+
return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(2)), " ").concat(sizes[i]) : numBytes + '';
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
var _default = bytesToSize;
|