util-helpers 4.12.0 → 4.12.3

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 (92) hide show
  1. package/dist/util-helpers.js +106 -58
  2. package/dist/util-helpers.js.map +1 -1
  3. package/dist/util-helpers.min.js +1 -1
  4. package/dist/util-helpers.min.js.map +1 -1
  5. package/esm/divide.js +19 -17
  6. package/esm/index.js +21 -22
  7. package/esm/minus.js +10 -8
  8. package/esm/plus.js +10 -8
  9. package/esm/round.js +5 -4
  10. package/esm/times.js +10 -8
  11. package/esm/utils/config.js +2 -1
  12. package/esm/utils/math.util.js +50 -3
  13. package/lib/divide.js +18 -16
  14. package/lib/index.js +6 -0
  15. package/lib/minus.js +9 -7
  16. package/lib/plus.js +9 -7
  17. package/lib/round.js +4 -3
  18. package/lib/times.js +9 -7
  19. package/lib/utils/config.js +3 -1
  20. package/lib/utils/math.util.js +51 -2
  21. package/package.json +3 -1
  22. package/types/src/blobToDataURL.d.ts +25 -0
  23. package/types/src/bytesToSize.d.ts +24 -0
  24. package/types/src/calculateCursorPosition.d.ts +26 -0
  25. package/types/src/dataURLToBlob.d.ts +15 -0
  26. package/types/src/divide.d.ts +21 -0
  27. package/types/src/formatBankCard.d.ts +35 -0
  28. package/types/src/formatMobile.d.ts +27 -0
  29. package/types/src/formatMoney.d.ts +50 -0
  30. package/types/src/index.d.ts +45 -0
  31. package/types/src/isBankCard.d.ts +32 -0
  32. package/types/src/isBusinessLicense.d.ts +29 -0
  33. package/types/src/isChinese.d.ts +41 -0
  34. package/types/src/isEmail.d.ts +19 -0
  35. package/types/src/isHMCard.d.ts +24 -0
  36. package/types/src/isIPv4.d.ts +25 -0
  37. package/types/src/isIPv6.d.ts +50 -0
  38. package/types/src/isIdCard.d.ts +34 -0
  39. package/types/src/isMobile.d.ts +19 -0
  40. package/types/src/isPassport.d.ts +21 -0
  41. package/types/src/isPassword.d.ts +46 -0
  42. package/types/src/isPostcode.d.ts +19 -0
  43. package/types/src/isPromiseLike.d.ts +21 -0
  44. package/types/src/isQQ.d.ts +19 -0
  45. package/types/src/isSocialCreditCode.d.ts +31 -0
  46. package/types/src/isSwiftCode.d.ts +22 -0
  47. package/types/src/isTWCard.d.ts +23 -0
  48. package/types/src/isTelephone.d.ts +25 -0
  49. package/types/src/isUrl.d.ts +34 -0
  50. package/types/src/isVehicle.d.ts +29 -0
  51. package/types/src/isWX.d.ts +19 -0
  52. package/types/src/minus.d.ts +21 -0
  53. package/types/src/normalizeString.d.ts +22 -0
  54. package/types/src/numberToChinese.d.ts +57 -0
  55. package/types/src/padZero.d.ts +24 -0
  56. package/types/src/parseIdCard.d.ts +107 -0
  57. package/types/src/plus.d.ts +21 -0
  58. package/types/src/randomString.d.ts +19 -0
  59. package/types/src/replaceChar.d.ts +54 -0
  60. package/types/src/round.d.ts +22 -0
  61. package/types/src/safeDate.d.ts +22 -0
  62. package/types/src/setDataURLPrefix.d.ts +24 -0
  63. package/types/src/strlen.d.ts +20 -0
  64. package/types/src/times.d.ts +21 -0
  65. package/types/src/utils/config.d.ts +13 -0
  66. package/types/src/utils/constants.d.ts +2 -0
  67. package/types/src/utils/convertToString.d.ts +8 -0
  68. package/types/src/utils/devWarn.d.ts +7 -0
  69. package/types/src/utils/math.util.d.ts +71 -0
  70. package/types/src/utils/type/index.d.ts +19 -0
  71. package/types/src/utils/type/isArguments.d.ts +18 -0
  72. package/types/src/utils/type/isArray.d.ts +18 -0
  73. package/types/src/utils/type/isBoolean.d.ts +18 -0
  74. package/types/src/utils/type/isDate.d.ts +18 -0
  75. package/types/src/utils/type/isError.d.ts +18 -0
  76. package/types/src/utils/type/isFunction.d.ts +18 -0
  77. package/types/src/utils/type/isMap.d.ts +18 -0
  78. package/types/src/utils/type/isNaN.d.ts +18 -0
  79. package/types/src/utils/type/isNil.d.ts +11 -0
  80. package/types/src/utils/type/isNull.d.ts +18 -0
  81. package/types/src/utils/type/isNumber.d.ts +27 -0
  82. package/types/src/utils/type/isObject.d.ts +21 -0
  83. package/types/src/utils/type/isRegExp.d.ts +18 -0
  84. package/types/src/utils/type/isSet.d.ts +18 -0
  85. package/types/src/utils/type/isString.d.ts +18 -0
  86. package/types/src/utils/type/isSymbol.d.ts +18 -0
  87. package/types/src/utils/type/isType.d.ts +11 -0
  88. package/types/src/utils/type/isUndefined.d.ts +21 -0
  89. package/types/src/utils/type/isWeakMap.d.ts +18 -0
  90. package/types/src/utils/type/isWeakSet.d.ts +18 -0
  91. package/types/src/validatePassword.d.ts +134 -0
  92. package/types/src/waitTime.d.ts +20 -0
