1
1
import convert from './convert' ;
2
2
3
- function reduce ( node , precision ) {
4
- if ( node . type === "MathExpression" )
5
- return reduceMathExpression ( node , precision ) ;
3
+ function reduce ( node , options ) {
4
+ if ( node . type === "MathExpression" )
5
+ return reduceMathExpression ( node , options ) ;
6
6
7
7
return node ;
8
8
}
@@ -33,10 +33,10 @@ function isValueType(type) {
33
33
return false ;
34
34
}
35
35
36
- function convertMathExpression ( node , precision ) {
37
- let nodes = convert ( node . left , node . right , precision ) ;
38
- let left = reduce ( nodes . left , precision ) ;
39
- let right = reduce ( nodes . right , precision ) ;
36
+ function convertMathExpression ( node , options ) {
37
+ let nodes = convert ( node . left , node . right , options ) ;
38
+ let left = reduce ( nodes . left , options ) ;
39
+ let right = reduce ( nodes . right , options ) ;
40
40
41
41
if ( left . type === "MathExpression" && right . type === "MathExpression" ) {
42
42
@@ -46,13 +46,13 @@ function convertMathExpression(node, precision) {
46
46
( left . operator === '+' && right . operator === '-' ) ) ) {
47
47
48
48
if ( isEqual ( left . right , right . right ) )
49
- nodes = convert ( left . left , right . left , precision ) ;
49
+ nodes = convert ( left . left , right . left , options ) ;
50
50
51
51
else if ( isEqual ( left . right , right . left ) )
52
- nodes = convert ( left . left , right . right , precision ) ;
52
+ nodes = convert ( left . left , right . right , options ) ;
53
53
54
- left = reduce ( nodes . left , precision ) ;
55
- right = reduce ( nodes . right , precision ) ;
54
+ left = reduce ( nodes . left , options ) ;
55
+ right = reduce ( nodes . right , options ) ;
56
56
57
57
}
58
58
}
@@ -69,14 +69,14 @@ function flip(operator) {
69
69
function flipValue ( node ) {
70
70
if ( isValueType ( node . type ) )
71
71
node . value = - node . value ;
72
- else if ( node . type == 'MathExpression' ) {
72
+ else if ( node . type === 'MathExpression' ) {
73
73
node . left = flipValue ( node . left ) ;
74
74
node . right = flipValue ( node . right ) ;
75
75
}
76
76
return node ;
77
77
}
78
78
79
- function reduceAddSubExpression ( node , precision ) {
79
+ function reduceAddSubExpression ( node , options ) {
80
80
const { left, right, operator : op } = node ;
81
81
82
82
if ( left . type === 'Function' || right . type === 'Function' )
@@ -122,10 +122,10 @@ function reduceAddSubExpression(node, precision) {
122
122
operator : op ,
123
123
left : left ,
124
124
right : right . left
125
- } , precision ) ;
125
+ } , options ) ;
126
126
node . right = right . right ;
127
127
node . operator = op === '-' ? flip ( right . operator ) : right . operator ;
128
- return reduce ( node , precision ) ;
128
+ return reduce ( node , options ) ;
129
129
}
130
130
// value + (something + value) => (value + value) + something
131
131
// value + (something - value) => (value - value) + something
@@ -138,15 +138,15 @@ function reduceAddSubExpression(node, precision) {
138
138
operator : op === '-' ? flip ( right . operator ) : right . operator ,
139
139
left : left ,
140
140
right : right . right
141
- } , precision ) ;
141
+ } , options ) ;
142
142
node . right = right . left ;
143
- return reduce ( node , precision ) ;
143
+ return reduce ( node , options ) ;
144
144
}
145
145
// value - (something + something) => value - something - something
146
146
else if ( op === '-' && right . operator === '+' ) {
147
147
node = Object . assign ( { } , node ) ;
148
148
node . right . operator = '-' ;
149
- return reduce ( node , precision ) ;
149
+ return reduce ( node , options ) ;
150
150
}
151
151
}
152
152
@@ -167,8 +167,8 @@ function reduceAddSubExpression(node, precision) {
167
167
operator : op ,
168
168
left : left . left ,
169
169
right : right
170
- } , precision ) ;
171
- return reduce ( node , precision ) ;
170
+ } , options ) ;
171
+ return reduce ( node , options ) ;
172
172
}
173
173
// (something + value) + value => something + (value + value)
174
174
// (something - value1) + value2 => something - (value2 - value1)
@@ -182,7 +182,7 @@ function reduceAddSubExpression(node, precision) {
182
182
operator : flip ( op ) ,
183
183
left : left . right ,
184
184
right : right
185
- } , precision ) ;
185
+ } , options ) ;
186
186
if ( node . right . value && node . right . value < 0 ) {
187
187
node . right . value = Math . abs ( node . right . value ) ;
188
188
node . operator = '+' ;
@@ -196,16 +196,16 @@ function reduceAddSubExpression(node, precision) {
196
196
operator : op ,
197
197
left : left . right ,
198
198
right : right
199
- } , precision ) ;
199
+ } , options ) ;
200
200
}
201
201
if ( node . right . value < 0 ) {
202
202
node . right . value *= - 1 ;
203
203
node . operator = node . operator === '-' ? '+' : '-' ;
204
204
}
205
- return reduce ( node , precision ) ;
205
+ return reduce ( node , options ) ;
206
206
}
207
207
}
208
-
208
+
209
209
if (
210
210
left . type === 'MathExpression' && right . type === 'MathExpression' &&
211
211
op === '-' && right . operator === '-'
@@ -269,15 +269,15 @@ function reduceMultiplicationExpression(node) {
269
269
return node ;
270
270
}
271
271
272
- function reduceMathExpression ( node , precision ) {
273
- node = convertMathExpression ( node , precision ) ;
272
+ function reduceMathExpression ( node , options ) {
273
+ node = convertMathExpression ( node , options ) ;
274
274
275
275
switch ( node . operator ) {
276
276
case "+" :
277
277
case "-" :
278
- return reduceAddSubExpression ( node , precision ) ;
278
+ return reduceAddSubExpression ( node , options ) ;
279
279
case "/" :
280
- return reduceDivisionExpression ( node , precision ) ;
280
+ return reduceDivisionExpression ( node , options ) ;
281
281
case "*" :
282
282
return reduceMultiplicationExpression ( node ) ;
283
283
}
0 commit comments