util-helpers 4.10.2 → 4.11.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.
- package/dist/util-helpers.js +991 -607
- 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 +17 -16
- package/esm/isChinese.js +24 -11
- package/esm/isSocialCreditCode.js +16 -9
- 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 +17 -16
- package/lib/isChinese.js +24 -11
- package/lib/isSocialCreditCode.js +16 -9
- 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 +4 -2
- package/types/isBusinessLicense.d.ts +7 -10
- package/types/isChinese.d.ts +13 -3
- package/types/isSocialCreditCode.d.ts +8 -5
- 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/dist/util-helpers.js
CHANGED
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
* @param {*} value 检查值
|
|
57
57
|
* @returns {boolean} 是否为 undefined 或 null
|
|
58
58
|
*/
|
|
59
|
+
|
|
59
60
|
function isNil(value) {
|
|
60
61
|
return isUndefined(value) || isNull(value);
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
var toString = Object.prototype.toString;
|
|
65
65
|
/**
|
|
66
66
|
* 检测值的 `toString` 类型
|
|
67
67
|
*
|
|
@@ -71,8 +71,9 @@
|
|
|
71
71
|
* @param {string} typename 类型名称
|
|
72
72
|
* @returns {boolean} 返回值的 `toString` 类型是否匹配
|
|
73
73
|
*/
|
|
74
|
+
|
|
74
75
|
function isType(value, typename) {
|
|
75
|
-
return toString.call(value) ===
|
|
76
|
+
return toString.call(value) === "[object ".concat(typename, "]");
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
/**
|
|
@@ -91,6 +92,7 @@
|
|
|
91
92
|
* isString(1)
|
|
92
93
|
* // => false
|
|
93
94
|
*/
|
|
95
|
+
|
|
94
96
|
function isString(value) {
|
|
95
97
|
return isType(value, 'String');
|
|
96
98
|
}
|
|
@@ -101,6 +103,7 @@
|
|
|
101
103
|
* @param {*} value 值
|
|
102
104
|
* @returns {string} 字符串
|
|
103
105
|
*/
|
|
106
|
+
|
|
104
107
|
function convertToString(value) {
|
|
105
108
|
return isString(value) ? value : String(value);
|
|
106
109
|
}
|
|
@@ -125,16 +128,16 @@
|
|
|
125
128
|
* normalizeString(1); // '1'
|
|
126
129
|
* normalizeString('a'); // 'a'
|
|
127
130
|
*/
|
|
131
|
+
|
|
128
132
|
function normalizeString(value) {
|
|
129
133
|
if (isNil(value)) {
|
|
130
134
|
return '';
|
|
131
135
|
}
|
|
136
|
+
|
|
132
137
|
return convertToString(value);
|
|
133
138
|
}
|
|
134
139
|
|
|
135
|
-
|
|
136
|
-
const reg$d = /^1[3456789]\d{9}$/;
|
|
137
|
-
|
|
140
|
+
var reg$d = /^1[3456789]\d{9}$/;
|
|
138
141
|
/**
|
|
139
142
|
* 检测值是否为手机号码
|
|
140
143
|
*
|
|
@@ -152,14 +155,13 @@
|
|
|
152
155
|
* // => false
|
|
153
156
|
*
|
|
154
157
|
*/
|
|
158
|
+
|
|
155
159
|
function isMobile(value) {
|
|
156
|
-
|
|
160
|
+
var valueStr = normalizeString(value);
|
|
157
161
|
return reg$d.test(valueStr);
|
|
158
162
|
}
|
|
159
163
|
|
|
160
|
-
|
|
161
|
-
const reg$c = /^(0\d{2,3}\-)?([2-9]\d{6,7})(\-\d{1,6})?$/;
|
|
162
|
-
|
|
164
|
+
var reg$c = /^(0\d{2,3}\-)?([2-9]\d{6,7})(\-\d{1,6})?$/;
|
|
163
165
|
/**
|
|
164
166
|
* 检测值是否为固定电话
|
|
165
167
|
*
|
|
@@ -183,14 +185,13 @@
|
|
|
183
185
|
* // => false
|
|
184
186
|
*
|
|
185
187
|
*/
|
|
188
|
+
|
|
186
189
|
function isTelephone(value) {
|
|
187
|
-
|
|
190
|
+
var valueStr = normalizeString(value);
|
|
188
191
|
return reg$c.test(valueStr);
|
|
189
192
|
}
|
|
190
193
|
|
|
191
|
-
|
|
192
|
-
const reg$b = /^\d{6}$/;
|
|
193
|
-
|
|
194
|
+
var reg$b = /^\d{6}$/;
|
|
194
195
|
/**
|
|
195
196
|
* 检测值是否为邮政编码,6位数字
|
|
196
197
|
*
|
|
@@ -208,14 +209,13 @@
|
|
|
208
209
|
* // => false
|
|
209
210
|
*
|
|
210
211
|
*/
|
|
212
|
+
|
|
211
213
|
function isPostcode(value) {
|
|
212
|
-
|
|
214
|
+
var valueStr = normalizeString(value);
|
|
213
215
|
return reg$b.test(valueStr);
|
|
214
216
|
}
|
|
215
217
|
|
|
216
|
-
|
|
217
|
-
const regIdCard$1 = /^[1-9]\d{5}(19|20)?\d{2}((0[1-9])|(1[012]))(([0-2][1-9])|10|20|30|31)\d{3}(\d|X)?$/i;
|
|
218
|
-
|
|
218
|
+
var regIdCard$1 = /^[1-9]\d{5}(19|20)?\d{2}((0[1-9])|(1[012]))(([0-2][1-9])|10|20|30|31)\d{3}(\d|X)?$/i;
|
|
219
219
|
/**
|
|
220
220
|
* 校验码计算
|
|
221
221
|
*
|
|
@@ -225,19 +225,22 @@
|
|
|
225
225
|
* @param {string} id 身份证号码
|
|
226
226
|
* @returns {boolean} 校验码是否正确
|
|
227
227
|
*/
|
|
228
|
+
|
|
228
229
|
function check(id) {
|
|
229
|
-
|
|
230
|
+
var index, sum, num;
|
|
231
|
+
|
|
230
232
|
for (sum = index = 0; index < 17; index++) {
|
|
231
|
-
sum +=
|
|
233
|
+
sum += Math.pow(2, 17 - index) % 11 * Number(id[index]);
|
|
232
234
|
}
|
|
233
|
-
|
|
235
|
+
|
|
236
|
+
num = (12 - sum % 11) % 11;
|
|
237
|
+
|
|
234
238
|
if (num < 10) {
|
|
235
239
|
return num === Number(id[17]);
|
|
236
240
|
} else {
|
|
237
241
|
return id[17].toUpperCase() === 'X';
|
|
238
242
|
}
|
|
239
243
|
}
|
|
240
|
-
|
|
241
244
|
/**
|
|
242
245
|
* 检测值是否为18位身份证号码。<br/>宽松模式下,支持15位身份证号码
|
|
243
246
|
*
|
|
@@ -267,8 +270,17 @@
|
|
|
267
270
|
* // => true
|
|
268
271
|
*
|
|
269
272
|
*/
|
|
270
|
-
|
|
271
|
-
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
function isIdCard(value) {
|
|
276
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
277
|
+
_ref$loose = _ref.loose,
|
|
278
|
+
loose = _ref$loose === void 0 ? false : _ref$loose,
|
|
279
|
+
_ref$checkCode = _ref.checkCode,
|
|
280
|
+
checkCode = _ref$checkCode === void 0 ? true : _ref$checkCode;
|
|
281
|
+
|
|
282
|
+
var valueStr = normalizeString(value);
|
|
283
|
+
|
|
272
284
|
if (valueStr.length === 15 && loose) {
|
|
273
285
|
return regIdCard$1.test(valueStr);
|
|
274
286
|
}
|
|
@@ -277,15 +289,14 @@
|
|
|
277
289
|
if (checkCode) {
|
|
278
290
|
return check(valueStr);
|
|
279
291
|
}
|
|
292
|
+
|
|
280
293
|
return true;
|
|
281
294
|
}
|
|
282
295
|
|
|
283
296
|
return false;
|
|
284
297
|
}
|
|
285
298
|
|
|
286
|
-
|
|
287
|
-
const reg$a = /^[\da-z]+([\-\.\_]?[\da-z]+)*@[\da-z]+([\-\.]?[\da-z]+)*(\.[a-z]{2,})+$/i;
|
|
288
|
-
|
|
299
|
+
var reg$a = /^[\da-z]+([\-\.\_]?[\da-z]+)*@[\da-z]+([\-\.]?[\da-z]+)*(\.[a-z]{2,})+$/i;
|
|
289
300
|
/**
|
|
290
301
|
* 检测值是否为Email
|
|
291
302
|
*
|
|
@@ -303,14 +314,13 @@
|
|
|
303
314
|
* // => false
|
|
304
315
|
*
|
|
305
316
|
*/
|
|
317
|
+
|
|
306
318
|
function isEmail(value) {
|
|
307
|
-
|
|
319
|
+
var valueStr = normalizeString(value);
|
|
308
320
|
return reg$a.test(valueStr);
|
|
309
321
|
}
|
|
310
322
|
|
|
311
|
-
|
|
312
|
-
const reg$9 = /^[1-9]\d{4,10}$/;
|
|
313
|
-
|
|
323
|
+
var reg$9 = /^[1-9]\d{4,10}$/;
|
|
314
324
|
/**
|
|
315
325
|
* 检测值是否为QQ号,非0开头,5至11位数字
|
|
316
326
|
*
|
|
@@ -328,14 +338,13 @@
|
|
|
328
338
|
* // => false
|
|
329
339
|
*
|
|
330
340
|
*/
|
|
341
|
+
|
|
331
342
|
function isQQ(value) {
|
|
332
|
-
|
|
343
|
+
var valueStr = normalizeString(value);
|
|
333
344
|
return reg$9.test(valueStr);
|
|
334
345
|
}
|
|
335
346
|
|
|
336
|
-
|
|
337
|
-
const reg$8 = /^[a-z]([-_a-z0-9]{5,19})+$/i;
|
|
338
|
-
|
|
347
|
+
var reg$8 = /^[a-z]([-_a-z0-9]{5,19})+$/i;
|
|
339
348
|
/**
|
|
340
349
|
* 检测值是否为微信号
|
|
341
350
|
*
|
|
@@ -353,14 +362,13 @@
|
|
|
353
362
|
* // => false
|
|
354
363
|
*
|
|
355
364
|
*/
|
|
365
|
+
|
|
356
366
|
function isWX(value) {
|
|
357
|
-
|
|
367
|
+
var valueStr = normalizeString(value);
|
|
358
368
|
return reg$8.test(valueStr);
|
|
359
369
|
}
|
|
360
370
|
|
|
361
|
-
|
|
362
|
-
const reg$7 = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})|([A-Z0-9]{5}[DF])|([DF][A-Z0-9]{5}))$/;
|
|
363
|
-
|
|
371
|
+
var reg$7 = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})|([A-Z0-9]{5}[DF])|([DF][A-Z0-9]{5}))$/;
|
|
364
372
|
/**
|
|
365
373
|
* 检测值是否为车牌号,支持新能源和非新能源车牌
|
|
366
374
|
*
|
|
@@ -388,35 +396,34 @@
|
|
|
388
396
|
* // => true
|
|
389
397
|
*
|
|
390
398
|
*/
|
|
399
|
+
|
|
391
400
|
function isVehicle(value) {
|
|
392
|
-
|
|
401
|
+
var valueStr = normalizeString(value);
|
|
393
402
|
return reg$7.test(valueStr);
|
|
394
403
|
}
|
|
395
404
|
|
|
396
|
-
//
|
|
397
|
-
const reg$6 = /^[1-9]\d{9,20}$/;
|
|
398
|
-
|
|
399
|
-
// 8~30位数字
|
|
400
|
-
const regLoose = /^\d{8,30}$/;
|
|
405
|
+
var reg$6 = /^[1-9]\d{9,20}$/; // 8~30位数字
|
|
401
406
|
|
|
407
|
+
var regLoose = /^\d{8,30}$/;
|
|
402
408
|
/**
|
|
403
409
|
* luhn 计算校验位
|
|
404
410
|
* @private
|
|
405
411
|
* @param {string} numStr 银行卡前面数字
|
|
406
412
|
* @returns {number}
|
|
407
413
|
*/
|
|
414
|
+
|
|
408
415
|
function sumCheckCode$2(numStr) {
|
|
409
|
-
|
|
416
|
+
var numArr = (numStr + '').replace(/\D/g, '').split('').reverse();
|
|
417
|
+
var sum = 0;
|
|
410
418
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
const currNum = parseInt(numArr[i]);
|
|
419
|
+
for (var i = 0; i < numArr.length; i++) {
|
|
420
|
+
var currNum = parseInt(numArr[i]);
|
|
414
421
|
sum += i % 2 === 0 ? currNum * 2 - (currNum > 4 ? 9 : 0) : currNum;
|
|
415
422
|
}
|
|
416
|
-
|
|
423
|
+
|
|
424
|
+
var mod = sum % 10;
|
|
417
425
|
return mod !== 0 ? 10 - mod : 0;
|
|
418
426
|
}
|
|
419
|
-
|
|
420
427
|
/**
|
|
421
428
|
* 检测值是否为银行卡号。正常模式(非0开头,10~21位数字)宽松模式(8~30位数字)
|
|
422
429
|
*
|
|
@@ -444,24 +451,30 @@
|
|
|
444
451
|
* // => true
|
|
445
452
|
*
|
|
446
453
|
*/
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
function isBankCard(value) {
|
|
457
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
458
|
+
_ref$loose = _ref.loose,
|
|
459
|
+
loose = _ref$loose === void 0 ? false : _ref$loose,
|
|
460
|
+
_ref$luhn = _ref.luhn,
|
|
461
|
+
luhn = _ref$luhn === void 0 ? false : _ref$luhn;
|
|
462
|
+
|
|
463
|
+
var valueStr = normalizeString(value);
|
|
464
|
+
var validateResult = loose ? regLoose.test(valueStr) : reg$6.test(valueStr);
|
|
450
465
|
|
|
451
466
|
if (validateResult && luhn) {
|
|
452
|
-
|
|
453
|
-
|
|
467
|
+
var precode = valueStr.substring(0, valueStr.length - 1);
|
|
468
|
+
var checkCode = valueStr[valueStr.length - 1];
|
|
454
469
|
return checkCode === String(sumCheckCode$2(precode));
|
|
455
470
|
}
|
|
471
|
+
|
|
456
472
|
return validateResult;
|
|
457
473
|
}
|
|
458
474
|
|
|
459
|
-
//
|
|
460
|
-
const baseReg$1 = /^[\dA-HJ-NPQRTUWXY]{2}\d{6}[\dA-HJ-NPQRTUWXY]{10}$/;
|
|
461
|
-
|
|
462
|
-
// 基础字符组成
|
|
463
|
-
const baseCodeArr = '0123456789ABCDEFGHJKLMNPQRTUWXY'.split('');
|
|
475
|
+
var baseReg$1 = /^[\dA-HJ-NPQRTUWXY]{2}\d{6}[\dA-HJ-NPQRTUWXY]{10}$/; // 基础字符组成
|
|
464
476
|
|
|
477
|
+
var baseCodeArr = '0123456789ABCDEFGHJKLMNPQRTUWXY'.split('');
|
|
465
478
|
/**
|
|
466
479
|
* 计算校验码
|
|
467
480
|
*
|
|
@@ -470,27 +483,31 @@
|
|
|
470
483
|
* @param {string} preCode 统一代码前17位
|
|
471
484
|
* @returns {string} 校验码
|
|
472
485
|
*/
|
|
486
|
+
|
|
473
487
|
function sumCheckCode$1(preCode) {
|
|
474
|
-
|
|
488
|
+
var total = 0; // 计算字符位置对应序号和加权因子的乘积,总和
|
|
475
489
|
|
|
476
|
-
|
|
477
|
-
for (let i = 0; i < 17; i++) {
|
|
490
|
+
var _loop = function _loop(i) {
|
|
478
491
|
// 字符位置对应的基础编码序号
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
492
|
+
var index = baseCodeArr.findIndex(function (item) {
|
|
493
|
+
return item === preCode[i];
|
|
494
|
+
}); // 加权因子
|
|
495
|
+
|
|
496
|
+
var wf = Math.pow(3, i) % 31; // 计算序号和加权因子的乘积,并计算级数之和
|
|
497
|
+
|
|
483
498
|
total += index * wf;
|
|
484
|
-
}
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
for (var i = 0; i < 17; i++) {
|
|
502
|
+
_loop(i);
|
|
503
|
+
} // 计算整数求余函数MOD
|
|
485
504
|
|
|
486
|
-
// 计算整数求余函数MOD
|
|
487
|
-
const remainder = total % 31;
|
|
488
|
-
// 校验码字符值序号
|
|
489
|
-
const checkCodeIndex = remainder !== 0 ? 31 - remainder : 0;
|
|
490
505
|
|
|
506
|
+
var remainder = total % 31; // 校验码字符值序号
|
|
507
|
+
|
|
508
|
+
var checkCodeIndex = remainder !== 0 ? 31 - remainder : 0;
|
|
491
509
|
return baseCodeArr[checkCodeIndex];
|
|
492
510
|
}
|
|
493
|
-
|
|
494
511
|
/**
|
|
495
512
|
* 检测值是否为统一社会信用代码,也叫三证合一组织代码。由18位数字和大写字母组成,不使用I、O、Z、S、V。
|
|
496
513
|
*
|
|
@@ -500,7 +517,7 @@
|
|
|
500
517
|
* @since 1.1.0
|
|
501
518
|
* @param {*} value 要检测的值
|
|
502
519
|
* @param {Object} [options] 配置项
|
|
503
|
-
* @param {boolean} [options.
|
|
520
|
+
* @param {boolean} [options.checkCode=true] 是否校验最后一位校验码,如果为false,不校验校验位。
|
|
504
521
|
* @returns {boolean} 值是否为统一社会信用代码
|
|
505
522
|
* @example
|
|
506
523
|
*
|
|
@@ -510,36 +527,45 @@
|
|
|
510
527
|
* isSocialCreditCode('91350100M000100Y4A');
|
|
511
528
|
* // => false
|
|
512
529
|
*
|
|
513
|
-
* //
|
|
514
|
-
* isSocialCreditCode('91350100M000100Y4A', {
|
|
530
|
+
* // 不校验校验位
|
|
531
|
+
* isSocialCreditCode('91350100M000100Y4A', { checkCode: false });
|
|
515
532
|
* // => true
|
|
516
533
|
*
|
|
534
|
+
* isSocialCreditCode('91350100M000100Y', { checkCode: false });
|
|
535
|
+
* // => false
|
|
536
|
+
*
|
|
517
537
|
*/
|
|
518
|
-
function isSocialCreditCode(value, { loose = false } = {}) {
|
|
519
|
-
const valueStr = normalizeString(value);
|
|
520
538
|
|
|
521
|
-
const passBaseRule = baseReg$1.test(valueStr);
|
|
522
539
|
|
|
523
|
-
|
|
524
|
-
|
|
540
|
+
function isSocialCreditCode(value) {
|
|
541
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
542
|
+
var valueStr = normalizeString(value); // @ts-ignore
|
|
543
|
+
// TODO 下个版本废弃 loose
|
|
544
|
+
|
|
545
|
+
var _options$loose = options.loose,
|
|
546
|
+
loose = _options$loose === void 0 ? false : _options$loose,
|
|
547
|
+
_options$checkCode = options.checkCode,
|
|
548
|
+
cc = _options$checkCode === void 0 ? true : _options$checkCode;
|
|
549
|
+
var needCheckCode = !loose && cc;
|
|
550
|
+
var passBaseRule = baseReg$1.test(valueStr); // 宽松模式 或 基础规则不通过直接返回
|
|
551
|
+
|
|
552
|
+
if (!needCheckCode || !passBaseRule) {
|
|
525
553
|
return passBaseRule;
|
|
526
|
-
}
|
|
554
|
+
} // 前17位
|
|
555
|
+
|
|
527
556
|
|
|
528
|
-
//
|
|
529
|
-
const preCode = valueStr.substring(0, 17);
|
|
530
|
-
// 校验码
|
|
531
|
-
const lastCode = valueStr.substring(valueStr.length - 1);
|
|
532
|
-
// 计算校验码
|
|
533
|
-
const checkCode = sumCheckCode$1(preCode);
|
|
557
|
+
var preCode = valueStr.substring(0, 17); // 校验码
|
|
534
558
|
|
|
559
|
+
var lastCode = valueStr.substring(valueStr.length - 1); // 计算校验码
|
|
560
|
+
|
|
561
|
+
var checkCode = sumCheckCode$1(preCode);
|
|
535
562
|
return lastCode === checkCode;
|
|
536
563
|
}
|
|
537
564
|
|
|
538
|
-
|
|
565
|
+
var config = {
|
|
539
566
|
// 禁用warning提示
|
|
540
567
|
disableWarning: true
|
|
541
568
|
};
|
|
542
|
-
|
|
543
569
|
/**
|
|
544
570
|
* 设置禁止warning提示
|
|
545
571
|
* @static
|
|
@@ -547,15 +573,29 @@
|
|
|
547
573
|
* @since 3.6.1
|
|
548
574
|
* @param {boolean} bool 是否禁止warning提示
|
|
549
575
|
*/
|
|
576
|
+
|
|
550
577
|
function setDisableWarning(bool) {
|
|
551
578
|
config.disableWarning = !!bool;
|
|
552
579
|
}
|
|
553
580
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
581
|
+
/**
|
|
582
|
+
* 打印警告信息
|
|
583
|
+
*
|
|
584
|
+
* @param {any[]} args 打印的信息
|
|
585
|
+
*/
|
|
586
|
+
|
|
587
|
+
function devWarn() {
|
|
588
|
+
if (!config.disableWarning) {
|
|
589
|
+
var _console;
|
|
590
|
+
|
|
591
|
+
(_console = console).warn.apply(_console, arguments);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
558
594
|
|
|
595
|
+
var regNumber = /[\d]/;
|
|
596
|
+
var regLowerCaseLetter = /[a-z]/;
|
|
597
|
+
var regUpperCaseLetter = /[A-Z]/;
|
|
598
|
+
var regAllNumberAndLetter = /[\d|a-z]/gi;
|
|
559
599
|
/**
|
|
560
600
|
* 是否包含数字
|
|
561
601
|
*
|
|
@@ -563,10 +603,10 @@
|
|
|
563
603
|
* @param {string} val 检查的值
|
|
564
604
|
* @returns {boolean} 是否包含数字
|
|
565
605
|
*/
|
|
606
|
+
|
|
566
607
|
function hasNumber(val) {
|
|
567
608
|
return regNumber.test(val);
|
|
568
609
|
}
|
|
569
|
-
|
|
570
610
|
/**
|
|
571
611
|
* 是否包含小写字母
|
|
572
612
|
*
|
|
@@ -574,10 +614,11 @@
|
|
|
574
614
|
* @param {string} val 检测的值
|
|
575
615
|
* @returns {boolean} 是否包含小写字母
|
|
576
616
|
*/
|
|
617
|
+
|
|
618
|
+
|
|
577
619
|
function hasLowerCaseLetter(val) {
|
|
578
620
|
return regLowerCaseLetter.test(val);
|
|
579
621
|
}
|
|
580
|
-
|
|
581
622
|
/**
|
|
582
623
|
* 是否包含大写字母
|
|
583
624
|
*
|
|
@@ -585,10 +626,11 @@
|
|
|
585
626
|
* @param {string} val 检测的值
|
|
586
627
|
* @returns {boolean} 是否包含大写字母
|
|
587
628
|
*/
|
|
629
|
+
|
|
630
|
+
|
|
588
631
|
function hasUpperCaseLetter(val) {
|
|
589
632
|
return regUpperCaseLetter.test(val);
|
|
590
633
|
}
|
|
591
|
-
|
|
592
634
|
/**
|
|
593
635
|
* 是否为十六进制
|
|
594
636
|
*
|
|
@@ -596,69 +638,87 @@
|
|
|
596
638
|
* @param {string} val 检测的值
|
|
597
639
|
* @returns {boolean} 是否为十六进制
|
|
598
640
|
*/
|
|
641
|
+
|
|
642
|
+
|
|
599
643
|
function hasHex(val) {
|
|
600
|
-
return val.indexOf('\\x') > -1 || val.indexOf(
|
|
644
|
+
return val.indexOf('\\x') > -1 || val.indexOf("\\u") > -1;
|
|
601
645
|
}
|
|
602
|
-
|
|
603
646
|
/**
|
|
604
647
|
* 是否包含特殊字符
|
|
605
648
|
*
|
|
606
649
|
* @private
|
|
607
650
|
* @param {string} val 检测的值
|
|
608
|
-
* @param {string}
|
|
651
|
+
* @param {string} chars 特殊字符
|
|
609
652
|
* @returns {boolean} 是否包含特殊字符
|
|
610
653
|
*/
|
|
611
|
-
|
|
612
|
-
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
function hasSpecialCharacter(val, chars) {
|
|
657
|
+
if (!chars || !val) {
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
var specialChars = val.replace(regAllNumberAndLetter, '');
|
|
662
|
+
|
|
663
|
+
if (!specialChars) {
|
|
613
664
|
return false;
|
|
614
665
|
}
|
|
615
666
|
|
|
616
|
-
|
|
617
|
-
const regChars = hasHex(chars) ? new RegExp(`[${chars}]`) : null;
|
|
667
|
+
var regChars = hasHex(chars) ? new RegExp("[".concat(chars, "]")) : null;
|
|
618
668
|
|
|
619
669
|
if (regChars) {
|
|
620
670
|
return regChars.test(specialChars);
|
|
621
671
|
}
|
|
622
672
|
|
|
623
|
-
|
|
624
|
-
specialChars.split('').some((charItem)
|
|
673
|
+
var ret = false;
|
|
674
|
+
specialChars.split('').some(function (charItem) {
|
|
625
675
|
if (chars.indexOf(charItem) > -1) {
|
|
626
676
|
ret = true;
|
|
627
677
|
}
|
|
678
|
+
|
|
628
679
|
return ret;
|
|
629
680
|
});
|
|
630
681
|
return ret;
|
|
631
682
|
}
|
|
632
|
-
|
|
633
683
|
/**
|
|
634
684
|
* 是否包含非法字符
|
|
635
685
|
*
|
|
636
686
|
* @private
|
|
637
687
|
* @param {string} val 检测的值
|
|
638
|
-
* @param {string} chars
|
|
688
|
+
* @param {string} chars 特殊字符
|
|
639
689
|
* @returns {boolean} 是否包含非法字符
|
|
640
690
|
*/
|
|
641
|
-
function hasUnallowableCharacter(val, chars = '') {
|
|
642
|
-
const specialChars = val.replace(regAllNumberAndLetter, '');
|
|
643
691
|
|
|
644
|
-
|
|
692
|
+
|
|
693
|
+
function hasUnallowableCharacter(val, chars) {
|
|
694
|
+
if (!val) {
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
var specialChars = val.replace(regAllNumberAndLetter, '');
|
|
699
|
+
|
|
700
|
+
if (!specialChars) {
|
|
701
|
+
return false;
|
|
702
|
+
} else if (!chars) {
|
|
645
703
|
return true;
|
|
646
704
|
}
|
|
647
705
|
|
|
648
|
-
|
|
706
|
+
var regChars = hasHex(chars) ? new RegExp("[^".concat(chars, "]")) : null;
|
|
707
|
+
|
|
649
708
|
if (regChars) {
|
|
650
709
|
return regChars.test(specialChars);
|
|
651
710
|
}
|
|
652
|
-
|
|
653
|
-
|
|
711
|
+
|
|
712
|
+
var ret = false;
|
|
713
|
+
specialChars.split('').some(function (charItem) {
|
|
654
714
|
if (chars.indexOf(charItem) === -1) {
|
|
655
715
|
ret = true;
|
|
656
716
|
}
|
|
717
|
+
|
|
657
718
|
return ret;
|
|
658
719
|
});
|
|
659
720
|
return ret;
|
|
660
721
|
}
|
|
661
|
-
|
|
662
722
|
/**
|
|
663
723
|
* @typedef {Object} PasswordContaines - 验证密码的包含内容
|
|
664
724
|
* @property {boolean} number - 包含数字
|
|
@@ -704,7 +764,7 @@
|
|
|
704
764
|
* }
|
|
705
765
|
* }
|
|
706
766
|
*
|
|
707
|
-
* validatePassword('a12345678', {level: 3});
|
|
767
|
+
* validatePassword('a12345678', { level: 3 });
|
|
708
768
|
* // =>
|
|
709
769
|
* {
|
|
710
770
|
* validated: false,
|
|
@@ -718,7 +778,7 @@
|
|
|
718
778
|
* }
|
|
719
779
|
* }
|
|
720
780
|
*
|
|
721
|
-
* validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true});
|
|
781
|
+
* validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true });
|
|
722
782
|
* // =>
|
|
723
783
|
* {
|
|
724
784
|
* validated: false,
|
|
@@ -733,7 +793,7 @@
|
|
|
733
793
|
* }
|
|
734
794
|
*
|
|
735
795
|
* // 自定义特殊字符
|
|
736
|
-
* validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true, special: '_一二三'});
|
|
796
|
+
* validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true, special: '_一二三' });
|
|
737
797
|
* // =>
|
|
738
798
|
* {
|
|
739
799
|
* validated: true,
|
|
@@ -747,35 +807,42 @@
|
|
|
747
807
|
* }
|
|
748
808
|
* }
|
|
749
809
|
*/
|
|
750
|
-
|
|
751
|
-
|
|
810
|
+
|
|
811
|
+
|
|
812
|
+
function validatePassword(value) {
|
|
813
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
814
|
+
_ref$level = _ref.level,
|
|
815
|
+
level = _ref$level === void 0 ? 2 : _ref$level,
|
|
816
|
+
_ref$ignoreCase = _ref.ignoreCase,
|
|
817
|
+
ignoreCase = _ref$ignoreCase === void 0 ? false : _ref$ignoreCase,
|
|
818
|
+
_ref$special = _ref.special,
|
|
819
|
+
special = _ref$special === void 0 ? '\\x21-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E' : _ref$special;
|
|
820
|
+
|
|
821
|
+
var valStr = value;
|
|
752
822
|
|
|
753
823
|
if (typeof value !== 'string') {
|
|
754
|
-
|
|
755
|
-
console.warn(`[validatePassword] value must be a string.`);
|
|
756
|
-
}
|
|
824
|
+
devWarn("[validatePassword] value must be a string.");
|
|
757
825
|
valStr = '';
|
|
758
826
|
}
|
|
759
827
|
|
|
760
|
-
|
|
828
|
+
var currentLevel = 0; // 包含数字
|
|
829
|
+
|
|
830
|
+
var containesNumber = hasNumber(valStr); // 包含小写字母
|
|
761
831
|
|
|
762
|
-
//
|
|
763
|
-
|
|
764
|
-
//
|
|
765
|
-
|
|
766
|
-
//
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
const containesSpecialCharacter = hasSpecialCharacter(valStr, special);
|
|
770
|
-
// 包含非法字符
|
|
771
|
-
const containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
|
|
832
|
+
var containesLowerCaseLetter = hasLowerCaseLetter(valStr); // 包含大写字母
|
|
833
|
+
|
|
834
|
+
var containesUpperCaseLetter = hasUpperCaseLetter(valStr); // 包含特殊字符
|
|
835
|
+
|
|
836
|
+
var containesSpecialCharacter = hasSpecialCharacter(valStr, special); // 包含非法字符,即含有非数字字母特殊字符以外的其他字符
|
|
837
|
+
|
|
838
|
+
var containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
|
|
772
839
|
|
|
773
840
|
if (containesNumber) {
|
|
774
841
|
currentLevel += 1;
|
|
775
|
-
}
|
|
842
|
+
} // 不区分大小写
|
|
843
|
+
|
|
776
844
|
|
|
777
845
|
if (ignoreCase) {
|
|
778
|
-
// 不区分大小写
|
|
779
846
|
if (containesLowerCaseLetter || containesUpperCaseLetter) {
|
|
780
847
|
currentLevel += 1;
|
|
781
848
|
}
|
|
@@ -784,6 +851,7 @@
|
|
|
784
851
|
if (containesLowerCaseLetter) {
|
|
785
852
|
currentLevel += 1;
|
|
786
853
|
}
|
|
854
|
+
|
|
787
855
|
if (containesUpperCaseLetter) {
|
|
788
856
|
currentLevel += 1;
|
|
789
857
|
}
|
|
@@ -791,13 +859,12 @@
|
|
|
791
859
|
|
|
792
860
|
if (containesSpecialCharacter) {
|
|
793
861
|
currentLevel += 1;
|
|
794
|
-
}
|
|
862
|
+
} // 验证结果
|
|
795
863
|
|
|
796
|
-
// 验证结果
|
|
797
|
-
const validated = currentLevel >= level && !containesUnallowableCharacter;
|
|
798
864
|
|
|
865
|
+
var validated = currentLevel >= level && !containesUnallowableCharacter;
|
|
799
866
|
return {
|
|
800
|
-
validated,
|
|
867
|
+
validated: validated,
|
|
801
868
|
level: currentLevel,
|
|
802
869
|
containes: {
|
|
803
870
|
number: containesNumber,
|
|
@@ -849,13 +916,24 @@
|
|
|
849
916
|
* isPassword(' _Aa12345678', {level: 3, ignoreCase: true});
|
|
850
917
|
* // => false
|
|
851
918
|
*/
|
|
852
|
-
function isPassword(value, { level = 2, ignoreCase = false, special = '\\x21-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E' } = {}) {
|
|
853
|
-
return validatePassword(value, { level, ignoreCase, special }).validated;
|
|
854
|
-
}
|
|
855
919
|
|
|
856
|
-
|
|
857
|
-
|
|
920
|
+
function isPassword(value) {
|
|
921
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
922
|
+
_ref$level = _ref.level,
|
|
923
|
+
level = _ref$level === void 0 ? 2 : _ref$level,
|
|
924
|
+
_ref$ignoreCase = _ref.ignoreCase,
|
|
925
|
+
ignoreCase = _ref$ignoreCase === void 0 ? false : _ref$ignoreCase,
|
|
926
|
+
_ref$special = _ref.special,
|
|
927
|
+
special = _ref$special === void 0 ? '\\x21-\\x2F\\x3A-\\x40\\x5B-\\x60\\x7B-\\x7E' : _ref$special;
|
|
928
|
+
|
|
929
|
+
return validatePassword(value, {
|
|
930
|
+
level: level,
|
|
931
|
+
ignoreCase: ignoreCase,
|
|
932
|
+
special: special
|
|
933
|
+
}).validated;
|
|
934
|
+
}
|
|
858
935
|
|
|
936
|
+
var reg$5 = /^((e[\da-z])|(de)|(se)|(pe)|([khm][\da-z]))[\da-z]{7}$/i;
|
|
859
937
|
/**
|
|
860
938
|
* 检测值是否为护照号
|
|
861
939
|
* 支持普通护照(E*)、外交护照(DE)、公务护照(SE)、公务普通护照(PE)、香港特区护照(K/KJ/H*)、澳门特区护照(MA/MB/M*),注意不区分大小写
|
|
@@ -875,49 +953,37 @@
|
|
|
875
953
|
* // => false
|
|
876
954
|
*
|
|
877
955
|
*/
|
|
956
|
+
|
|
878
957
|
function isPassport(value) {
|
|
879
|
-
|
|
958
|
+
var valueStr = normalizeString(value);
|
|
880
959
|
return reg$5.test(valueStr);
|
|
881
960
|
}
|
|
882
961
|
|
|
883
|
-
|
|
962
|
+
var chineseDictionary = {
|
|
884
963
|
// 基本汉字
|
|
885
|
-
chineseBasic:
|
|
886
|
-
|
|
964
|
+
chineseBasic: "[\u4E00-\u9FA5]",
|
|
887
965
|
// 基本汉字补充
|
|
888
|
-
chineseExtend:
|
|
889
|
-
|
|
966
|
+
chineseExtend: "[\u9EA6-\u9FEF]",
|
|
890
967
|
// 汉字扩展A
|
|
891
|
-
chineseExtendA:
|
|
892
|
-
|
|
968
|
+
chineseExtendA: "[\u3400-\u4DB5]",
|
|
893
969
|
// 汉字扩展B
|
|
894
|
-
chineseExtendB:
|
|
895
|
-
|
|
970
|
+
chineseExtendB: "[\uD840\uDC00-\uD869\uDED6]",
|
|
896
971
|
// 汉字扩展C
|
|
897
|
-
chineseExtendC:
|
|
898
|
-
|
|
972
|
+
chineseExtendC: "[\uD869\uDF00-\uD86D\uDF34]",
|
|
899
973
|
// 汉字扩展D
|
|
900
|
-
chineseExtendD:
|
|
901
|
-
|
|
974
|
+
chineseExtendD: "[\uD86D\uDF40-\uD86E\uDC1D]",
|
|
902
975
|
// 汉字扩展E
|
|
903
|
-
chineseExtendE:
|
|
904
|
-
|
|
976
|
+
chineseExtendE: "[\uD86E\uDC20-\uD873\uDEA1]",
|
|
905
977
|
// 汉字扩展F
|
|
906
|
-
chineseExtendF:
|
|
978
|
+
chineseExtendF: "[\uD873\uDEB0-\uD87A\uDFE0]"
|
|
907
979
|
};
|
|
980
|
+
var looseChineseRegExp = chineseDictionary.chineseBasic + '+';
|
|
981
|
+
var chineseRegExp = '^' + chineseDictionary.chineseBasic + '+$';
|
|
982
|
+
var chineseWithExtend = '(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')';
|
|
983
|
+
var looseChineseExtendRegExp = chineseWithExtend + '+';
|
|
984
|
+
var chineseExtendRegExp = '^' + chineseWithExtend + '+$'; // eslint-disable-next-line no-prototype-builtins
|
|
908
985
|
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
let chineseRegExp = '^' + chineseDictionary.chineseBasic + '+$';
|
|
912
|
-
|
|
913
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
914
|
-
const supportRegExpUnicode = RegExp.prototype.hasOwnProperty('unicode');
|
|
915
|
-
|
|
916
|
-
if (supportRegExpUnicode) {
|
|
917
|
-
looseChineseRegExp = '(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')+';
|
|
918
|
-
chineseRegExp = '^(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')+$';
|
|
919
|
-
}
|
|
920
|
-
|
|
986
|
+
var supportRegExpUnicode = RegExp.prototype.hasOwnProperty('unicode');
|
|
921
987
|
/**
|
|
922
988
|
* 检测值是否为中文
|
|
923
989
|
*
|
|
@@ -928,6 +994,7 @@
|
|
|
928
994
|
* @param {*} value 要检测的值
|
|
929
995
|
* @param {Object} [options] 配置项
|
|
930
996
|
* @param {boolean} [options.loose=false] 宽松模式。如果为true,只要包含中文即为true
|
|
997
|
+
* @param {boolean} [options.useExtend=false] 使用统一表意文字扩展A-F。注意:如果不支持 `RegExp.prototype.unicode`,扩展字符集将自动不生效,如IE浏览器。
|
|
931
998
|
* @returns {boolean} 值是否为中文
|
|
932
999
|
* @example
|
|
933
1000
|
*
|
|
@@ -938,22 +1005,40 @@
|
|
|
938
1005
|
* // => false
|
|
939
1006
|
*
|
|
940
1007
|
* // 宽松模式,只要包含中文即为true
|
|
941
|
-
* isChinese('林A', {loose: true});
|
|
1008
|
+
* isChinese('林A', { loose: true });
|
|
942
1009
|
* // => true
|
|
943
1010
|
*
|
|
944
|
-
* isChinese('A林A', {loose: true});
|
|
1011
|
+
* isChinese('A林A', { loose: true });
|
|
945
1012
|
* // => true
|
|
946
1013
|
*
|
|
1014
|
+
* isChinese('𠮷');
|
|
1015
|
+
* // => false
|
|
1016
|
+
*
|
|
1017
|
+
* // 使用中文扩展字符集,需要浏览器支持 RegExp.prototype.unicode 才生效。
|
|
1018
|
+
* isChinese('𠮷', { useExtend: true });
|
|
1019
|
+
* // => true
|
|
1020
|
+
* isChinese('𠮷aa', { useExtend: true, loose: true });
|
|
1021
|
+
* // => true
|
|
947
1022
|
*/
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
1023
|
+
|
|
1024
|
+
function isChinese(value) {
|
|
1025
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
1026
|
+
_ref$loose = _ref.loose,
|
|
1027
|
+
loose = _ref$loose === void 0 ? false : _ref$loose,
|
|
1028
|
+
_ref$useExtend = _ref.useExtend,
|
|
1029
|
+
useExtend = _ref$useExtend === void 0 ? false : _ref$useExtend;
|
|
1030
|
+
|
|
1031
|
+
var valueStr = normalizeString(value);
|
|
1032
|
+
var basicRegExp = loose ? looseChineseRegExp : chineseRegExp;
|
|
1033
|
+
var extendRegExp = loose ? looseChineseExtendRegExp : chineseExtendRegExp;
|
|
1034
|
+
var hasExtend = useExtend && supportRegExpUnicode;
|
|
1035
|
+
var resultRegExp = hasExtend ? extendRegExp : basicRegExp;
|
|
1036
|
+
var flag = hasExtend ? 'u' : undefined;
|
|
1037
|
+
var reg = new RegExp(resultRegExp, flag);
|
|
951
1038
|
return reg.test(valueStr);
|
|
952
1039
|
}
|
|
953
1040
|
|
|
954
|
-
|
|
955
|
-
const reg$4 = /^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
|
|
956
|
-
|
|
1041
|
+
var reg$4 = /^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
|
|
957
1042
|
/**
|
|
958
1043
|
* 检测值是否为ipv4
|
|
959
1044
|
*
|
|
@@ -977,14 +1062,13 @@
|
|
|
977
1062
|
* // => false
|
|
978
1063
|
*
|
|
979
1064
|
*/
|
|
1065
|
+
|
|
980
1066
|
function isIPv4(value) {
|
|
981
|
-
|
|
1067
|
+
var valueStr = normalizeString(value);
|
|
982
1068
|
return reg$4.test(valueStr);
|
|
983
1069
|
}
|
|
984
1070
|
|
|
985
|
-
|
|
986
|
-
const reg$3 = /^((([0-9A-F]{1,4}:){7}([0-9A-F]{1,4}|:))|(([0-9A-F]{1,4}:){6}(:[0-9A-F]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3})|:))|(([0-9A-F]{1,4}:){5}(((:[0-9A-F]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3})|:))|(([0-9A-F]{1,4}:){4}(((:[0-9A-F]{1,4}){1,3})|((:[0-9A-F]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){3}(((:[0-9A-F]{1,4}){1,4})|((:[0-9A-F]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){2}(((:[0-9A-F]{1,4}){1,5})|((:[0-9A-F]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){1}(((:[0-9A-F]{1,4}){1,6})|((:[0-9A-F]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(:(((:[0-9A-F]{1,4}){1,7})|((:[0-9A-F]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:)))(%.+)?$/i;
|
|
987
|
-
|
|
1071
|
+
var reg$3 = /^((([0-9A-F]{1,4}:){7}([0-9A-F]{1,4}|:))|(([0-9A-F]{1,4}:){6}(:[0-9A-F]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3})|:))|(([0-9A-F]{1,4}:){5}(((:[0-9A-F]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3})|:))|(([0-9A-F]{1,4}:){4}(((:[0-9A-F]{1,4}){1,3})|((:[0-9A-F]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){3}(((:[0-9A-F]{1,4}){1,4})|((:[0-9A-F]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){2}(((:[0-9A-F]{1,4}){1,5})|((:[0-9A-F]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(([0-9A-F]{1,4}:){1}(((:[0-9A-F]{1,4}){1,6})|((:[0-9A-F]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:))|(:(((:[0-9A-F]{1,4}){1,7})|((:[0-9A-F]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|\d{1,2})(\.(25[0-5]|2[0-4]\d|1\d\d|\d{1,2})){3}))|:)))(%.+)?$/i;
|
|
988
1072
|
/**
|
|
989
1073
|
* 检测值是否为ipv6
|
|
990
1074
|
*
|
|
@@ -1033,14 +1117,13 @@
|
|
|
1033
1117
|
* // => false
|
|
1034
1118
|
*
|
|
1035
1119
|
*/
|
|
1120
|
+
|
|
1036
1121
|
function isIPv6(value) {
|
|
1037
|
-
|
|
1122
|
+
var valueStr = normalizeString(value);
|
|
1038
1123
|
return reg$3.test(valueStr);
|
|
1039
1124
|
}
|
|
1040
1125
|
|
|
1041
|
-
|
|
1042
|
-
const reg$2 = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\*\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
|
|
1043
|
-
|
|
1126
|
+
var reg$2 = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\*\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
|
|
1044
1127
|
/**
|
|
1045
1128
|
* 检测值是否为url
|
|
1046
1129
|
*
|
|
@@ -1073,14 +1156,13 @@
|
|
|
1073
1156
|
* isUrl('http://www.example.com/test/123?foo=bar');
|
|
1074
1157
|
* // => true
|
|
1075
1158
|
*/
|
|
1159
|
+
|
|
1076
1160
|
function isUrl(value) {
|
|
1077
|
-
|
|
1161
|
+
var valueStr = normalizeString(value);
|
|
1078
1162
|
return reg$2.test(valueStr);
|
|
1079
1163
|
}
|
|
1080
1164
|
|
|
1081
|
-
|
|
1082
|
-
const baseReg = /^\d{15}$/;
|
|
1083
|
-
|
|
1165
|
+
var baseReg = /^\d{15}$/;
|
|
1084
1166
|
/**
|
|
1085
1167
|
* 计算校验码
|
|
1086
1168
|
*
|
|
@@ -1089,29 +1171,29 @@
|
|
|
1089
1171
|
* @param {string} preCode 营业执照前14位
|
|
1090
1172
|
* @returns {number} 校验码
|
|
1091
1173
|
*/
|
|
1174
|
+
|
|
1092
1175
|
function sumCheckCode(preCode) {
|
|
1093
|
-
|
|
1176
|
+
var retNum; // 校验位数字
|
|
1094
1177
|
|
|
1095
|
-
|
|
1178
|
+
var pj = 10; // Pj+1 11,初始为10
|
|
1096
1179
|
|
|
1097
|
-
for (
|
|
1098
|
-
|
|
1099
|
-
|
|
1180
|
+
for (var j = 0; j < 14; j++) {
|
|
1181
|
+
var sj = pj + Number(preCode[j]);
|
|
1182
|
+
var sj10 = sj % 10;
|
|
1100
1183
|
sj10 = sj10 === 0 ? 10 : sj10;
|
|
1101
|
-
|
|
1184
|
+
var pj1 = sj10 * 2;
|
|
1102
1185
|
pj = pj1 % 11;
|
|
1103
|
-
}
|
|
1186
|
+
} // 反模10计算
|
|
1104
1187
|
|
|
1105
|
-
|
|
1106
|
-
if (pj ===
|
|
1107
|
-
retNum =
|
|
1188
|
+
|
|
1189
|
+
if (pj === 1) {
|
|
1190
|
+
retNum = 0;
|
|
1108
1191
|
} else {
|
|
1109
1192
|
retNum = 11 - pj;
|
|
1110
1193
|
}
|
|
1111
1194
|
|
|
1112
1195
|
return retNum;
|
|
1113
1196
|
}
|
|
1114
|
-
|
|
1115
1197
|
/**
|
|
1116
1198
|
* 检测值是否为营业执照号,也叫工商注册号。由14位数字本体码和1位数字校验码组成,其中本体码从左至右依次为:6位首次登记机关码、8位顺序码。
|
|
1117
1199
|
*
|
|
@@ -1121,45 +1203,257 @@
|
|
|
1121
1203
|
* @since 3.5.0
|
|
1122
1204
|
* @param {*} value 要检测的值
|
|
1123
1205
|
* @param {Object} [options] 配置项
|
|
1124
|
-
* @param {boolean} [options.
|
|
1206
|
+
* @param {boolean} [options.checkCode=true] 是否校验最后一位校验码,如果为false,不校验校验位。
|
|
1125
1207
|
* @returns {boolean} 值是否为营业执照号
|
|
1126
1208
|
* @example
|
|
1127
1209
|
*
|
|
1128
1210
|
* isBusinessLicense('310115600985533');
|
|
1129
1211
|
* // => true
|
|
1130
1212
|
*
|
|
1131
|
-
* isBusinessLicense('3101156009');
|
|
1132
|
-
* // => false
|
|
1133
|
-
*
|
|
1134
|
-
* isBusinessLicense('3101156009', { loose: true });
|
|
1135
|
-
* // => false
|
|
1136
|
-
*
|
|
1137
1213
|
* isBusinessLicense('310115600985535');
|
|
1138
1214
|
* // => false
|
|
1139
1215
|
*
|
|
1140
|
-
* isBusinessLicense('310115600985535', {
|
|
1216
|
+
* isBusinessLicense('310115600985535', { checkCode: false });
|
|
1141
1217
|
* // => true
|
|
1218
|
+
*
|
|
1219
|
+
* isBusinessLicense('31011560098', { checkCode: false });
|
|
1220
|
+
* // => false
|
|
1142
1221
|
*/
|
|
1143
|
-
function isBusinessLicense(value, { loose = false } = {}) {
|
|
1144
|
-
const valueStr = normalizeString(value);
|
|
1145
1222
|
|
|
1146
|
-
const passBaseRule = baseReg.test(valueStr);
|
|
1147
1223
|
|
|
1148
|
-
|
|
1149
|
-
|
|
1224
|
+
function isBusinessLicense(value) {
|
|
1225
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1226
|
+
var valueStr = normalizeString(value); // @ts-ignore
|
|
1227
|
+
// TODO 下个版本废弃 loose
|
|
1228
|
+
|
|
1229
|
+
var _options$loose = options.loose,
|
|
1230
|
+
loose = _options$loose === void 0 ? false : _options$loose,
|
|
1231
|
+
_options$checkCode = options.checkCode,
|
|
1232
|
+
cc = _options$checkCode === void 0 ? true : _options$checkCode;
|
|
1233
|
+
var needCheckCode = !loose && cc;
|
|
1234
|
+
var passBaseRule = baseReg.test(valueStr); // 宽松模式 或 基础规则不通过直接返回
|
|
1235
|
+
|
|
1236
|
+
if (!needCheckCode || !passBaseRule) {
|
|
1150
1237
|
return passBaseRule;
|
|
1151
|
-
}
|
|
1238
|
+
} // 前14位
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
var preCode = valueStr.substring(0, 14); // 校验码
|
|
1152
1242
|
|
|
1153
|
-
//
|
|
1154
|
-
const preCode = valueStr.substring(0, 14);
|
|
1155
|
-
// 校验码
|
|
1156
|
-
const lastCode = valueStr.substring(valueStr.length - 1);
|
|
1157
|
-
// 计算校验码
|
|
1158
|
-
const checkCode = sumCheckCode(preCode);
|
|
1243
|
+
var lastCode = valueStr.substring(valueStr.length - 1); // 计算校验码
|
|
1159
1244
|
|
|
1245
|
+
var checkCode = sumCheckCode(preCode);
|
|
1160
1246
|
return lastCode === String(checkCode);
|
|
1161
1247
|
}
|
|
1162
1248
|
|
|
1249
|
+
function _typeof(obj) {
|
|
1250
|
+
"@babel/helpers - typeof";
|
|
1251
|
+
|
|
1252
|
+
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
|
1253
|
+
_typeof = function (obj) {
|
|
1254
|
+
return typeof obj;
|
|
1255
|
+
};
|
|
1256
|
+
} else {
|
|
1257
|
+
_typeof = function (obj) {
|
|
1258
|
+
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
return _typeof(obj);
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
function _wrapRegExp() {
|
|
1266
|
+
_wrapRegExp = function (re, groups) {
|
|
1267
|
+
return new BabelRegExp(re, undefined, groups);
|
|
1268
|
+
};
|
|
1269
|
+
|
|
1270
|
+
var _super = RegExp.prototype;
|
|
1271
|
+
|
|
1272
|
+
var _groups = new WeakMap();
|
|
1273
|
+
|
|
1274
|
+
function BabelRegExp(re, flags, groups) {
|
|
1275
|
+
var _this = new RegExp(re, flags);
|
|
1276
|
+
|
|
1277
|
+
_groups.set(_this, groups || _groups.get(re));
|
|
1278
|
+
|
|
1279
|
+
return _setPrototypeOf(_this, BabelRegExp.prototype);
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
_inherits(BabelRegExp, RegExp);
|
|
1283
|
+
|
|
1284
|
+
BabelRegExp.prototype.exec = function (str) {
|
|
1285
|
+
var result = _super.exec.call(this, str);
|
|
1286
|
+
|
|
1287
|
+
if (result) result.groups = buildGroups(result, this);
|
|
1288
|
+
return result;
|
|
1289
|
+
};
|
|
1290
|
+
|
|
1291
|
+
BabelRegExp.prototype[Symbol.replace] = function (str, substitution) {
|
|
1292
|
+
if (typeof substitution === "string") {
|
|
1293
|
+
var groups = _groups.get(this);
|
|
1294
|
+
|
|
1295
|
+
return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) {
|
|
1296
|
+
return "$" + groups[name];
|
|
1297
|
+
}));
|
|
1298
|
+
} else if (typeof substitution === "function") {
|
|
1299
|
+
var _this = this;
|
|
1300
|
+
|
|
1301
|
+
return _super[Symbol.replace].call(this, str, function () {
|
|
1302
|
+
var args = arguments;
|
|
1303
|
+
|
|
1304
|
+
if (typeof args[args.length - 1] !== "object") {
|
|
1305
|
+
args = [].slice.call(args);
|
|
1306
|
+
args.push(buildGroups(args, _this));
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
return substitution.apply(this, args);
|
|
1310
|
+
});
|
|
1311
|
+
} else {
|
|
1312
|
+
return _super[Symbol.replace].call(this, str, substitution);
|
|
1313
|
+
}
|
|
1314
|
+
};
|
|
1315
|
+
|
|
1316
|
+
function buildGroups(result, re) {
|
|
1317
|
+
var g = _groups.get(re);
|
|
1318
|
+
|
|
1319
|
+
return Object.keys(g).reduce(function (groups, name) {
|
|
1320
|
+
groups[name] = result[g[name]];
|
|
1321
|
+
return groups;
|
|
1322
|
+
}, Object.create(null));
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
return _wrapRegExp.apply(this, arguments);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
function _inherits(subClass, superClass) {
|
|
1329
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
|
1330
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
|
1334
|
+
constructor: {
|
|
1335
|
+
value: subClass,
|
|
1336
|
+
writable: true,
|
|
1337
|
+
configurable: true
|
|
1338
|
+
}
|
|
1339
|
+
});
|
|
1340
|
+
if (superClass) _setPrototypeOf(subClass, superClass);
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
function _setPrototypeOf(o, p) {
|
|
1344
|
+
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
|
1345
|
+
o.__proto__ = p;
|
|
1346
|
+
return o;
|
|
1347
|
+
};
|
|
1348
|
+
|
|
1349
|
+
return _setPrototypeOf(o, p);
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
function _isNativeReflectConstruct() {
|
|
1353
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
|
1354
|
+
if (Reflect.construct.sham) return false;
|
|
1355
|
+
if (typeof Proxy === "function") return true;
|
|
1356
|
+
|
|
1357
|
+
try {
|
|
1358
|
+
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
|
1359
|
+
return true;
|
|
1360
|
+
} catch (e) {
|
|
1361
|
+
return false;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
function _construct(Parent, args, Class) {
|
|
1366
|
+
if (_isNativeReflectConstruct()) {
|
|
1367
|
+
_construct = Reflect.construct;
|
|
1368
|
+
} else {
|
|
1369
|
+
_construct = function _construct(Parent, args, Class) {
|
|
1370
|
+
var a = [null];
|
|
1371
|
+
a.push.apply(a, args);
|
|
1372
|
+
var Constructor = Function.bind.apply(Parent, a);
|
|
1373
|
+
var instance = new Constructor();
|
|
1374
|
+
if (Class) _setPrototypeOf(instance, Class.prototype);
|
|
1375
|
+
return instance;
|
|
1376
|
+
};
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
return _construct.apply(null, arguments);
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
function _slicedToArray(arr, i) {
|
|
1383
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
function _toConsumableArray(arr) {
|
|
1387
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
function _arrayWithoutHoles(arr) {
|
|
1391
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
function _arrayWithHoles(arr) {
|
|
1395
|
+
if (Array.isArray(arr)) return arr;
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
function _iterableToArray(iter) {
|
|
1399
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
function _iterableToArrayLimit(arr, i) {
|
|
1403
|
+
var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]);
|
|
1404
|
+
|
|
1405
|
+
if (_i == null) return;
|
|
1406
|
+
var _arr = [];
|
|
1407
|
+
var _n = true;
|
|
1408
|
+
var _d = false;
|
|
1409
|
+
|
|
1410
|
+
var _s, _e;
|
|
1411
|
+
|
|
1412
|
+
try {
|
|
1413
|
+
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
|
|
1414
|
+
_arr.push(_s.value);
|
|
1415
|
+
|
|
1416
|
+
if (i && _arr.length === i) break;
|
|
1417
|
+
}
|
|
1418
|
+
} catch (err) {
|
|
1419
|
+
_d = true;
|
|
1420
|
+
_e = err;
|
|
1421
|
+
} finally {
|
|
1422
|
+
try {
|
|
1423
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
1424
|
+
} finally {
|
|
1425
|
+
if (_d) throw _e;
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
return _arr;
|
|
1430
|
+
}
|
|
1431
|
+
|
|
1432
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
1433
|
+
if (!o) return;
|
|
1434
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
1435
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
1436
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
1437
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
1438
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
function _arrayLikeToArray(arr, len) {
|
|
1442
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
1443
|
+
|
|
1444
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
1445
|
+
|
|
1446
|
+
return arr2;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
function _nonIterableSpread() {
|
|
1450
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
function _nonIterableRest() {
|
|
1454
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1163
1457
|
/**
|
|
1164
1458
|
* 检测值是否类似Promise对象
|
|
1165
1459
|
*
|
|
@@ -1180,12 +1474,10 @@
|
|
|
1180
1474
|
* => true
|
|
1181
1475
|
*/
|
|
1182
1476
|
function isPromiseLike(obj) {
|
|
1183
|
-
return obj !== null && (
|
|
1477
|
+
return obj !== null && (_typeof(obj) === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
|
1184
1478
|
}
|
|
1185
1479
|
|
|
1186
|
-
|
|
1187
|
-
const regHMCard = /^[hm]{1}([0-9]{10}|[0-9]{8})$/i;
|
|
1188
|
-
|
|
1480
|
+
var regHMCard = /^[hm]{1}([0-9]{10}|[0-9]{8})$/i;
|
|
1189
1481
|
/**
|
|
1190
1482
|
* 检测值是否为港澳居民来往内地通行证,俗称回乡证或回乡卡。
|
|
1191
1483
|
*
|
|
@@ -1208,17 +1500,15 @@
|
|
|
1208
1500
|
* isHMCard('m32031177') // true
|
|
1209
1501
|
* isHMCard('M32031177') // true
|
|
1210
1502
|
*/
|
|
1503
|
+
|
|
1211
1504
|
function isHMCard(value) {
|
|
1212
|
-
|
|
1505
|
+
var valueStr = normalizeString(value);
|
|
1213
1506
|
return regHMCard.test(valueStr);
|
|
1214
1507
|
}
|
|
1215
1508
|
|
|
1216
|
-
//
|
|
1217
|
-
const regTWCard = /^\d{8}$/i;
|
|
1218
|
-
|
|
1219
|
-
// 一次性短期台胞证
|
|
1220
|
-
const singleRegTWCard = /^[\da-z]{10,12}$/i;
|
|
1509
|
+
var regTWCard = /^\d{8}$/i; // 一次性短期台胞证
|
|
1221
1510
|
|
|
1511
|
+
var singleRegTWCard = /^[\da-z]{10,12}$/i;
|
|
1222
1512
|
/**
|
|
1223
1513
|
* 检测值是否为台湾居民来往大陆通行证,俗称台胞证。
|
|
1224
1514
|
*
|
|
@@ -1238,16 +1528,22 @@
|
|
|
1238
1528
|
* isTWCard('F290299977') // false
|
|
1239
1529
|
* isTWCard('F290299977', { loose: true }) // true
|
|
1240
1530
|
*/
|
|
1241
|
-
|
|
1242
|
-
|
|
1531
|
+
|
|
1532
|
+
function isTWCard(value) {
|
|
1533
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
1534
|
+
_ref$loose = _ref.loose,
|
|
1535
|
+
loose = _ref$loose === void 0 ? false : _ref$loose;
|
|
1536
|
+
|
|
1537
|
+
var valueStr = normalizeString(value);
|
|
1538
|
+
|
|
1243
1539
|
if (regTWCard.test(valueStr)) {
|
|
1244
1540
|
return true;
|
|
1245
1541
|
}
|
|
1542
|
+
|
|
1246
1543
|
return loose ? singleRegTWCard.test(valueStr) : false;
|
|
1247
1544
|
}
|
|
1248
1545
|
|
|
1249
|
-
|
|
1250
|
-
|
|
1546
|
+
var reg$1 = /^[A-Z]{6}[A-Z\d]{2}(?:[A-Z\d]{3})?$/;
|
|
1251
1547
|
/**
|
|
1252
1548
|
* 检测值是否为 Swift Code。8位或11位,前6位为大写字母,7-8位为大写字母或数字,9-11位为可选的大写字母或数字。
|
|
1253
1549
|
*
|
|
@@ -1268,15 +1564,16 @@
|
|
|
1268
1564
|
* isSwiftCode('ICBKCNBJBJM') // true
|
|
1269
1565
|
*
|
|
1270
1566
|
*/
|
|
1567
|
+
|
|
1271
1568
|
function isSwiftCode(value) {
|
|
1272
|
-
|
|
1569
|
+
var valueStr = normalizeString(value);
|
|
1273
1570
|
return reg$1.test(valueStr);
|
|
1274
1571
|
}
|
|
1275
1572
|
|
|
1276
1573
|
// 最大安全数字
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1574
|
+
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; // 最小安全数字
|
|
1575
|
+
|
|
1576
|
+
var MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || -9007199254740991;
|
|
1280
1577
|
|
|
1281
1578
|
/**
|
|
1282
1579
|
* 参考: https://github.com/nefe/number-precision/blob/master/src/index.ts
|
|
@@ -1284,17 +1581,16 @@
|
|
|
1284
1581
|
* 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
|
|
1285
1582
|
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
|
|
1286
1583
|
*/
|
|
1287
|
-
|
|
1288
1584
|
/**
|
|
1289
1585
|
* 是否为科学计数法数字
|
|
1290
1586
|
*
|
|
1291
1587
|
* @param {string} num 检查值
|
|
1292
1588
|
* @returns {boolean}
|
|
1293
1589
|
*/
|
|
1590
|
+
|
|
1294
1591
|
function isScientificNumber(num) {
|
|
1295
1592
|
return /\d+\.?\d*e[\+\-]*\d+/i.test(num);
|
|
1296
1593
|
}
|
|
1297
|
-
|
|
1298
1594
|
/**
|
|
1299
1595
|
* 把错误的数据转正
|
|
1300
1596
|
*
|
|
@@ -1305,61 +1601,63 @@
|
|
|
1305
1601
|
*
|
|
1306
1602
|
* strip(0.09999999999999998)=0.1
|
|
1307
1603
|
*/
|
|
1308
|
-
|
|
1604
|
+
|
|
1605
|
+
function strip(num) {
|
|
1606
|
+
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 12;
|
|
1309
1607
|
return +parseFloat(num.toPrecision(precision));
|
|
1310
1608
|
}
|
|
1311
|
-
|
|
1312
1609
|
/**
|
|
1313
1610
|
* 计算数字的小数点长度,支持科学记数法
|
|
1314
1611
|
*
|
|
1315
1612
|
* @param {number|string} num 输入数
|
|
1316
1613
|
* @returns {number} 小数点长度
|
|
1317
1614
|
*/
|
|
1615
|
+
|
|
1318
1616
|
function digitLength(num) {
|
|
1319
1617
|
// Get digit length of e
|
|
1320
|
-
|
|
1321
|
-
|
|
1618
|
+
var eSplit = num.toString().split(/e/i);
|
|
1619
|
+
var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
|
|
1322
1620
|
return len > 0 ? len : 0;
|
|
1323
1621
|
}
|
|
1324
|
-
|
|
1325
1622
|
/**
|
|
1326
1623
|
* 把小数转成整数,支持科学计数法。如果是小数则放大成整数
|
|
1327
1624
|
*
|
|
1328
1625
|
* @param {number|string} num 输入数
|
|
1329
1626
|
* @returns {number}
|
|
1330
1627
|
*/
|
|
1628
|
+
|
|
1331
1629
|
function float2Fixed(num) {
|
|
1332
|
-
|
|
1333
|
-
|
|
1630
|
+
var strNum = String(num);
|
|
1631
|
+
|
|
1632
|
+
if (!isScientificNumber(strNum)) {
|
|
1633
|
+
return Number(strNum.replace('.', ''));
|
|
1334
1634
|
}
|
|
1335
|
-
|
|
1635
|
+
|
|
1636
|
+
var dLen = digitLength(num);
|
|
1336
1637
|
return dLen > 0 ? strip(+num * Math.pow(10, dLen)) : +num;
|
|
1337
1638
|
}
|
|
1338
|
-
|
|
1339
1639
|
/**
|
|
1340
1640
|
* 检测数字是否越界,如果越界给出提示
|
|
1341
1641
|
* @param {number} num 输入数
|
|
1342
1642
|
* @returns
|
|
1343
1643
|
*/
|
|
1644
|
+
|
|
1344
1645
|
function checkBoundary(num) {
|
|
1345
1646
|
if (+num > MAX_SAFE_INTEGER || +num < MIN_SAFE_INTEGER) {
|
|
1346
|
-
|
|
1347
|
-
console.warn(`${num} is beyond boundary when transfer to integer, the results may not be accurate`);
|
|
1348
|
-
}
|
|
1647
|
+
devWarn("".concat(num, " is beyond boundary when transfer to integer, the results may not be accurate"));
|
|
1349
1648
|
}
|
|
1350
1649
|
}
|
|
1351
|
-
|
|
1352
1650
|
/**
|
|
1353
1651
|
* 去掉左边数字0
|
|
1354
1652
|
*
|
|
1355
1653
|
* @param {string} num 数字字符串
|
|
1356
1654
|
* @returns {string}
|
|
1357
1655
|
*/
|
|
1358
|
-
function trimLeftZero(num) {
|
|
1359
|
-
const reg = /^([+-])?(0+)([0-9\.]+)$/;
|
|
1360
|
-
const result = reg.exec(num);
|
|
1361
1656
|
|
|
1362
|
-
|
|
1657
|
+
function trimLeftZero(num) {
|
|
1658
|
+
var reg = /^([+-])?(0+)([0-9\.]+)$/;
|
|
1659
|
+
var result = reg.exec(num);
|
|
1660
|
+
var sign;
|
|
1363
1661
|
|
|
1364
1662
|
if (result) {
|
|
1365
1663
|
sign = result[1] || '';
|
|
@@ -1368,7 +1666,6 @@
|
|
|
1368
1666
|
|
|
1369
1667
|
return num;
|
|
1370
1668
|
}
|
|
1371
|
-
|
|
1372
1669
|
/**
|
|
1373
1670
|
* 科学计数法转换成普通数字
|
|
1374
1671
|
*
|
|
@@ -1377,37 +1674,64 @@
|
|
|
1377
1674
|
* 2.小数点前边是0,小数点后十分位(包含十分位)之后连续零的个数大于等于6个
|
|
1378
1675
|
*
|
|
1379
1676
|
* @param {string | number} num 科学计数法数字
|
|
1380
|
-
* @returns {string} 转换后的数字字符串
|
|
1677
|
+
* @returns {string | number} 转换后的数字字符串
|
|
1381
1678
|
*/
|
|
1679
|
+
|
|
1382
1680
|
function scientificToNumber(num) {
|
|
1383
|
-
|
|
1384
|
-
const zero = '0';
|
|
1385
|
-
const parts = String(num).toLowerCase().split('e');
|
|
1386
|
-
const e = parts.pop(); // 存储指数
|
|
1387
|
-
// @ts-ignore
|
|
1388
|
-
const l = Math.abs(e); // 取绝对值,l-1就是0的个数
|
|
1389
|
-
// @ts-ignore
|
|
1390
|
-
const sign = e / l; //判断正负
|
|
1391
|
-
const coeff_array = parts[0].split('.'); // 将系数按照小数点拆分
|
|
1681
|
+
var strNum = String(num);
|
|
1392
1682
|
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
} else {
|
|
1398
|
-
const dec = coeff_array[1];
|
|
1683
|
+
if (!isScientificNumber(strNum)) {
|
|
1684
|
+
return num;
|
|
1685
|
+
}
|
|
1686
|
+
/** @type string */
|
|
1399
1687
|
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1688
|
+
|
|
1689
|
+
var ret;
|
|
1690
|
+
var zero = '0';
|
|
1691
|
+
var parts = strNum.toLowerCase().split('e');
|
|
1692
|
+
var e = parts.pop(); // 存储指数
|
|
1693
|
+
// @ts-ignore
|
|
1694
|
+
|
|
1695
|
+
var l = Math.abs(e); // 取绝对值,l-1就是0的个数
|
|
1696
|
+
// @ts-ignore
|
|
1697
|
+
|
|
1698
|
+
var sign = e / l; //判断正负
|
|
1699
|
+
|
|
1700
|
+
var coeff_array = parts[0].split('.'); // 将系数按照小数点拆分
|
|
1701
|
+
// 如果是小数
|
|
1702
|
+
|
|
1703
|
+
if (sign === -1) {
|
|
1704
|
+
// 整数部分
|
|
1705
|
+
var intVal = trimLeftZero(coeff_array[0]); // 整数部分大于科学计数后面部分
|
|
1706
|
+
// 如: 10e-1, 10.2e-1
|
|
1707
|
+
|
|
1708
|
+
if (intVal.length > l) {
|
|
1709
|
+
var thanLen = intVal.length - l;
|
|
1710
|
+
var dec = coeff_array[1] || '';
|
|
1711
|
+
ret = intVal.slice(0, thanLen); // 处理 10e-1, 100e-1
|
|
1712
|
+
|
|
1713
|
+
if (intVal.slice(thanLen) !== '0' || dec) {
|
|
1714
|
+
ret += '.' + intVal.slice(thanLen) + dec;
|
|
1406
1715
|
}
|
|
1716
|
+
} else {
|
|
1717
|
+
// 整数部分小于等于科学计数后面部分
|
|
1718
|
+
// 如: 1e-1, 0.2e-1, 1.2e-2, 1.2e-1
|
|
1719
|
+
ret = zero + '.' + new Array(l - intVal.length + 1).join(zero) + coeff_array.join('');
|
|
1720
|
+
}
|
|
1721
|
+
} else {
|
|
1722
|
+
// 小数部分
|
|
1723
|
+
var _dec = coeff_array[1] || ''; // 如果是整数,将整数除第一位之外的非零数字计入位数,相应的减少0的个数
|
|
1724
|
+
|
|
1725
|
+
|
|
1726
|
+
if (l - _dec.length < 0) {
|
|
1727
|
+
ret = trimLeftZero(coeff_array[0] + _dec.substring(0, l)) + '.' + _dec.substring(l);
|
|
1728
|
+
} else {
|
|
1729
|
+
// 拼接字符串,如果是整数,不需要拼接小数点
|
|
1730
|
+
ret = coeff_array.join('') + new Array(l - _dec.length + 1).join(zero);
|
|
1407
1731
|
}
|
|
1408
1732
|
}
|
|
1409
|
-
|
|
1410
|
-
return
|
|
1733
|
+
|
|
1734
|
+
return trimLeftZero(ret);
|
|
1411
1735
|
}
|
|
1412
1736
|
|
|
1413
1737
|
/**
|
|
@@ -1426,12 +1750,12 @@
|
|
|
1426
1750
|
* isNaN(1)
|
|
1427
1751
|
* // => false
|
|
1428
1752
|
*/
|
|
1753
|
+
|
|
1429
1754
|
function _isNaN(value) {
|
|
1430
1755
|
return isType(value, 'Number') && isNaN(value);
|
|
1431
1756
|
}
|
|
1432
1757
|
|
|
1433
|
-
|
|
1434
|
-
|
|
1758
|
+
var reg = /^[+-]?\d*\.?\d*$/;
|
|
1435
1759
|
/**
|
|
1436
1760
|
* 检查数字或数字字符串
|
|
1437
1761
|
*
|
|
@@ -1439,24 +1763,21 @@
|
|
|
1439
1763
|
* @param {string} num
|
|
1440
1764
|
* @returns 是否为数字
|
|
1441
1765
|
*/
|
|
1442
|
-
function checkNumber(num) {
|
|
1443
|
-
if (!(reg.test(num) || isScientificNumber(num)) || _isNaN(num) || (typeof num !== 'number' && typeof num !== 'string') || num === '') {
|
|
1444
|
-
if (!config.disableWarning) {
|
|
1445
|
-
console.warn(`${num} invalid parameter.`);
|
|
1446
|
-
}
|
|
1447
1766
|
|
|
1767
|
+
function checkNumber(num) {
|
|
1768
|
+
if (!(reg.test(num) || isScientificNumber(num)) || _isNaN(num) || typeof num !== 'number' && typeof num !== 'string' || num === '') {
|
|
1769
|
+
devWarn("".concat(num, " invalid parameter."));
|
|
1448
1770
|
return false;
|
|
1449
|
-
}
|
|
1450
|
-
|
|
1451
|
-
// 数字超限如果不是是字符串,可能有异常
|
|
1771
|
+
} // 数字超限如果不是是字符串,可能有异常
|
|
1452
1772
|
// 如 1111111111111111111111 // => 1.1111111111111111e+21
|
|
1773
|
+
|
|
1774
|
+
|
|
1453
1775
|
if (typeof num === 'number') {
|
|
1454
1776
|
checkBoundary(num);
|
|
1455
1777
|
}
|
|
1456
1778
|
|
|
1457
1779
|
return true;
|
|
1458
1780
|
}
|
|
1459
|
-
|
|
1460
1781
|
/**
|
|
1461
1782
|
* 格式化整数部分
|
|
1462
1783
|
*
|
|
@@ -1465,20 +1786,22 @@
|
|
|
1465
1786
|
* @param {string} thousand 千分位符号
|
|
1466
1787
|
* @returns 格式化后的值
|
|
1467
1788
|
*/
|
|
1789
|
+
|
|
1790
|
+
|
|
1468
1791
|
function formatInt(intStr, thousand) {
|
|
1469
|
-
|
|
1792
|
+
var txt = '';
|
|
1470
1793
|
intStr = trimLeftZero(intStr);
|
|
1471
1794
|
intStr = intStr[0] === '+' ? intStr.substring(1) : intStr; // 去掉+符号
|
|
1472
|
-
const negativeSymbol = Number(intStr) < 0 ? '-' : '';
|
|
1473
|
-
const reArr = negativeSymbol ? intStr.substring(1).split('').reverse() : intStr.split('').reverse();
|
|
1474
1795
|
|
|
1475
|
-
|
|
1796
|
+
var negativeSymbol = Number(intStr) < 0 ? '-' : '';
|
|
1797
|
+
var reArr = negativeSymbol ? intStr.substring(1).split('').reverse() : intStr.split('').reverse();
|
|
1798
|
+
|
|
1799
|
+
for (var i = 0; i < reArr.length; i++) {
|
|
1476
1800
|
txt += reArr[i] + ((i + 1) % 3 === 0 && i + 1 !== reArr.length ? thousand : '');
|
|
1477
1801
|
}
|
|
1478
1802
|
|
|
1479
1803
|
return negativeSymbol + txt.split('').reverse().join('');
|
|
1480
1804
|
}
|
|
1481
|
-
|
|
1482
1805
|
/**
|
|
1483
1806
|
* 格式化小数部分,如果使用 toFixed,超大额数字会自动被截断
|
|
1484
1807
|
*
|
|
@@ -1488,16 +1811,18 @@
|
|
|
1488
1811
|
* @param {string} decimal 小数点符号
|
|
1489
1812
|
* @returns 格式化后的值
|
|
1490
1813
|
*/
|
|
1814
|
+
|
|
1815
|
+
|
|
1491
1816
|
function formatDec(decStr, precision, decimal) {
|
|
1492
1817
|
if (precision === 0) {
|
|
1493
1818
|
return '';
|
|
1494
1819
|
}
|
|
1495
1820
|
|
|
1496
|
-
|
|
1497
|
-
|
|
1821
|
+
var zero = 0;
|
|
1822
|
+
var ret = '';
|
|
1498
1823
|
|
|
1499
1824
|
if (decStr && Number(decStr) > 0) {
|
|
1500
|
-
|
|
1825
|
+
var tmpNum = parseFloat('0.' + decStr);
|
|
1501
1826
|
ret = tmpNum.toFixed(precision).substring(2);
|
|
1502
1827
|
} else {
|
|
1503
1828
|
ret = zero.toFixed(precision).substring(2);
|
|
@@ -1505,7 +1830,6 @@
|
|
|
1505
1830
|
|
|
1506
1831
|
return decimal + ret;
|
|
1507
1832
|
}
|
|
1508
|
-
|
|
1509
1833
|
/**
|
|
1510
1834
|
* 格式化金额
|
|
1511
1835
|
*
|
|
@@ -1549,27 +1873,41 @@
|
|
|
1549
1873
|
* formatMoney(1000.00, { decimal: '&' });
|
|
1550
1874
|
* // => 1,000&00
|
|
1551
1875
|
*/
|
|
1552
|
-
|
|
1876
|
+
|
|
1877
|
+
|
|
1878
|
+
var formatMoney = function formatMoney(num) {
|
|
1879
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
1880
|
+
_ref$precision = _ref.precision,
|
|
1881
|
+
precision = _ref$precision === void 0 ? 2 : _ref$precision,
|
|
1882
|
+
symbol = _ref.symbol,
|
|
1883
|
+
_ref$thousand = _ref.thousand,
|
|
1884
|
+
thousand = _ref$thousand === void 0 ? ',' : _ref$thousand,
|
|
1885
|
+
_ref$decimal = _ref.decimal,
|
|
1886
|
+
decimal = _ref$decimal === void 0 ? '.' : _ref$decimal;
|
|
1887
|
+
|
|
1553
1888
|
// 数字参数不正确,返回空字符串
|
|
1554
1889
|
// @ts-ignore
|
|
1555
1890
|
if (!checkNumber(num)) {
|
|
1556
1891
|
return '';
|
|
1557
|
-
}
|
|
1892
|
+
} // 参数规整化
|
|
1893
|
+
|
|
1558
1894
|
|
|
1559
|
-
// 参数规整化
|
|
1560
1895
|
if (typeof precision !== 'number' || _isNaN(precision) || precision < 0) {
|
|
1561
1896
|
precision = 2;
|
|
1562
1897
|
} else if (precision > 10) {
|
|
1563
1898
|
precision = 10;
|
|
1564
1899
|
}
|
|
1900
|
+
|
|
1565
1901
|
symbol = typeof symbol === 'string' ? symbol : '';
|
|
1566
1902
|
thousand = typeof thousand === 'string' ? thousand : ',';
|
|
1567
|
-
decimal = typeof decimal === 'string' ? decimal : '.';
|
|
1903
|
+
decimal = typeof decimal === 'string' ? decimal : '.'; // 转换数字字符串,支持科学记数法
|
|
1904
|
+
|
|
1905
|
+
var strNum = scientificToNumber(num) + ''; // 整数和小数部分
|
|
1568
1906
|
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1907
|
+
var _strNum$split = strNum.split('.'),
|
|
1908
|
+
_strNum$split2 = _slicedToArray(_strNum$split, 2),
|
|
1909
|
+
intStr = _strNum$split2[0],
|
|
1910
|
+
decStr = _strNum$split2[1];
|
|
1573
1911
|
|
|
1574
1912
|
return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
|
|
1575
1913
|
};
|
|
@@ -1604,13 +1942,20 @@
|
|
|
1604
1942
|
* // => 6228-4804-0256-4890
|
|
1605
1943
|
*
|
|
1606
1944
|
*/
|
|
1607
|
-
function formatBankCard(bankCardNo = '', { char = ' ', length = 4 } = {}) {
|
|
1608
|
-
const reg = new RegExp(`(.{${length}})`, 'g');
|
|
1609
|
-
const regChar = new RegExp(`${char}`, 'g');
|
|
1610
1945
|
|
|
1611
|
-
|
|
1612
|
-
|
|
1946
|
+
function formatBankCard() {
|
|
1947
|
+
var bankCardNo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
1613
1948
|
|
|
1949
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
1950
|
+
_ref$char = _ref["char"],
|
|
1951
|
+
_char = _ref$char === void 0 ? ' ' : _ref$char,
|
|
1952
|
+
_ref$length = _ref.length,
|
|
1953
|
+
length = _ref$length === void 0 ? 4 : _ref$length;
|
|
1954
|
+
|
|
1955
|
+
var reg = new RegExp("(.{".concat(length, "})"), 'g');
|
|
1956
|
+
var regChar = new RegExp("".concat(_char), 'g');
|
|
1957
|
+
var realValue = normalizeString(bankCardNo).replace(regChar, '');
|
|
1958
|
+
var str = realValue.replace(reg, "$1".concat(_char));
|
|
1614
1959
|
return realValue.length % length === 0 ? str.substring(0, str.length - 1) : str;
|
|
1615
1960
|
}
|
|
1616
1961
|
|
|
@@ -1660,63 +2005,69 @@
|
|
|
1660
2005
|
* // => 林**
|
|
1661
2006
|
*
|
|
1662
2007
|
*/
|
|
1663
|
-
function replaceChar(str = '', { start = 3, end = -4, char = '*', repeat, exclude } = {}) {
|
|
1664
|
-
const strLen = str.length;
|
|
1665
2008
|
|
|
1666
|
-
|
|
2009
|
+
function replaceChar(str) {
|
|
2010
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
2011
|
+
_ref$start = _ref.start,
|
|
2012
|
+
start = _ref$start === void 0 ? 3 : _ref$start,
|
|
2013
|
+
_ref$end = _ref.end,
|
|
2014
|
+
end = _ref$end === void 0 ? -4 : _ref$end,
|
|
2015
|
+
_ref$char = _ref["char"],
|
|
2016
|
+
_char = _ref$char === void 0 ? '*' : _ref$char,
|
|
2017
|
+
repeat = _ref.repeat,
|
|
2018
|
+
exclude = _ref.exclude;
|
|
2019
|
+
|
|
2020
|
+
var realStr = normalizeString(str);
|
|
2021
|
+
var strLen = realStr.length; // 开始位置超过str长度
|
|
2022
|
+
|
|
1667
2023
|
if (Math.abs(start) >= strLen) {
|
|
1668
|
-
return
|
|
2024
|
+
return realStr;
|
|
1669
2025
|
}
|
|
1670
2026
|
|
|
1671
2027
|
start = start >= 0 ? start : strLen + start;
|
|
1672
|
-
end = end >= 0 ? end : strLen + end;
|
|
2028
|
+
end = end >= 0 ? end : strLen + end; // 开始位置大于结束位置
|
|
1673
2029
|
|
|
1674
|
-
// 开始位置大于结束位置
|
|
1675
2030
|
if (start >= end) {
|
|
1676
|
-
return
|
|
2031
|
+
return realStr;
|
|
1677
2032
|
}
|
|
1678
2033
|
|
|
1679
|
-
|
|
2034
|
+
var middleStr = realStr.substring(start, end);
|
|
1680
2035
|
|
|
1681
2036
|
if (exclude) {
|
|
1682
|
-
|
|
1683
|
-
middleStr = middleStr.replace(reg,
|
|
2037
|
+
var reg = new RegExp("[^".concat(exclude, "]"), 'g');
|
|
2038
|
+
middleStr = middleStr.replace(reg, _char);
|
|
1684
2039
|
} else {
|
|
1685
2040
|
repeat = typeof repeat === 'number' && repeat >= 0 ? repeat : middleStr.length;
|
|
1686
|
-
middleStr =
|
|
2041
|
+
middleStr = _char.repeat(repeat);
|
|
1687
2042
|
}
|
|
1688
2043
|
|
|
1689
|
-
return
|
|
2044
|
+
return realStr.substring(0, start) + middleStr + realStr.substring(end);
|
|
1690
2045
|
}
|
|
1691
2046
|
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
const chnUnitChar = ['', '十', '百', '千'];
|
|
2047
|
+
var chnNumberChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
|
|
2048
|
+
var chnUnitChar = ['', '十', '百', '千']; // 繁体
|
|
1695
2049
|
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
const big5UnitChar = ['', '拾', '佰', '仟'];
|
|
1699
|
-
|
|
1700
|
-
// 数字字符、计数单位
|
|
2050
|
+
var big5NumberChar = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
|
|
2051
|
+
var big5UnitChar = ['', '拾', '佰', '仟']; // 数字字符、计数单位
|
|
1701
2052
|
|
|
1702
2053
|
/**
|
|
1703
2054
|
* @type {string[]}
|
|
1704
2055
|
* @private
|
|
1705
2056
|
*/
|
|
1706
|
-
let numberChar;
|
|
1707
2057
|
|
|
2058
|
+
var numberChar;
|
|
1708
2059
|
/**
|
|
1709
2060
|
* @type {string[]}
|
|
1710
2061
|
* @private
|
|
1711
2062
|
*/
|
|
1712
|
-
let unitChar;
|
|
1713
2063
|
|
|
2064
|
+
var unitChar;
|
|
1714
2065
|
/**
|
|
1715
2066
|
* @type {string[]}
|
|
1716
2067
|
* @private
|
|
1717
2068
|
*/
|
|
1718
|
-
let unitSection;
|
|
1719
2069
|
|
|
2070
|
+
var unitSection;
|
|
1720
2071
|
/**
|
|
1721
2072
|
* 每个小节的内部进行转化
|
|
1722
2073
|
*
|
|
@@ -1724,17 +2075,18 @@
|
|
|
1724
2075
|
* @param {number} section 数字
|
|
1725
2076
|
* @returns {string} 转化的数字
|
|
1726
2077
|
*/
|
|
2078
|
+
|
|
1727
2079
|
function sectionToChinese(section) {
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
2080
|
+
var str = '';
|
|
2081
|
+
var chnstr = '';
|
|
2082
|
+
var zero = false; //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
|
|
2083
|
+
|
|
2084
|
+
var unitPos = 0;
|
|
1732
2085
|
|
|
1733
2086
|
while (section > 0) {
|
|
1734
2087
|
// 对数字取余10,得到的数即为个位数
|
|
1735
|
-
|
|
2088
|
+
var v = section % 10; //如果数字为零,则对字符串进行补零
|
|
1736
2089
|
|
|
1737
|
-
//如果数字为零,则对字符串进行补零
|
|
1738
2090
|
if (v == 0) {
|
|
1739
2091
|
if (zero) {
|
|
1740
2092
|
//如果遇到连续多次取余都是0,那么只需补一个零即可
|
|
@@ -1748,12 +2100,13 @@
|
|
|
1748
2100
|
str += unitChar[unitPos];
|
|
1749
2101
|
chnstr = str + chnstr;
|
|
1750
2102
|
}
|
|
2103
|
+
|
|
1751
2104
|
unitPos++;
|
|
1752
2105
|
section = Math.floor(section / 10);
|
|
1753
2106
|
}
|
|
2107
|
+
|
|
1754
2108
|
return chnstr;
|
|
1755
2109
|
}
|
|
1756
|
-
|
|
1757
2110
|
/**
|
|
1758
2111
|
* 转换整数
|
|
1759
2112
|
*
|
|
@@ -1761,52 +2114,56 @@
|
|
|
1761
2114
|
* @param {number} num 要转换的数字
|
|
1762
2115
|
* @returns {string} 中文数字
|
|
1763
2116
|
*/
|
|
1764
|
-
function convertInteger(num) {
|
|
1765
|
-
num = Math.floor(num);
|
|
1766
2117
|
|
|
1767
|
-
let unitPos = 0;
|
|
1768
|
-
let strIns = '',
|
|
1769
|
-
chnStr = '';
|
|
1770
|
-
let needZero = false;
|
|
1771
2118
|
|
|
1772
|
-
|
|
2119
|
+
function convertInteger(num) {
|
|
2120
|
+
var numInt = Math.floor(num);
|
|
2121
|
+
var unitPos = 0;
|
|
2122
|
+
var strIns = '';
|
|
2123
|
+
var chnStr = '';
|
|
2124
|
+
var needZero = false;
|
|
2125
|
+
|
|
2126
|
+
if (numInt === 0) {
|
|
1773
2127
|
return numberChar[0];
|
|
1774
2128
|
}
|
|
1775
|
-
|
|
1776
|
-
|
|
2129
|
+
|
|
2130
|
+
while (numInt > 0) {
|
|
2131
|
+
var section = numInt % 10000;
|
|
2132
|
+
|
|
1777
2133
|
if (needZero) {
|
|
1778
2134
|
chnStr = numberChar[0] + chnStr;
|
|
1779
2135
|
}
|
|
2136
|
+
|
|
1780
2137
|
strIns = sectionToChinese(section);
|
|
1781
2138
|
strIns += section !== 0 ? unitSection[unitPos] : unitSection[0];
|
|
1782
2139
|
chnStr = strIns + chnStr;
|
|
1783
2140
|
needZero = section < 1000 && section > 0;
|
|
1784
|
-
|
|
2141
|
+
numInt = Math.floor(numInt / 10000);
|
|
1785
2142
|
unitPos++;
|
|
1786
2143
|
}
|
|
2144
|
+
|
|
1787
2145
|
return chnStr;
|
|
1788
2146
|
}
|
|
1789
|
-
|
|
1790
2147
|
/**
|
|
1791
2148
|
* 转换小数
|
|
1792
2149
|
*
|
|
1793
2150
|
* @private
|
|
1794
2151
|
* @param {number} num 要转换的数字
|
|
1795
2152
|
*/
|
|
1796
|
-
function convertDecimal(num) {
|
|
1797
|
-
const numStr = num + '';
|
|
1798
|
-
const index = numStr.indexOf('.');
|
|
1799
2153
|
|
|
1800
|
-
|
|
2154
|
+
|
|
2155
|
+
function convertDecimal(num) {
|
|
2156
|
+
var strNum = num + '';
|
|
2157
|
+
var index = strNum.indexOf('.');
|
|
2158
|
+
var ret = '';
|
|
1801
2159
|
|
|
1802
2160
|
if (index > -1) {
|
|
1803
|
-
|
|
2161
|
+
var decimalStr = strNum.slice(index + 1);
|
|
1804
2162
|
ret = mapNumberChar(parseInt(decimalStr));
|
|
1805
2163
|
}
|
|
1806
2164
|
|
|
1807
2165
|
return ret;
|
|
1808
2166
|
}
|
|
1809
|
-
|
|
1810
2167
|
/**
|
|
1811
2168
|
* 映射为中文数字
|
|
1812
2169
|
*
|
|
@@ -1814,17 +2171,18 @@
|
|
|
1814
2171
|
* @param {number} num 要处理的数字
|
|
1815
2172
|
* @returns {string} 返回中文数字的映射
|
|
1816
2173
|
*/
|
|
2174
|
+
|
|
2175
|
+
|
|
1817
2176
|
function mapNumberChar(num) {
|
|
1818
|
-
|
|
1819
|
-
|
|
2177
|
+
var strNum = num + '';
|
|
2178
|
+
var ret = '';
|
|
1820
2179
|
|
|
1821
|
-
for (
|
|
1822
|
-
ret += numberChar[parseInt(
|
|
2180
|
+
for (var i = 0, len = strNum.length; i < len; i++) {
|
|
2181
|
+
ret += numberChar[parseInt(strNum[i])];
|
|
1823
2182
|
}
|
|
1824
2183
|
|
|
1825
2184
|
return ret;
|
|
1826
2185
|
}
|
|
1827
|
-
|
|
1828
2186
|
/**
|
|
1829
2187
|
* 数字转中文数字
|
|
1830
2188
|
* 不在安全数字 -9007199254740991~9007199254740991 内,处理会有异常
|
|
@@ -1870,68 +2228,65 @@
|
|
|
1870
2228
|
* // => 一九九〇
|
|
1871
2229
|
*
|
|
1872
2230
|
*/
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
2231
|
+
|
|
2232
|
+
|
|
2233
|
+
function numberToChinese(num) {
|
|
2234
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
2235
|
+
_ref$big = _ref.big5,
|
|
2236
|
+
big5 = _ref$big === void 0 ? false : _ref$big,
|
|
2237
|
+
_ref$unit = _ref.unit,
|
|
2238
|
+
unit = _ref$unit === void 0 ? true : _ref$unit,
|
|
2239
|
+
_ref$decimal = _ref.decimal,
|
|
2240
|
+
decimal = _ref$decimal === void 0 ? '点' : _ref$decimal,
|
|
2241
|
+
_ref$zero = _ref.zero,
|
|
2242
|
+
zero = _ref$zero === void 0 ? '' : _ref$zero,
|
|
2243
|
+
_ref$negative = _ref.negative,
|
|
2244
|
+
negative = _ref$negative === void 0 ? '负' : _ref$negative,
|
|
2245
|
+
_ref$unitConfig = _ref.unitConfig,
|
|
2246
|
+
unitConfig = _ref$unitConfig === void 0 ? {} : _ref$unitConfig;
|
|
2247
|
+
|
|
1887
2248
|
// 非数字 或 NaN 不处理
|
|
1888
2249
|
if (typeof num !== 'number' || isNaN(num)) {
|
|
1889
|
-
|
|
1890
|
-
console.warn(`参数错误 ${num},请传入数字`);
|
|
1891
|
-
}
|
|
1892
|
-
|
|
2250
|
+
devWarn("\u53C2\u6570\u9519\u8BEF ".concat(num, "\uFF0C\u8BF7\u4F20\u5165\u6570\u5B57"));
|
|
1893
2251
|
return '';
|
|
1894
|
-
}
|
|
2252
|
+
} // 超过安全数字提示
|
|
2253
|
+
|
|
1895
2254
|
|
|
1896
|
-
//
|
|
1897
|
-
checkBoundary(num);
|
|
2255
|
+
checkBoundary(num); // 设置数字字符和计数单位
|
|
1898
2256
|
|
|
1899
|
-
// 设置数字字符和计数单位
|
|
1900
2257
|
if (big5) {
|
|
1901
2258
|
numberChar = big5NumberChar.slice();
|
|
1902
2259
|
unitChar = big5UnitChar.slice();
|
|
1903
2260
|
} else {
|
|
1904
2261
|
numberChar = chnNumberChar.slice();
|
|
1905
2262
|
unitChar = chnUnitChar.slice();
|
|
1906
|
-
}
|
|
2263
|
+
} // 设置节点计数单位,万、亿、万亿
|
|
1907
2264
|
|
|
1908
|
-
// 设置节点计数单位,万、亿、万亿
|
|
1909
|
-
const defaultUnitWan = '万';
|
|
1910
|
-
const defaultUnitYi = '亿';
|
|
1911
|
-
unitSection = ['', unitConfig.w || defaultUnitWan, unitConfig.y || defaultUnitYi, unitConfig.w && unitConfig.y ? unitConfig.w + unitConfig.y : defaultUnitWan + defaultUnitYi];
|
|
1912
2265
|
|
|
1913
|
-
|
|
2266
|
+
var unitWan = (unitConfig === null || unitConfig === void 0 ? void 0 : unitConfig.w) || '万';
|
|
2267
|
+
var unitYi = (unitConfig === null || unitConfig === void 0 ? void 0 : unitConfig.y) || '亿';
|
|
2268
|
+
var unitWanYi = unitWan + unitYi;
|
|
2269
|
+
unitSection = ['', unitWan, unitYi, unitWanYi]; // 设置0
|
|
2270
|
+
|
|
1914
2271
|
if (zero) {
|
|
1915
2272
|
numberChar[0] = zero;
|
|
1916
|
-
}
|
|
2273
|
+
} // 前置字符,负数处理
|
|
1917
2274
|
|
|
1918
|
-
// 前置字符,负数处理
|
|
1919
|
-
const preStr = num < 0 ? negative : '';
|
|
1920
2275
|
|
|
1921
|
-
// 整数和小数
|
|
1922
|
-
|
|
2276
|
+
var preStr = num < 0 ? negative : ''; // 整数和小数
|
|
2277
|
+
|
|
2278
|
+
var chnInteger, chnDecimal;
|
|
2279
|
+
var numAbs = Math.abs(num); // 处理整数
|
|
1923
2280
|
|
|
1924
|
-
// 处理整数
|
|
1925
2281
|
if (unit) {
|
|
1926
|
-
chnInteger = convertInteger(
|
|
2282
|
+
chnInteger = convertInteger(numAbs);
|
|
1927
2283
|
} else {
|
|
1928
|
-
chnInteger = mapNumberChar(Math.floor(
|
|
1929
|
-
}
|
|
2284
|
+
chnInteger = mapNumberChar(Math.floor(numAbs));
|
|
2285
|
+
} // 处理小数
|
|
1930
2286
|
|
|
1931
|
-
// 处理小数
|
|
1932
|
-
chnDecimal = convertDecimal(num);
|
|
1933
2287
|
|
|
1934
|
-
|
|
2288
|
+
chnDecimal = convertDecimal(numAbs);
|
|
2289
|
+
return chnDecimal ? "".concat(preStr).concat(chnInteger).concat(decimal).concat(chnDecimal) : "".concat(preStr).concat(chnInteger);
|
|
1935
2290
|
}
|
|
1936
2291
|
|
|
1937
2292
|
/**
|
|
@@ -1957,74 +2312,36 @@
|
|
|
1957
2312
|
* // => 1 GB
|
|
1958
2313
|
*/
|
|
1959
2314
|
function bytesToSize(bytes) {
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
// 存储单位
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
return sizes[i] ?
|
|
1967
|
-
}
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
['36', '江西省'],
|
|
1991
|
-
['37', '山东省'],
|
|
1992
|
-
|
|
1993
|
-
// 华中地区: 河南省|410000,湖北省|420000,湖南省|430000
|
|
1994
|
-
['41', '河南省'],
|
|
1995
|
-
['42', '湖北省'],
|
|
1996
|
-
['43', '湖南省'],
|
|
1997
|
-
|
|
1998
|
-
// 华南地区:广东省|440000,广西壮族自治区|450000,海南省|460000
|
|
1999
|
-
['44', '广东省'],
|
|
2000
|
-
['45', '广西壮族自治区'],
|
|
2001
|
-
['46', '海南省'],
|
|
2002
|
-
|
|
2003
|
-
// 西南地区:重庆市|500000,四川省|510000,贵州省|520000,云南省|530000,西藏自治区|540000
|
|
2004
|
-
['50', '重庆市'],
|
|
2005
|
-
['51', '四川省'],
|
|
2006
|
-
['52', '贵州省'],
|
|
2007
|
-
['53', '云南省'],
|
|
2008
|
-
['54', '西藏自治区'],
|
|
2009
|
-
|
|
2010
|
-
// 西北地区: 陕西省|610000,甘肃省|620000,青海省|630000,宁夏回族自治区|640000,新疆维吾尔自治区|650000
|
|
2011
|
-
['61', '陕西省'],
|
|
2012
|
-
['62', '甘肃省'],
|
|
2013
|
-
['63', '青海省'],
|
|
2014
|
-
['64', '宁夏回族自治区'],
|
|
2015
|
-
['65', '新疆维吾尔自治区'],
|
|
2016
|
-
|
|
2017
|
-
// 台湾地区:台湾省|710000
|
|
2018
|
-
// 台湾居民公民身份号码地址码为830000,参考 http://www.wanweibaike.com/wiki-中华人民共和国行政区划代码_(7区)、https://zh.wikipedia.org/wiki/港澳台居民居住证
|
|
2019
|
-
['71', '台湾省'],
|
|
2020
|
-
['83', '台湾省'],
|
|
2021
|
-
|
|
2022
|
-
// 港澳地区:香港特别行政区|810000,澳门特别行政区|820000
|
|
2023
|
-
['81', '香港特别行政区'],
|
|
2024
|
-
['82', '澳门特别行政区']
|
|
2025
|
-
];
|
|
2026
|
-
|
|
2027
|
-
// 第一位数字是以前的大区制代码。第二位是大区所在省市编码。全国共分为8个大区:华北(1)、东北(2)、华东(3)、中南(4)、西南(5)、西北(6)、台湾(7)和港澳(8)。
|
|
2315
|
+
var numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
|
|
2316
|
+
if (numBytes === 0 || isNaN(numBytes)) return '0 B';
|
|
2317
|
+
var k = 1024; // 存储单位
|
|
2318
|
+
|
|
2319
|
+
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
2320
|
+
var i = Math.floor(Math.log(numBytes) / Math.log(k));
|
|
2321
|
+
return sizes[i] ? "".concat(Number((numBytes / Math.pow(k, i)).toFixed(2)), " ").concat(sizes[i]) : numBytes + '';
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
var regIdCard = /*#__PURE__*/_wrapRegExp(/^([0-9]{2})([0-9]{2})([0-9]{2})((?:[0-9]{2})?[0-9]{2})([0-9]{2})([0-9]{2})[0-9]{2}([0-9])(?:[0-9]|X)?$/i, {
|
|
2325
|
+
province: 1,
|
|
2326
|
+
city: 2,
|
|
2327
|
+
area: 3,
|
|
2328
|
+
year: 4,
|
|
2329
|
+
month: 5,
|
|
2330
|
+
day: 6,
|
|
2331
|
+
gender: 7
|
|
2332
|
+
});
|
|
2333
|
+
|
|
2334
|
+
var Provinces = [// 华北地区:北京市|110000,天津市|120000,河北省|130000,山西省|140000,内蒙古自治区|150000
|
|
2335
|
+
['11', '北京市'], ['12', '天津市'], ['13', '河北省'], ['14', '山西省'], ['15', '内蒙古自治区'], // 东北地区: 辽宁省|210000,吉林省|220000,黑龙江省|230000
|
|
2336
|
+
['21', '辽宁省'], ['22', '吉林省'], ['23', '黑龙江省'], // 华东地区: 上海市|310000,江苏省|320000,浙江省|330000,安徽省|340000,福建省|350000,江西省|360000,山东省|370000
|
|
2337
|
+
['31', '上海市'], ['32', '江苏省'], ['33', '浙江省'], ['34', '安徽省'], ['35', '福建省'], ['36', '江西省'], ['37', '山东省'], // 华中地区: 河南省|410000,湖北省|420000,湖南省|430000
|
|
2338
|
+
['41', '河南省'], ['42', '湖北省'], ['43', '湖南省'], // 华南地区:广东省|440000,广西壮族自治区|450000,海南省|460000
|
|
2339
|
+
['44', '广东省'], ['45', '广西壮族自治区'], ['46', '海南省'], // 西南地区:重庆市|500000,四川省|510000,贵州省|520000,云南省|530000,西藏自治区|540000
|
|
2340
|
+
['50', '重庆市'], ['51', '四川省'], ['52', '贵州省'], ['53', '云南省'], ['54', '西藏自治区'], // 西北地区: 陕西省|610000,甘肃省|620000,青海省|630000,宁夏回族自治区|640000,新疆维吾尔自治区|650000
|
|
2341
|
+
['61', '陕西省'], ['62', '甘肃省'], ['63', '青海省'], ['64', '宁夏回族自治区'], ['65', '新疆维吾尔自治区'], // 台湾地区:台湾省|710000
|
|
2342
|
+
// 台湾居民公民身份号码地址码为830000,参考 http://www.wanweibaike.com/wiki-中华人民共和国行政区划代码_(7区)、https://zh.wikipedia.org/wiki/港澳台居民居住证
|
|
2343
|
+
['71', '台湾省'], ['83', '台湾省'], // 港澳地区:香港特别行政区|810000,澳门特别行政区|820000
|
|
2344
|
+
['81', '香港特别行政区'], ['82', '澳门特别行政区']]; // 第一位数字是以前的大区制代码。第二位是大区所在省市编码。全国共分为8个大区:华北(1)、东北(2)、华东(3)、中南(4)、西南(5)、西北(6)、台湾(7)和港澳(8)。
|
|
2028
2345
|
// const Regions = [
|
|
2029
2346
|
// ['1', '华北地区'],
|
|
2030
2347
|
// ['2', '东北地区'],
|
|
@@ -2089,62 +2406,52 @@
|
|
|
2089
2406
|
* }
|
|
2090
2407
|
*
|
|
2091
2408
|
*/
|
|
2409
|
+
|
|
2092
2410
|
function parseIdCard(id) {
|
|
2093
|
-
if (!isIdCard(id, {
|
|
2411
|
+
if (!isIdCard(id, {
|
|
2412
|
+
loose: true
|
|
2413
|
+
})) {
|
|
2094
2414
|
return null;
|
|
2095
2415
|
}
|
|
2096
2416
|
|
|
2097
|
-
|
|
2417
|
+
var info = regIdCard.exec(id);
|
|
2098
2418
|
|
|
2099
2419
|
if (!info) {
|
|
2100
2420
|
return null;
|
|
2101
2421
|
}
|
|
2102
|
-
|
|
2103
2422
|
/**
|
|
2104
2423
|
* @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }}
|
|
2105
2424
|
*
|
|
2106
2425
|
*/
|
|
2107
|
-
|
|
2108
|
-
province: '',
|
|
2109
|
-
city: '',
|
|
2110
|
-
area: '',
|
|
2111
|
-
year: '',
|
|
2112
|
-
month: '',
|
|
2113
|
-
day: '',
|
|
2114
|
-
gender: ''
|
|
2115
|
-
};
|
|
2426
|
+
// @ts-ignore
|
|
2116
2427
|
|
|
2117
|
-
if (info.groups) {
|
|
2118
|
-
// @ts-ignore
|
|
2119
|
-
origin = info.groups;
|
|
2120
|
-
} else {
|
|
2121
|
-
origin = {
|
|
2122
|
-
province: info[1],
|
|
2123
|
-
city: info[2],
|
|
2124
|
-
area: info[3],
|
|
2125
|
-
year: info[4],
|
|
2126
|
-
month: info[5],
|
|
2127
|
-
day: info[6],
|
|
2128
|
-
gender: info[7]
|
|
2129
|
-
};
|
|
2130
|
-
}
|
|
2131
2428
|
|
|
2132
|
-
|
|
2429
|
+
var origin = (info === null || info === void 0 ? void 0 : info.groups) || {
|
|
2430
|
+
province: info[1],
|
|
2431
|
+
city: info[2],
|
|
2432
|
+
area: info[3],
|
|
2433
|
+
year: info[4],
|
|
2434
|
+
month: info[5],
|
|
2435
|
+
day: info[6],
|
|
2436
|
+
gender: info[7]
|
|
2437
|
+
};
|
|
2438
|
+
var province = Provinces.find(function (item) {
|
|
2439
|
+
return item[0] === origin.province;
|
|
2440
|
+
});
|
|
2133
2441
|
|
|
2134
2442
|
if (!province) {
|
|
2135
2443
|
return null;
|
|
2136
2444
|
}
|
|
2137
2445
|
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
// const region = Regions.find(item => item[0] === origin.province?.substring(0, 1));
|
|
2446
|
+
var birthday = "".concat(origin.year, "-").concat(origin.month, "-").concat(origin.day);
|
|
2447
|
+
var gender = Number(origin.gender) % 2 === 0 ? '女' : '男'; // const region = Regions.find(item => item[0] === origin.province?.substring(0, 1));
|
|
2141
2448
|
|
|
2142
2449
|
return {
|
|
2143
2450
|
// region,
|
|
2144
2451
|
province: province[1],
|
|
2145
|
-
birthday,
|
|
2146
|
-
gender,
|
|
2147
|
-
origin
|
|
2452
|
+
birthday: birthday,
|
|
2453
|
+
gender: gender,
|
|
2454
|
+
origin: origin
|
|
2148
2455
|
};
|
|
2149
2456
|
}
|
|
2150
2457
|
|
|
@@ -2176,12 +2483,17 @@
|
|
|
2176
2483
|
* });
|
|
2177
2484
|
*/
|
|
2178
2485
|
function blobToDataURL(blob) {
|
|
2179
|
-
return new Promise((resolve, reject)
|
|
2180
|
-
|
|
2181
|
-
reader.readAsDataURL(blob);
|
|
2182
|
-
|
|
2183
|
-
reader.onload = ()
|
|
2184
|
-
|
|
2486
|
+
return new Promise(function (resolve, reject) {
|
|
2487
|
+
var reader = new FileReader();
|
|
2488
|
+
reader.readAsDataURL(blob); // @ts-ignore
|
|
2489
|
+
|
|
2490
|
+
reader.onload = function () {
|
|
2491
|
+
return resolve(reader.result);
|
|
2492
|
+
};
|
|
2493
|
+
|
|
2494
|
+
reader.onerror = function (error) {
|
|
2495
|
+
return reject(error);
|
|
2496
|
+
};
|
|
2185
2497
|
});
|
|
2186
2498
|
}
|
|
2187
2499
|
|
|
@@ -2199,17 +2511,20 @@
|
|
|
2199
2511
|
* dataURLToBlob(dataurl);
|
|
2200
2512
|
*/
|
|
2201
2513
|
function dataURLToBlob(dataurl) {
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2514
|
+
var arr = dataurl.split(','); // @ts-ignore
|
|
2515
|
+
|
|
2516
|
+
var mime = arr[0].match(/:(.*?);/)[1];
|
|
2517
|
+
var bstr = atob(arr[1]);
|
|
2518
|
+
var n = bstr.length;
|
|
2519
|
+
var u8arr = new Uint8Array(n);
|
|
2208
2520
|
|
|
2209
2521
|
while (n--) {
|
|
2210
2522
|
u8arr[n] = bstr.charCodeAt(n);
|
|
2211
2523
|
}
|
|
2212
|
-
|
|
2524
|
+
|
|
2525
|
+
return new Blob([u8arr], {
|
|
2526
|
+
type: mime
|
|
2527
|
+
});
|
|
2213
2528
|
}
|
|
2214
2529
|
|
|
2215
2530
|
/**
|
|
@@ -2234,12 +2549,12 @@
|
|
|
2234
2549
|
* setDataURLPrefix(data, ''); // data:;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
|
|
2235
2550
|
* setDataURLPrefix(data, '', false); // data:,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
|
|
2236
2551
|
*/
|
|
2237
|
-
function setDataURLPrefix(data
|
|
2238
|
-
|
|
2552
|
+
function setDataURLPrefix(data) {
|
|
2553
|
+
var mimetype = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'image/png';
|
|
2554
|
+
var base64 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
2555
|
+
return "data:".concat(mimetype).concat(base64 ? ';base64' : '', ",").concat(data);
|
|
2239
2556
|
}
|
|
2240
2557
|
|
|
2241
|
-
// TODO: 函数重载,类型参照 Date
|
|
2242
|
-
|
|
2243
2558
|
/**
|
|
2244
2559
|
* 创建一个 Date 实例日期对象,同 new Date() 。<br/>
|
|
2245
2560
|
* 规避了苹果设备浏览器不支持部分格式(YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。
|
|
@@ -2260,15 +2575,20 @@
|
|
|
2260
2575
|
* safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
|
|
2261
2576
|
*
|
|
2262
2577
|
*/
|
|
2263
|
-
|
|
2264
|
-
|
|
2578
|
+
|
|
2579
|
+
function safeDate(value) {
|
|
2580
|
+
var safeValue = typeof value === 'string' ? value.replace(/[\\.-]/g, '/') : value;
|
|
2581
|
+
|
|
2582
|
+
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
2583
|
+
args[_key - 1] = arguments[_key];
|
|
2584
|
+
}
|
|
2265
2585
|
|
|
2266
2586
|
if (args && args.length > 0) {
|
|
2267
2587
|
// @ts-ignore
|
|
2268
|
-
return
|
|
2269
|
-
}
|
|
2588
|
+
return _construct(Date, [safeValue].concat(args));
|
|
2589
|
+
} // @ts-ignore
|
|
2590
|
+
|
|
2270
2591
|
|
|
2271
|
-
// @ts-ignore
|
|
2272
2592
|
return isNil(safeValue) ? new Date() : new Date(safeValue);
|
|
2273
2593
|
}
|
|
2274
2594
|
|
|
@@ -2295,16 +2615,23 @@
|
|
|
2295
2615
|
* formatMobile('133456') // '133 456'
|
|
2296
2616
|
* formatMobile('13345678') // '133 4567 8'
|
|
2297
2617
|
*/
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2618
|
+
|
|
2619
|
+
function formatMobile(mobileNo) {
|
|
2620
|
+
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
|
|
2621
|
+
_ref$char = _ref["char"],
|
|
2622
|
+
_char = _ref$char === void 0 ? ' ' : _ref$char;
|
|
2623
|
+
|
|
2624
|
+
var regChar = new RegExp(_char, 'g');
|
|
2625
|
+
var realValue = normalizeString(mobileNo).replace(regChar, '').substring(0, 11);
|
|
2301
2626
|
|
|
2302
2627
|
if (realValue.length > 7) {
|
|
2303
|
-
return realValue.replace(/^(...)(....)/g,
|
|
2628
|
+
return realValue.replace(/^(...)(....)/g, "$1".concat(_char, "$2").concat(_char));
|
|
2304
2629
|
}
|
|
2630
|
+
|
|
2305
2631
|
if (realValue.length > 3) {
|
|
2306
|
-
return realValue.replace(/^(...)/g,
|
|
2632
|
+
return realValue.replace(/^(...)/g, "$1".concat(_char));
|
|
2307
2633
|
}
|
|
2634
|
+
|
|
2308
2635
|
return realValue;
|
|
2309
2636
|
}
|
|
2310
2637
|
|
|
@@ -2330,9 +2657,11 @@
|
|
|
2330
2657
|
* padZero(688, 5); // '00688'
|
|
2331
2658
|
* padZero('688', 5); // '00688'
|
|
2332
2659
|
*/
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2660
|
+
|
|
2661
|
+
function padZero(value) {
|
|
2662
|
+
var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
|
|
2663
|
+
var str = normalizeString(value);
|
|
2664
|
+
var len = str.length;
|
|
2336
2665
|
|
|
2337
2666
|
if (typeof size !== 'number' || size < 0) {
|
|
2338
2667
|
size = 0;
|
|
@@ -2341,6 +2670,7 @@
|
|
|
2341
2670
|
if (len < size) {
|
|
2342
2671
|
return '0'.repeat(size - len) + str;
|
|
2343
2672
|
}
|
|
2673
|
+
|
|
2344
2674
|
return str;
|
|
2345
2675
|
}
|
|
2346
2676
|
|
|
@@ -2369,6 +2699,7 @@
|
|
|
2369
2699
|
* isNumber('1')
|
|
2370
2700
|
* // => false
|
|
2371
2701
|
*/
|
|
2702
|
+
|
|
2372
2703
|
function isNumber(value) {
|
|
2373
2704
|
return isType(value, 'Number');
|
|
2374
2705
|
}
|
|
@@ -2392,25 +2723,31 @@
|
|
|
2392
2723
|
* times(3, 0.6, 2, 10);
|
|
2393
2724
|
* // => 36
|
|
2394
2725
|
*/
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2726
|
+
|
|
2727
|
+
function times() {
|
|
2728
|
+
for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2729
|
+
nums[_key] = arguments[_key];
|
|
2399
2730
|
}
|
|
2400
2731
|
|
|
2401
|
-
|
|
2732
|
+
var num1 = nums[0],
|
|
2733
|
+
num2 = nums[1],
|
|
2734
|
+
rest = nums.slice(2);
|
|
2735
|
+
|
|
2736
|
+
if (rest.length > 0) {
|
|
2737
|
+
return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
|
|
2738
|
+
} // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
|
|
2739
|
+
|
|
2740
|
+
|
|
2402
2741
|
if ((!isNumber(num2) || _isNaN(num2)) && !isString(num2)) {
|
|
2403
2742
|
// @ts-ignore
|
|
2404
2743
|
return num1;
|
|
2405
2744
|
}
|
|
2406
2745
|
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2746
|
+
var num1Changed = float2Fixed(num1);
|
|
2747
|
+
var num2Changed = float2Fixed(num2);
|
|
2748
|
+
var baseNum = digitLength(num1) + digitLength(num2);
|
|
2749
|
+
var leftValue = num1Changed * num2Changed;
|
|
2412
2750
|
checkBoundary(leftValue);
|
|
2413
|
-
|
|
2414
2751
|
return leftValue / Math.pow(10, baseNum);
|
|
2415
2752
|
}
|
|
2416
2753
|
|
|
@@ -2433,20 +2770,27 @@
|
|
|
2433
2770
|
* plus(0.1, 0.2, 0.3, 0.4);
|
|
2434
2771
|
* // => 1
|
|
2435
2772
|
*/
|
|
2436
|
-
function plus(...nums) {
|
|
2437
|
-
const [num1, num2, ...rest] = nums;
|
|
2438
2773
|
|
|
2439
|
-
|
|
2440
|
-
|
|
2774
|
+
function plus() {
|
|
2775
|
+
for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2776
|
+
nums[_key] = arguments[_key];
|
|
2441
2777
|
}
|
|
2442
2778
|
|
|
2443
|
-
|
|
2779
|
+
var num1 = nums[0],
|
|
2780
|
+
num2 = nums[1],
|
|
2781
|
+
rest = nums.slice(2);
|
|
2782
|
+
|
|
2783
|
+
if (rest.length > 0) {
|
|
2784
|
+
return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
|
|
2785
|
+
} // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
|
|
2786
|
+
|
|
2787
|
+
|
|
2444
2788
|
if ((!isNumber(num2) || _isNaN(num2)) && !isString(num2)) {
|
|
2445
2789
|
// @ts-ignore
|
|
2446
2790
|
return num1;
|
|
2447
2791
|
}
|
|
2448
2792
|
|
|
2449
|
-
|
|
2793
|
+
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
|
|
2450
2794
|
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
|
|
2451
2795
|
}
|
|
2452
2796
|
|
|
@@ -2469,20 +2813,27 @@
|
|
|
2469
2813
|
* minus(1, 0.9, 0.02, 0.08);
|
|
2470
2814
|
* // => 0
|
|
2471
2815
|
*/
|
|
2472
|
-
function minus(...nums) {
|
|
2473
|
-
const [num1, num2, ...rest] = nums;
|
|
2474
2816
|
|
|
2475
|
-
|
|
2476
|
-
|
|
2817
|
+
function minus() {
|
|
2818
|
+
for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2819
|
+
nums[_key] = arguments[_key];
|
|
2477
2820
|
}
|
|
2478
2821
|
|
|
2479
|
-
|
|
2822
|
+
var num1 = nums[0],
|
|
2823
|
+
num2 = nums[1],
|
|
2824
|
+
rest = nums.slice(2);
|
|
2825
|
+
|
|
2826
|
+
if (rest.length > 0) {
|
|
2827
|
+
return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
|
|
2828
|
+
} // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
|
|
2829
|
+
|
|
2830
|
+
|
|
2480
2831
|
if ((!isNumber(num2) || _isNaN(num2)) && !isString(num2)) {
|
|
2481
2832
|
// @ts-ignore
|
|
2482
2833
|
return num1;
|
|
2483
2834
|
}
|
|
2484
2835
|
|
|
2485
|
-
|
|
2836
|
+
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
|
|
2486
2837
|
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
|
|
2487
2838
|
}
|
|
2488
2839
|
|
|
@@ -2505,24 +2856,31 @@
|
|
|
2505
2856
|
* divide(1000, 10, 10, 10);
|
|
2506
2857
|
* // => 1
|
|
2507
2858
|
*/
|
|
2508
|
-
function divide(...nums) {
|
|
2509
|
-
const [num1, num2, ...rest] = nums;
|
|
2510
2859
|
|
|
2511
|
-
|
|
2512
|
-
|
|
2860
|
+
function divide() {
|
|
2861
|
+
for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
2862
|
+
nums[_key] = arguments[_key];
|
|
2513
2863
|
}
|
|
2514
2864
|
|
|
2515
|
-
|
|
2865
|
+
var num1 = nums[0],
|
|
2866
|
+
num2 = nums[1],
|
|
2867
|
+
rest = nums.slice(2);
|
|
2868
|
+
|
|
2869
|
+
if (rest.length > 0) {
|
|
2870
|
+
return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
|
|
2871
|
+
} // 兼容处理,如果第2个参数为非数字或字符串时,返回第一个参数
|
|
2872
|
+
|
|
2873
|
+
|
|
2516
2874
|
if ((!isNumber(num2) || _isNaN(num2)) && !isString(num2)) {
|
|
2517
2875
|
// @ts-ignore
|
|
2518
2876
|
return num1;
|
|
2519
2877
|
}
|
|
2520
2878
|
|
|
2521
|
-
|
|
2522
|
-
|
|
2879
|
+
var num1Changed = float2Fixed(num1);
|
|
2880
|
+
var num2Changed = float2Fixed(num2);
|
|
2523
2881
|
checkBoundary(num1Changed);
|
|
2524
|
-
checkBoundary(num2Changed);
|
|
2525
|
-
|
|
2882
|
+
checkBoundary(num2Changed); // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
|
|
2883
|
+
|
|
2526
2884
|
return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
|
|
2527
2885
|
}
|
|
2528
2886
|
|
|
@@ -2546,8 +2904,17 @@
|
|
|
2546
2904
|
* round(4060, -2);
|
|
2547
2905
|
* // => 4100
|
|
2548
2906
|
*/
|
|
2549
|
-
|
|
2550
|
-
|
|
2907
|
+
|
|
2908
|
+
function round(num) {
|
|
2909
|
+
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
2910
|
+
|
|
2911
|
+
// 兼容处理,如果参数为非数字或字符串时,直接返回
|
|
2912
|
+
if ((!isNumber(num) || _isNaN(num)) && !isString(num)) {
|
|
2913
|
+
// @ts-ignore
|
|
2914
|
+
return num;
|
|
2915
|
+
}
|
|
2916
|
+
|
|
2917
|
+
var base = Math.pow(10, precision);
|
|
2551
2918
|
return divide(Math.round(times(num, base)), base);
|
|
2552
2919
|
}
|
|
2553
2920
|
|
|
@@ -2569,14 +2936,13 @@
|
|
|
2569
2936
|
* // do something
|
|
2570
2937
|
* })
|
|
2571
2938
|
*/
|
|
2572
|
-
function waitTime(
|
|
2573
|
-
|
|
2939
|
+
function waitTime() {
|
|
2940
|
+
var time = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1000;
|
|
2941
|
+
return new Promise(function (resolve) {
|
|
2574
2942
|
setTimeout(resolve, time);
|
|
2575
2943
|
});
|
|
2576
2944
|
}
|
|
2577
2945
|
|
|
2578
|
-
// ref: https://github.com/ant-design/ant-design-mobile/blob/v2/components/input-item/index.tsx#L240
|
|
2579
|
-
|
|
2580
2946
|
/**
|
|
2581
2947
|
* 计算输入框的值格式化后光标位置
|
|
2582
2948
|
*
|
|
@@ -2597,22 +2963,28 @@
|
|
|
2597
2963
|
* @param {'mobile'|'bankCard'} [options.type] 格式化类型,内置手机号码和银行卡号特殊处理
|
|
2598
2964
|
* @returns {number} 格式化后的光标位置
|
|
2599
2965
|
*/
|
|
2600
|
-
function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue, { placeholderChar = ' ', maskReg = /\D/g, type } = {}) {
|
|
2601
|
-
const realCtrlValue = normalizeString(prevCtrlValue);
|
|
2602
|
-
const realRawValue = normalizeString(rawValue);
|
|
2603
|
-
const placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
|
|
2604
2966
|
|
|
2605
|
-
|
|
2606
|
-
|
|
2967
|
+
function calculateCursorPosition(prevPos, prevCtrlValue, rawValue, ctrlValue) {
|
|
2968
|
+
var _ref = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {},
|
|
2969
|
+
_ref$placeholderChar = _ref.placeholderChar,
|
|
2970
|
+
placeholderChar = _ref$placeholderChar === void 0 ? ' ' : _ref$placeholderChar,
|
|
2971
|
+
_ref$maskReg = _ref.maskReg,
|
|
2972
|
+
maskReg = _ref$maskReg === void 0 ? /\D/g : _ref$maskReg,
|
|
2973
|
+
type = _ref.type;
|
|
2607
2974
|
|
|
2608
|
-
|
|
2975
|
+
var realCtrlValue = normalizeString(prevCtrlValue);
|
|
2976
|
+
var realRawValue = normalizeString(rawValue);
|
|
2977
|
+
var placeholderChars = Array.isArray(placeholderChar) ? placeholderChar : [placeholderChar];
|
|
2978
|
+
var editLength = realRawValue.length - realCtrlValue.length;
|
|
2979
|
+
var isAddition = editLength > 0;
|
|
2980
|
+
var pos = prevPos;
|
|
2609
2981
|
|
|
2610
2982
|
if (isAddition) {
|
|
2611
|
-
|
|
2612
|
-
|
|
2983
|
+
var additionStr = realRawValue.substring(pos - editLength, pos);
|
|
2984
|
+
var ctrlCharCount = additionStr.replace(maskReg, '').length;
|
|
2613
2985
|
pos -= editLength - ctrlCharCount;
|
|
2986
|
+
var placeholderCharCount = 0;
|
|
2614
2987
|
|
|
2615
|
-
let placeholderCharCount = 0;
|
|
2616
2988
|
while (ctrlCharCount > 0) {
|
|
2617
2989
|
if (placeholderChars.indexOf(ctrlValue.charAt(pos - ctrlCharCount + placeholderCharCount)) !== -1) {
|
|
2618
2990
|
placeholderCharCount++;
|
|
@@ -2620,20 +2992,20 @@
|
|
|
2620
2992
|
ctrlCharCount--;
|
|
2621
2993
|
}
|
|
2622
2994
|
}
|
|
2995
|
+
|
|
2623
2996
|
pos += placeholderCharCount;
|
|
2624
2997
|
}
|
|
2625
2998
|
|
|
2626
|
-
if (
|
|
2999
|
+
if (type === 'mobile' && (pos === 4 || pos === 9) || type === 'bankCard' && pos > 0 && pos % 5 === 0) {
|
|
2627
3000
|
pos -= 1;
|
|
2628
3001
|
}
|
|
2629
3002
|
|
|
2630
3003
|
return pos;
|
|
2631
3004
|
}
|
|
2632
3005
|
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
3006
|
+
var numberChars = '0123456789';
|
|
3007
|
+
var letterChars = 'abcdefghijklmnopqrstuvwxyz';
|
|
3008
|
+
var defaultChars = numberChars + letterChars + letterChars.toUpperCase();
|
|
2637
3009
|
/**
|
|
2638
3010
|
* @private
|
|
2639
3011
|
* @param {number} [len=0] 长度
|
|
@@ -2641,14 +3013,19 @@
|
|
|
2641
3013
|
* @param {string} [prefix=''] 前缀部分,不计入长度
|
|
2642
3014
|
* @returns {string}
|
|
2643
3015
|
*/
|
|
2644
|
-
|
|
3016
|
+
|
|
3017
|
+
function internalRandomString() {
|
|
3018
|
+
var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
3019
|
+
var optionalChars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultChars;
|
|
3020
|
+
var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
3021
|
+
|
|
2645
3022
|
while (len-- > 0) {
|
|
2646
|
-
|
|
3023
|
+
var r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
|
|
2647
3024
|
return internalRandomString(len, optionalChars, prefix + r);
|
|
2648
3025
|
}
|
|
3026
|
+
|
|
2649
3027
|
return prefix;
|
|
2650
3028
|
}
|
|
2651
|
-
|
|
2652
3029
|
/**
|
|
2653
3030
|
* 生成随机字符串
|
|
2654
3031
|
*
|
|
@@ -2666,10 +3043,13 @@
|
|
|
2666
3043
|
* randomString(5, 'abc'); // ccbcb
|
|
2667
3044
|
* randomString(8, 'abcefg'); // bcgcfabg
|
|
2668
3045
|
*/
|
|
2669
|
-
function randomString(len = 0, optionalChars) {
|
|
2670
3046
|
|
|
2671
|
-
const realChars = typeof optionalChars === 'string' && optionalChars ? optionalChars : defaultChars;
|
|
2672
3047
|
|
|
3048
|
+
function randomString() {
|
|
3049
|
+
var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
3050
|
+
var optionalChars = arguments.length > 1 ? arguments[1] : undefined;
|
|
3051
|
+
|
|
3052
|
+
var realChars = typeof optionalChars === 'string' && optionalChars ? optionalChars : defaultChars;
|
|
2673
3053
|
return internalRandomString(len, realChars);
|
|
2674
3054
|
}
|
|
2675
3055
|
|
|
@@ -2691,17 +3071,21 @@
|
|
|
2691
3071
|
* strlen('🍎'); // 4
|
|
2692
3072
|
*
|
|
2693
3073
|
*/
|
|
3074
|
+
|
|
2694
3075
|
function strlen(str) {
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
3076
|
+
var realStr = normalizeString(str);
|
|
3077
|
+
var len = 0;
|
|
3078
|
+
|
|
3079
|
+
for (var i = 0; i < realStr.length; i++) {
|
|
3080
|
+
var c = realStr.charCodeAt(i);
|
|
3081
|
+
|
|
3082
|
+
if (c >= 0x0001 && c <= 0x007e || 0xff60 <= c && c <= 0xff9f) {
|
|
2700
3083
|
len++;
|
|
2701
3084
|
} else {
|
|
2702
3085
|
len += 2;
|
|
2703
3086
|
}
|
|
2704
3087
|
}
|
|
3088
|
+
|
|
2705
3089
|
return len;
|
|
2706
3090
|
}
|
|
2707
3091
|
|