diff --git a/docs/divide.js.html b/docs/divide.js.html index 53fbf20b..fb981fc8 100644 --- a/docs/divide.js.html +++ b/docs/divide.js.html @@ -106,7 +106,7 @@

divide.js


diff --git a/docs/formatBankCard.js.html b/docs/formatBankCard.js.html index 8f3730dd..1bcd2fb2 100644 --- a/docs/formatBankCard.js.html +++ b/docs/formatBankCard.js.html @@ -83,19 +83,19 @@

formatBankCard.js

* */ function formatBankCard(bankCardNo = '', { - char = ' ', - length = 4 + char = ' ', + length = 4 } = {}) { - const reg = new RegExp(`(\\d{${length}})`, 'g'); + const reg = new RegExp(`(\\d{${length}})`, 'g'); - const needRemoveLastChar = bankCardNo.length % length === 0; + const needRemoveLastChar = bankCardNo.length % length === 0; - const str = bankCardNo.replace(reg, `$1${char}`); + const str = bankCardNo.replace(reg, `$1${char}`); - return needRemoveLastChar ? str.substr(0, str.length - 1) : str; + return needRemoveLastChar ? str.substr(0, str.length - 1) : str; } -export default formatBankCard +export default formatBankCard; @@ -109,7 +109,7 @@

formatBankCard.js


diff --git a/docs/formatMoney.js.html b/docs/formatMoney.js.html index 95ddd80d..e490ad29 100644 --- a/docs/formatMoney.js.html +++ b/docs/formatMoney.js.html @@ -56,9 +56,64 @@

formatMoney.js

