util-helpers 4.12.2 → 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.
@@ -562,7 +562,7 @@
562
562
  return lastCode === checkCode;
563
563
  }
564
564
 
565
- const version = "4.12.2";
565
+ const version = "4.12.3";
566
566
 
567
567
  var config = {
568
568
  // 禁用warning提示
@@ -1628,6 +1628,27 @@
1628
1628
  return isType(value, 'Number');
1629
1629
  }
1630
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
+
1631
1652
  /**
1632
1653
  * 参考: https://github.com/nefe/number-precision/blob/master/src/index.ts
1633
1654
  *
@@ -1635,33 +1656,48 @@
1635
1656
  * 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
1636
1657
  */
1637
1658
  /**
1638
- * 值是否为有效的数值
1659
+ * 将值转换为有效数值
1639
1660
  *
1640
- * @param {*} value 待检测的值
1641
- * @returns {boolean} 是否为有效的数值
1661
+ * @param {*} value 要转换的值
1662
+ * @returns {number|string} 有效数值
1642
1663
  */
1643
1664
 
1644
- function isEffectiveNumeric(value) {
1645
- if (isNumber(value) && !isNaN(value)) {
1646
- return true;
1647
- } // 避免空字符串 或 带空格的字符串
1648
-
1665
+ function transformEffectiveNumber(value) {
1666
+ /** @type {string|number|undefined} */
1667
+ var ret;
1649
1668
 
1650
1669
  if (isString(value)) {
1651
- var fmtStrValue = value.trim(); // 带空格的字符串也不转换数字
1652
- // Number(' ') => 0
1653
-
1654
- if (fmtStrValue === value) {
1655
- var numValue = fmtStrValue ? Number(fmtStrValue) : NaN;
1670
+ ret = value.trim(); // ' 15' ' 15 ' 兼容 Number(string) 处理
1656
1671
 
1657
- if (!isNaN(numValue)) {
1658
- return true;
1659
- }
1672
+ if (ret === '') {
1673
+ ret = Number(ret);
1674
+ } else if (Number.isNaN(Number(ret))) {
1675
+ // string如果可以转换为number,默认不转换为number类型
1676
+ ret = Number.NaN;
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);
1660
1690
  }
1691
+ } else {
1692
+ ret = value;
1661
1693
  }
1662
1694
 
1663
- devWarn("".concat(value, " is not a valid number."));
1664
- return false;
1695
+ if (Number.isNaN(ret)) {
1696
+ return Number.NaN;
1697
+ } // @ts-ignore
1698
+
1699
+
1700
+ return ret;
1665
1701
  }
1666
1702
  /**
1667
1703
  * 是否为科学计数法数字
@@ -2736,7 +2772,7 @@
2736
2772
  }
2737
2773
 
2738
2774
  /**
2739
- * 精确乘法,支持多个数相乘
2775
+ * 精确乘法,支持多个数相乘,乘数默认为 1 。
2740
2776
  *
2741
2777
  * @static
2742
2778
  * @alias module:Math.times
@@ -2761,18 +2797,20 @@
2761
2797
  }
2762
2798
 
2763
2799
  var num1 = nums[0],
2764
- num2 = nums[1],
2800
+ _nums$ = nums[1],
2801
+ num2 = _nums$ === void 0 ? 1 : _nums$,
2765
2802
  rest = nums.slice(2);
2766
2803
 
2767
2804
  if (rest.length > 0) {
2768
2805
  return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
2769
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2806
+ }
2770
2807
 
2808
+ num1 = transformEffectiveNumber(num1);
2809
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2810
+ // @ts-ignore
2771
2811
 
2772
- if (!isEffectiveNumeric(num1)) {
2773
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2774
- } else if (!isEffectiveNumeric(num2)) {
2775
- return Number(num1);
2812
+ if (isNaN(num1) || isNaN(num2)) {
2813
+ return Number.NaN;
2776
2814
  }
2777
2815
 
2778
2816
  var num1Changed = float2Fixed(num1);
@@ -2784,7 +2822,7 @@
2784
2822
  }
2785
2823
 
2786
2824
  /**
2787
- * 精确加法,支持多个数相加
2825
+ * 精确加法,支持多个数相加,加数默认为 0 。
2788
2826
  *
2789
2827
  * @static
2790
2828
  * @alias module:Math.plus
@@ -2809,18 +2847,20 @@
2809
2847
  }
2810
2848
 
2811
2849
  var num1 = nums[0],
2812
- num2 = nums[1],
2850
+ _nums$ = nums[1],
2851
+ num2 = _nums$ === void 0 ? 0 : _nums$,
2813
2852
  rest = nums.slice(2);
2814
2853
 
2815
2854
  if (rest.length > 0) {
2816
2855
  return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
2817
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2856
+ }
2818
2857
 
2858
+ num1 = transformEffectiveNumber(num1);
2859
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2860
+ // @ts-ignore
2819
2861
 
2820
- if (!isEffectiveNumeric(num1)) {
2821
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2822
- } else if (!isEffectiveNumeric(num2)) {
2823
- return Number(num1);
2862
+ if (isNaN(num1) || isNaN(num2)) {
2863
+ return Number.NaN;
2824
2864
  }
2825
2865
 
2826
2866
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
@@ -2828,7 +2868,7 @@
2828
2868
  }
2829
2869
 
2830
2870
  /**
2831
- * 精确减法,支持多个数相减
2871
+ * 精确减法,支持多个数相减,减数默认为 0 。
2832
2872
  *
2833
2873
  * @static
2834
2874
  * @alias module:Math.minus
@@ -2853,18 +2893,20 @@
2853
2893
  }
2854
2894
 
2855
2895
  var num1 = nums[0],
2856
- num2 = nums[1],
2896
+ _nums$ = nums[1],
2897
+ num2 = _nums$ === void 0 ? 0 : _nums$,
2857
2898
  rest = nums.slice(2);
2858
2899
 
2859
2900
  if (rest.length > 0) {
2860
2901
  return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
2861
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2902
+ }
2862
2903
 
2904
+ num1 = transformEffectiveNumber(num1);
2905
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2906
+ // @ts-ignore
2863
2907
 
2864
- if (!isEffectiveNumeric(num1)) {
2865
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2866
- } else if (!isEffectiveNumeric(num2)) {
2867
- return Number(num1);
2908
+ if (isNaN(num1) || isNaN(num2)) {
2909
+ return Number.NaN;
2868
2910
  }
2869
2911
 
2870
2912
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
@@ -2872,23 +2914,23 @@
2872
2914
  }
2873
2915
 
2874
2916
  /**
2875
- * 精确除法,支持多个数相除
2917
+ * 精确除法,支持多个数相除,除数默认为 1 。
2876
2918
  *
2877
2919
  * @static
2878
2920
  * @alias module:Math.divide
2879
2921
  * @since 3.1.0
2880
- * @param {...number|string} nums 除数和被除数
2922
+ * @param {...number|string} nums 被除数和除数
2881
2923
  * @returns {number} 商数
2882
2924
  * @example
2883
2925
  *
2884
- * divide(1.21, 1.1);
2885
- * // => 1.1
2886
- *
2887
- * divide(1000, 10, 10);
2888
- * // => 10
2889
- *
2890
- * divide(1000, 10, 10, 10);
2891
- * // => 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
2892
2934
  */
2893
2935
 
2894
2936
  function divide() {
@@ -2897,18 +2939,20 @@
2897
2939
  }
2898
2940
 
2899
2941
  var num1 = nums[0],
2900
- num2 = nums[1],
2942
+ _nums$ = nums[1],
2943
+ num2 = _nums$ === void 0 ? 1 : _nums$,
2901
2944
  rest = nums.slice(2);
2902
2945
 
2903
2946
  if (rest.length > 0) {
2904
2947
  return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
2905
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2948
+ }
2906
2949
 
2950
+ num1 = transformEffectiveNumber(num1);
2951
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2952
+ // @ts-ignore
2907
2953
 
2908
- if (!isEffectiveNumeric(num1)) {
2909
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2910
- } else if (!isEffectiveNumeric(num2)) {
2911
- return Number(num1);
2954
+ if (isNaN(num1) || isNaN(num2)) {
2955
+ return Number.NaN;
2912
2956
  }
2913
2957
 
2914
2958
  var num1Changed = float2Fixed(num1);
@@ -2942,10 +2986,11 @@
2942
2986
 
2943
2987
  function round(num) {
2944
2988
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
2989
+ num = transformEffectiveNumber(num); // 兼容处理,如果参数包含无效数值时,返回 NaN
2990
+ // @ts-ignore
2945
2991
 
2946
- // 兼容处理,如果参数包含无效数值时,返回第一个参数
2947
- if (!isEffectiveNumeric(num)) {
2948
- return NaN;
2992
+ if (isNaN(num)) {
2993
+ return Number.NaN;
2949
2994
  }
2950
2995
 
2951
2996
  var base = Math.pow(10, precision);