@@ -562,6 +562,8 @@
562
562
  return lastCode === checkCode;
563
563
  }
564
564
 
565
+ const version = "4.12.3";
566
+
565
567
  var config = {
566
568
  // 禁用warning提示
567
569
  disableWarning: true
@@ -1626,6 +1628,27 @@
1626
1628
  return isType(value, 'Number');
1627
1629
  }
1628
1630
 
1631
+ /**
1632
+ * 检查值是否为Symbol
1633
+ *
1634
+ * @static
1635
+ * @alias module:Type.isSymbol
1636
+ * @since 1.1.0
1637
+ * @param {*} value 检查值
1638
+ * @returns {boolean} 是否为Symbol
1639
+ * @example
1640
+ *
1641
+ * isSymbol(Symbol.iterator)
1642
+ * // => true
1643
+ *
1644
+ * isSymbol("abc")
1645
+ * // => false
1646
+ */
1647
+
1648
+ function isSymbol(value) {
1649
+ return isType(value, 'Symbol');
1650
+ }
1651
+
1629
1652
  /**
1630
1653
  * 参考: https://github.com/nefe/number-precision/blob/master/src/index.ts
1631
1654
  *
@@ -1633,33 +1656,48 @@
1633
1656
  * 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
1634
1657
  */
1635
1658
  /**
1636
- * 值是否为有效的数值
1659
+ * 将值转换为有效数值
1637
1660
  *
1638
- * @param {*} value 待检测的值
1639
- * @returns {boolean} 是否为有效的数值
1661
+ * @param {*} value 要转换的值
1662
+ * @returns {number|string} 有效数值
1640
1663
  */
1641
1664
 
