11import convert from './convert' ;
22
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 ) ;
66
77 return node ;
88}
@@ -33,10 +33,10 @@ function isValueType(type) {
3333 return false ;
3434}
3535
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 ) ;
4040
4141 if ( left . type === "MathExpression" && right . type === "MathExpression" ) {
4242
@@ -46,13 +46,13 @@ function convertMathExpression(node, precision) {
4646 ( left . operator === '+' && right . operator === '-' ) ) ) {
4747
4848 if ( isEqual ( left . right , right . right ) )
49- nodes = convert ( left . left , right . left , precision ) ;
49+ nodes = convert ( left . left , right . left , options ) ;
5050
5151 else if ( isEqual ( left . right , right . left ) )
52- nodes = convert ( left . left , right . right , precision ) ;
52+ nodes = convert ( left . left , right . right , options ) ;
5353
54- left = reduce ( nodes . left , precision ) ;
55- right = reduce ( nodes . right , precision ) ;
54+ left = reduce ( nodes . left , options ) ;
55+ right = reduce ( nodes . right , options ) ;
5656
5757 }
5858 }
@@ -69,14 +69,14 @@ function flip(operator) {
6969function flipValue ( node ) {
7070 if ( isValueType ( node . type ) )
7171 node . value = - node . value ;
72- else if ( node . type == 'MathExpression' ) {
72+ else if ( node . type === 'MathExpression' ) {
7373 node . left = flipValue ( node . left ) ;
7474 node . right = flipValue ( node . right ) ;
7575 }
7676 return node ;
7777}
7878
79- function reduceAddSubExpression ( node , precision ) {
79+ function reduceAddSubExpression ( node , options ) {
8080 const { left, right, operator : op } = node ;
8181
8282 if ( left . type === 'Function' || right . type === 'Function' )
@@ -122,10 +122,10 @@ function reduceAddSubExpression(node, precision) {
122122 operator : op ,
123123 left : left ,
124124 right : right . left
125- } , precision ) ;
125+ } , options ) ;
126126 node . right = right . right ;
127127 node . operator = op === '-' ? flip ( right . operator ) : right . operator ;
128- return reduce ( node , precision ) ;
128+ return reduce ( node , options ) ;
129129 }
130130 // value + (something + value) => (value + value) + something
131131 // value + (something - value) => (value - value) + something
@@ -138,15 +138,15 @@ function reduceAddSubExpression(node, precision) {
138138 operator : op === '-' ? flip ( right . operator ) : right . operator ,
139139 left : left ,
140140 right : right . right
141- } , precision ) ;
141+ } , options ) ;
142142 node . right = right . left ;
143- return reduce ( node , precision ) ;
143+ return reduce ( node , options ) ;
144144 }
145145 // value - (something + something) => value - something - something
146146 else if ( op === '-' && right . operator === '+' ) {
147147 node = Object . assign ( { } , node ) ;
148148 node . right . operator = '-' ;
149- return reduce ( node , precision ) ;
149+ return reduce ( node , options ) ;
150150 }
151151 }
152152
@@ -167,8 +167,8 @@ function reduceAddSubExpression(node, precision) {
167167 operator : op ,
168168 left : left . left ,
169169 right : right
170- } , precision ) ;
171- return reduce ( node , precision ) ;
170+ } , options ) ;
171+ return reduce ( node , options ) ;
172172 }
173173 // (something + value) + value => something + (value + value)
174174 // (something - value1) + value2 => something - (value2 - value1)
@@ -182,7 +182,7 @@ function reduceAddSubExpression(node, precision) {
182182 operator : flip ( op ) ,
183183 left : left . right ,
184184 right : right
185- } , precision ) ;
185+ } , options ) ;
186186 if ( node . right . value && node . right . value < 0 ) {
187187 node . right . value = Math . abs ( node . right . value ) ;
188188 node . operator = '+' ;
@@ -196,16 +196,16 @@ function reduceAddSubExpression(node, precision) {
196196 operator : op ,
197197 left : left . right ,
198198 right : right
199- } , precision ) ;
199+ } , options ) ;
200200 }
201201 if ( node . right . value < 0 ) {
202202 node . right . value *= - 1 ;
203203 node . operator = node . operator === '-' ? '+' : '-' ;
204204 }
205- return reduce ( node , precision ) ;
205+ return reduce ( node , options ) ;
206206 }
207207 }
208-
208+
209209 if (
210210 left . type === 'MathExpression' && right . type === 'MathExpression' &&
211211 op === '-' && right . operator === '-'
@@ -269,15 +269,15 @@ function reduceMultiplicationExpression(node) {
269269 return node ;
270270}
271271
272- function reduceMathExpression ( node , precision ) {
273- node = convertMathExpression ( node , precision ) ;
272+ function reduceMathExpression ( node , options ) {
273+ node = convertMathExpression ( node , options ) ;
274274
275275 switch ( node . operator ) {
276276 case "+" :
277277 case "-" :
278- return reduceAddSubExpression ( node , precision ) ;
278+ return reduceAddSubExpression ( node , options ) ;
279279 case "/" :
280- return reduceDivisionExpression ( node , precision ) ;
280+ return reduceDivisionExpression ( node , options ) ;
281281 case "*" :
282282 return reduceMultiplicationExpression ( node ) ;
283283 }
0 commit comments