util-helpers 4.8.1 → 4.9.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # util-helpers
2
2
 
3
- [![npm][npm]][npm-url] ![GitHub](https://img.shields.io/github/license/doly-dev/util-helpers.svg) [![Build Status](https://travis-ci.org/doly-dev/util-helpers.svg?branch=master)](https://travis-ci.org/doly-dev/util-helpers)
3
+ [![npm][npm]][npm-url] ![GitHub](https://img.shields.io/github/license/doly-dev/util-helpers.svg)
4
4
 
5
5
  [util-helpers](https://doly-dev.github.io/util-helpers/index.html) 是一个基于业务场景的工具方法库。
6
6
 
@@ -50,7 +50,7 @@
50
50
  }
51
51
 
52
52
  // 手机号码 11位数字,以1开头,第二位是3456789其中一个,后面再加9个数字
53
- const reg$c = /^1[3456789]\d{9}$/;
53
+ const reg$d = /^1[3456789]\d{9}$/;
54
54
 
55
55
  /**
56
56
  * 检测值是否为手机号码
@@ -71,11 +71,11 @@
71
71
  */
72
72
  function isMobile(value) {
73
73
  const valueStr = convertToString(value);
74
- return reg$c.test(valueStr);
74
+ return reg$d.test(valueStr);
75
75
  }
76
76
 
77
77
  // 固定电话 支持区号和分机号 3~4位区号,以0开头;7~8位直播号,以2~9开头;1~6位分机号
78
- const reg$b = /^(0\d{2,3}\-)?([2-9]\d{6,7})(\-\d{1,6})?$/;
78
+ const reg$c = /^(0\d{2,3}\-)?([2-9]\d{6,7})(\-\d{1,6})?$/;
79
79
 
80
80
  /**
81
81
  * 检测值是否为固定电话
@@ -102,11 +102,11 @@
102
102
  */
103
103
  function isTelephone(value) {
104
104
  const valueStr = convertToString(value);
105
- return reg$b.test(valueStr);
105
+ return reg$c.test(valueStr);
106
106
  }
107
107
 
108
108
  // 邮政编码
109
- const reg$a = /^\d{6}$/;
109
+ const reg$b = /^\d{6}$/;
110
110
 
111
111
  /**
112
112
  * 检测值是否为邮政编码,6位数字
@@ -127,7 +127,7 @@
127
127
  */
128
128
  function isPostcode(value) {
129
129
  const valueStr = convertToString(value);
130
- return reg$a.test(valueStr);
130
+ return reg$b.test(valueStr);
131
131
  }
132
132
 
133
133
  // 身份证号正则
@@ -201,7 +201,7 @@
201
201
  }
202
202
 
203
203
  // 邮箱
204
- const reg$9 = /^[\da-z]+([\-\.\_]?[\da-z]+)*@[\da-z]+([\-\.]?[\da-z]+)*(\.[a-z]{2,})+$/i;
204
+ const reg$a = /^[\da-z]+([\-\.\_]?[\da-z]+)*@[\da-z]+([\-\.]?[\da-z]+)*(\.[a-z]{2,})+$/i;
205
205
 
206
206
  /**
207
207
  * 检测值是否为Email
@@ -222,11 +222,11 @@
222
222
  */
223
223
  function isEmail(value) {
224
224
  const valueStr = convertToString(value);
225
- return reg$9.test(valueStr);
225
+ return reg$a.test(valueStr);
226
226
  }
227
227
 
228
228
  // QQ号正则
229
- const reg$8 = /^[1-9]\d{4,10}$/;
229
+ const reg$9 = /^[1-9]\d{4,10}$/;
230
230
 
231
231
  /**
232
232
  * 检测值是否为QQ号,非0开头,5至11位数字
@@ -247,11 +247,11 @@
247
247
  */
248
248
  function isQQ(value) {
249
249
  const valueStr = convertToString(value);
250
- return reg$8.test(valueStr);
250
+ return reg$9.test(valueStr);
251
251
  }
252
252
 
253
253
  // 微信号 6至20位,以字母开头,字母,数字,减号(连接符),下划线
254
- const reg$7 = /^[a-z]([-_a-z0-9]{5,19})+$/i;
254
+ const reg$8 = /^[a-z]([-_a-z0-9]{5,19})+$/i;
255
255
 
256
256
  /**
257
257
  * 检测值是否为微信号
@@ -272,11 +272,11 @@
272
272
  */
273
273
  function isWX(value) {
274
274
  const valueStr = convertToString(value);
275
- return reg$7.test(valueStr);
275
+ return reg$8.test(valueStr);
276
276
  }
277
277
 
278
278
  // 车牌号正则
279
- const reg$6 = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})|([A-Z0-9]{5}[DF])|([DF][A-Z0-9]{5}))$/;
279
+ 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}))$/;
280
280
 
281
281
  /**
282
282
  * 检测值是否为车牌号,支持新能源和非新能源车牌
@@ -307,23 +307,23 @@
307
307
  */
308
308
  function isVehicle(value) {
309
309
  const valueStr = convertToString(value);
310
- return reg$6.test(valueStr);
310
+ return reg$7.test(valueStr);
311
311
  }
312
312
 
313
313
  // 非0开头,10~21位数字
314
- const reg$5 = /^[1-9]\d{9,20}$/;
314
+ const reg$6 = /^[1-9]\d{9,20}$/;
315
315
 
316
316
  // 8~30位数字
317
317
  const regLoose = /^\d{8,30}$/;
318
318
 
