util-helpers 4.12.2 → 4.12.5

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)
3
+ [![npm][npm]][npm-url] [![Build and Deploy Docs](https://github.com/doly-dev/util-helpers/actions/workflows/ci.yml/badge.svg)](https://github.com/doly-dev/util-helpers/actions/workflows/ci.yml) [![codecov](https://codecov.io/gh/doly-dev/util-helpers/branch/master/graph/badge.svg?token=nhm6Zrmmyq)](https://codecov.io/gh/doly-dev/util-helpers) ![npm](https://img.shields.io/npm/dt/util-helpers) ![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
 
@@ -562,7 +562,7 @@
562
562
  return lastCode === checkCode;
563
563
  }
564
564
 
565
- const version = "4.12.2";
565
+ const version = "4.12.5";
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,43 @@
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
1670
+ ret = value.trim(); // ' 15' ' 15 ' 兼容 Number(string) 处理
1653
1671
 
1654
- if (fmtStrValue === value) {
1655
- var numValue = fmtStrValue ? Number(fmtStrValue) : NaN;
1656
-
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;
1660
1677
  }
1678
+ } else if (isSymbol(value)) {
1679
+ // 例如 Symbol 包装器对象将会报错
1680
+ // symObj = Object(Symbol());
1681
+ // Number(symObj); // TypeError: Cannot convert a Symbol value to a number
1682
+ ret = Number.NaN;
1683
+ } else if (!isNumber(value)) {
1684
+ // 其余非数字类型通过 Number 转换
1685
+ ret = Number(value);
1686
+ } else {
1687
+ ret = value;
1661
1688
  }
1662
1689
 
1663
- devWarn("".concat(value, " is not a valid number."));
1664
- return false;
1690
+ if (Number.isNaN(ret)) {
1691
+ return Number.NaN;
1692
+ } // @ts-ignore
1693
+
1694
+
1695
+ return ret;
1665
1696
  }