1642
- function isEffectiveNumeric(value) {
1643
- if (isNumber(value) && !isNaN(value)) {
1644
- return true;
1645
- } // 避免空字符串 或 带空格的字符串
1646
-
1665
+ function transformEffectiveNumber(value) {
1666
+ /** @type {string|number|undefined} */
1667
+ var ret;
1647
1668
 
1648
1669
  if (isString(value)) {
1649
- var fmtStrValue = value.trim(); // 带空格的字符串也不转换数字
1650
- // Number(' ') => 0
1651
-
1652
- if (fmtStrValue === value) {
1653
- var numValue = fmtStrValue ? Number(fmtStrValue) : NaN;
1670
+ ret = value.trim(); // ' 15' ' 15 ' 兼容 Number(string) 处理
1654
1671
 
1655
- if (isNumber(numValue) && !isNaN(numValue)) {
1656
- return true;
1657
- }
1672
+ if (ret === '') {
1673
+ ret = Number(ret);
1674
+ } else if (Number.isNaN(Number(ret))) {
1675
+ // string如果可以转换为number,默认不转换为number类型
1676
+ ret = Number.NaN;
1658
1677
  }
1678
+ } else if (isSymbol(value)) {
1679
+ ret = Number.NaN;
1680
+ } else if (!isNumber(value)) {
1681
+ // 其余非数字类型通过 Number 转换
1682
+ // 例如 Symbol 包装器对象将会报错
1683
+ // symObj = Object(Symbol());
1684
+ // Number(symObj); // TypeError: Cannot convert a Symbol value to a number
1685
+ try {
1686
+ ret = Number(value);
1687
+ } catch (err) {
1688
+ ret = Number.NaN;
1689
+ console.error(err);
1690
+ }
1691
+ } else {
1692
+ ret = value;
1659
1693
  }
1660
1694
 
1661
- devWarn("".concat(value, " is not a valid number."));
1662
- return false;
1695
+ if (Number.isNaN(ret)) {
1696
+ return Number.NaN;
1697
+ } // @ts-ignore
1698
+
1699
+
1700
+ return ret;
1663
1701
  }
1664
1702
  /**
1665
1703
  * 是否为科学计数法数字
@@ -2734,7 +2772,7 @@
2734
2772
  }
2735
2773
 
2736
2774
  /**
2737
- * 精确乘法,支持多个数相乘
2775
+ * 精确乘法,支持多个数相乘,乘数默认为 1 。
2738
2776
  *
2739
2777
  * @static
2740
2778
  * @alias module:Math.times
@@ -2759,18 +2797,20 @@
2759
2797
  }
2760
2798
 
2761
2799
  var num1 = nums[0],
2762
- num2 = nums[1],
2800
+ _nums$ = nums[1],
2801
+ num2 = _nums$ === void 0 ? 1 : _nums$,
2763
2802
  rest = nums.slice(2);
2764
2803
 
2765
2804
  if (rest.length > 0) {
2766
2805
  return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
2767
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2806
+ }
2768
2807
 
2808
+ num1 = transformEffectiveNumber(num1);
2809
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2810
+ // @ts-ignore
2769
2811
 
2770
- if (!isEffectiveNumeric(num1)) {
2771
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2772
- } else if (!isEffectiveNumeric(num2)) {
2773
- return Number(num1);
2812
+ if (isNaN(num1) || isNaN(num2)) {
2813
+ return Number.NaN;
2774
2814
  }
2775
2815
 
2776
2816
  var num1Changed = float2Fixed(num1);
@@ -2782,7 +2822,7 @@
2782
2822
  }
2783
2823
 
2784
2824
  /**
2785
- * 精确加法,支持多个数相加
2825
+ * 精确加法,支持多个数相加,加数默认为 0 。
2786
2826
  *
2787
2827
  * @static
2788
2828
  * @alias module:Math.plus
@@ -2807,18 +2847,20 @@
2807
2847
  }
2808
2848
 
2809
2849
  var num1 = nums[0],
2810
- num2 = nums[1],
2850
+ _nums$ = nums[1],
2851
+ num2 = _nums$ === void 0 ? 0 : _nums$,
2811
2852
  rest = nums.slice(2);
2812
2853
 
2813
2854
  if (rest.length > 0) {
2814
2855
  return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
2815
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2856
+ }
2816
2857
 
2858
+ num1 = transformEffectiveNumber(num1);
2859
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2860
+ // @ts-ignore
2817
2861
 
2818
- if (!isEffectiveNumeric(num1)) {
2819
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2820
- } else if (!isEffectiveNumeric(num2)) {
2821
- return Number(num1);
2862
+ if (isNaN(num1) || isNaN(num2)) {
2863
+ return Number.NaN;
2822
2864
  }
2823
2865
 
2824
2866
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
@@ -2826,7 +2868,7 @@
2826
2868
  }
2827
2869
 
2828
2870
  /**
2829
- * 精确减法,支持多个数相减
2871
+ * 精确减法,支持多个数相减,减数默认为 0 。
2830
2872
  *
2831
2873
  * @static
2832
2874
  * @alias module:Math.minus
@@ -2851,18 +2893,20 @@
2851
2893
  }
2852
2894
 
2853
2895
  var num1 = nums[0],
2854
- num2 = nums[1],
2896
+ _nums$ = nums[1],
2897
+ num2 = _nums$ === void 0 ? 0 : _nums$,
2855
2898
  rest = nums.slice(2);
2856
2899
 
2857
2900
  if (rest.length > 0) {
2858
2901
  return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
2859
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2902
+ }
2860
2903
 
2904
+ num1 = transformEffectiveNumber(num1);
2905
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2906
+ // @ts-ignore
2861
2907
 
2862
- if (!isEffectiveNumeric(num1)) {
2863
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2864
- } else if (!isEffectiveNumeric(num2)) {
2865
- return Number(num1);
2908
+ if (isNaN(num1) || isNaN(num2)) {
2909
+ return Number.NaN;
2866
2910
  }
2867
2911
 
2868
2912
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
@@ -2870,23 +2914,23 @@
2870
2914
  }
2871
2915
 
2872
2916
  /**
2873
- * 精确除法,支持多个数相除
2917
+ * 精确除法,支持多个数相除,除数默认为 1 。
2874
2918
  *
2875
2919
  * @static
2876
2920
  * @alias module:Math.divide
2877
2921
  * @since 3.1.0
2878
- * @param {...number|string} nums 除数和被除数
2922
+ * @param {...number|string} nums 被除数和除数
2879
2923
  * @returns {number} 商数
2880
2924
  * @example
2881
2925
  *
2882
- * divide(1.21, 1.1);
2883
- * // => 1.1
2884
- *
2885
- * divide(1000, 10, 10);
2886
- * // => 10
2887
- *
2888
- * divide(1000, 10, 10, 10);
2889
- * // => 1
2926
+ * divide(1.21); // 1.21 除数默认为 1 ,即 1.21/1 = 1.21
2927
+ * divide(1.21, 1.1); // 1.1
2928
+ * divide(1000, 10, 10); // 10
2929
+ * divide(1000, 10, 10, 10); // 1
2930
+ *
2931
+ * divide(); // NaN 如果没有传入参数,被除数默认为 undefined 。 Number(undefined) 转换为 NaN ,NaN/1 = NaN
2932
+ * divide(null); // 0 Number(null) 转换为 0 , 0/1 = 0
2933
+ * divide('1.5 ', 0.5); // 3 Number('1.5 ') 转换为 1.5 ,1.5/0.5 = 3
2890
2934
  */
2891
2935
 
2892
2936
  function divide() {
@@ -2895,18 +2939,20 @@
2895
2939
  }
2896
2940
 
2897
2941
  var num1 = nums[0],
2898
- num2 = nums[1],
2942
+ _nums$ = nums[1],
2943
+ num2 = _nums$ === void 0 ? 1 : _nums$,
2899
2944
  rest = nums.slice(2);
2900
2945
 
2901
2946
  if (rest.length > 0) {
2902
2947
  return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
2903
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2948
+ }
2904
2949
 
2950
+ num1 = transformEffectiveNumber(num1);
2951
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2952
+ // @ts-ignore
2905
2953
 
2906
- if (!isEffectiveNumeric(num1)) {
2907
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2908
- } else if (!isEffectiveNumeric(num2)) {
2909
- return Number(num1);
2954
+ if (isNaN(num1) || isNaN(num2)) {
2955
+ return Number.NaN;
2910
2956
  }
2911
2957
 
2912
2958
  var num1Changed = float2Fixed(num1);
@@ -2940,10 +2986,11 @@
2940
2986
 
2941
2987
  function round(num) {
2942
2988
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
2989
+ num = transformEffectiveNumber(num); // 兼容处理,如果参数包含无效数值时,返回 NaN
2990
+ // @ts-ignore
2943
2991
 
2944
- // 兼容处理,如果参数包含无效数值时,返回第一个参数
2945
- if (!isEffectiveNumeric(num)) {
2946
- return NaN;
2992
+ if (isNaN(num)) {
2993
+ return Number.NaN;
2947
2994
  }
2948
2995
 
2949
2996
  var base = Math.pow(10, precision);
@@ -3165,6 +3212,7 @@
3165
3212
  exports.strlen = strlen;
3166
3213
  exports.times = times;
3167
3214
  exports.validatePassword = validatePassword;
3215
+ exports.version = version;
3168
3216
  exports.waitTime = waitTime;
3169
3217
 
3170
3218
  Object.defineProperty(exports, '__esModule', { value: true });