1
1
import convertUnit from "./convertUnit" ;
2
2
3
- function reduce ( node , precision ) {
4
- if ( node . type === "MathExpression" ) {
5
- return reduceMathExpression ( node , precision ) ;
6
- }
7
-
8
- return node ;
9
- }
10
-
11
3
function isEqual ( left , right ) {
12
4
return left . type === right . type && left . value === right . value ;
13
5
}
@@ -34,58 +26,6 @@ function isValueType(type) {
34
26
return false ;
35
27
}
36
28
37
- function covertNodesUnits ( left , right , precision ) {
38
- switch ( left . type ) {
39
- case 'LengthValue' :
40
- case 'AngleValue' :
41
- case 'TimeValue' :
42
- case 'FrequencyValue' :
43
- case 'ResolutionValue' :
44
- if ( right . type === left . type && right . unit && left . unit ) {
45
- const converted = convertUnit ( right . value , right . unit , left . unit , precision ) ;
46
-
47
- right = {
48
- type : left . type ,
49
- value : converted ,
50
- unit : left . unit ,
51
- } ;
52
- }
53
-
54
- return { left, right } ;
55
- default :
56
- return { left, right } ;
57
- }
58
- }
59
-
60
- function convertMathExpression ( node , precision ) {
61
- let nodes = covertNodesUnits ( node . left , node . right , precision ) ;
62
- let left = reduce ( nodes . left , precision ) ;
63
- let right = reduce ( nodes . right , precision ) ;
64
-
65
- if ( left . type === "MathExpression" && right . type === "MathExpression" ) {
66
-
67
- if ( ( ( left . operator === '/' && right . operator === '*' ) ||
68
- ( left . operator === '-' && right . operator === '+' ) ) ||
69
- ( ( left . operator === '*' && right . operator === '/' ) ||
70
- ( left . operator === '+' && right . operator === '-' ) ) ) {
71
-
72
- if ( isEqual ( left . right , right . right ) ) {
73
- nodes = covertNodesUnits ( left . left , right . left , precision ) ;
74
- } else if ( isEqual ( left . right , right . left ) ) {
75
- nodes = covertNodesUnits ( left . left , right . right , precision ) ;
76
- }
77
-
78
- left = reduce ( nodes . left , precision ) ;
79
- right = reduce ( nodes . right , precision ) ;
80
-
81
- }
82
- }
83
-
84
- node . left = left ;
85
- node . right = right ;
86
- return node ;
87
- }
88
-
89
29
function flip ( operator ) {
90
30
return operator === '+' ? '-' : '+' ;
91
31
}
@@ -133,7 +73,7 @@ function reduceAddSubExpression(node, precision) {
133
73
134
74
// value <op> (expr)
135
75
if (
136
- isValueType ( left . type ) &&
76
+ isValueType ( left . type ) &&
137
77
( right . operator === '+' || right . operator === '-' ) &&
138
78
right . type === 'MathExpression'
139
79
) {
@@ -150,7 +90,7 @@ function reduceAddSubExpression(node, precision) {
150
90
right : right . left
151
91
} , precision ) ;
152
92
node . right = right . right ;
153
- node . operator = op === '-' ? flip ( right . operator ) : right . operator ;
93
+ node . operator = op === '-' ? flip ( right . operator ) : right . operator ;
154
94
return reduce ( node , precision ) ;
155
95
}
156
96
// value + (something + value) => (value + value) + something
@@ -172,7 +112,7 @@ function reduceAddSubExpression(node, precision) {
172
112
173
113
// (expr) <op> value
174
114
if (
175
- left . type === 'MathExpression' &&
115
+ left . type === 'MathExpression' &&
176
116
( left . operator === '+' || left . operator === '-' ) &&
177
117
isValueType ( right . type )
178
118
) {
@@ -288,17 +228,67 @@ function reduceMultiplicationExpression(node) {
288
228
return node ;
289
229
}
290
230
291
- function reduceMathExpression ( node , precision ) {
292
- node = convertMathExpression ( node , precision ) ;
293
-
294
- switch ( node . operator ) {
295
- case "+" :
296
- case "-" :
297
- return reduceAddSubExpression ( node , precision ) ;
298
- case "/" :
299
- return reduceDivisionExpression ( node , precision ) ;
300
- case "*" :
301
- return reduceMultiplicationExpression ( node ) ;
231
+ function covertNodesUnits ( left , right , precision ) {
232
+ switch ( left . type ) {
233
+ case 'LengthValue' :
234
+ case 'AngleValue' :
235
+ case 'TimeValue' :
236
+ case 'FrequencyValue' :
237
+ case 'ResolutionValue' :
238
+ if ( right . type === left . type && right . unit && left . unit ) {
239
+ const converted = convertUnit ( right . value , right . unit , left . unit , precision ) ;
240
+
241
+ right = {
242
+ type : left . type ,
243
+ value : converted ,
244
+ unit : left . unit ,
245
+ } ;
246
+ }
247
+
248
+ return { left, right } ;
249
+ default :
250
+ return { left, right } ;
251
+ }
252
+ }
253
+
254
+ function reduce ( node , precision ) {
255
+ if ( node . type === "MathExpression" ) {
256
+ let nodes = covertNodesUnits ( node . left , node . right , precision ) ;
257
+ let left = reduce ( nodes . left , precision ) ;
258
+ let right = reduce ( nodes . right , precision ) ;
259
+
260
+ if ( left . type === "MathExpression" && right . type === "MathExpression" ) {
261
+ if ( ( ( left . operator === '/' && right . operator === '*' ) ||
262
+ ( left . operator === '-' && right . operator === '+' ) ) ||
263
+ ( ( left . operator === '*' && right . operator === '/' ) ||
264
+ ( left . operator === '+' && right . operator === '-' ) ) ) {
265
+
266
+ if ( isEqual ( left . right , right . right ) ) {
267
+ nodes = covertNodesUnits ( left . left , right . left , precision ) ;
268
+ } else if ( isEqual ( left . right , right . left ) ) {
269
+ nodes = covertNodesUnits ( left . left , right . right , precision ) ;
270
+ }
271
+
272
+ left = reduce ( nodes . left , precision ) ;
273
+ right = reduce ( nodes . right , precision ) ;
274
+
275
+ }
276
+ }
277
+
278
+ node . left = left ;
279
+ node . right = right ;
280
+
281
+ switch ( node . operator ) {
282
+ case "+" :
283
+ case "-" :
284
+ return reduceAddSubExpression ( node , precision ) ;
285
+ case "/" :
286
+ return reduceDivisionExpression ( node , precision ) ;
287
+ case "*" :
288
+ return reduceMultiplicationExpression ( node ) ;
289
+ }
290
+
291
+ return node ;
302
292
}
303
293
304
294
return node ;
0 commit comments