util-helpers 4.9.0 → 4.10.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.
Files changed (54) hide show
  1. package/README.md +2 -0
  2. package/dist/util-helpers.js +146 -103
  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 +2 -2
  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 +2 -2
  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 +2 -2
  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 +2 -2
  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/isSwiftCode.d.ts +10 -0
  53. package/types/randomString.d.ts +1 -1
  54. 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
 
@@ -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,7 +1209,7 @@
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
 
@@ -1150,7 +1233,7 @@
1150
1233
  * isTWCard('F290299977') // true
1151
1234
  */
1152
1235
  function isTWCard(value) {
1153
- const valueStr = convertToString(value);
1236
+ const valueStr = normalizeString(value);
1154
1237
  return regTWCard.test(valueStr);
1155
1238
  }
1156
1239
 
@@ -1165,9 +1248,19 @@
1165
1248
  * @since 4.9.0
1166
1249
  * @param {*} value 要检测的值
1167
1250
  * @returns {boolean} 值是否为 Swift Code
1251
+ * @example
1252
+ *
1253
+ * isSwiftCode('DEUTDEFF') // true
1254
+ * isSwiftCode('deutdeff') // false
1255
+ *
1256
+ * isSwiftCode('BKTWTWTP010') // true
1257
+ * isSwiftCode('010BKTWTWTP') // false
1258
+ *
1259
+ * isSwiftCode('ICBKCNBJBJM') // true
1260
+ *
1168
1261
  */
1169
1262
  function isSwiftCode(value) {
1170
- const valueStr = convertToString(value);
1263
+ const valueStr = normalizeString(value);
1171
1264
  return reg$1.test(valueStr);
1172
1265
  }
1173
1266
 
@@ -1472,89 +1565,6 @@
1472
1565
  return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
1473
1566
  };
1474
1567
 
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
1568
  /**
1559
1569
  * 格式化银行卡号
1560
1570
  *
@@ -2637,7 +2647,7 @@
2637
2647
  * @alias module:Other.randomString
2638
2648
  * @since 4.8.0
2639
2649
  * @param {number} [len=0] 长度
2640
- * @param {string} [optionalChars] 允许的字符,默认为数字和大小写字母
2650
+ * @param {string} [optionalChars='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'] 允许的字符,默认为数字和大小写字母
2641
2651
  * @returns {string} 随机字符串
2642
2652
  * @example
2643
2653
  * randomString(5); // slk23
@@ -2654,6 +2664,38 @@
2654
2664
  return internalRandomString(len, realChars);
2655
2665
  }
2656
2666
 
2667
+ /**
2668
+ * 获取字符长度。中文汉字占2个字符,英文占1个字符,特殊如emoji占4个字符。
2669
+ *
2670
+ * @static
2671
+ * @alias module:Other.strlen
2672
+ * @since 4.10.0
2673
+ * @param {string} str 字符串
2674
+ * @returns {number} 字符长度
2675
+ * @example
2676
+ *
2677
+ * strlen('你好a'); // 5
2678
+ * strlen('你好,世界!'); // 12
2679
+ * strlen('严両丞丽'); // 8
2680
+ * strlen('abcde'); // 5
2681
+ * strlen('𠮷'); // 4
2682
+ * strlen('🍎'); // 4
2683
+ *
2684
+ */
2685
+ function strlen(str) {
2686
+ const realStr = normalizeString(str);
2687
+ let len = 0;
2688
+ for (let i = 0; i < realStr.length; i++) {
2689
+ const c = realStr.charCodeAt(i);
2690
+ if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
2691
+ len++;
2692
+ } else {
2693
+ len += 2;
2694
+ }
2695
+ }
2696
+ return len;
2697
+ }
2698
+
2657
2699
  exports.blobToDataURL = blobToDataURL;
2658
2700
  exports.bytesToSize = bytesToSize;
2659
2701
  exports.calculateCursorPosition = calculateCursorPosition;
@@ -2695,6 +2737,7 @@
2695
2737
  exports.safeDate = safeDate;
2696
2738
  exports.setDataURLPrefix = setDataURLPrefix;
2697
2739
  exports.setDisableWarning = setDisableWarning;
2740
+ exports.strlen = strlen;
2698
2741
  exports.times = times;
2699
2742
  exports.validatePassword = validatePassword;
2700
2743
  exports.waitTime = waitTime;