util-helpers 4.10.3 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/util-helpers.js +171 -133
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/bytesToSize.js +4 -3
- package/esm/formatMoney.js +7 -10
- package/esm/isBusinessLicense.js +2 -2
- package/esm/isChinese.js +24 -11
- package/esm/numberToChinese.js +30 -36
- package/esm/parseIdCard.js +9 -24
- package/esm/replaceChar.js +8 -7
- package/esm/round.js +8 -0
- package/esm/utils/devWarn.js +16 -0
- package/esm/utils/math.util.js +52 -29
- package/esm/validatePassword.js +25 -20
- package/lib/bytesToSize.js +4 -3
- package/lib/formatMoney.js +7 -10
- package/lib/isBusinessLicense.js +2 -2
- package/lib/isChinese.js +24 -11
- package/lib/numberToChinese.js +32 -36
- package/lib/parseIdCard.js +9 -24
- package/lib/replaceChar.js +11 -8
- package/lib/round.js +9 -0
- package/lib/utils/devWarn.js +24 -0
- package/lib/utils/math.util.js +54 -29
- package/lib/validatePassword.js +27 -20
- package/package.json +3 -2
- package/types/isChinese.d.ts +13 -3
- package/types/replaceChar.d.ts +1 -1
- package/types/utils/devWarn.d.ts +7 -0
- package/types/utils/math.util.d.ts +2 -2
- package/types/validatePassword.d.ts +3 -3
package/dist/util-helpers.js
CHANGED
|
@@ -558,6 +558,17 @@
|
|
|
558
558
|
config.disableWarning = !!bool;
|
|
559
559
|
}
|
|
560
560
|
|
|
561
|
+
/**
|
|
562
|
+
* 打印警告信息
|
|
563
|
+
*
|
|
564
|
+
* @param {any[]} args 打印的信息
|
|
565
|
+
*/
|
|
566
|
+
function devWarn(...args) {
|
|
567
|
+
if (!config.disableWarning) {
|
|
568
|
+
console.warn(...args);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
561
572
|
const regNumber = /[\d]/;
|
|
562
573
|
const regLowerCaseLetter = /[a-z]/;
|
|
563
574
|
const regUpperCaseLetter = /[A-Z]/;
|
|
@@ -612,15 +623,20 @@
|
|
|
612
623
|
*
|
|
613
624
|
* @private
|
|
614
625
|
* @param {string} val 检测的值
|
|
615
|
-
* @param {string}
|
|
626
|
+
* @param {string} chars 特殊字符
|
|
616
627
|
* @returns {boolean} 是否包含特殊字符
|
|
617
628
|
*/
|
|
618
|
-
function hasSpecialCharacter(val, chars
|
|
619
|
-
if (!chars) {
|
|
629
|
+
function hasSpecialCharacter(val, chars) {
|
|
630
|
+
if (!chars || !val) {
|
|
620
631
|
return false;
|
|
621
632
|
}
|
|
622
633
|
|
|
623
634
|
const specialChars = val.replace(regAllNumberAndLetter, '');
|
|
635
|
+
|
|
636
|
+
if (!specialChars) {
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
|
|
624
640
|
const regChars = hasHex(chars) ? new RegExp(`[${chars}]`) : null;
|
|
625
641
|
|
|
626
642
|
if (regChars) {
|
|
@@ -642,13 +658,19 @@
|
|
|
642
658
|
*
|
|
643
659
|
* @private
|
|
644
660
|
* @param {string} val 检测的值
|
|
645
|
-
* @param {string} chars
|
|
661
|
+
* @param {string} chars 特殊字符
|
|
646
662
|
* @returns {boolean} 是否包含非法字符
|
|
647
663
|
*/
|
|
648
|
-
function hasUnallowableCharacter(val, chars
|
|
664
|
+
function hasUnallowableCharacter(val, chars) {
|
|
665
|
+
if (!val) {
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
668
|
+
|
|
649
669
|
const specialChars = val.replace(regAllNumberAndLetter, '');
|
|
650
670
|
|
|
651
|
-
if (!
|
|
671
|
+
if (!specialChars) {
|
|
672
|
+
return false;
|
|
673
|
+
} else if (!chars) {
|
|
652
674
|
return true;
|
|
653
675
|
}
|
|
654
676
|
|
|
@@ -711,7 +733,7 @@
|
|
|
711
733
|
* }
|
|
712
734
|
* }
|
|
713
735
|
*
|
|
714
|
-
* validatePassword('a12345678', {level: 3});
|
|
736
|
+
* validatePassword('a12345678', { level: 3 });
|
|
715
737
|
* // =>
|
|
716
738
|
* {
|
|
717
739
|
* validated: false,
|
|
@@ -725,7 +747,7 @@
|
|
|
725
747
|
* }
|
|
726
748
|
* }
|
|
727
749
|
*
|
|
728
|
-
* validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true});
|
|
750
|
+
* validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true });
|
|
729
751
|
* // =>
|
|
730
752
|
* {
|
|
731
753
|
* validated: false,
|
|
@@ -740,7 +762,7 @@
|
|
|
740
762
|
* }
|
|
741
763
|
*
|
|
742
764
|
* // 自定义特殊字符
|
|
743
|
-
* validatePassword('_Aa一二三45678', {level: 3, ignoreCase: true, special: '_一二三'});
|
|
765
|
+
* validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true, special: '_一二三' });
|
|
744
766
|
* // =>
|
|
745
767
|
* {
|
|
746
768
|
* validated: true,
|
|
@@ -758,9 +780,7 @@
|
|
|
758
780
|
let valStr = value;
|
|
759
781
|
|
|
760
782
|
if (typeof value !== 'string') {
|
|
761
|
-
|
|
762
|
-
console.warn(`[validatePassword] value must be a string.`);
|
|
763
|
-
}
|
|
783
|
+
devWarn(`[validatePassword] value must be a string.`);
|
|
764
784
|
valStr = '';
|
|
765
785
|
}
|
|
766
786
|
|
|
@@ -774,15 +794,15 @@
|
|
|
774
794
|
const containesUpperCaseLetter = hasUpperCaseLetter(valStr);
|
|
775
795
|
// 包含特殊字符
|
|
776
796
|
const containesSpecialCharacter = hasSpecialCharacter(valStr, special);
|
|
777
|
-
//
|
|
797
|
+
// 包含非法字符,即含有非数字字母特殊字符以外的其他字符
|
|
778
798
|
const containesUnallowableCharacter = hasUnallowableCharacter(valStr, special);
|
|
779
799
|
|
|
780
800
|
if (containesNumber) {
|
|
781
801
|
currentLevel += 1;
|
|
782
802
|
}
|
|
783
803
|
|
|
804
|
+
// 不区分大小写
|
|
784
805
|
if (ignoreCase) {
|
|
785
|
-
// 不区分大小写
|
|
786
806
|
if (containesLowerCaseLetter || containesUpperCaseLetter) {
|
|
787
807
|
currentLevel += 1;
|
|
788
808
|
}
|
|
@@ -913,18 +933,16 @@
|
|
|
913
933
|
chineseExtendF: '[\u{2CEB0}-\u{2EBE0}]'
|
|
914
934
|
};
|
|
915
935
|
|
|
916
|
-
|
|
936
|
+
const looseChineseRegExp = chineseDictionary.chineseBasic + '+';
|
|
937
|
+
const chineseRegExp = '^' + chineseDictionary.chineseBasic + '+$';
|
|
917
938
|
|
|
918
|
-
|
|
939
|
+
const chineseWithExtend = '(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')';
|
|
940
|
+
const looseChineseExtendRegExp = chineseWithExtend + '+';
|
|
941
|
+
const chineseExtendRegExp = '^' + chineseWithExtend + '+$';
|
|
919
942
|
|
|
920
943
|
// eslint-disable-next-line no-prototype-builtins
|
|
921
944
|
const supportRegExpUnicode = RegExp.prototype.hasOwnProperty('unicode');
|
|
922
945
|
|
|
923
|
-
if (supportRegExpUnicode) {
|
|
924
|
-
looseChineseRegExp = '(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')+';
|
|
925
|
-
chineseRegExp = '^(?:' + chineseDictionary.chineseBasic + '|' + chineseDictionary.chineseExtend + '|' + chineseDictionary.chineseExtendA + '|' + chineseDictionary.chineseExtendB + '|' + chineseDictionary.chineseExtendC + '|' + chineseDictionary.chineseExtendD + '|' + chineseDictionary.chineseExtendE + '|' + chineseDictionary.chineseExtendF + ')+$';
|
|
926
|
-
}
|
|
927
|
-
|
|
928
946
|
/**
|
|
929
947
|
* 检测值是否为中文
|
|
930
948
|
*
|
|
@@ -935,6 +953,7 @@
|
|
|
935
953
|
* @param {*} value 要检测的值
|
|
936
954
|
* @param {Object} [options] 配置项
|
|
937
955
|
* @param {boolean} [options.loose=false] 宽松模式。如果为true,只要包含中文即为true
|
|
956
|
+
* @param {boolean} [options.useExtend=false] 使用统一表意文字扩展A-F。注意:如果不支持 `RegExp.prototype.unicode`,扩展字符集将自动不生效,如IE浏览器。
|
|
938
957
|
* @returns {boolean} 值是否为中文
|
|
939
958
|
* @example
|
|
940
959
|
*
|
|
@@ -945,16 +964,30 @@
|
|
|
945
964
|
* // => false
|
|
946
965
|
*
|
|
947
966
|
* // 宽松模式,只要包含中文即为true
|
|
948
|
-
* isChinese('林A', {loose: true});
|
|
967
|
+
* isChinese('林A', { loose: true });
|
|
949
968
|
* // => true
|
|
950
969
|
*
|
|
951
|
-
* isChinese('A林A', {loose: true});
|
|
970
|
+
* isChinese('A林A', { loose: true });
|
|
952
971
|
* // => true
|
|
953
972
|
*
|
|
973
|
+
* isChinese('𠮷');
|
|
974
|
+
* // => false
|
|
975
|
+
*
|
|
976
|
+
* // 使用中文扩展字符集,需要浏览器支持 RegExp.prototype.unicode 才生效。
|
|
977
|
+
* isChinese('𠮷', { useExtend: true });
|
|
978
|
+
* // => true
|
|
979
|
+
* isChinese('𠮷aa', { useExtend: true, loose: true });
|
|
980
|
+
* // => true
|
|
954
981
|
*/
|
|
955
|
-
function isChinese(value, { loose = false } = {}) {
|
|
982
|
+
function isChinese(value, { loose = false, useExtend = false } = {}) {
|
|
956
983
|
const valueStr = normalizeString(value);
|
|
957
|
-
const
|
|
984
|
+
const basicRegExp = loose ? looseChineseRegExp : chineseRegExp;
|
|
985
|
+
const extendRegExp = loose ? looseChineseExtendRegExp : chineseExtendRegExp;
|
|
986
|
+
|
|
987
|
+
const hasExtend = useExtend && supportRegExpUnicode;
|
|
988
|
+
const resultRegExp = hasExtend ? extendRegExp : basicRegExp;
|
|
989
|
+
const flag = hasExtend ? 'u' : undefined;
|
|
990
|
+
const reg = new RegExp(resultRegExp, flag);
|
|
958
991
|
return reg.test(valueStr);
|
|
959
992
|
}
|
|
960
993
|
|
|
@@ -1110,8 +1143,8 @@
|
|
|
1110
1143
|
}
|
|
1111
1144
|
|
|
1112
1145
|
// 反模10计算
|
|
1113
|
-
if (pj ===
|
|
1114
|
-
retNum =
|
|
1146
|
+
if (pj === 1) {
|
|
1147
|
+
retNum = 0;
|
|
1115
1148
|
} else {
|
|
1116
1149
|
retNum = 11 - pj;
|
|
1117
1150
|
}
|
|
@@ -1337,8 +1370,9 @@
|
|
|
1337
1370
|
* @returns {number}
|
|
1338
1371
|
*/
|
|
1339
1372
|
function float2Fixed(num) {
|
|
1340
|
-
|
|
1341
|
-
|
|
1373
|
+
const strNum = String(num);
|
|
1374
|
+
if (!isScientificNumber(strNum)) {
|
|
1375
|
+
return Number(strNum.replace('.', ''));
|
|
1342
1376
|
}
|
|
1343
1377
|
const dLen = digitLength(num);
|
|
1344
1378
|
return dLen > 0 ? strip(+num * Math.pow(10, dLen)) : +num;
|
|
@@ -1351,9 +1385,7 @@
|
|
|
1351
1385
|
*/
|
|
1352
1386
|
function checkBoundary(num) {
|
|
1353
1387
|
if (+num > MAX_SAFE_INTEGER || +num < MIN_SAFE_INTEGER) {
|
|
1354
|
-
|
|
1355
|
-
console.warn(`${num} is beyond boundary when transfer to integer, the results may not be accurate`);
|
|
1356
|
-
}
|
|
1388
|
+
devWarn(`${num} is beyond boundary when transfer to integer, the results may not be accurate`);
|
|
1357
1389
|
}
|
|
1358
1390
|
}
|
|
1359
1391
|
|
|
@@ -1385,37 +1417,63 @@
|
|
|
1385
1417
|
* 2.小数点前边是0,小数点后十分位(包含十分位)之后连续零的个数大于等于6个
|
|
1386
1418
|
*
|
|
1387
1419
|
* @param {string | number} num 科学计数法数字
|
|
1388
|
-
* @returns {string} 转换后的数字字符串
|
|
1420
|
+
* @returns {string | number} 转换后的数字字符串
|
|
1389
1421
|
*/
|
|
1390
1422
|
function scientificToNumber(num) {
|
|
1391
|
-
|
|
1392
|
-
const zero = '0';
|
|
1393
|
-
const parts = String(num).toLowerCase().split('e');
|
|
1394
|
-
const e = parts.pop(); // 存储指数
|
|
1395
|
-
// @ts-ignore
|
|
1396
|
-
const l = Math.abs(e); // 取绝对值,l-1就是0的个数
|
|
1397
|
-
// @ts-ignore
|
|
1398
|
-
const sign = e / l; //判断正负
|
|
1399
|
-
const coeff_array = parts[0].split('.'); // 将系数按照小数点拆分
|
|
1423
|
+
const strNum = String(num);
|
|
1400
1424
|
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
num = zero + '.' + new Array(l).join(zero) + coeff_array.join('');
|
|
1405
|
-
} else {
|
|
1406
|
-
const dec = coeff_array[1];
|
|
1425
|
+
if (!isScientificNumber(strNum)) {
|
|
1426
|
+
return num;
|
|
1427
|
+
}
|
|
1407
1428
|
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1429
|
+
/** @type string */
|
|
1430
|
+
let ret;
|
|
1431
|
+
|
|
1432
|
+
const zero = '0';
|
|
1433
|
+
const parts = strNum.toLowerCase().split('e');
|
|
1434
|
+
const e = parts.pop(); // 存储指数
|
|
1435
|
+
// @ts-ignore
|
|
1436
|
+
const l = Math.abs(e); // 取绝对值,l-1就是0的个数
|
|
1437
|
+
// @ts-ignore
|
|
1438
|
+
const sign = e / l; //判断正负
|
|
1439
|
+
const coeff_array = parts[0].split('.'); // 将系数按照小数点拆分
|
|
1440
|
+
|
|
1441
|
+
// 如果是小数
|
|
1442
|
+
if (sign === -1) {
|
|
1443
|
+
// 整数部分
|
|
1444
|
+
const intVal = trimLeftZero(coeff_array[0]);
|
|
1445
|
+
|
|
1446
|
+
// 整数部分大于科学计数后面部分
|
|
1447
|
+
// 如: 10e-1, 10.2e-1
|
|
1448
|
+
if (intVal.length > l) {
|
|
1449
|
+
const thanLen = intVal.length - l;
|
|
1450
|
+
const dec = coeff_array[1] || '';
|
|
1451
|
+
|
|
1452
|
+
ret = intVal.slice(0, thanLen);
|
|
1453
|
+
|
|
1454
|
+
// 处理 10e-1, 100e-1
|
|
1455
|
+
if (intVal.slice(thanLen) !== '0' || dec) {
|
|
1456
|
+
ret += '.' + intVal.slice(thanLen) + dec;
|
|
1414
1457
|
}
|
|
1458
|
+
} else {
|
|
1459
|
+
// 整数部分小于等于科学计数后面部分
|
|
1460
|
+
// 如: 1e-1, 0.2e-1, 1.2e-2, 1.2e-1
|
|
1461
|
+
ret = zero + '.' + new Array(l - intVal.length + 1).join(zero) + coeff_array.join('');
|
|
1462
|
+
}
|
|
1463
|
+
} else {
|
|
1464
|
+
// 小数部分
|
|
1465
|
+
const dec = coeff_array[1] || '';
|
|
1466
|
+
|
|
1467
|
+
// 如果是整数,将整数除第一位之外的非零数字计入位数,相应的减少0的个数
|
|
1468
|
+
if (l - dec.length < 0) {
|
|
1469
|
+
ret = trimLeftZero(coeff_array[0] + dec.substring(0, l)) + '.' + dec.substring(l);
|
|
1470
|
+
} else {
|
|
1471
|
+
// 拼接字符串,如果是整数,不需要拼接小数点
|
|
1472
|
+
ret = coeff_array.join('') + new Array(l - dec.length + 1).join(zero);
|
|
1415
1473
|
}
|
|
1416
1474
|
}
|
|
1417
|
-
|
|
1418
|
-
return
|
|
1475
|
+
|
|
1476
|
+
return trimLeftZero(ret);
|
|
1419
1477
|
}
|
|
1420
1478
|
|
|
1421
1479
|
/**
|
|
@@ -1449,9 +1507,7 @@
|
|
|
1449
1507
|
*/
|
|
1450
1508
|
function checkNumber(num) {
|
|
1451
1509
|
if (!(reg.test(num) || isScientificNumber(num)) || _isNaN(num) || (typeof num !== 'number' && typeof num !== 'string') || num === '') {
|
|
1452
|
-
|
|
1453
|
-
console.warn(`${num} invalid parameter.`);
|
|
1454
|
-
}
|
|
1510
|
+
devWarn(`${num} invalid parameter.`);
|
|
1455
1511
|
|
|
1456
1512
|
return false;
|
|
1457
1513
|
}
|
|
@@ -1575,9 +1631,9 @@
|
|
|
1575
1631
|
decimal = typeof decimal === 'string' ? decimal : '.';
|
|
1576
1632
|
|
|
1577
1633
|
// 转换数字字符串,支持科学记数法
|
|
1578
|
-
const
|
|
1634
|
+
const strNum = scientificToNumber(num) + '';
|
|
1579
1635
|
// 整数和小数部分
|
|
1580
|
-
const [intStr, decStr] =
|
|
1636
|
+
const [intStr, decStr] = strNum.split('.');
|
|
1581
1637
|
|
|
1582
1638
|
return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
|
|
1583
1639
|
};
|
|
@@ -1668,12 +1724,13 @@
|
|
|
1668
1724
|
* // => 林**
|
|
1669
1725
|
*
|
|
1670
1726
|
*/
|
|
1671
|
-
function replaceChar(str
|
|
1672
|
-
const
|
|
1727
|
+
function replaceChar(str, { start = 3, end = -4, char = '*', repeat, exclude } = {}) {
|
|
1728
|
+
const realStr = normalizeString(str);
|
|
1729
|
+
const strLen = realStr.length;
|
|
1673
1730
|
|
|
1674
1731
|
// 开始位置超过str长度
|
|
1675
1732
|
if (Math.abs(start) >= strLen) {
|
|
1676
|
-
return
|
|
1733
|
+
return realStr;
|
|
1677
1734
|
}
|
|
1678
1735
|
|
|
1679
1736
|
start = start >= 0 ? start : strLen + start;
|
|
@@ -1681,10 +1738,10 @@
|
|
|
1681
1738
|
|
|
1682
1739
|
// 开始位置大于结束位置
|
|
1683
1740
|
if (start >= end) {
|
|
1684
|
-
return
|
|
1741
|
+
return realStr;
|
|
1685
1742
|
}
|
|
1686
1743
|
|
|
1687
|
-
let middleStr =
|
|
1744
|
+
let middleStr = realStr.substring(start, end);
|
|
1688
1745
|
|
|
1689
1746
|
if (exclude) {
|
|
1690
1747
|
const reg = new RegExp(`[^${exclude}]`, 'g');
|
|
@@ -1694,7 +1751,7 @@
|
|
|
1694
1751
|
middleStr = char.repeat(repeat);
|
|
1695
1752
|
}
|
|
1696
1753
|
|
|
1697
|
-
return
|
|
1754
|
+
return realStr.substring(0, start) + middleStr + realStr.substring(end);
|
|
1698
1755
|
}
|
|
1699
1756
|
|
|
1700
1757
|
// 简体
|
|
@@ -1733,10 +1790,10 @@
|
|
|
1733
1790
|
* @returns {string} 转化的数字
|
|
1734
1791
|
*/
|
|
1735
1792
|
function sectionToChinese(section) {
|
|
1736
|
-
let str = ''
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1793
|
+
let str = '';
|
|
1794
|
+
let chnstr = '';
|
|
1795
|
+
let zero = false; //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
|
|
1796
|
+
let unitPos = 0;
|
|
1740
1797
|
|
|
1741
1798
|
while (section > 0) {
|
|
1742
1799
|
// 对数字取余10,得到的数即为个位数
|
|
@@ -1770,18 +1827,18 @@
|
|
|
1770
1827
|
* @returns {string} 中文数字
|
|
1771
1828
|
*/
|
|
1772
1829
|
function convertInteger(num) {
|
|
1773
|
-
|
|
1830
|
+
let numInt = Math.floor(num);
|
|
1774
1831
|
|
|
1775
1832
|
let unitPos = 0;
|
|
1776
|
-
let strIns = ''
|
|
1777
|
-
|
|
1833
|
+
let strIns = '';
|
|
1834
|
+
let chnStr = '';
|
|
1778
1835
|
let needZero = false;
|
|
1779
1836
|
|
|
1780
|
-
if (
|
|
1837
|
+
if (numInt === 0) {
|
|
1781
1838
|
return numberChar[0];
|
|
1782
1839
|
}
|
|
1783
|
-
while (
|
|
1784
|
-
var section =
|
|
1840
|
+
while (numInt > 0) {
|
|
1841
|
+
var section = numInt % 10000;
|
|
1785
1842
|
if (needZero) {
|
|
1786
1843
|
chnStr = numberChar[0] + chnStr;
|
|
1787
1844
|
}
|
|
@@ -1789,7 +1846,7 @@
|
|
|
1789
1846
|
strIns += section !== 0 ? unitSection[unitPos] : unitSection[0];
|
|
1790
1847
|
chnStr = strIns + chnStr;
|
|
1791
1848
|
needZero = section < 1000 && section > 0;
|
|
1792
|
-
|
|
1849
|
+
numInt = Math.floor(numInt / 10000);
|
|
1793
1850
|
unitPos++;
|
|
1794
1851
|
}
|
|
1795
1852
|
return chnStr;
|
|
@@ -1802,13 +1859,13 @@
|
|
|
1802
1859
|
* @param {number} num 要转换的数字
|
|
1803
1860
|
*/
|
|
1804
1861
|
function convertDecimal(num) {
|
|
1805
|
-
const
|
|
1806
|
-
const index =
|
|
1862
|
+
const strNum = num + '';
|
|
1863
|
+
const index = strNum.indexOf('.');
|
|
1807
1864
|
|
|
1808
1865
|
let ret = '';
|
|
1809
1866
|
|
|
1810
1867
|
if (index > -1) {
|
|
1811
|
-
let decimalStr =
|
|
1868
|
+
let decimalStr = strNum.slice(index + 1);
|
|
1812
1869
|
ret = mapNumberChar(parseInt(decimalStr));
|
|
1813
1870
|
}
|
|
1814
1871
|
|
|
@@ -1823,11 +1880,11 @@
|
|
|
1823
1880
|
* @returns {string} 返回中文数字的映射
|
|
1824
1881
|
*/
|
|
1825
1882
|
function mapNumberChar(num) {
|
|
1826
|
-
const
|
|
1883
|
+
const strNum = num + '';
|
|
1827
1884
|
let ret = '';
|
|
1828
1885
|
|
|
1829
|
-
for (let i = 0, len =
|
|
1830
|
-
ret += numberChar[parseInt(
|
|
1886
|
+
for (let i = 0, len = strNum.length; i < len; i++) {
|
|
1887
|
+
ret += numberChar[parseInt(strNum[i])];
|
|
1831
1888
|
}
|
|
1832
1889
|
|
|
1833
1890
|
return ret;
|
|
@@ -1878,25 +1935,10 @@
|
|
|
1878
1935
|
* // => 一九九〇
|
|
1879
1936
|
*
|
|
1880
1937
|
*/
|
|
1881
|
-
function numberToChinese(
|
|
1882
|
-
num,
|
|
1883
|
-
{
|
|
1884
|
-
big5 = false,
|
|
1885
|
-
unit = true,
|
|
1886
|
-
decimal = '点',
|
|
1887
|
-
zero = '',
|
|
1888
|
-
negative = '负',
|
|
1889
|
-
unitConfig = {
|
|
1890
|
-
w: '万', // '萬'
|
|
1891
|
-
y: '亿' // '億'
|
|
1892
|
-
}
|
|
1893
|
-
} = {}
|
|
1894
|
-
) {
|
|
1938
|
+
function numberToChinese(num, { big5 = false, unit = true, decimal = '点', zero = '', negative = '负', unitConfig = {} } = {}) {
|
|
1895
1939
|
// 非数字 或 NaN 不处理
|
|
1896
1940
|
if (typeof num !== 'number' || isNaN(num)) {
|
|
1897
|
-
|
|
1898
|
-
console.warn(`参数错误 ${num},请传入数字`);
|
|
1899
|
-
}
|
|
1941
|
+
devWarn(`参数错误 ${num},请传入数字`);
|
|
1900
1942
|
|
|
1901
1943
|
return '';
|
|
1902
1944
|
}
|
|
@@ -1914,9 +1956,10 @@
|
|
|
1914
1956
|
}
|
|
1915
1957
|
|
|
1916
1958
|
// 设置节点计数单位,万、亿、万亿
|
|
1917
|
-
const
|
|
1918
|
-
const
|
|
1919
|
-
|
|
1959
|
+
const unitWan = unitConfig?.w || '万';
|
|
1960
|
+
const unitYi = unitConfig?.y || '亿';
|
|
1961
|
+
const unitWanYi = unitWan + unitYi;
|
|
1962
|
+
unitSection = ['', unitWan, unitYi, unitWanYi];
|
|
1920
1963
|
|
|
1921
1964
|
// 设置0
|
|
1922
1965
|
if (zero) {
|
|
@@ -1928,16 +1971,17 @@
|
|
|
1928
1971
|
|
|
1929
1972
|
// 整数和小数
|
|
1930
1973
|
let chnInteger, chnDecimal;
|
|
1974
|
+
const numAbs = Math.abs(num);
|
|
1931
1975
|
|
|
1932
1976
|
// 处理整数
|
|
1933
1977
|
if (unit) {
|
|
1934
|
-
chnInteger = convertInteger(
|
|
1978
|
+
chnInteger = convertInteger(numAbs);
|
|
1935
1979
|
} else {
|
|
1936
|
-
chnInteger = mapNumberChar(Math.floor(
|
|
1980
|
+
chnInteger = mapNumberChar(Math.floor(numAbs));
|
|
1937
1981
|
}
|
|
1938
1982
|
|
|
1939
1983
|
// 处理小数
|
|
1940
|
-
chnDecimal = convertDecimal(
|
|
1984
|
+
chnDecimal = convertDecimal(numAbs);
|
|
1941
1985
|
|
|
1942
1986
|
return chnDecimal ? `${preStr}${chnInteger}${decimal}${chnDecimal}` : `${preStr}${chnInteger}`;
|
|
1943
1987
|
}
|
|
@@ -1965,13 +2009,15 @@
|
|
|
1965
2009
|
* // => 1 GB
|
|
1966
2010
|
*/
|
|
1967
2011
|
function bytesToSize(bytes) {
|
|
1968
|
-
|
|
2012
|
+
const numBytes = typeof bytes !== 'number' ? Number(bytes) : bytes;
|
|
2013
|
+
if (numBytes === 0 || isNaN(numBytes)) return '0 B';
|
|
2014
|
+
|
|
1969
2015
|
const k = 1024;
|
|
1970
2016
|
// 存储单位
|
|
1971
2017
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
1972
|
-
const i = Math.floor(Math.log(
|
|
2018
|
+
const i = Math.floor(Math.log(numBytes) / Math.log(k));
|
|
1973
2019
|
|
|
1974
|
-
return sizes[i] ? `${Number((
|
|
2020
|
+
return sizes[i] ? `${Number((numBytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}` : numBytes + '';
|
|
1975
2021
|
}
|
|
1976
2022
|
|
|
1977
2023
|
const regIdCard = /^(?<province>\d{2})(?<city>\d{2})(?<area>\d{2})(?<year>(?:\d{2})?\d{2})(?<month>\d{2})(?<day>\d{2})\d{2}(?<gender>\d)(?:\d|X)?$/i;
|
|
@@ -2112,31 +2158,17 @@
|
|
|
2112
2158
|
* @type {{ province: string, city: string, area: string, year: string, month: string, day: string, gender: string }}
|
|
2113
2159
|
*
|
|
2114
2160
|
*/
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2161
|
+
// @ts-ignore
|
|
2162
|
+
const origin = info?.groups || {
|
|
2163
|
+
province: info[1],
|
|
2164
|
+
city: info[2],
|
|
2165
|
+
area: info[3],
|
|
2166
|
+
year: info[4],
|
|
2167
|
+
month: info[5],
|
|
2168
|
+
day: info[6],
|
|
2169
|
+
gender: info[7]
|
|
2123
2170
|
};
|
|
2124
2171
|
|
|
2125
|
-
if (info.groups) {
|
|
2126
|
-
// @ts-ignore
|
|
2127
|
-
origin = info.groups;
|
|
2128
|
-
} else {
|
|
2129
|
-
origin = {
|
|
2130
|
-
province: info[1],
|
|
2131
|
-
city: info[2],
|
|
2132
|
-
area: info[3],
|
|
2133
|
-
year: info[4],
|
|
2134
|
-
month: info[5],
|
|
2135
|
-
day: info[6],
|
|
2136
|
-
gender: info[7]
|
|
2137
|
-
};
|
|
2138
|
-
}
|
|
2139
|
-
|
|
2140
2172
|
const province = Provinces.find((item) => item[0] === origin.province);
|
|
2141
2173
|
|
|
2142
2174
|
if (!province) {
|
|
@@ -2555,6 +2587,12 @@
|
|
|
2555
2587
|
* // => 4100
|
|
2556
2588
|
*/
|
|
2557
2589
|
function round(num, precision = 0) {
|
|
2590
|
+
// 兼容处理,如果参数为非数字或字符串时,直接返回
|
|
2591
|
+
if ((!isNumber(num) || _isNaN(num)) && !isString(num)) {
|
|
2592
|
+
// @ts-ignore
|
|
2593
|
+
return num;
|
|
2594
|
+
}
|
|
2595
|
+
|
|
2558
2596
|
const base = Math.pow(10, precision);
|
|
2559
2597
|
return divide(Math.round(times(num, base)), base);
|
|
2560
2598
|
}
|