File tree 4 files changed +26
-12
lines changed
src/evaluate-by-operator/operator
test/unit/evaluate-by-operator/operator
4 files changed +26
-12
lines changed Original file line number Diff line number Diff line change 1
1
import { toNumber } from './../../helper/number' ;
2
2
import { ERROR_VALUE } from './../../error' ;
3
+ import BigNumber from 'bignumber.js' ;
3
4
4
5
export const SYMBOL = '+' ;
5
6
6
7
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 ) ;
8
12
9
- if ( isNaN ( result ) ) {
13
+ if ( isNaN ( result ) ) {
14
+ throw Error ( ERROR_VALUE ) ;
15
+ }
16
+
17
+ return result ;
18
+ } catch ( error ) {
10
19
throw Error ( ERROR_VALUE ) ;
11
20
}
12
-
13
- return result ;
14
21
} ;
15
22
16
23
func . SYMBOL = SYMBOL ;
Original file line number Diff line number Diff line change 1
1
import { toNumber } from './../../helper/number' ;
2
2
import { ERROR_VALUE } from './../../error' ;
3
+ import BigNumber from 'bignumber.js' ;
3
4
4
5
export const SYMBOL = '-' ;
5
6
6
7
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 ) ;
8
12
9
- if ( isNaN ( result ) ) {
13
+ if ( isNaN ( result ) ) {
14
+ throw Error ( ERROR_VALUE ) ;
15
+ }
16
+
17
+ return result ;
18
+ } catch ( error ) {
10
19
throw Error ( ERROR_VALUE ) ;
11
20
}
12
-
13
- return result ;
14
21
} ;
15
22
16
23
func . SYMBOL = SYMBOL ;
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ describe('add operator', () => {
9
9
expect ( func ( 2 , 8.8 ) ) . to . eq ( 10.8 ) ;
10
10
expect ( func ( '2' , 8.8 ) ) . to . eq ( 10.8 ) ;
11
11
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 ) ;
13
13
expect ( ( ) => func ( 'foo' , ' ' , 'bar' , ' baz' ) ) . to . throw ( 'VALUE' ) ;
14
14
expect ( ( ) => func ( 'foo' , 2 ) ) . to . throw ( 'VALUE' ) ;
15
15
} ) ;
Original file line number Diff line number Diff line change @@ -6,9 +6,9 @@ describe('minus operator', () => {
6
6
} ) ;
7
7
8
8
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 ) ;
12
12
expect ( func ( '2' , '-8.8' , 6 , 0.4 ) ) . to . eq ( 4.4 ) ;
13
13
expect ( ( ) => func ( 'foo' , ' ' , 'bar' , ' baz' ) ) . to . throw ( 'VALUE' ) ;
14
14
expect ( ( ) => func ( 'foo' , 2 ) ) . to . throw ( 'VALUE' ) ;
You can’t perform that action at this time.
0 commit comments