util-helpers 4.11.1 → 4.12.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.
- package/dist/util-helpers.js +103 -71
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/divide.js +6 -6
- package/esm/index.js +32 -0
- package/esm/minus.js +6 -6
- package/esm/plus.js +6 -6
- package/esm/round.js +4 -5
- package/esm/times.js +6 -6
- package/esm/utils/math.util.js +30 -0
- package/lib/divide.js +5 -6
- package/lib/minus.js +5 -6
- package/lib/plus.js +5 -6
- package/lib/round.js +4 -5
- package/lib/times.js +5 -6
- package/lib/utils/math.util.js +33 -0
- package/package.json +1 -1
- package/types/utils/math.util.d.ts +7 -0
package/dist/util-helpers.js
CHANGED
|
@@ -1575,12 +1575,92 @@
|
|
|
1575
1575
|
|
|
1576
1576
|
var MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || -9007199254740991;
|
|
1577
1577
|
|
|
1578
|
+
/**
|
|
1579
|
+
* 检查值是否为NaN
|
|
1580
|
+
*
|
|
1581
|
+
* @static
|
|
1582
|
+
* @alias module:Type.isNaN
|
|
1583
|
+
* @since 1.1.0
|
|
1584
|
+
* @param {*} value 检查值
|
|
1585
|
+
* @returns {boolean} 是否为NaN
|
|
1586
|
+
* @example
|
|
1587
|
+
*
|
|
1588
|
+
* isNaN(NaN)
|
|
1589
|
+
* // => true
|
|
1590
|
+
*
|
|
1591
|
+
* isNaN(1)
|
|
1592
|
+
* // => false
|
|
1593
|
+
*/
|
|
1594
|
+
|
|
1595
|
+
function _isNaN(value) {
|
|
1596
|
+
return isType(value, 'Number') && isNaN(value);
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/**
|
|
1600
|
+
* 检查值是否为Number
|
|
1601
|
+
*
|
|
1602
|
+
* @static
|
|
1603
|
+
* @alias module:Type.isNumber
|
|
1604
|
+
* @since 1.1.0
|
|
1605
|
+
* @param {*} value 检查值
|
|
1606
|
+
* @returns {boolean} 是否为Number
|
|
1607
|
+
* @example
|
|
1608
|
+
*
|
|
1609
|
+
* isNumber(1)
|
|
1610
|
+
* // => true
|
|
1611
|
+
*
|
|
1612
|
+
* isNumber(Number.MIN_VALUE)
|
|
1613
|
+
* // => true
|
|
1614
|
+
*
|
|
1615
|
+
* isNumber(Infinity)
|
|
1616
|
+
* // => true
|
|
1617
|
+
*
|
|
1618
|
+
* isNumber(NaN)
|
|
1619
|
+
* // => true
|
|
1620
|
+
*
|
|
1621
|
+
* isNumber('1')
|
|
1622
|
+
* // => false
|
|
1623
|
+
*/
|
|
1624
|
+
|
|
1625
|
+
function isNumber(value) {
|
|
1626
|
+
return isType(value, 'Number');
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1578
1629
|
/**
|
|
1579
1630
|
* 参考: https://github.com/nefe/number-precision/blob/master/src/index.ts
|
|
1580
1631
|
*
|
|
1581
1632
|
* 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
|
|
1582
1633
|
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
|
|
1583
1634
|
*/
|
|
1635
|
+
/**
|
|
1636
|
+
* 值是否为有效的数值
|
|
1637
|
+
*
|
|
1638
|
+
* @param {*} value 待检测的值
|
|
1639
|
+
* @returns {boolean} 是否为有效的数值
|
|
1640
|
+
*/
|
|
1641
|
+
|
|
1642
|
+
function isEffectiveNumeric(value) {
|
|
1643
|
+
if (isNumber(value) && !isNaN(value)) {
|
|
1644
|
+
return true;
|
|
1645
|
+
} // 避免空字符串 或 带空格的字符串
|
|
1646
|
+
|
|
1647
|
+
|
|
1648
|
+
if (isString(value)) {
|
|
1649
|
+
var fmtStrValue = value.trim(); // 带空格的字符串也不转换数字
|
|
1650
|
+
// Number(' ') => 0
|
|
1651
|
+
|
|
1652
|
+
if (fmtStrValue === value) {
|
|
1653
|
+
var numValue = fmtStrValue ? Number(fmtStrValue) : NaN;
|
|
1654
|
+
|
|
1655
|
+
if (isNumber(numValue) && !isNaN(numValue)) {
|
|
1656
|
+
return true;
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
devWarn("".concat(value, " is not a valid number."));
|
|
1662
|
+
return false;
|
|
1663
|
+
}
|
|
1584
1664
|
/**
|
|
1585
1665
|
* 是否为科学计数法数字
|
|
1586
1666
|
*
|
|
@@ -1734,27 +1814,6 @@
|
|
|
1734
1814
|
return trimLeftZero(ret);
|
|
1735
1815
|
}
|
|
1736
1816
|
|
|
1737
|
-
/**
|
|
1738
|
-
* 检查值是否为NaN
|
|
1739
|
-
*
|
|
1740
|
-
* @static
|
|
1741
|
-
* @alias module:Type.isNaN
|
|
1742
|
-
* @since 1.1.0
|
|
1743
|
-
* @param {*} value 检查值
|
|
1744
|
-
* @returns {boolean} 是否为NaN
|
|
1745
|
-
* @example
|
|
1746
|
-
*
|
|
1747
|
-
* isNaN(NaN)
|
|
1748
|
-
* // => true
|
|
1749
|
-
*
|
|
1750
|
-
* isNaN(1)
|
|
1751
|
-
* // => false
|
|
1752
|
-
*/
|
|
1753
|
-
|
|
1754
|
-
function _isNaN(value) {
|
|
1755
|
-
return isType(value, 'Number') && isNaN(value);
|
|
1756
|
-
}
|
|
1757
|
-
|
|
1758
1817
|
var reg = /^[+-]?\d*\.?\d*$/;
|
|
1759
1818
|
/**
|
|
1760
1819
|
* 检查数字或数字字符串
|
|
@@ -2674,36 +2733,6 @@
|
|
|
2674
2733
|
return str;
|
|
2675
2734
|
}
|
|
2676
2735
|
|
|
2677
|
-
/**
|
|
2678
|
-
* 检查值是否为Number
|
|
2679
|
-
*
|
|
2680
|
-
* @static
|
|
2681
|
-
* @alias module:Type.isNumber
|
|
2682
|
-
* @since 1.1.0
|
|
2683
|
-
* @param {*} value 检查值
|
|
2684
|
-
* @returns {boolean} 是否为Number
|
|
2685
|
-
* @example
|
|
2686
|
-
*
|
|
2687
|
-
* isNumber(1)
|
|
2688
|
-
* // => true
|
|
2689
|
-
*
|
|
2690
|
-
* isNumber(Number.MIN_VALUE)
|
|
2691
|
-
* // => true
|
|
2692
|
-
*
|
|
2693
|
-
* isNumber(Infinity)
|
|
2694
|
-
* // => true
|
|
2695
|
-
*
|
|
2696
|
-
* isNumber(NaN)
|
|
2697
|
-
* // => true
|
|
2698
|
-
*
|
|
2699
|
-
* isNumber('1')
|
|
2700
|
-
* // => false
|
|
2701
|
-
*/
|
|
2702
|
-
|
|
2703
|
-
function isNumber(value) {
|
|
2704
|
-
return isType(value, 'Number');
|
|
2705
|
-
}
|
|
2706
|
-
|
|
2707
2736
|
/**
|
|
2708
2737
|
* 精确乘法,支持多个数相乘
|
|
2709
2738
|
*
|
|
@@ -2735,12 +2764,13 @@
|
|
|
2735
2764
|
|
|
2736
2765
|
if (rest.length > 0) {
|
|
2737
2766
|
return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
|
|
2738
|
-
} //
|
|
2767
|
+
} // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
|
|
2739
2768
|
|
|
2740
2769
|
|
|
2741
|
-
if (
|
|
2742
|
-
|
|
2743
|
-
|
|
2770
|
+
if (!isEffectiveNumeric(num1)) {
|
|
2771
|
+
return isEffectiveNumeric(num2) ? Number(num2) : NaN;
|
|
2772
|
+
} else if (!isEffectiveNumeric(num2)) {
|
|
2773
|
+
return Number(num1);
|
|
2744
2774
|
}
|
|
2745
2775
|
|
|
2746
2776
|
var num1Changed = float2Fixed(num1);
|
|
@@ -2782,12 +2812,13 @@
|
|
|
2782
2812
|
|
|
2783
2813
|
if (rest.length > 0) {
|
|
2784
2814
|
return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
|
|
2785
|
-
} //
|
|
2815
|
+
} // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
|
|
2786
2816
|
|
|
2787
2817
|
|
|
2788
|
-
if (
|
|
2789
|
-
|
|
2790
|
-
|
|
2818
|
+
if (!isEffectiveNumeric(num1)) {
|
|
2819
|
+
return isEffectiveNumeric(num2) ? Number(num2) : NaN;
|
|
2820
|
+
} else if (!isEffectiveNumeric(num2)) {
|
|
2821
|
+
return Number(num1);
|
|
2791
2822
|
}
|
|
2792
2823
|
|
|
2793
2824
|
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
|
|
@@ -2825,12 +2856,13 @@
|
|
|
2825
2856
|
|
|
2826
2857
|
if (rest.length > 0) {
|
|
2827
2858
|
return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
|
|
2828
|
-
} //
|
|
2859
|
+
} // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
|
|
2829
2860
|
|
|
2830
2861
|
|
|
2831
|
-
if (
|
|
2832
|
-
|
|
2833
|
-
|
|
2862
|
+
if (!isEffectiveNumeric(num1)) {
|
|
2863
|
+
return isEffectiveNumeric(num2) ? Number(num2) : NaN;
|
|
2864
|
+
} else if (!isEffectiveNumeric(num2)) {
|
|
2865
|
+
return Number(num1);
|
|
2834
2866
|
}
|
|
2835
2867
|
|
|
2836
2868
|
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
|
|
@@ -2868,12 +2900,13 @@
|
|
|
2868
2900
|
|
|
2869
2901
|
if (rest.length > 0) {
|
|
2870
2902
|
return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
|
|
2871
|
-
} //
|
|
2903
|
+
} // 兼容处理,如果参数包含无效数值时,尝试取出有效数值参数
|
|
2872
2904
|
|
|
2873
2905
|
|
|
2874
|
-
if (
|
|
2875
|
-
|
|
2876
|
-
|
|
2906
|
+
if (!isEffectiveNumeric(num1)) {
|
|
2907
|
+
return isEffectiveNumeric(num2) ? Number(num2) : NaN;
|
|
2908
|
+
} else if (!isEffectiveNumeric(num2)) {
|
|
2909
|
+
return Number(num1);
|
|
2877
2910
|
}
|
|
2878
2911
|
|
|
2879
2912
|
var num1Changed = float2Fixed(num1);
|
|
@@ -2908,10 +2941,9 @@
|
|
|
2908
2941
|
function round(num) {
|
|
2909
2942
|
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
2910
2943
|
|
|
2911
|
-
//
|
|
2912
|
-
if (
|
|
2913
|
-
|
|
2914
|
-
return num;
|
|
2944
|
+
// 兼容处理,如果参数包含无效数值时,返回第一个参数
|
|
2945
|
+
if (!isEffectiveNumeric(num)) {
|
|
2946
|
+
return NaN;
|
|
2915
2947
|
}
|
|
2916
2948
|
|
|
2917
2949
|
var base = Math.pow(10, precision);
|