Skip to content

Commit 79d76c7

Browse files
committed
Used bignumber.js for add and minus
1 parent 1e7eef1 commit 79d76c7

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed
+11-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import {toNumber} from './../../helper/number';
22
import {ERROR_VALUE} from './../../error';
3+
import BigNumber from 'bignumber.js';
34

45
export const SYMBOL = '+';
56

67
export default function func(first, ...rest) {
7-
const result = rest.reduce((acc, value) => acc + toNumber(value), toNumber(first));
8+
try {
9+
const result = rest.reduce((acc, value) => {
10+
return (new BigNumber(acc)).plus(new BigNumber(value)).toNumber();
11+
}, first);
812

9-
if (isNaN(result)) {
13+
if (isNaN(result)) {
14+
throw Error(ERROR_VALUE);
15+
}
16+
17+
return result;
18+
} catch (error) {
1019
throw Error(ERROR_VALUE);
1120
}
12-
13-
return result;
1421
};
1522

1623
func.SYMBOL = SYMBOL;
+11-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import {toNumber} from './../../helper/number';
22
import {ERROR_VALUE} from './../../error';
3+
import BigNumber from 'bignumber.js';
34

45
export const SYMBOL = '-';
56

67
export default function func(first, ...rest) {
7-
const result = rest.reduce((acc, value) => acc - toNumber(value), toNumber(first));
8+
try {
9+
const result = rest.reduce((acc, value) => {
10+
return (new BigNumber(acc)).minus(new BigNumber(value)).toNumber();
11+
}, first);
812

9-
if (isNaN(result)) {
13+
if (isNaN(result)) {
14+
throw Error(ERROR_VALUE);
15+
}
16+
17+
return result;
18+
} catch (error) {
1019
throw Error(ERROR_VALUE);
1120
}
12-
13-
return result;
1421
};
1522

1623
func.SYMBOL = SYMBOL;

test/unit/evaluate-by-operator/operator/add.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('add operator', () => {
99
expect(func(2, 8.8)).to.eq(10.8);
1010
expect(func('2', 8.8)).to.eq(10.8);
1111
expect(func('2', '8.8')).to.eq(10.8);
12-
expect(func('2', '-8.8', 6, 0.4)).to.eq(-0.4000000000000007);
12+
expect(func('2', '-8.8', 6, 0.4)).to.eq(-0.4);
1313
expect(() => func('foo', ' ', 'bar', ' baz')).to.throw('VALUE');
1414
expect(() => func('foo', 2)).to.throw('VALUE');
1515
});

test/unit/evaluate-by-operator/operator/minus.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ describe('minus operator', () => {
66
});
77

88
it('should correctly process values', () => {
9-
expect(func(2, 8.8)).to.eq(-6.800000000000001);
10-
expect(func('2', 8.8)).to.eq(-6.800000000000001);
11-
expect(func('2', '8.8')).to.eq(-6.800000000000001);
9+
expect(func(2, 8.8)).to.eq(-6.8);
10+
expect(func('2', 8.8)).to.eq(-6.8);
11+
expect(func('2', '8.8')).to.eq(-6.8);
1212
expect(func('2', '-8.8', 6, 0.4)).to.eq(4.4);
1313
expect(() => func('foo', ' ', 'bar', ' baz')).to.throw('VALUE');
1414
expect(() => func('foo', 2)).to.throw('VALUE');

0 commit comments

Comments
 (0)