util-helpers 4.7.1 → 4.8.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/README.md +1 -0
- package/dist/util-helpers.js +73 -34
- 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/index.js +1 -0
- package/esm/isBankCard.js +30 -4
- package/esm/isIdCard.js +1 -1
- package/esm/isSocialCreditCode.js +10 -29
- package/esm/randomString.js +56 -0
- package/lib/index.js +8 -0
- package/lib/isBankCard.js +30 -4
- package/lib/isIdCard.js +1 -1
- package/lib/isSocialCreditCode.js +10 -29
- package/lib/randomString.js +63 -0
- package/package.json +1 -1
- package/types/index.d.ts +1 -0
- package/types/isBankCard.d.ts +3 -1
- package/types/randomString.d.ts +19 -0
package/README.md
CHANGED
|
@@ -81,6 +81,7 @@ formatMoney('1000'); // => 1,000.00
|
|
|
81
81
|
- [validatePassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.validatePassword) - 验证密码
|
|
82
82
|
- 其他
|
|
83
83
|
- [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
|
|
84
|
+
- [randomString](https://doly-dev.github.io/util-helpers/module-Other.html#.randomString) - 随机字符串
|
|
84
85
|
- [waitTime](https://doly-dev.github.io/util-helpers/module-Other.html#.waitTime) - 等待时间返回 Promise
|
|
85
86
|
|
|
86
87
|
## 精选第三方工具库
|
package/dist/util-helpers.js
CHANGED
|
@@ -151,7 +151,7 @@
|
|
|
151
151
|
if (num < 10) {
|
|
152
152
|
return num === Number(id[17]);
|
|
153
153
|
} else {
|
|
154
|
-
return id[17]
|
|
154
|
+
return id[17].toUpperCase() === 'X';
|
|
155
155
|
}
|
|
156
156
|
}
|
|
157
157
|
|
|
@@ -316,6 +316,24 @@
|
|
|
316
316
|
// 8~30位数字
|
|
317
317
|
const regLoose = /^\d{8,30}$/;
|
|
318
318
|
|
|
319
|
+
/**
|
|
320
|
+
* luhn 计算校验位
|
|
321
|
+
*
|
|
322
|
+
* @param {string} num 银行卡前面数字
|
|
323
|
+
* @returns
|
|
324
|
+
*/
|
|
325
|
+
function sumCheckCode$2(num) {
|
|
326
|
+
const numArr = (num + '').replace(/\D/g, '').split('').reverse();
|
|
327
|
+
|
|
328
|
+
let sum = 0;
|
|
329
|
+
for (let i = 0; i < numArr.length; i++) {
|
|
330
|
+
const currNum = parseInt(numArr[i]);
|
|
331
|
+
sum += i % 2 === 0 ? currNum * 2 - (currNum > 4 ? 9 : 0) : currNum;
|
|
332
|
+
}
|
|
333
|
+
const mod = sum % 10;
|
|
334
|
+
return mod !== 0 ? 10 - mod : 0;
|
|
335
|
+
}
|
|
336
|
+
|
|
319
337
|
/**
|
|
320
338
|
* 检测值是否为银行卡号。正常模式(非0开头,10~21位数字)宽松模式(8~30位数字)
|
|
321
339
|
*
|
|
@@ -326,6 +344,7 @@
|
|
|
326
344
|
* @param {*} value 要检测的值
|
|
327
345
|
* @param {Object} [options] 配置项
|
|
328
346
|
* @param {boolean} [options.loose=false] 宽松模式,8~30位数字
|
|
347
|
+
* @param {boolean} [options.luhn=false] 使用 Luhn 算法校验校验码
|
|
329
348
|
* @returns {boolean} 值是否为银行卡号
|
|
330
349
|
* @example
|
|
331
350
|
*
|
|
@@ -342,13 +361,16 @@
|
|
|
342
361
|
* // => true
|
|
343
362
|
*
|
|
344
363
|
*/
|
|
345
|
-
function isBankCard(value, { loose = false } = {}) {
|
|
364
|
+
function isBankCard(value, { loose = false, luhn = false } = {}) {
|
|
346
365
|
const valueStr = convertToString(value);
|
|
366
|
+
const validateResult = loose ? regLoose.test(valueStr) : reg$5.test(valueStr);
|
|
347
367
|
|
|
348
|
-
if (
|
|
349
|
-
|
|
368
|
+
if (validateResult && luhn) {
|
|
369
|
+
const precode = valueStr.substring(0, valueStr.length - 1);
|
|
370
|
+
const checkCode = valueStr[valueStr.length - 1];
|
|
371
|
+
return checkCode === String(sumCheckCode$2(precode));
|
|
350
372
|
}
|
|
351
|
-
return
|
|
373
|
+
return validateResult;
|
|
352
374
|
}
|
|
353
375
|
|
|
354
376
|
// 基础规则,由18位数字和大写字母组成,不使用I、O、Z、S、V。
|
|
@@ -357,31 +379,6 @@
|
|
|
357
379
|
// 基础字符组成
|
|
358
380
|
const baseCodeArr = '0123456789ABCDEFGHJKLMNPQRTUWXY'.split('');
|
|
359
381
|
|
|
360
|
-
//加权因子
|
|
361
|
-
const weightFactor = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* 获取字符位置
|
|
365
|
-
*
|
|
366
|
-
* @private
|
|
367
|
-
* @param {string} code 字符
|
|
368
|
-
* @returns {number} 字符所在基础字符的位置
|
|
369
|
-
*/
|
|
370
|
-
function getBaseCodeIndex(code) {
|
|
371
|
-
let ret;
|
|
372
|
-
|
|
373
|
-
baseCodeArr.some((item, index) => {
|
|
374
|
-
if (item === code) {
|
|
375
|
-
ret = index;
|
|
376
|
-
return true;
|
|
377
|
-
}
|
|
378
|
-
return false;
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
// @ts-ignore
|
|
382
|
-
return ret;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
382
|
/**
|
|
386
383
|
* 计算校验码
|
|
387
384
|
*
|
|
@@ -391,16 +388,14 @@
|
|
|
391
388
|
* @returns {string} 校验码
|
|
392
389
|
*/
|
|
393
390
|
function sumCheckCode$1(preCode) {
|
|
394
|
-
// const preCodeArr = preCode.split('');
|
|
395
|
-
|
|
396
391
|
let total = 0;
|
|
397
392
|
|
|
398
393
|
// 计算字符位置对应序号和加权因子的乘积,总和
|
|
399
394
|
for (let i = 0; i < 17; i++) {
|
|
400
395
|
// 字符位置对应的基础编码序号
|
|
401
|
-
const index =
|
|
396
|
+
const index = baseCodeArr.findIndex((item) => item === preCode[i]);
|
|
402
397
|
// 加权因子
|
|
403
|
-
const wf =
|
|
398
|
+
const wf = Math.pow(3, i) % 31;
|
|
404
399
|
// 计算序号和加权因子的乘积,并计算级数之和
|
|
405
400
|
total += index * wf;
|
|
406
401
|
}
|
|
@@ -2599,6 +2594,49 @@
|
|
|
2599
2594
|
return pos;
|
|
2600
2595
|
}
|
|
2601
2596
|
|
|
2597
|
+
const numberChars = '0123456789';
|
|
2598
|
+
const letterChars = 'abcdefghijklmnopqrstuvwxyz';
|
|
2599
|
+
const defaultChars = numberChars + letterChars + letterChars.toUpperCase();
|
|
2600
|
+
|
|
2601
|
+
/**
|
|
2602
|
+
* @private
|
|
2603
|
+
* @param {number} [len=0] 长度
|
|
2604
|
+
* @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
|
|
2605
|
+
* @param {string} [prefix=''] 前缀部分,不计入长度
|
|
2606
|
+
* @returns {string}
|
|
2607
|
+
*/
|
|
2608
|
+
function internalRandomString(len = 0, optionalChars = defaultChars, prefix = '') {
|
|
2609
|
+
while (len-- > 0) {
|
|
2610
|
+
const r = optionalChars[Math.floor(Math.random() * optionalChars.length)];
|
|
2611
|
+
return internalRandomString(len, optionalChars, prefix + r);
|
|
2612
|
+
}
|
|
2613
|
+
return prefix;
|
|
2614
|
+
}
|
|
2615
|
+
|
|
2616
|
+
/**
|
|
2617
|
+
* 生成随机字符串
|
|
2618
|
+
*
|
|
2619
|
+
* @static
|
|
2620
|
+
* @alias module:Other.randomString
|
|
2621
|
+
* @since 4.8.0
|
|
2622
|
+
* @param {number} [len=0] 长度
|
|
2623
|
+
* @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
|
|
2624
|
+
* @returns {string} 随机字符串
|
|
2625
|
+
* @example
|
|
2626
|
+
* randomString(5); // slk23
|
|
2627
|
+
* randomString(8); // 71mHqo2A
|
|
2628
|
+
*
|
|
2629
|
+
* // 自定义允许的字符
|
|
2630
|
+
* randomString(5, 'abc'); // ccbcb
|
|
2631
|
+
* randomString(8, 'abcefg'); // bcgcfabg
|
|
2632
|
+
*/
|
|
2633
|
+
function randomString(len = 0, optionalChars) {
|
|
2634
|
+
|
|
2635
|
+
const realChars = typeof optionalChars === 'string' && optionalChars ? optionalChars : defaultChars;
|
|
2636
|
+
|
|
2637
|
+
return internalRandomString(len, realChars);
|
|
2638
|
+
}
|
|
2639
|
+
|
|
2602
2640
|
exports.blobToDataURL = blobToDataURL;
|
|
2603
2641
|
exports.bytesToSize = bytesToSize;
|
|
2604
2642
|
exports.calculateCursorPosition = calculateCursorPosition;
|
|
@@ -2633,6 +2671,7 @@
|
|
|
2633
2671
|
exports.padZero = padZero;
|
|
2634
2672
|
exports.parseIdCard = parseIdCard;
|
|
2635
2673
|
exports.plus = plus;
|
|
2674
|
+
exports.randomString = randomString;
|
|
2636
2675
|
exports.replaceChar = replaceChar;
|
|
2637
2676
|
exports.round = round;
|
|
2638
2677
|
exports.safeDate = safeDate;
|