-
import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from './utils/constants';
+            
import { checkBoundary, scientificToNumber } from './utils/math.util';
+import isNaN from './utils/type/isNaN';
+
+// 检查数字或数字字符串
+function checkNumber(num) {
+  if (
+    isNaN(num) ||
+    (typeof num !== 'number' && typeof num !== 'string') ||
+    num === '' ||
+    typeof Number(num) !== 'number' ||
+    num === Infinity || 
+    num === -Infinity
+  ) {
+    console.error(`${num} invalid parameter.`);
+    return false;
+  }
+
+  // 数字超限如果不是是字符串,可能有异常
+  // 如 1111111111111111111111 // => 1.1111111111111111e+21
+  if (typeof num === 'number') {
+    checkBoundary(num);
+  }
+
+  return true;
+}
+
+// 格式化整数部分
+function formatInt(intStr, thousand) {
+  let txt = '';
+  intStr = intStr[0] === '+' ? intStr.substr(1) : intStr; // 去掉+符号
+  const negativeSymbol = Number(intStr) < 0 ? '-' : '';
+  const reArr = negativeSymbol ? intStr.substr(1).split('').reverse() : intStr.split('').reverse();
 
-const reg = /^[+-]?\d*\.?\d*$/;
+  for (let i = 0; i < reArr.length; i++) {
+    txt += reArr[i] + ((i + 1) % 3 === 0 && i + 1 !== reArr.length ? thousand : '');
+  }
+
+  return negativeSymbol + txt.split('').reverse().join('');
+}
+
+// 格式化小数部分,如果使用 toFixed,超大额数字会自动被截断
+function formatDec(decStr, precision, decimal){
+  if(precision === 0){
+    return '';
+  }
+
+  const zero = 0;
+  let ret = '';
+  
+  if(decStr && Number(decStr) > 0){
+    let tmpNum = parseFloat('0.' + decStr);
+    ret = tmpNum.toFixed(precision).substr(2);
+  }else{
+    ret = zero.toFixed(precision).substr(2);
+  }
+
+  return decimal + ret;
+}
 
 /**
  * 格式化金额
@@ -68,7 +123,7 @@ 

formatMoney.js

* @since 1.1.0 * @param {String | Number} num 需转换金额 (最大:9007199254740991 最小: -9007199254740991) * @param {Object} [options] - 金额格式化配置 - * @param {String | Number} [options.precision] - 保留位数 (最高:10位) + * @param {String | Number} [options.precision=2] - 保留位数 (最高:10位) * @param {String} [options.symbol] - 货币符号 * @param {String} [options.thousand=,] - 千分位符号 * @param {String} [options.decimal=.] - 小数位符号 @@ -103,52 +158,28 @@

formatMoney.js

* formatMoney(1000.00, { thousand: '&' }); * // => 1,000&00 */ -const formatMoney = (num, { precision, symbol, thousand = ',', decimal = '.' } = {}) => { - if (!reg.test(num) || num === '' || !((typeof num) === 'string' || (typeof num) === 'number')) { - return ''; - } - if (+num > MAX_SAFE_INTEGER || +num < MIN_SAFE_INTEGER) { +const formatMoney = (num, { precision = 2, symbol, thousand = ',', decimal = '.' } = {}) => { + // 数字参数不正确,返回空字符串 + if (!checkNumber(num)) { return ''; } - if (!precision || !((typeof precision) === 'string' || (typeof precision) === 'number')) { - precision = 2; - } else { - if (precision <= 0) { - precision = 2; - } - - if (precision >= 10) { - precision = 10; - } - } - num = parseFloat((num + '').replace(/[^\d\.-]/g, '')).toFixed(precision) + ''; - const reArr = num.split('.')[0].split('').reverse(); - let dot = num.split('.')[1]; - dot = dot === null ? '' : (typeof decimal === 'string' ? decimal : '.') + dot; - let txt = ''; - let res = ''; - if (reArr[reArr.length - 1] === '-') { - for (let i = 0; i < reArr.length; i++) { - if (reArr[i] === '-') { - txt += reArr[i] + ''; - continue; - } - txt += reArr[i] + ((i + 1) % 3 === 0 && i + 1 !== reArr.length - 1 ? (typeof thousand === 'string' ? thousand : ',') : ''); - } - } else { - for (let i = 0; i < reArr.length; i++) { - txt += reArr[i] + ((i + 1) % 3 === 0 && i + 1 !== reArr.length ? (typeof thousand === 'string' ? thousand : ',') : ''); - } + // 参数规整化 + if (typeof precision !== 'number' || isNaN(precision) || precision < 0) { + precision = 2; + } else if (precision > 10) { + precision = 10; } + symbol = typeof symbol === 'string' ? symbol : ''; + thousand = typeof thousand === 'string' ? thousand : ','; + decimal = typeof decimal === 'string' ? decimal : '.'; - if (symbol && (typeof symbol) === 'string') { - res = (symbol + txt.split('').reverse().join('') + dot); - } else { - res = (txt.split('').reverse().join('') + dot); - } + // 转换数字字符串,支持科学记数法 + const numStr = scientificToNumber(num) + ''; + // 整数和小数部分 + const [intStr, decStr] = numStr.split('.'); - return res; + return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal); }; export default formatMoney;
@@ -165,7 +196,7 @@

formatMoney.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index 4d6fbf1e..1f8b6638 100644 --- a/docs/index.html +++ b/docs/index.html @@ -86,21 +86,20 @@

使用

npm 包的 util-helpers/dist 目录下提供了 util-helpers.js 以及 util-helpers.min.js。你也可以通过 UNPKG 进行下载。

强烈不推荐使用已构建文件,这样无法按需加载。

示例

-
import { isMobile, formatMoney } from 'util-helpers'
+
import { formatBankCard } from 'util-helpers'
 
-isMobile("13000000000") // => true
-formatMoney('1000') // => 1,000.00
+formatBankCard('6228480402564890018'); // => 6228 4804 0256 4890 018
+formatBankCard('6228480402564890018', {char: '-'}); // => 6228-4804-0256-4890-018
 

按需引入

如果你使用 babel,下面两种方式都可以只加载用到的组件。

  • 方式一:指定模块文件,所有模块都放在 lib 目录下
-
import isMobile from 'util-helpers/lib/isMobile'
-import formatMoney from 'util-helpers/lib/formatMoney'
+
import formatBankCard from 'util-helpers/lib/formatBankCard'
 
['import', { 
   libraryName: 'util-helpers', 
@@ -177,7 +176,7 @@ 

精选第三方工具库


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/index.js.html b/docs/index.js.html index a3bf660e..e0ab2985 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -95,6 +95,7 @@

index.js

* @module Math * @since 3.1.0 * @see {@link https://github.com/camsong/blog/issues/9|JavaScript 浮点数陷阱及解法} + * @see {@link https://2zbuy.csb.app/|JS浮点数计算测试} */ export { default as plus } from './plus'; export { default as minus } from './minus'; @@ -115,7 +116,7 @@

index.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isBankCard.js.html b/docs/isBankCard.js.html index 61245dc5..2abc4802 100644 --- a/docs/isBankCard.js.html +++ b/docs/isBankCard.js.html @@ -80,7 +80,7 @@

isBankCard.js

* */ function isBankCard(value) { - return reg.test(value); + return reg.test(value); } export default isBankCard;
@@ -97,7 +97,7 @@

isBankCard.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isChinese.js.html b/docs/isChinese.js.html index a390ac68..c8d5f0c4 100644 --- a/docs/isChinese.js.html +++ b/docs/isChinese.js.html @@ -88,25 +88,25 @@

isChinese.js

const supportRegExpUnicode = RegExp.prototype.hasOwnProperty('unicode'); -if(supportRegExpUnicode){ - looseChineseRegExp = '(?:'+ - chineseDictionary.chineseBasic + '|' + - chineseDictionary.chineseExtend + '|' + - chineseDictionary.chineseExtendA + '|' + - chineseDictionary.chineseExtendB + '|' + - chineseDictionary.chineseExtendC + '|' + - chineseDictionary.chineseExtendD + '|' + - chineseDictionary.chineseExtendE + '|' + - chineseDictionary.chineseExtendF + ')+'; - chineseRegExp = '^(?:'+ - chineseDictionary.chineseBasic + '|' + - chineseDictionary.chineseExtend + '|' + - chineseDictionary.chineseExtendA + '|' + - chineseDictionary.chineseExtendB + '|' + - chineseDictionary.chineseExtendC + '|' + - chineseDictionary.chineseExtendD + '|' + - chineseDictionary.chineseExtendE + '|' + - chineseDictionary.chineseExtendF + ')+$'; +if (supportRegExpUnicode) { + looseChineseRegExp = '(?:' + + chineseDictionary.chineseBasic + '|' + + chineseDictionary.chineseExtend + '|' + + chineseDictionary.chineseExtendA + '|' + + chineseDictionary.chineseExtendB + '|' + + chineseDictionary.chineseExtendC + '|' + + chineseDictionary.chineseExtendD + '|' + + chineseDictionary.chineseExtendE + '|' + + chineseDictionary.chineseExtendF + ')+'; + chineseRegExp = '^(?:' + + chineseDictionary.chineseBasic + '|' + + chineseDictionary.chineseExtend + '|' + + chineseDictionary.chineseExtendA + '|' + + chineseDictionary.chineseExtendB + '|' + + chineseDictionary.chineseExtendC + '|' + + chineseDictionary.chineseExtendD + '|' + + chineseDictionary.chineseExtendE + '|' + + chineseDictionary.chineseExtendF + ')+$'; } /** @@ -137,10 +137,10 @@

isChinese.js

* */ function isChinese(value, { - loose = false + loose = false } = {}) { - const reg = new RegExp(loose ? looseChineseRegExp : chineseRegExp, supportRegExpUnicode ? 'u' : null); - return reg.test(value); + const reg = new RegExp(loose ? looseChineseRegExp : chineseRegExp, supportRegExpUnicode ? 'u' : null); + return reg.test(value); } export default isChinese;
@@ -157,7 +157,7 @@

isChinese.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isEmail.js.html b/docs/isEmail.js.html index 5861397a..a7d001f6 100644 --- a/docs/isEmail.js.html +++ b/docs/isEmail.js.html @@ -77,7 +77,7 @@

isEmail.js

* */ function isEmail(value) { - return reg.test(value); + return reg.test(value); } export default isEmail;
@@ -94,7 +94,7 @@

isEmail.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isIPv4.js.html b/docs/isIPv4.js.html index 53e3f602..f94c150a 100644 --- a/docs/isIPv4.js.html +++ b/docs/isIPv4.js.html @@ -83,7 +83,7 @@

isIPv4.js

* */ function isIPv4(value) { - return reg.test(value); + return reg.test(value); } export default isIPv4;
@@ -100,7 +100,7 @@

isIPv4.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isIPv6.js.html b/docs/isIPv6.js.html index 7a2791de..73a3ba96 100644 --- a/docs/isIPv6.js.html +++ b/docs/isIPv6.js.html @@ -108,7 +108,7 @@

isIPv6.js

* */ function isIPv6(value) { - return reg.test(value); + return reg.test(value); } export default isIPv6; @@ -125,7 +125,7 @@

isIPv6.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isIdCard.js.html b/docs/isIdCard.js.html index 5a1583a1..1527537d 100644 --- a/docs/isIdCard.js.html +++ b/docs/isIdCard.js.html @@ -83,7 +83,7 @@

isIdCard.js

* */ function isIdCard(value) { - return regIdCard18.test(value) || regIdCard15.test(value); + return regIdCard18.test(value) || regIdCard15.test(value); } export default isIdCard; @@ -100,7 +100,7 @@

isIdCard.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isMobile.js.html b/docs/isMobile.js.html index 1fd2a19e..7b8960ff 100644 --- a/docs/isMobile.js.html +++ b/docs/isMobile.js.html @@ -77,7 +77,7 @@

isMobile.js

* */ function isMobile(value) { - return reg.test(value); + return reg.test(value); } export default isMobile; @@ -94,7 +94,7 @@

isMobile.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isPassport.js.html b/docs/isPassport.js.html index a78fec66..59bf9b88 100644 --- a/docs/isPassport.js.html +++ b/docs/isPassport.js.html @@ -79,7 +79,7 @@

isPassport.js

* */ function isPassport(value) { - return reg.test(value); + return reg.test(value); } export default isPassport; @@ -96,7 +96,7 @@

isPassport.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isPassword.js.html b/docs/isPassword.js.html index 0ea557b4..937a189d 100644 --- a/docs/isPassword.js.html +++ b/docs/isPassword.js.html @@ -95,35 +95,35 @@

isPassword.js

* */ function isPassword(value, { - level = 2, - ignoreCase = false, - special = "-_!@#$%^&*" + level = 2, + ignoreCase = false, + special = "-_!@#$%^&*" } = {}) { - const numRegStr = "\\d"; - const lowercaseRegStr = "a-z"; - const uppercaseRegStr = "A-Z"; - const ignorecaseRegStr = "a-zA-Z"; - - let reg = null; - - if(level === 1){ - reg = new RegExp(`^(?:${numRegStr}+|[${ignorecaseRegStr}]+|[${special}]+)$/`); - }else if(level === 2){ - if(ignoreCase){ - reg = new RegExp(`^(?![${ignorecaseRegStr}]+$)(?!${numRegStr}+$)(?![${special}]+$)[${ignorecaseRegStr}${numRegStr}${special}]+$`); - }else{ - reg = new RegExp(`^(?![${lowercaseRegStr}]+$)(?![${uppercaseRegStr}]+$)(?!${numRegStr}+$)(?![${special}]+$)[${ignorecaseRegStr}${numRegStr}${special}]+$`); - } - }else if(level === 3){ - if(ignoreCase){ - // ^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$ - reg = new RegExp(`^(?![${ignorecaseRegStr}]+$)(?!${numRegStr}+$)(?![${special}]+$)(?![${ignorecaseRegStr}${numRegStr}]+$)(?![${ignorecaseRegStr}${special}]+$)(?![${numRegStr}${special}]+$)[${ignorecaseRegStr}${numRegStr}${special}]+$`); - }else{ - reg = new RegExp(`^(?![${lowercaseRegStr}]+$)(?![${uppercaseRegStr}]+$)(?!${numRegStr}+$)(?![${special}]+$)(?![${lowercaseRegStr}${numRegStr}]+$)(?![${uppercaseRegStr}${numRegStr}]+$)(?![${lowercaseRegStr}${special}]+$)(?![${uppercaseRegStr}${special}]+$)(?![${numRegStr}${special}]+$)[${ignorecaseRegStr}${numRegStr}${special}]+$`); - } + const numRegStr = "\\d"; + const lowercaseRegStr = "a-z"; + const uppercaseRegStr = "A-Z"; + const ignorecaseRegStr = "a-zA-Z"; + + let reg = null; + + if (level === 1) { + reg = new RegExp(`^(?:${numRegStr}+|[${ignorecaseRegStr}]+|[${special}]+)$/`); + } else if (level === 2) { + if (ignoreCase) { + reg = new RegExp(`^(?![${ignorecaseRegStr}]+$)(?!${numRegStr}+$)(?![${special}]+$)[${ignorecaseRegStr}${numRegStr}${special}]+$`); + } else { + reg = new RegExp(`^(?![${lowercaseRegStr}]+$)(?![${uppercaseRegStr}]+$)(?!${numRegStr}+$)(?![${special}]+$)[${ignorecaseRegStr}${numRegStr}${special}]+$`); } + } else if (level === 3) { + if (ignoreCase) { + // ^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$ + reg = new RegExp(`^(?![${ignorecaseRegStr}]+$)(?!${numRegStr}+$)(?![${special}]+$)(?![${ignorecaseRegStr}${numRegStr}]+$)(?![${ignorecaseRegStr}${special}]+$)(?![${numRegStr}${special}]+$)[${ignorecaseRegStr}${numRegStr}${special}]+$`); + } else { + reg = new RegExp(`^(?![${lowercaseRegStr}]+$)(?![${uppercaseRegStr}]+$)(?!${numRegStr}+$)(?![${special}]+$)(?![${lowercaseRegStr}${numRegStr}]+$)(?![${uppercaseRegStr}${numRegStr}]+$)(?![${lowercaseRegStr}${special}]+$)(?![${uppercaseRegStr}${special}]+$)(?![${numRegStr}${special}]+$)[${ignorecaseRegStr}${numRegStr}${special}]+$`); + } + } - return reg ? reg.test(value) : false; + return reg ? reg.test(value) : false; } export default isPassword; @@ -140,7 +140,7 @@

isPassword.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isPostcode.js.html b/docs/isPostcode.js.html index fe9d4745..3130a54c 100644 --- a/docs/isPostcode.js.html +++ b/docs/isPostcode.js.html @@ -77,7 +77,7 @@

isPostcode.js

* */ function isPostcode(value) { - return reg.test(value); + return reg.test(value); } export default isPostcode; @@ -94,7 +94,7 @@

isPostcode.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isQQ.js.html b/docs/isQQ.js.html index 3f01093f..e992be53 100644 --- a/docs/isQQ.js.html +++ b/docs/isQQ.js.html @@ -77,7 +77,7 @@

isQQ.js

* */ function isQQ(value) { - return reg.test(value); + return reg.test(value); } export default isQQ; @@ -94,7 +94,7 @@

isQQ.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isSocialCreditCode.js.html b/docs/isSocialCreditCode.js.html index 39b6769f..6192266d 100644 --- a/docs/isSocialCreditCode.js.html +++ b/docs/isSocialCreditCode.js.html @@ -73,7 +73,17 @@

isSocialCreditCode.js

* @returns {Number} 字符所在基础字符的位置 */ function getBaseCodeIndex(code) { - return baseCodeArr.findIndex(item => item === code); + let ret; + + baseCodeArr.some((item, index)=>{ + if(item === code){ + ret = index; + return true; + } + return false; + }); + + return ret; } /** @@ -85,30 +95,30 @@

isSocialCreditCode.js

* @returns {String} 校验码 */ function sumCheckCode(preCode) { - const preCodeArr = preCode.split(''); - - let total = 0; - - // 计算字符位置对应序号和加权因子的乘积,总和 - for (let i = 0; i < 17; i++) { - // 字符位置对应的基础编码序号 - const index = getBaseCodeIndex(preCode[i]); - // 加权因子 - const wf = weightFactor[i]; - // 计算序号和加权因子的乘积,并计算级数之和 - total += index * wf; - } - - // 计算整数求余函数MOD - const remainder = total % 31; - // 校验码字符值序号 - const checkCodeIndex = 31 - remainder; - - return baseCodeArr[checkCodeIndex]; + // const preCodeArr = preCode.split(''); + + let total = 0; + + // 计算字符位置对应序号和加权因子的乘积,总和 + for (let i = 0; i < 17; i++) { + // 字符位置对应的基础编码序号 + const index = getBaseCodeIndex(preCode[i]); + // 加权因子 + const wf = weightFactor[i]; + // 计算序号和加权因子的乘积,并计算级数之和 + total += index * wf; + } + + // 计算整数求余函数MOD + const remainder = total % 31; + // 校验码字符值序号 + const checkCodeIndex = 31 - remainder; + + return baseCodeArr[checkCodeIndex]; } /** - * 检测值是否为统一社会信用代码 + * 检测值是否为统一社会信用代码,也叫三证合一组织代码 * * @static * @alias module:Validator.isSocialCreditCode @@ -132,22 +142,22 @@

isSocialCreditCode.js

* */ function isSocialCreditCode(value, { - loose = false + loose = false } = {}) { - const passBaseRule = baseReg.test(value); - - if (!loose && passBaseRule) { - // 前17位 - const preCode = value.substr(0, 17); - // 校验码 - const lastCode = value.substr(-1); - // 计算校验码 - const checkCode = sumCheckCode(preCode); - - return lastCode === checkCode; - } else { - return passBaseRule; - } + const passBaseRule = baseReg.test(value); + + if (!loose && passBaseRule) { + // 前17位 + const preCode = value.substr(0, 17); + // 校验码 + const lastCode = value.substr(-1); + // 计算校验码 + const checkCode = sumCheckCode(preCode); + + return lastCode === checkCode; + } else { + return passBaseRule; + } } export default isSocialCreditCode; @@ -164,7 +174,7 @@

isSocialCreditCode.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isTelephone.js.html b/docs/isTelephone.js.html index 72952183..ea046354 100644 --- a/docs/isTelephone.js.html +++ b/docs/isTelephone.js.html @@ -83,7 +83,7 @@

isTelephone.js

* */ function isTelephone(value) { - return reg.test(value); + return reg.test(value); } export default isTelephone; @@ -100,7 +100,7 @@

isTelephone.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isVehicle.js.html b/docs/isVehicle.js.html index c5be523c..9db56b9c 100644 --- a/docs/isVehicle.js.html +++ b/docs/isVehicle.js.html @@ -77,7 +77,7 @@

isVehicle.js

* */ function isVehicle(value) { - return reg.test(value); + return reg.test(value); } export default isVehicle; @@ -94,7 +94,7 @@

isVehicle.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/isWX.js.html b/docs/isWX.js.html index 0d0a7d08..4287f773 100644 --- a/docs/isWX.js.html +++ b/docs/isWX.js.html @@ -77,7 +77,7 @@

isWX.js

* */ function isWX(value) { - return reg.test(value); + return reg.test(value); } export default isWX; @@ -94,7 +94,7 @@

isWX.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/minus.js.html b/docs/minus.js.html index 0a587f3c..14c466b9 100644 --- a/docs/minus.js.html +++ b/docs/minus.js.html @@ -102,7 +102,7 @@

minus.js


- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/module-Math.html b/docs/module-Math.html index a6e7edfd..ed5d2bb2 100644 --- a/docs/module-Math.html +++ b/docs/module-Math.html @@ -121,6 +121,8 @@

Math

@@ -1425,7 +1427,7 @@
Returns:

- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/module-Processor.html b/docs/module-Processor.html index cd54df45..7769383e 100644 --- a/docs/module-Processor.html +++ b/docs/module-Processor.html @@ -518,7 +518,7 @@

(static)
Source:
@@ -752,6 +752,8 @@

Properties
+ 2 + @@ -1999,7 +2001,7 @@
Returns:

- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/module-Validator.html b/docs/module-Validator.html index 85cf9aee..4df3a0e9 100644 --- a/docs/module-Validator.html +++ b/docs/module-Validator.html @@ -2444,7 +2444,7 @@

(static)
Source:
@@ -2494,7 +2494,7 @@

(static)
- 检测值是否为统一社会信用代码 + 检测值是否为统一社会信用代码,也叫三证合一组织代码
@@ -3256,7 +3256,7 @@
Returns:

- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/module-processor.html b/docs/module-processor.html index cd54df45..7769383e 100644 --- a/docs/module-processor.html +++ b/docs/module-processor.html @@ -518,7 +518,7 @@

(static)
Source:
@@ -752,6 +752,8 @@

Properties
+ 2 + @@ -1999,7 +2001,7 @@
Returns:

- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/module-validator.html b/docs/module-validator.html index 85cf9aee..4df3a0e9 100644 --- a/docs/module-validator.html +++ b/docs/module-validator.html @@ -2444,7 +2444,7 @@

(static)
Source:
@@ -2494,7 +2494,7 @@

(static)
- 检测值是否为统一社会信用代码 + 检测值是否为统一社会信用代码,也叫三证合一组织代码
@@ -3256,7 +3256,7 @@
Returns:

- Documentation generated by JSDoc 3.6.3 on Sun Dec 22 2019 19:08:44 GMT+0800 (GMT+08:00) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Sun Jan 12 2020 02:15:44 GMT+0800 (GMT+08:00) using the docdash theme.
diff --git a/docs/numberToChinese.js.html b/docs/numberToChinese.js.html index 19a963a6..138d4e14 100644 --- a/docs/numberToChinese.js.html +++ b/docs/numberToChinese.js.html @@ -77,33 +77,33 @@

numberToChinese.js

* @returns {String} 转化的数字 */ function sectionToChinese(section) { - let str = '', - chnstr = '', - zero = false, //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零 - unitPos = 0; - - while (section > 0) { - // 对数字取余10,得到的数即为个位数 - let v = section % 10; - - //如果数字为零,则对字符串进行补零 - if (v == 0) { - if (zero) { - //如果遇到连续多次取余都是0,那么只需补一个零即可 - zero = false; - chnstr = numberChar[v] + chnstr; - } - } else { - //第一次取余之后,如果再次取余为零,则需要补零 - zero = true; - str = numberChar[v]; - str += unitChar[unitPos]; - chnstr = str + chnstr; - } - unitPos++; - section = Math.floor(section / 10); + let str = '', + chnstr = '', + zero = false, //zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零 + unitPos = 0; + + while (section > 0) { + // 对数字取余10,得到的数即为个位数 + let v = section % 10; + + //如果数字为零,则对字符串进行补零 + if (v == 0) { + if (zero) { + //如果遇到连续多次取余都是0,那么只需补一个零即可 + zero = false; + chnstr = numberChar[v] + chnstr; + } + } else { + //第一次取余之后,如果再次取余为零,则需要补零 + zero = true; + str = numberChar[v]; + str += unitChar[unitPos]; + chnstr = str + chnstr; } - return chnstr; + unitPos++; + section = Math.floor(section / 10); + } + return chnstr; } /** @@ -114,28 +114,28 @@

numberToChinese.js

* @returns {String} 中文数字 */ function convertInteger(num) { - num = Math.floor(num); - - let unitPos = 0; - let strIns = '', chnStr = ''; - let needZero = false; - - if (num === 0) { - return numberChar[0]; + num = Math.floor(num); + + let unitPos = 0; + let strIns = '', chnStr = ''; + let needZero = false; + + if (num === 0) { + return numberChar[0]; + } + while (num > 0) { + var section = num % 10000; + if (needZero) { + chnStr = numberChar[0] + chnStr; } - while (num > 0) { - var section = num % 10000; - if (needZero) { - chnStr = numberChar[0] + chnStr; - } - strIns = sectionToChinese(section); - strIns += (section !== 0) ? unitSection[unitPos] : unitSection[0]; - chnStr = strIns + chnStr; - needZero = (section < 1000) && (section > 0); - num = Math.floor(num / 10000); - unitPos++; - } - return chnStr; + strIns = sectionToChinese(section); + strIns += (section !== 0) ? unitSection[unitPos] : unitSection[0]; + chnStr = strIns + chnStr; + needZero = (section < 1000) && (section > 0); + num = Math.floor(num / 10000); + unitPos++; + } + return chnStr; } /** @@ -145,17 +145,17 @@

numberToChinese.js

* @param {Number} num 要转换的数字 */ function convertDecimal(num) { - const numStr = num + ''; - const index = numStr.indexOf("."); + const numStr = num + ''; + const index = numStr.indexOf("."); - let ret = ''; + let ret = ''; - if (index > -1) { - let decimalStr = numStr.slice(index + 1); - ret = mapNumberChar(parseInt(decimalStr)); - } + if (index > -1) { + let decimalStr = numStr.slice(index + 1); + ret = mapNumberChar(parseInt(decimalStr)); + } - return ret; + return ret; } /** @@ -166,14 +166,14 @@

numberToChinese.js

* @returns {String} 返回中文数字的映射 */ function mapNumberChar(num) { - const numStr = num + ''; - let ret = ''; + const numStr = num + ''; + let ret = ''; - for (let i = 0, len = numStr.length; i < len; i++) { - ret += numberChar[parseInt(numStr[i])]; - } + for (let i = 0, len = numStr.length; i < len; i++) { + ret += numberChar[parseInt(numStr[i])]; + } - return ret; + return ret; } /** @@ -222,62 +222,62 @@

numberToChinese.js

* */ function numberToChinese(num, { - big5 = false, - unit = true, - decimal = '点', - zero = '', - negative = '负', - unitConfig = { - w: '万', // '萬' - y: '亿' // '億' - } + big5 = false, + unit = true, + decimal = '点', + zero = '', + negative = '负', + unitConfig = { + w: '万', // '萬' + y: '亿' // '億' + } } = {}) { - // 非数字 或 NaN 不处理 - if (typeof num !== 'number' || isNaN(num)) { - console.error(`参数错误 ${num},请传入数字`); - return ''; - } - - // 超过安全数字提示 - checkBoundary(num); - - // 设置数字字符和计数单位 - if (big5) { - numberChar = big5NumberChar.slice(); - unitChar = big5UnitChar.slice(); - } else { - numberChar = chnNumberChar.slice(); - unitChar = chnUnitChar.slice(); - } - - // 设置节点计数单位,万、亿、万亿 - unitSection = ['', unitConfig.w, unitConfig.y, unitConfig.w + unitConfig.y]; - - // 设置0 - if (zero) { - numberChar[0] = zero; - } - - // 前置字符,负数处理 - const preStr = num < 0 ? negative : ''; - - // 整数和小数 - let chnInteger, chnDecimal; - - // 处理整数 - if (unit) { - chnInteger = convertInteger(num); - } else { - chnInteger = mapNumberChar(Math.floor(num)); - } - - // 处理小数 - chnDecimal = convertDecimal(num); - - return chnDecimal ? `${preStr}${chnInteger}${decimal}${chnDecimal}` : `${preStr}${chnInteger}`; + // 非数字 或 NaN 不处理 + if (typeof num !== 'number' || isNaN(num)) { + console.error(`参数错误 ${num},请传入数字`); + return ''; + } + + // 超过安全数字提示 + checkBoundary(num); + + // 设置数字字符和计数单位 + if (big5) { + numberChar = big5NumberChar.slice(); + unitChar = big5UnitChar.slice(); + } else { + numberChar = chnNumberChar.slice(); + unitChar = chnUnitChar.slice(); + } + + // 设置节点计数单位,万、亿、万亿 + unitSection = ['', unitConfig.w, unitConfig.y, unitConfig.w + unitConfig.y]; + + // 设置0 + if (zero) { + numberChar[0] = zero; + } + + // 前置字符,负数处理 + const preStr = num < 0 ? negative : ''; + + // 整数和小数 + let chnInteger, chnDecimal; + + // 处理整数 + if (unit) { + chnInteger = convertInteger(num); + } else { + chnInteger = mapNumberChar(Math.floor(num)); + } + + // 处理小数 + chnDecimal = convertDecimal(num); + + return chnDecimal ? `${preStr}${chnInteger}${decimal}${chnDecimal}` : `${preStr}${chnInteger}`; } -export default numberToChinese +export default numberToChinese;

@@ -291,7 +291,7 @@

numberToChinese.js


diff --git a/docs/plus.js.html b/docs/plus.js.html index 532582c3..d58a4fff 100644 --- a/docs/plus.js.html +++ b/docs/plus.js.html @@ -102,7 +102,7 @@

plus.js


diff --git a/docs/replaceChar.js.html b/docs/replaceChar.js.html index 76fa1b83..af29cbbe 100644 --- a/docs/replaceChar.js.html +++ b/docs/replaceChar.js.html @@ -99,44 +99,44 @@

replaceChar.js

* */ function replaceChar(str = '', { - start = 3, - end = -4, - char = '*', - repeat, - exclude + start = 3, + end = -4, + char = '*', + repeat, + exclude } = {}) { - const strLen = str.length; + const strLen = str.length; - // 开始位置超过str长度 - if (Math.abs(start) >= strLen) { - return str; - } + // 开始位置超过str长度 + if (Math.abs(start) >= strLen) { + return str; + } - start = start >= 0 ? start : strLen + start; - end = end >= 0 ? end : strLen + end; + start = start >= 0 ? start : strLen + start; + end = end >= 0 ? end : strLen + end; - // 开始位置大于结束位置 - if (start >= end) { - return str; - } + // 开始位置大于结束位置 + if (start >= end) { + return str; + } - let middleStr = ''; + let middleStr = ''; - if (exclude) { - repeat = Math.round(end - start); + if (exclude) { + repeat = Math.round(end - start); - const maskStr = str.substr(start, repeat); - const reg = new RegExp(`[^${exclude}]`, 'g'); - middleStr = maskStr.replace(reg, char); - } else { - repeat = typeof repeat === 'number' && repeat >= 0 ? repeat : Math.round(end - start); - middleStr = char.repeat(repeat); - } + const maskStr = str.substr(start, repeat); + const reg = new RegExp(`[^${exclude}]`, 'g'); + middleStr = maskStr.replace(reg, char); + } else { + repeat = typeof repeat === 'number' && repeat >= 0 ? repeat : Math.round(end - start); + middleStr = char.repeat(repeat); + } - return str.substr(0, start) + middleStr + str.substr(end); + return str.substr(0, start) + middleStr + str.substr(end); } -export default replaceChar +export default replaceChar; @@ -150,7 +150,7 @@

replaceChar.js


diff --git a/docs/round.js.html b/docs/round.js.html index 394951af..5cf4c6f8 100644 --- a/docs/round.js.html +++ b/docs/round.js.html @@ -98,7 +98,7 @@

round.js


diff --git a/docs/times.js.html b/docs/times.js.html index 1bfcbbe9..f24543ed 100644 --- a/docs/times.js.html +++ b/docs/times.js.html @@ -107,7 +107,7 @@

times.js