util-helpers 4.9.0 → 4.10.2

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.
Files changed (56) hide show
  1. package/README.md +2 -0
  2. package/dist/util-helpers.js +163 -111
  3. package/dist/util-helpers.js.map +1 -1
  4. package/dist/util-helpers.min.js +1 -1
  5. package/dist/util-helpers.min.js.map +1 -1
  6. package/esm/index.js +1 -0
  7. package/esm/isBankCard.js +2 -2
  8. package/esm/isBusinessLicense.js +2 -2
  9. package/esm/isChinese.js +2 -2
  10. package/esm/isEmail.js +2 -2
  11. package/esm/isHMCard.js +2 -2
  12. package/esm/isIPv4.js +2 -2
  13. package/esm/isIPv6.js +2 -2
  14. package/esm/isIdCard.js +2 -2
  15. package/esm/isMobile.js +2 -2
  16. package/esm/isPassport.js +4 -4
  17. package/esm/isPostcode.js +2 -2
  18. package/esm/isQQ.js +2 -2
  19. package/esm/isSocialCreditCode.js +2 -2
  20. package/esm/isSwiftCode.js +12 -2
  21. package/esm/isTWCard.js +19 -5
  22. package/esm/isTelephone.js +2 -2
  23. package/esm/isUrl.js +2 -2
  24. package/esm/isVehicle.js +2 -2
  25. package/esm/isWX.js +2 -2
  26. package/esm/randomString.js +1 -1
  27. package/esm/strlen.js +38 -0
  28. package/lib/index.js +8 -0
  29. package/lib/isBankCard.js +2 -2
  30. package/lib/isBusinessLicense.js +2 -2
  31. package/lib/isChinese.js +2 -2
  32. package/lib/isEmail.js +2 -2
  33. package/lib/isHMCard.js +2 -2
  34. package/lib/isIPv4.js +2 -2
  35. package/lib/isIPv6.js +2 -2
  36. package/lib/isIdCard.js +2 -2
  37. package/lib/isMobile.js +2 -2
  38. package/lib/isPassport.js +5 -5
  39. package/lib/isPostcode.js +2 -2
  40. package/lib/isQQ.js +2 -2
  41. package/lib/isSocialCreditCode.js +2 -2
  42. package/lib/isSwiftCode.js +12 -2
  43. package/lib/isTWCard.js +20 -6
  44. package/lib/isTelephone.js +2 -2
  45. package/lib/isUrl.js +2 -2
  46. package/lib/isVehicle.js +2 -2
  47. package/lib/isWX.js +2 -2
  48. package/lib/randomString.js +1 -1
  49. package/lib/strlen.js +48 -0
  50. package/package.json +1 -1
  51. package/types/index.d.ts +1 -0
  52. package/types/isPassport.d.ts +1 -1
  53. package/types/isSwiftCode.d.ts +10 -0
  54. package/types/isTWCard.d.ts +7 -2
  55. package/types/randomString.d.ts +1 -1
  56. package/types/strlen.d.ts +20 -0