319
319
  /**
320
320
  * luhn 计算校验位
321
- *
322
- * @param {string} num 银行卡前面数字
323
- * @returns
321
+ * @private
322
+ * @param {string} numStr 银行卡前面数字
323
+ * @returns {number}
324
324
  */
325
- function sumCheckCode$2(num) {
326
- const numArr = (num + '').replace(/\D/g, '').split('').reverse();
325
+ function sumCheckCode$2(numStr) {
326
+ const numArr = (numStr + '').replace(/\D/g, '').split('').reverse();
327
327
 
328
328
  let sum = 0;
329
329
  for (let i = 0; i < numArr.length; i++) {
@@ -363,7 +363,7 @@
363
363
  */
364
364
  function isBankCard(value, { loose = false, luhn = false } = {}) {
365
365
  const valueStr = convertToString(value);
366
- const validateResult = loose ? regLoose.test(valueStr) : reg$5.test(valueStr);
366
+ const validateResult = loose ? regLoose.test(valueStr) : reg$6.test(valueStr);
367
367
 
368
368
  if (validateResult && luhn) {
369
369
  const precode = valueStr.substring(0, valueStr.length - 1);
@@ -403,7 +403,7 @@
403
403
  // 计算整数求余函数MOD
404
404
  const remainder = total % 31;
405
405
  // 校验码字符值序号
406
- const checkCodeIndex = 31 - remainder;
406
+ const checkCodeIndex = remainder !== 0 ? 31 - remainder : 0;
407
407
 
408
408
  return baseCodeArr[checkCodeIndex];
409
409
  }
@@ -771,7 +771,7 @@
771
771
  }
772
772
 
773
773
  // 护照号 9位,包括首字母和数字;支持 普通护照(E*)、外交护照(DE)、公务护照(SE)、公务普通护照(PE)、香港特区护照(K/KJ)、澳门特区护照(MA)
774
- const reg$4 = /^((e[\da-z])|(de)|(se)|(pe)|(k[\da-z])|(kj)|(ma))[\da-z]{7}$/i;
774
+ const reg$5 = /^((e[\da-z])|(de)|(se)|(pe)|(k[\da-z])|(kj)|(ma))[\da-z]{7}$/i;
775
775
 
776
776
  /**
777
777
  * 检测值是否为护照号
@@ -794,7 +794,7 @@
794
794
  */
795
795
  function isPassport(value) {
796
796
  const valueStr = convertToString(value);
797
- return reg$4.test(valueStr);
797
+ return reg$5.test(valueStr);
798
798
  }
799
799
 
800
800
  const chineseDictionary = {
@@ -869,7 +869,7 @@
869
869
  }
870
870
 
871
871
  // ipv4正则
872
- const reg$3 = /^((\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])$/;
872
+ 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])$/;
873
873
 
874
874
  /**
875
875
  * 检测值是否为ipv4
@@ -896,11 +896,11 @@
896
896
  */
897
897
  function isIPv4(value) {
898
898
  const valueStr = convertToString(value);
899
- return reg$3.test(valueStr);
899
+ return reg$4.test(valueStr);
900
900
  }
901
901
 
902
902
  // ipv6正则
903
- const reg$2 = /^((([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;
903
+ 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;
904
904
 
905
905
  /**
906
906
  * 检测值是否为ipv6
@@ -952,11 +952,11 @@
952
952
  */
953
953
  function isIPv6(value) {
954
954
  const valueStr = convertToString(value);
955
- return reg$2.test(valueStr);
955
+ return reg$3.test(valueStr);
956
956
  }
957
957
 
958
958
  // url正则
959
- const reg$1 = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
959
+ const reg$2 = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\*\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
960
960
 
961
961
  /**
962
962
  * 检测值是否为url
@@ -992,7 +992,7 @@
992
992
  */
993
993
  function isUrl(value) {
994
994
  const valueStr = convertToString(value);
995
- return reg$1.test(valueStr);
995
+ return reg$2.test(valueStr);
996
996
  }
997
997
 
998
998
  // 基础规则,由14位数字本体码和1位数字校验码组成,其中本体码从左至右依次为:6位首次登记机关码、8位顺序码。
@@ -1154,6 +1154,23 @@
1154
1154
  return regTWCard.test(valueStr);
1155
1155
  }
1156
1156
 
1157
+ const reg$1 = /^[A-Z]{6}[A-Z\d]{2}(?:[A-Z\d]{3})?$/;
1158
+
1159
+ /**
1160
+ * 检测值是否为 Swift Code。8位或11位,前6位为大写字母,7-8位为大写字母或数字,9-11位为可选的大写字母或数字。
1161
+ *
1162
+ * @static
1163
+ * @alias module:Validator.isSwiftCode
1164
+ * @see 参考 {@link https://zh.wikipedia.org/wiki/ISO_9362|ISO 9362}
1165
+ * @since 4.9.0
1166
+ * @param {*} value 要检测的值
1167
+ * @returns {boolean} 值是否为 Swift Code
1168
+ */
1169
+ function isSwiftCode(value) {
1170
+ const valueStr = convertToString(value);
1171
+ return reg$1.test(valueStr);
1172
+ }
1173
+
1157
1174
  // 最大安全数字
1158
1175
  const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
1159
1176
  // 最小安全数字
@@ -2660,6 +2677,7 @@
2660
2677
  exports.isPromiseLike = isPromiseLike;
2661
2678
  exports.isQQ = isQQ;
2662
2679
  exports.isSocialCreditCode = isSocialCreditCode;
2680
+ exports.isSwiftCode = isSwiftCode;
2663
2681
  exports.isTWCard = isTWCard;
2664
2682
  exports.isTelephone = isTelephone;
2665
2683
  exports.isUrl = isUrl;