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 +1 -1
- package/dist/util-helpers.js +99 -59
- 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 +19 -17
- package/esm/index.js +20 -23
- package/esm/minus.js +10 -8
- package/esm/plus.js +10 -8
- package/esm/round.js +5 -4
- package/esm/times.js +10 -8
- package/esm/utils/config.js +1 -1
- package/esm/utils/math.util.js +29 -19
- package/lib/divide.js +18 -16
- package/lib/minus.js +9 -7
- package/lib/plus.js +9 -7
- package/lib/round.js +4 -3
- package/lib/times.js +9 -7
- package/lib/utils/config.js +1 -1
- package/lib/utils/math.util.js +29 -19
- package/package.json +1 -1
- package/types/src/divide.d.ts +9 -9
- package/types/src/minus.d.ts +1 -1
- package/types/src/plus.d.ts +1 -1
- package/types/src/times.d.ts +1 -1
- package/types/src/utils/math.util.d.ts +4 -4
package/esm/divide.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
-
import { strip, digitLength, float2Fixed, checkBoundary,
|
|
2
|
+
import { strip, digitLength, float2Fixed, checkBoundary, transformEffectiveNumber } from './utils/math.util';
|
|
3
3
|
import times from './times';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 精确除法,支持多个数相除,除数默认为 1 。
|
|
6
6
|
*
|
|
7
7
|
* @static
|
|
8
8
|
* @alias module:Math.divide
|
|
9
9
|
* @since 3.1.0
|
|
10
|
-
* @param {...number|string} nums
|
|
10
|
+
* @param {...number|string} nums 被除数和除数
|
|
11
11
|
* @returns {number} 商数
|
|
12
12
|
* @example
|
|
13
13
|
*
|
|
14
|
-
* divide(1.21
|
|
15
|
-
* //
|
|
16
|
-
*
|
|
17
|
-
* divide(1000, 10, 10);
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* divide(
|
|
21
|
-
* //
|
|
14
|
+
* divide(1.21); // 1.21 除数默认为 1 ,即 1.21/1 = 1.21
|
|
15
|
+
* divide(1.21, 1.1); // 1.1
|
|
16
|
+
* divide(1000, 10, 10); // 10
|
|
17
|
+
* divide(1000, 10, 10, 10); // 1
|
|
18
|
+
*
|
|
19
|
+
* divide(); // NaN 如果没有传入参数,被除数默认为 undefined 。 Number(undefined) 转换为 NaN ,NaN/1 = NaN
|
|
20
|
+
* divide(null); // 0 Number(null) 转换为 0 , 0/1 = 0
|
|
21
|
+
* divide('1.5 ', 0.5); // 3 Number('1.5 ') 转换为 1.5 ,1.5/0.5 = 3
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
function divide() {
|
|
@@ -27,18 +27,20 @@ function divide() {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
var num1 = nums[0],
|
|
30
|
-
|
|
30
|
+
_nums$ = nums[1],
|
|
31
|
+
num2 = _nums$ === void 0 ? 1 : _nums$,
|
|
31
32
|
rest = nums.slice(2);
|
|
32
33
|
|
|
33
34
|
if (rest.length > 0) {
|
|
34
35
|
return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
|
|
35
|
-
}
|
|
36
|
+
}
|
|
36
37
|
|
|
38
|
+
num1 = transformEffectiveNumber(num1);
|
|
39
|
+
num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
40
|
+
// @ts-ignore
|
|
37
41
|
|
|
38
|
-
if (
|
|
39
|
-
return
|
|
40
|
-
} else if (!isEffectiveNumeric(num2)) {
|
|
41
|
-
return Number(num1);
|
|
42
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
43
|
+
return Number.NaN;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
var num1Changed = float2Fixed(num1);
|
package/esm/index.js
CHANGED
|
@@ -54,38 +54,35 @@ export { default as padZero } from './padZero';
|
|
|
54
54
|
* @see 参考 {@link https://github.com/camsong/blog/issues/9|JavaScript 浮点数陷阱及解法}
|
|
55
55
|
* @see 参考 {@link https://2zbuy.csb.app/|JS浮点数计算测试}
|
|
56
56
|
* @example
|
|
57
|
-
* // 从 4.12.0
|
|
58
|
-
* //
|
|
57
|
+
* // 从 4.12.0 版本开始,规范了有效数值。(注意:4.12.3 对有效数值重新定义)
|
|
58
|
+
* // 有效数值即能通过 Number(value) 转为数字,且不能为 NaN 。
|
|
59
59
|
*
|
|
60
60
|
* // 以下为有效数值:
|
|
61
61
|
* // 1. 数字: 1, -1, 1e-2, 1.312, 0.1, Infinity
|
|
62
|
-
* // 2.
|
|
62
|
+
* // 2. 字符串: '1', '10e2', '-1', '0.1', '', ' ', ' 15', ' 15 '
|
|
63
|
+
* // 4. 其他类型: null, new Date(), [], new Array(), true, false,...
|
|
63
64
|
*
|
|
64
65
|
* // 以下为无效数值:
|
|
65
|
-
* // 1.
|
|
66
|
-
* // 2.
|
|
67
|
-
*
|
|
68
|
-
* // 注意:Number('') 、 Number(' ') 、 Number(new Date) 、 Number(true)等虽然可以返回数字,但都不是有效数值。
|
|
69
|
-
* // 因为它们分别是空字符串、空格字符串和非数字或字符串类型。
|
|
70
|
-
*
|
|
66
|
+
* // 1. 字符串: '1a', '-12a', '10.2.2', '10e2.1'
|
|
67
|
+
* // 2. 其他类型: undefined, [], {}, Symbol(), function(){}, ()=>{}
|
|
71
68
|
*
|
|
72
69
|
* // 计算说明:
|
|
73
|
-
* // 1
|
|
74
|
-
* plus(); // NaN
|
|
75
|
-
* plus(
|
|
76
|
-
* plus(
|
|
70
|
+
* // 四则运算的第二个参数都是有默认值(乘数和除数默认1,加数和减数默认0)
|
|
71
|
+
* plus(); // NaN 0个参数时,被加数转换为 Number(undefined) NaN ,NaN+0 = NaN 。其他计算方法如果没有参数一样返回 NaN 。
|
|
72
|
+
* plus(0.1); // 0.1 第二个参数,加数默认为 0
|
|
73
|
+
* plus(undefined, 0.1); // NaN 第一个参数被加数转换为 Number(undefined) NaN ,NaN+0 = NaN 。其他计算方法如果第一个参数为无效数值一样返回 NaN 。
|
|
74
|
+
* plus(true, null); // 1 Number(true) 转换为 1 , Number(null) 转换为 0 , 1+0=1
|
|
77
75
|
*
|
|
78
|
-
* // 2.
|
|
79
|
-
* plus(0.1); //
|
|
80
|
-
* plus(
|
|
81
|
-
* plus(true, 0.1); //
|
|
82
|
-
* plus(true, 0.1, 0.2); // 0.3
|
|
83
|
-
* plus('', 0.1, ' ', new Date(), 0.2); // 0.3
|
|
84
|
-
* plus(0.1, true, 0.2, null); // 0.3
|
|
76
|
+
* // 2. 参数中包含无效数值,返回NaN
|
|
77
|
+
* plus('0.1', ' a'); // NaN
|
|
78
|
+
* plus(true, {}); // NaN
|
|
79
|
+
* plus(true, 0.1, Symbol()); // NaN
|
|
85
80
|
*
|
|
86
|
-
* //
|
|
87
|
-
*
|
|
88
|
-
* plus(0.1,
|
|
81
|
+
* // 注意:
|
|
82
|
+
* // 如果第二个及后面的参数如果值为 undefined 取默认值,即乘除数取 1 ,加减法取 0 。
|
|
83
|
+
* plus(0.1, undefined); // 0.1
|
|
84
|
+
* plus(0.1, undefined, 0.2, undefined); // 0.3 后面的 undefined 取默认值 0
|
|
85
|
+
* times(0.1, undefined, 0.2, undefined); // 0.02 后面的 undefined 取默认值 1
|
|
89
86
|
*
|
|
90
87
|
*/
|
|
91
88
|
|
package/esm/minus.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
-
import { digitLength,
|
|
2
|
+
import { digitLength, transformEffectiveNumber } from './utils/math.util';
|
|
3
3
|
import times from './times';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 精确减法,支持多个数相减,减数默认为 0 。
|
|
6
6
|
*
|
|
7
7
|
* @static
|
|
8
8
|
* @alias module:Math.minus
|
|
@@ -27,18 +27,20 @@ function minus() {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
var num1 = nums[0],
|
|
30
|
-
|
|
30
|
+
_nums$ = nums[1],
|
|
31
|
+
num2 = _nums$ === void 0 ? 0 : _nums$,
|
|
31
32
|
rest = nums.slice(2);
|
|
32
33
|
|
|
33
34
|
if (rest.length > 0) {
|
|
34
35
|
return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
|
|
35
|
-
}
|
|
36
|
+
}
|
|
36
37
|
|
|
38
|
+
num1 = transformEffectiveNumber(num1);
|
|
39
|
+
num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
40
|
+
// @ts-ignore
|
|
37
41
|
|
|
38
|
-
if (
|
|
39
|
-
return
|
|
40
|
-
} else if (!isEffectiveNumeric(num2)) {
|
|
41
|
-
return Number(num1);
|
|
42
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
43
|
+
return Number.NaN;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
|
package/esm/plus.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
-
import { digitLength,
|
|
2
|
+
import { digitLength, transformEffectiveNumber } from './utils/math.util';
|
|
3
3
|
import times from './times';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 精确加法,支持多个数相加,加数默认为 0 。
|
|
6
6
|
*
|
|
7
7
|
* @static
|
|
8
8
|
* @alias module:Math.plus
|
|
@@ -27,18 +27,20 @@ function plus() {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
var num1 = nums[0],
|
|
30
|
-
|
|
30
|
+
_nums$ = nums[1],
|
|
31
|
+
num2 = _nums$ === void 0 ? 0 : _nums$,
|
|
31
32
|
rest = nums.slice(2);
|
|
32
33
|
|
|
33
34
|
if (rest.length > 0) {
|
|
34
35
|
return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
|
|
35
|
-
}
|
|
36
|
+
}
|
|
36
37
|
|
|
38
|
+
num1 = transformEffectiveNumber(num1);
|
|
39
|
+
num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
40
|
+
// @ts-ignore
|
|
37
41
|
|
|
38
|
-
if (
|
|
39
|
-
return
|
|
40
|
-
} else if (!isEffectiveNumeric(num2)) {
|
|
41
|
-
return Number(num1);
|
|
42
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
43
|
+
return Number.NaN;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
|
package/esm/round.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import divide from './divide';
|
|
2
2
|
import times from './times';
|
|
3
|
-
import {
|
|
3
|
+
import { transformEffectiveNumber } from './utils/math.util';
|
|
4
4
|
/**
|
|
5
5
|
* 四舍五入,支持设置精度
|
|
6
6
|
*
|
|
@@ -24,10 +24,11 @@ import { isEffectiveNumeric } from './utils/math.util';
|
|
|
24
24
|
|
|
25
25
|
function round(num) {
|
|
26
26
|
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
27
|
+
num = transformEffectiveNumber(num); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
28
|
+
// @ts-ignore
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return NaN;
|
|
30
|
+
if (isNaN(num)) {
|
|
31
|
+
return Number.NaN;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
var base = Math.pow(10, precision);
|
package/esm/times.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
-
import { digitLength, float2Fixed, checkBoundary,
|
|
2
|
+
import { digitLength, float2Fixed, checkBoundary, transformEffectiveNumber } from './utils/math.util';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 精确乘法,支持多个数相乘,乘数默认为 1 。
|
|
5
5
|
*
|
|
6
6
|
* @static
|
|
7
7
|
* @alias module:Math.times
|
|
@@ -26,18 +26,20 @@ function times() {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
var num1 = nums[0],
|
|
29
|
-
|
|
29
|
+
_nums$ = nums[1],
|
|
30
|
+
num2 = _nums$ === void 0 ? 1 : _nums$,
|
|
30
31
|
rest = nums.slice(2);
|
|
31
32
|
|
|
32
33
|
if (rest.length > 0) {
|
|
33
34
|
return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
|
|
34
|
-
}
|
|
35
|
+
}
|
|
35
36
|
|
|
37
|
+
num1 = transformEffectiveNumber(num1);
|
|
38
|
+
num2 = transformEffectiveNumber(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
39
|
+
// @ts-ignore
|
|
36
40
|
|
|
37
|
-
if (
|
|
38
|
-
return
|
|
39
|
-
} else if (!isEffectiveNumeric(num2)) {
|
|
40
|
-
return Number(num1);
|
|
41
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
42
|
+
return Number.NaN;
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
var num1Changed = float2Fixed(num1);
|
package/esm/utils/config.js
CHANGED
package/esm/utils/math.util.js
CHANGED
|
@@ -6,35 +6,45 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } from './constants';
|
|
8
8
|
import devWarn from './devWarn';
|
|
9
|
-
import { isNumber, isString } from './type';
|
|
9
|
+
import { isNumber, isString, isSymbol } from './type';
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* 将值转换为有效数值
|
|
12
12
|
*
|
|
13
|
-
* @param {*} value
|
|
14
|
-
* @returns {
|
|
13
|
+
* @param {*} value 要转换的值
|
|
14
|
+
* @returns {number|string} 有效数值
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
export function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} // 避免空字符串 或 带空格的字符串
|
|
21
|
-
|
|
17
|
+
export function transformEffectiveNumber(value) {
|
|
18
|
+
/** @type {string|number|undefined} */
|
|
19
|
+
var ret;
|
|
22
20
|
|
|
23
21
|
if (isString(value)) {
|
|
24
|
-
|
|
25
|
-
// Number(' ') => 0
|
|
22
|
+
ret = value.trim(); // ' 15' ' 15 ' 兼容 Number(string) 处理
|
|
26
23
|
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
24
|
+
if (ret === '') {
|
|
25
|
+
ret = Number(ret);
|
|
26
|
+
} else if (Number.isNaN(Number(ret))) {
|
|
27
|
+
// string如果可以转换为number,默认不转换为number类型
|
|
28
|
+
ret = Number.NaN;
|
|
33
29
|
}
|
|
30
|
+
} else if (isSymbol(value)) {
|
|
31
|
+
// 例如 Symbol 包装器对象将会报错
|
|
32
|
+
// symObj = Object(Symbol());
|
|
33
|
+
// Number(symObj); // TypeError: Cannot convert a Symbol value to a number
|
|
34
|
+
ret = Number.NaN;
|
|
35
|
+
} else if (!isNumber(value)) {
|
|
36
|
+
// 其余非数字类型通过 Number 转换
|
|
37
|
+
ret = Number(value);
|
|
38
|
+
} else {
|
|
39
|
+
ret = value;
|
|
34
40
|
}
|
|
35
41
|
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
if (Number.isNaN(ret)) {
|
|
43
|
+
return Number.NaN;
|
|
44
|
+
} // @ts-ignore
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
return ret;
|
|
38
48
|
}
|
|
39
49
|
/**
|
|
40
50
|
* 是否为科学计数法数字
|
package/lib/divide.js
CHANGED
|
@@ -24,23 +24,23 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
24
24
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* 精确除法,支持多个数相除,除数默认为 1 。
|
|
28
28
|
*
|
|
29
29
|
* @static
|
|
30
30
|
* @alias module:Math.divide
|
|
31
31
|
* @since 3.1.0
|
|
32
|
-
* @param {...number|string} nums
|
|
32
|
+
* @param {...number|string} nums 被除数和除数
|
|
33
33
|
* @returns {number} 商数
|
|
34
34
|
* @example
|
|
35
35
|
*
|
|
36
|
-
* divide(1.21
|
|
37
|
-
* //
|
|
38
|
-
*
|
|
39
|
-
* divide(1000, 10, 10);
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* divide(
|
|
43
|
-
* //
|
|
36
|
+
* divide(1.21); // 1.21 除数默认为 1 ,即 1.21/1 = 1.21
|
|
37
|
+
* divide(1.21, 1.1); // 1.1
|
|
38
|
+
* divide(1000, 10, 10); // 10
|
|
39
|
+
* divide(1000, 10, 10, 10); // 1
|
|
40
|
+
*
|
|
41
|
+
* divide(); // NaN 如果没有传入参数,被除数默认为 undefined 。 Number(undefined) 转换为 NaN ,NaN/1 = NaN
|
|
42
|
+
* divide(null); // 0 Number(null) 转换为 0 , 0/1 = 0
|
|
43
|
+
* divide('1.5 ', 0.5); // 3 Number('1.5 ') 转换为 1.5 ,1.5/0.5 = 3
|
|
44
44
|
*/
|
|
45
45
|
function divide() {
|
|
46
46
|
for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
@@ -48,18 +48,20 @@ function divide() {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
var num1 = nums[0],
|
|
51
|
-
|
|
51
|
+
_nums$ = nums[1],
|
|
52
|
+
num2 = _nums$ === void 0 ? 1 : _nums$,
|
|
52
53
|
rest = nums.slice(2);
|
|
53
54
|
|
|
54
55
|
if (rest.length > 0) {
|
|
55
56
|
return divide.apply(void 0, [divide(num1, num2)].concat(_toConsumableArray(rest)));
|
|
56
|
-
}
|
|
57
|
+
}
|
|
57
58
|
|
|
59
|
+
num1 = (0, _math.transformEffectiveNumber)(num1);
|
|
60
|
+
num2 = (0, _math.transformEffectiveNumber)(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
61
|
+
// @ts-ignore
|
|
58
62
|
|
|
59
|
-
if (
|
|
60
|
-
return
|
|
61
|
-
} else if (!(0, _math.isEffectiveNumeric)(num2)) {
|
|
62
|
-
return Number(num1);
|
|
63
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
64
|
+
return Number.NaN;
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
var num1Changed = (0, _math.float2Fixed)(num1);
|
package/lib/minus.js
CHANGED
|
@@ -24,7 +24,7 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
24
24
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* 精确减法,支持多个数相减,减数默认为 0 。
|
|
28
28
|
*
|
|
29
29
|
* @static
|
|
30
30
|
* @alias module:Math.minus
|
|
@@ -48,18 +48,20 @@ function minus() {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
var num1 = nums[0],
|
|
51
|
-
|
|
51
|
+
_nums$ = nums[1],
|
|
52
|
+
num2 = _nums$ === void 0 ? 0 : _nums$,
|
|
52
53
|
rest = nums.slice(2);
|
|
53
54
|
|
|
54
55
|
if (rest.length > 0) {
|
|
55
56
|
return minus.apply(void 0, [minus(num1, num2)].concat(_toConsumableArray(rest)));
|
|
56
|
-
}
|
|
57
|
+
}
|
|
57
58
|
|
|
59
|
+
num1 = (0, _math.transformEffectiveNumber)(num1);
|
|
60
|
+
num2 = (0, _math.transformEffectiveNumber)(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
61
|
+
// @ts-ignore
|
|
58
62
|
|
|
59
|
-
if (
|
|
60
|
-
return
|
|
61
|
-
} else if (!(0, _math.isEffectiveNumeric)(num2)) {
|
|
62
|
-
return Number(num1);
|
|
63
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
64
|
+
return Number.NaN;
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
var baseNum = Math.pow(10, Math.max((0, _math.digitLength)(num1), (0, _math.digitLength)(num2)));
|
package/lib/plus.js
CHANGED
|
@@ -24,7 +24,7 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
24
24
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* 精确加法,支持多个数相加,加数默认为 0 。
|
|
28
28
|
*
|
|
29
29
|
* @static
|
|
30
30
|
* @alias module:Math.plus
|
|
@@ -48,18 +48,20 @@ function plus() {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
var num1 = nums[0],
|
|
51
|
-
|
|
51
|
+
_nums$ = nums[1],
|
|
52
|
+
num2 = _nums$ === void 0 ? 0 : _nums$,
|
|
52
53
|
rest = nums.slice(2);
|
|
53
54
|
|
|
54
55
|
if (rest.length > 0) {
|
|
55
56
|
return plus.apply(void 0, [plus(num1, num2)].concat(_toConsumableArray(rest)));
|
|
56
|
-
}
|
|
57
|
+
}
|
|
57
58
|
|
|
59
|
+
num1 = (0, _math.transformEffectiveNumber)(num1);
|
|
60
|
+
num2 = (0, _math.transformEffectiveNumber)(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
61
|
+
// @ts-ignore
|
|
58
62
|
|
|
59
|
-
if (
|
|
60
|
-
return
|
|
61
|
-
} else if (!(0, _math.isEffectiveNumeric)(num2)) {
|
|
62
|
-
return Number(num1);
|
|
63
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
64
|
+
return Number.NaN;
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
var baseNum = Math.pow(10, Math.max((0, _math.digitLength)(num1), (0, _math.digitLength)(num2)));
|
package/lib/round.js
CHANGED
|
@@ -35,10 +35,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
|
|
|
35
35
|
*/
|
|
36
36
|
function round(num) {
|
|
37
37
|
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
38
|
+
num = (0, _math.transformEffectiveNumber)(num); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
39
|
+
// @ts-ignore
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return NaN;
|
|
41
|
+
if (isNaN(num)) {
|
|
42
|
+
return Number.NaN;
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
var base = Math.pow(10, precision);
|
package/lib/times.js
CHANGED
|
@@ -20,7 +20,7 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
|
|
|
20
20
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* 精确乘法,支持多个数相乘,乘数默认为 1 。
|
|
24
24
|
*
|
|
25
25
|
* @static
|
|
26
26
|
* @alias module:Math.times
|
|
@@ -44,18 +44,20 @@ function times() {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
var num1 = nums[0],
|
|
47
|
-
|
|
47
|
+
_nums$ = nums[1],
|
|
48
|
+
num2 = _nums$ === void 0 ? 1 : _nums$,
|
|
48
49
|
rest = nums.slice(2);
|
|
49
50
|
|
|
50
51
|
if (rest.length > 0) {
|
|
51
52
|
return times.apply(void 0, [times(num1, num2)].concat(_toConsumableArray(rest)));
|
|
52
|
-
}
|
|
53
|
+
}
|
|
53
54
|
|
|
55
|
+
num1 = (0, _math.transformEffectiveNumber)(num1);
|
|
56
|
+
num2 = (0, _math.transformEffectiveNumber)(num2); // 兼容处理,如果参数包含无效数值时,返回 NaN
|
|
57
|
+
// @ts-ignore
|
|
54
58
|
|
|
55
|
-
if (
|
|
56
|
-
return
|
|
57
|
-
} else if (!(0, _math.isEffectiveNumeric)(num2)) {
|
|
58
|
-
return Number(num1);
|
|
59
|
+
if (isNaN(num1) || isNaN(num2)) {
|
|
60
|
+
return Number.NaN;
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
var num1Changed = (0, _math.float2Fixed)(num1);
|
package/lib/utils/config.js
CHANGED
package/lib/utils/math.util.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.transformEffectiveNumber = transformEffectiveNumber;
|
|
7
7
|
exports.isScientificNumber = isScientificNumber;
|
|
8
8
|
exports.strip = strip;
|
|
9
9
|
exports.digitLength = digitLength;
|
|
@@ -28,32 +28,42 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
|
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
31
|
+
* 将值转换为有效数值
|
|
32
32
|
*
|
|
33
|
-
* @param {*} value
|
|
34
|
-
* @returns {
|
|
33
|
+
* @param {*} value 要转换的值
|
|
34
|
+
* @returns {number|string} 有效数值
|
|
35
35
|
*/
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
} // 避免空字符串 或 带空格的字符串
|
|
40
|
-
|
|
36
|
+
function transformEffectiveNumber(value) {
|
|
37
|
+
/** @type {string|number|undefined} */
|
|
38
|
+
var ret;
|
|
41
39
|
|
|
42
40
|
if ((0, _type.isString)(value)) {
|
|
43
|
-
|
|
44
|
-
// Number(' ') => 0
|
|
41
|
+
ret = value.trim(); // ' 15' ' 15 ' 兼容 Number(string) 处理
|
|
45
42
|
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
43
|
+
if (ret === '') {
|
|
44
|
+
ret = Number(ret);
|
|
45
|
+
} else if (Number.isNaN(Number(ret))) {
|
|
46
|
+
// string如果可以转换为number,默认不转换为number类型
|
|
47
|
+
ret = Number.NaN;
|
|
52
48
|
}
|
|
49
|
+
} else if ((0, _type.isSymbol)(value)) {
|
|
50
|
+
// 例如 Symbol 包装器对象将会报错
|
|
51
|
+
// symObj = Object(Symbol());
|
|
52
|
+
// Number(symObj); // TypeError: Cannot convert a Symbol value to a number
|
|
53
|
+
ret = Number.NaN;
|
|
54
|
+
} else if (!(0, _type.isNumber)(value)) {
|
|
55
|
+
// 其余非数字类型通过 Number 转换
|
|
56
|
+
ret = Number(value);
|
|
57
|
+
} else {
|
|
58
|
+
ret = value;
|
|
53
59
|
}
|
|
54
60
|
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
if (Number.isNaN(ret)) {
|
|
62
|
+
return Number.NaN;
|
|
63
|
+
} // @ts-ignore
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
return ret;
|
|
57
67
|
}
|
|
58
68
|
/**
|
|
59
69
|
* 是否为科学计数法数字
|
package/package.json
CHANGED
package/types/src/divide.d.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
export default divide;
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* 精确除法,支持多个数相除,除数默认为 1 。
|
|
4
4
|
*
|
|
5
5
|
* @static
|
|
6
6
|
* @alias module:Math.divide
|
|
7
7
|
* @since 3.1.0
|
|
8
|
-
* @param {...number|string} nums
|
|
8
|
+
* @param {...number|string} nums 被除数和除数
|
|
9
9
|
* @returns {number} 商数
|
|
10
10
|
* @example
|
|
11
11
|
*
|
|
12
|
-
* divide(1.21
|
|
13
|
-
* //
|
|
12
|
+
* divide(1.21); // 1.21 除数默认为 1 ,即 1.21/1 = 1.21
|
|
13
|
+
* divide(1.21, 1.1); // 1.1
|
|
14
|
+
* divide(1000, 10, 10); // 10
|
|
15
|
+
* divide(1000, 10, 10, 10); // 1
|
|
14
16
|
*
|
|
15
|
-
* divide(
|
|
16
|
-
* //
|
|
17
|
-
*
|
|
18
|
-
* divide(1000, 10, 10, 10);
|
|
19
|
-
* // => 1
|
|
17
|
+
* divide(); // NaN 如果没有传入参数,被除数默认为 undefined 。 Number(undefined) 转换为 NaN ,NaN/1 = NaN
|
|
18
|
+
* divide(null); // 0 Number(null) 转换为 0 , 0/1 = 0
|
|
19
|
+
* divide('1.5 ', 0.5); // 3 Number('1.5 ') 转换为 1.5 ,1.5/0.5 = 3
|
|
20
20
|
*/
|
|
21
21
|
declare function divide(...nums: (number | string)[]): number;
|
package/types/src/minus.d.ts
CHANGED