package/README.md CHANGED
@@ -70,6 +70,7 @@ formatMoney('1000'); // => 1,000.00
70
70
  - [isVehicle](https://doly-dev.github.io/util-helpers/module-Validator.html#.isVehicle) - 车牌号
71
71
  - [isBankCard](https://doly-dev.github.io/util-helpers/module-Validator.html#.isBankCard) - 银行卡
72
72
  - [isSocialCreditCode](https://doly-dev.github.io/util-helpers/module-Validator.html#.isSocialCreditCode) - 统一社会信用代码,也叫三证合一组织代码
73
+ - [isSwiftCode](https://doly-dev.github.io/util-helpers/module-Validator.html#.isSwiftCode) - Swift Code
73
74
  - [isPassword](https://doly-dev.github.io/util-helpers/module-Validator.html#.isPassword) 密码强度
74
75
  - [isPassport](https://doly-dev.github.io/util-helpers/module-Validator.html#.isPassport) - 护照号
75
76
  - [isPromiseLike](https://doly-dev.github.io/util-helpers/module-Validator.html#.isPromiseLike) - 类似 Promise 对象
@@ -82,6 +83,7 @@ formatMoney('1000'); // => 1,000.00
82
83
  - 其他
83
84
  - [calculateCursorPosition](https://doly-dev.github.io/util-helpers/module-Other.html#.calculateCursorPosition) - 计算光标位置
84
85
  - [randomString](https://doly-dev.github.io/util-helpers/module-Other.html#.randomString) - 随机字符串
86
+ - [strlen](https://doly-dev.github.io/util-helpers/module-Other.html#.strlen) - 字符长度
85
87
  - [waitTime](https://doly-dev.github.io/util-helpers/module-Other.html#.waitTime) - 等待时间返回 Promise
86
88
 
87
89
  ## 精选第三方工具库
@@ -4,6 +4,62 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.utilHelpers = {}));
5
5
  }(this, (function (exports) { 'use strict';
6
6
 
7
+ /**
8
+ * 检查值是否为Undefined
9
+ *
10
+ * @static
11
+ * @alias module:Type.isUndefined
12
+ * @since 1.1.0
13
+ * @param {*} value 检查值
14
+ * @returns {boolean} 是否为Undefined
15
+ * @example
16
+ *
17
+ * isUndefined(undefined)
18
+ * // => true
19
+ *
20
+ * isUndefined(void 0)
21
+ * // => true
22
+ *
23
+ * isUndefined(null)
24
+ * // => false
25
+ */
26
+ function isUndefined(value) {
27
+ return value === void 0;
28
+ }
29
+
30
+ /**
31
+ * 检查值是否为Null
32
+ *
33
+ * @static
34
+ * @alias module:Type.isNull
35
+ * @since 1.1.0
36
+ * @param {*} value 检查值
37
+ * @returns {boolean} 是否为Null
38
+ * @example
39
+ *
40
+ * isNull(null)
41
+ * // => true
42
+ *
43
+ * isNull(void 0)
44
+ * // => false
45
+ */
46
+ function isNull(value) {
47
+ return value === null;
48
+ }
49
+
50
+ /**
51
+ * 检查值是否为 undefined 或 null
52
+ *
53
+ * @static
54
+ * @alias module:Type.isNaN
55
+ * @since 4.3.0
56
+ * @param {*} value 检查值
57
+ * @returns {boolean} 是否为 undefined 或 null
58
+ */
59
+ function isNil(value) {
60
+ return isUndefined(value) || isNull(value);
61
+ }
62
+
7
63
  const toString = Object.prototype.toString;
8
64
 
9
65
  /**
@@ -49,6 +105,33 @@
49
105
  return isString(value) ? value : String(value);
50
106
  }
51
107
 
108
+ /**
109
+ * 规整化字符串。如果值为 undefined 或 null 将转为空字符串,如果值不是字符串类型将转为字符串。
110
+ *
111
+ * @static
112
+ * @alias module:Processor.normalizeString
113
+ * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
114
+ * @since 4.3.0
115
+ * @param {*} value 待处理的值
116
+ * @returns {string} 规整化的值
117
+ * @example
118
+ * normalizeString(); // ''
119
+ * normalizeString(undefined); // ''
120
+ * normalizeString(void 0); // ''
121
+ * normalizeString(null); // ''
122
+ *
123
+ * normalizeString(true); // 'true'
124
+ * normalizeString(NaN); // 'NaN'
125
+ * normalizeString(1); // '1'
126
+ * normalizeString('a'); // 'a'
127
+ */
128
+ function normalizeString(value) {
129
+ if (isNil(value)) {
130
+ return '';
131
+ }
132
+ return convertToString(value);
133
+ }
134
+
52
135
  // 手机号码 11位数字,以1开头,第二位是3456789其中一个,后面再加9个数字
53
136
  const reg$d = /^1[3456789]\d{9}$/;
54
137
 
@@ -70,7 +153,7 @@
70
153
  *
71
154
  */
72
155
  function isMobile(value) {
73
- const valueStr = convertToString(value);
156
+ const valueStr = normalizeString(value);
74
157
  return reg$d.test(valueStr);
75
158
  }
76
159
 
@@ -101,7 +184,7 @@
101
184
  *
102
185
  */
103
186
  function isTelephone(value) {
104
- const valueStr = convertToString(value);
187
+ const valueStr = normalizeString(value);
105
188
  return reg$c.test(valueStr);
106
189
  }
107
190
 
@@ -126,7 +209,7 @@
126
209
  *
127
210
  */
128
211
  function isPostcode(value) {
129
- const valueStr = convertToString(value);
212
+ const valueStr = normalizeString(value);
130
213
  return reg$b.test(valueStr);
131
214
  }
132
215
 
@@ -185,7 +268,7 @@
185
268
  *
186
269
  */
187
270
  function isIdCard(value, { loose = false, checkCode = true } = {}) {
188
- const valueStr = convertToString(value);
271
+ const valueStr = normalizeString(value);
189
272
  if (valueStr.length === 15 && loose) {
190
273
  return regIdCard$1.test(valueStr);
191
274
  }
@@ -221,7 +304,7 @@
221
304
  *
222
305
  */
223
306
  function isEmail(value) {
224
- const valueStr = convertToString(value);
307
+ const valueStr = normalizeString(value);
225
308
  return reg$a.test(valueStr);
226
309
  }
227
310
 
@@ -246,7 +329,7 @@
246
329
  *
247
330
  */
248
331
  function isQQ(value) {
249
- const valueStr = convertToString(value);
332
+ const valueStr = normalizeString(value);
250
333
  return reg$9.test(valueStr);
251
334
  }
252
335
 
@@ -271,7 +354,7 @@
271
354
  *
272
355
  */
273
356
  function isWX(value) {
274
- const valueStr = convertToString(value);
357
+ const valueStr = normalizeString(value);
275
358
  return reg$8.test(valueStr);
276
359
  }
277
360
 
@@ -306,7 +389,7 @@
306
389
  *
307
390
  */
308
391
  function isVehicle(value) {
309
- const valueStr = convertToString(value);
392
+ const valueStr = normalizeString(value);
310
393
  return reg$7.test(valueStr);
311
394
  }
312
395
 
@@ -362,7 +445,7 @@
362
445
  *
363
446
  */
364
447
  function isBankCard(value, { loose = false, luhn = false } = {}) {
365
- const valueStr = convertToString(value);
448
+ const valueStr = normalizeString(value);
366
449
  const validateResult = loose ? regLoose.test(valueStr) : reg$6.test(valueStr);
367
450
 
368
451
  if (validateResult && luhn) {
@@ -433,7 +516,7 @@
433
516
  *
434
517
  */
435
518
  function isSocialCreditCode(value, { loose = false } = {}) {
436
- const valueStr = convertToString(value);
519
+ const valueStr = normalizeString(value);
437
520
 
438
521
  const passBaseRule = baseReg$1.test(valueStr);
439
522
 
@@ -770,12 +853,12 @@
770
853
  return validatePassword(value, { level, ignoreCase, special }).validated;
771
854
  }
772
855
 
773
- // 护照号 9位,包括首字母和数字;支持 普通护照(E*)、外交护照(DE)、公务护照(SE)、公务普通护照(PE)、香港特区护照(K/KJ)、澳门特区护照(MA)
774
- const reg$5 = /^((e[\da-z])|(de)|(se)|(pe)|(k[\da-z])|(kj)|(ma))[\da-z]{7}$/i;
856
+ // 护照号 9位,包括首字母和数字;支持 普通护照(E*)、外交护照(DE)、公务护照(SE)、公务普通护照(PE)、香港特区护照(K/KJ/H*)、澳门特区护照(MA/MB/M*)
857
+ const reg$5 = /^((e[\da-z])|(de)|(se)|(pe)|([khm][\da-z]))[\da-z]{7}$/i;
775
858
 
776
859
  /**
777
860
  * 检测值是否为护照号
778
- * 支持普通护照(E*)、外交护照(DE)、公务护照(SE)、公务普通护照(PE)、香港特区护照(K/KJ)、澳门特区护照(MA)
861
+ * 支持普通护照(E*)、外交护照(DE)、公务护照(SE)、公务普通护照(PE)、香港特区护照(K/KJ/H*)、澳门特区护照(MA/MB/M*),注意不区分大小写
779
862
  *
780
863
  * @static
781
864
  * @alias module:Validator.isPassport
@@ -793,7 +876,7 @@
793
876
  *
794
877
  */
795
878
  function isPassport(value) {
796
- const valueStr = convertToString(value);
879
+ const valueStr = normalizeString(value);
797
880
  return reg$5.test(valueStr);
798
881
  }
799
882
 
@@ -863,7 +946,7 @@
863
946
  *
864
947
  */
865
948
  function isChinese(value, { loose = false } = {}) {
866
- const valueStr = convertToString(value);
949
+ const valueStr = normalizeString(value);
867
950
  const reg = new RegExp(loose ? looseChineseRegExp : chineseRegExp, supportRegExpUnicode ? 'u' : undefined);
868
951
  return reg.test(valueStr);
869
952
  }
@@ -895,7 +978,7 @@
895
978
  *
896
979
  */
897
980
  function isIPv4(value) {
898
- const valueStr = convertToString(value);
981
+ const valueStr = normalizeString(value);
899
982
  return reg$4.test(valueStr);
900
983
  }
901
984
 
@@ -951,7 +1034,7 @@
951
1034
  *
952
1035
  */
953
1036
  function isIPv6(value) {
954
- const valueStr = convertToString(value);
1037
+ const valueStr = normalizeString(value);
955
1038
  return reg$3.test(valueStr);
956
1039
  }
957
1040
 
@@ -991,7 +1074,7 @@
991
1074
  * // => true
992
1075
  */
993
1076
  function isUrl(value) {
994
- const valueStr = convertToString(value);
1077
+ const valueStr = normalizeString(value);
995
1078
  return reg$2.test(valueStr);
996
1079
  }
997
1080
 
@@ -1058,7 +1141,7 @@
1058
1141
  * // => true
1059
1142
  */
1060
1143
  function isBusinessLicense(value, { loose = false } = {}) {
1061
- const valueStr = convertToString(value);
1144
+ const valueStr = normalizeString(value);
1062
1145
 
1063
1146
  const passBaseRule = baseReg.test(valueStr);
1064
1147
 
@@ -1126,12 +1209,15 @@
1126
1209
  * isHMCard('M32031177') // true
1127
1210
  */
1128
1211
  function isHMCard(value) {
1129
- const valueStr = convertToString(value);
1212
+ const valueStr = normalizeString(value);
1130
1213
  return regHMCard.test(valueStr);
1131
1214
  }
1132
1215
 
1133
- // 台湾居民来往大陆通行证正则,支持一次性短期台胞证
1134
- const regTWCard = /^(\d{8}|[\da-z]{10})$/i;
1216
+ // 台湾居民来往大陆通行证正则
1217
+ const regTWCard = /^\d{8}$/i;
1218
+
1219
+ // 一次性短期台胞证
1220
+ const singleRegTWCard = /^[\da-z]{10,12}$/i;
1135
1221
 
1136
1222
  /**
1137
1223
  * 检测值是否为台湾居民来往大陆通行证,俗称台胞证。
@@ -1141,17 +1227,23 @@
1141
1227
  * @since 4.0.0
1142
1228
  * @see 参考 {@link https://zh.wikipedia.org/wiki/台湾居民来往大陆通行证|台湾居民来往大陆通行证}
1143
1229
  * @param {*} value 要检测的值
1230
+ * @param {Object} [options] 配置项
1231
+ * @param {boolean} [options.loose=false] 宽松模式。如果为true,表示支持一次性短期通行证
1144
1232
  * @returns {boolean} 是否为台湾居民来往大陆通行证
1145
1233
  * @example
1146
1234
  * isTWCard('12345678') // true
1147
1235
  * isTWCard('07257456') // true
1148
1236
  *
1149
1237
  * // 一次性短期
1150
- * isTWCard('F290299977') // true
1238
+ * isTWCard('F290299977') // false
1239
+ * isTWCard('F290299977', { loose: true }) // true
1151
1240
  */
1152
- function isTWCard(value) {
1153
- const valueStr = convertToString(value);
1154
- return regTWCard.test(valueStr);
1241
+ function isTWCard(value, { loose = false } = {}) {
1242
+ const valueStr = normalizeString(value);
1243
+ if (regTWCard.test(valueStr)) {
1244
+ return true;
1245
+ }
1246
+ return loose ? singleRegTWCard.test(valueStr) : false;
1155
1247
  }
1156
1248
 
1157
1249
  const reg$1 = /^[A-Z]{6}[A-Z\d]{2}(?:[A-Z\d]{3})?$/;
@@ -1165,9 +1257,19 @@
1165
1257
  * @since 4.9.0
1166
1258
  * @param {*} value 要检测的值
1167
1259
  * @returns {boolean} 值是否为 Swift Code
1260
+ * @example
1261
+ *
1262
+ * isSwiftCode('DEUTDEFF') // true
1263
+ * isSwiftCode('deutdeff') // false
1264
+ *
1265
+ * isSwiftCode('BKTWTWTP010') // true
1266
+ * isSwiftCode('010BKTWTWTP') // false
1267
+ *
1268
+ * isSwiftCode('ICBKCNBJBJM') // true
1269
+ *
1168
1270
  */
1169
1271
  function isSwiftCode(value) {
1170
- const valueStr = convertToString(value);
1272
+ const valueStr = normalizeString(value);
1171
1273
  return reg$1.test(valueStr);
1172
1274
  }
1173
1275
 
@@ -1472,89 +1574,6 @@
1472
1574
  return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
1473
1575
  };
1474
1576
 
1475
- /**
1476
- * 检查值是否为Undefined
1477
- *
1478
- * @static
1479
- * @alias module:Type.isUndefined
1480
- * @since 1.1.0
1481
- * @param {*} value 检查值
1482
- * @returns {boolean} 是否为Undefined
1483
- * @example
1484
- *
1485
- * isUndefined(undefined)
1486
- * // => true
1487
- *
1488
- * isUndefined(void 0)
1489
- * // => true
1490
- *
1491
- * isUndefined(null)
1492
- * // => false
1493
- */
1494
- function isUndefined(value) {
1495
- return value === void 0;
1496
- }
1497
-
1498
- /**
1499
- * 检查值是否为Null
1500
- *
1501
- * @static
1502
- * @alias module:Type.isNull
1503
- * @since 1.1.0
1504
- * @param {*} value 检查值
1505
- * @returns {boolean} 是否为Null
1506
- * @example
1507
- *
1508
- * isNull(null)
1509
- * // => true
1510
- *
1511
- * isNull(void 0)
1512
- * // => false
1513
- */
1514
- function isNull(value) {
1515
- return value === null;
1516
- }
1517
-
1518
- /**
1519
- * 检查值是否为 undefined 或 null
1520
- *
1521
- * @static
1522
- * @alias module:Type.isNaN
1523
- * @since 4.3.0
1524
- * @param {*} value 检查值
1525
- * @returns {boolean} 是否为 undefined 或 null
1526
- */
1527
- function isNil(value) {
1528
- return isUndefined(value) || isNull(value);
1529
- }
1530
-
1531
- /**
1532
- * 规整化字符串。如果值为 undefined 或 null 将转为空字符串,如果值不是字符串类型将转为字符串。
1533
- *
1534
- * @static
1535
- * @alias module:Processor.normalizeString
1536
- * @see 参考 {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String#string_instances|String}
1537
- * @since 4.3.0
1538
- * @param {*} value 待处理的值
1539
- * @returns {string} 规整化的值
1540
- * @example
1541
- * normalizeString(); // ''
1542
- * normalizeString(undefined); // ''
1543
- * normalizeString(void 0); // ''
1544
- * normalizeString(null); // ''
1545
- *
1546
- * normalizeString(true); // 'true'
1547
- * normalizeString(NaN); // 'NaN'
1548
- * normalizeString(1); // '1'
1549
- * normalizeString('a'); // 'a'
1550
- */
1551
- function normalizeString(value) {
1552
- if (isNil(value)) {
1553
- return '';
1554
- }
1555
- return convertToString(value);
1556
- }
1557
-
1558
1577
  /**
1559
1578
  * 格式化银行卡号
1560
1579
  *
@@ -2637,7 +2656,7 @@
2637
2656
  * @alias module:Other.randomString
2638
2657
  * @since 4.8.0
2639
2658
  * @param {number} [len=0] 长度
2640
- * @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
2659
+ * @param {string} [optionalChars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'] 允许的字符,默认为数字和大小写字母
2641
2660
  * @returns {string} 随机字符串
2642
2661
  * @example
2643
2662
  * randomString(5); // slk23
@@ -2654,6 +2673,38 @@
2654
2673
  return internalRandomString(len, realChars);
2655
2674
  }
2656
2675
 
2676
+ /**
2677
+ * 获取字符长度。中文汉字占2个字符,英文占1个字符,特殊如emoji占4个字符。
2678
+ *
2679
+ * @static
2680
+ * @alias module:Other.strlen
2681
+ * @since 4.10.0
2682
+ * @param {string} str 字符串
2683
+ * @returns {number} 字符长度
2684
+ * @example
2685
+ *
2686
+ * strlen('你好a'); // 5
2687
+ * strlen('你好,世界!'); // 12
2688
+ * strlen('严両丞丽'); // 8
2689
+ * strlen('abcde'); // 5
2690
+ * strlen('𠮷'); // 4
2691
+ * strlen('🍎'); // 4
2692
+ *
2693
+ */
2694
+ function strlen(str) {
2695
+ const realStr = normalizeString(str);
2696
+ let len = 0;
2697
+ for (let i = 0; i < realStr.length; i++) {
2698
+ const c = realStr.charCodeAt(i);
2699
+ if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
2700
+ len++;
2701
+ } else {
2702
+ len += 2;
2703
+ }
2704
+ }
2705
+ return len;
2706
+ }
2707
+
2657
2708
  exports.blobToDataURL = blobToDataURL;
2658
2709
  exports.bytesToSize = bytesToSize;
2659
2710
  exports.calculateCursorPosition = calculateCursorPosition;
@@ -2695,6 +2746,7 @@
2695
2746
  exports.safeDate = safeDate;
2696
2747
  exports.setDataURLPrefix = setDataURLPrefix;
2697
2748
  exports.setDisableWarning = setDisableWarning;
2749
+ exports.strlen = strlen;
2698
2750
  exports.times = times;
2699
2751
  exports.validatePassword = validatePassword;
2700
2752
  exports.waitTime = waitTime;