1666
1697
  /**
1667
1698
  * 是否为科学计数法数字
@@ -2736,7 +2767,7 @@
2736
2767
  }
2737
2768
 
2738
2769
  /**
2739
- * 精确乘法,支持多个数相乘
2770
+ * 精确乘法,支持多个数相乘,乘数默认为 1 。
2740
2771
  *
2741
2772
  * @static
2742
2773
  * @alias module:Math.times
@@ -2761,18 +2792,20 @@
2761
2792
  }
2762
2793
 
2763
2794
  var num1 = nums[0],
2764
- num2 = nums[1],
2795
+ _nums$ = nums[1],
2796
+ num2 = _nums$ === void 0 ? 1 : _nums$,
2765
2797
  rest = nums.slice(2);
2766
2798
 
2767
2799
  if (rest.length > 0) {
2768
2800
  return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
2769
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2801
+ }
2770
2802
 
2803
+ num1 = transformEffectiveNumber(num1);
2804
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2805
+ // @ts-ignore
2771
2806
 
2772
- if (!isEffectiveNumeric(num1)) {
2773
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2774
- } else if (!isEffectiveNumeric(num2)) {
2775
- return Number(num1);
2807
+ if (isNaN(num1) || isNaN(num2)) {
2808
+ return Number.NaN;
2776
2809
  }
2777
2810
 
2778
2811
  var num1Changed = float2Fixed(num1);
@@ -2784,7 +2817,7 @@
2784
2817
  }
2785
2818
 
2786
2819
  /**
2787
- * 精确加法,支持多个数相加
2820
+ * 精确加法,支持多个数相加,加数默认为 0 。
2788
2821
  *
2789
2822
  * @static
2790
2823
  * @alias module:Math.plus
@@ -2809,18 +2842,20 @@
2809
2842
  }
2810
2843
 
2811
2844
  var num1 = nums[0],
2812
- num2 = nums[1],
2845
+ _nums$ = nums[1],
2846
+ num2 = _nums$ === void 0 ? 0 : _nums$,
2813
2847
  rest = nums.slice(2);
2814
2848
 
2815
2849
  if (rest.length > 0) {
2816
2850
  return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
2817
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2851
+ }
2818
2852
 
2853
+ num1 = transformEffectiveNumber(num1);
2854
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2855
+ // @ts-ignore
2819
2856
 
2820
- if (!isEffectiveNumeric(num1)) {
2821
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2822
- } else if (!isEffectiveNumeric(num2)) {
2823
- return Number(num1);
2857
+ if (isNaN(num1) || isNaN(num2)) {
2858
+ return Number.NaN;
2824
2859
  }
2825
2860
 
2826
2861
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
@@ -2828,7 +2863,7 @@
2828
2863
  }
2829
2864
 
2830
2865
  /**
2831
- * 精确减法,支持多个数相减
2866
+ * 精确减法,支持多个数相减,减数默认为 0 。
2832
2867
  *
2833
2868
  * @static
2834
2869
  * @alias module:Math.minus
@@ -2853,18 +2888,20 @@
2853
2888
  }
2854
2889
 
2855
2890
  var num1 = nums[0],
2856
- num2 = nums[1],
2891
+ _nums$ = nums[1],
2892
+ num2 = _nums$ === void 0 ? 0 : _nums$,
2857
2893
  rest = nums.slice(2);
2858
2894
 
2859
2895
  if (rest.length > 0) {
2860
2896
  return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
2861
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2897
+ }
2862
2898
 
2899
+ num1 = transformEffectiveNumber(num1);
2900
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2901
+ // @ts-ignore
2863
2902
 
2864
- if (!isEffectiveNumeric(num1)) {
2865
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2866
- } else if (!isEffectiveNumeric(num2)) {
2867
- return Number(num1);
2903
+ if (isNaN(num1) || isNaN(num2)) {
2904
+ return Number.NaN;
2868
2905
  }
2869
2906
 
2870
2907
  var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
@@ -2872,23 +2909,23 @@
2872
2909
  }
2873
2910
 
2874
2911
  /**
2875
- * 精确除法,支持多个数相除
2912
+ * 精确除法,支持多个数相除,除数默认为 1 。
2876
2913
  *
2877
2914
  * @static
2878
2915
  * @alias module:Math.divide
2879
2916
  * @since 3.1.0
2880
- * @param {...number|string} nums 除数和被除数
2917
+ * @param {...number|string} nums 被除数和除数
2881
2918
  * @returns {number} 商数
2882
2919
  * @example
2883
2920
  *
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
2921
+ * divide(1.21); // 1.21 除数默认为 1 ,即 1.21/1 = 1.21
2922
+ * divide(1.21, 1.1); // 1.1
2923
+ * divide(1000, 10, 10); // 10
2924
+ * divide(1000, 10, 10, 10); // 1
2925
+ *
2926
+ * divide(); // NaN 如果没有传入参数,被除数默认为 undefined 。 Number(undefined) 转换为 NaN ,NaN/1 = NaN
2927
+ * divide(null); // 0 Number(null) 转换为 0 , 0/1 = 0
2928
+ * divide('1.5 ', 0.5); // 3 Number('1.5 ') 转换为 1.5 ,1.5/0.5 = 3
2892
2929
  */
2893
2930
 
2894
2931
  function divide() {
@@ -2897,18 +2934,20 @@
2897
2934
  }
2898
2935
 
2899
2936
  var num1 = nums[0],
2900
- num2 = nums[1],
2937
+ _nums$ = nums[1],
2938
+ num2 = _nums$ === void 0 ? 1 : _nums$,
2901
2939
  rest = nums.slice(2);
2902
2940
 
2903
2941
  if (rest.length > 0) {
2904
2942
  return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
2905
- } // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
2943
+ }
2906
2944
 
2945
+ num1 = transformEffectiveNumber(num1);
2946
+ num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
2947
+ // @ts-ignore
2907
2948
 
2908
- if (!isEffectiveNumeric(num1)) {
2909
- return isEffectiveNumeric(num2) ? Number(num2) : NaN;
2910
- } else if (!isEffectiveNumeric(num2)) {
2911
- return Number(num1);
2949
+ if (isNaN(num1) || isNaN(num2)) {
2950
+ return Number.NaN;
2912
2951
  }
2913
2952
 
2914
2953
  var num1Changed = float2Fixed(num1);
@@ -2942,10 +2981,11 @@
2942
2981
 
2943
2982
  function round(num) {
2944
2983
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
2984
+ num = transformEffectiveNumber(num); // 兼容处理,如果参数包含无效数值时,返回 NaN
2985
+ // @ts-ignore
2945
2986
 
2946
- // 兼容处理,如果参数包含无效数值时,返回第一个参数
2947
- if (!isEffectiveNumeric(num)) {
2948
- return NaN;
2987
+ if (isNaN(num)) {
2988
+ return Number.NaN;
2949
2989
  }
2950
2990
 
2951
2991
  var base = Math.pow(10, precision);