Skip to content

Commit 0f01794

Browse files
refactor: reducer
1 parent d71d9cf commit 0f01794

File tree

1 file changed

+64
-74
lines changed

1 file changed

+64
-74
lines changed

src/lib/reducer.js

+64-74
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import convertUnit from "./convertUnit";
22

3-
function reduce(node, precision) {
4-
if (node.type === "MathExpression") {
5-
return reduceMathExpression(node, precision);
6-
}
7-
8-
return node;
9-
}
10-
113
function isEqual(left, right) {
124
return left.type === right.type && left.value === right.value;
135
}
@@ -34,58 +26,6 @@ function isValueType(type) {
3426
return false;
3527
}
3628

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-
8929
function flip(operator) {
9030
return operator === '+' ? '-' : '+';
9131
}
@@ -133,7 +73,7 @@ function reduceAddSubExpression(node, precision) {
13373

13474
// value <op> (expr)
13575
if (
136-
isValueType(left.type) &&
76+
isValueType(left.type) &&
13777
(right.operator === '+' || right.operator === '-') &&
13878
right.type === 'MathExpression'
13979
) {
@@ -150,7 +90,7 @@ function reduceAddSubExpression(node, precision) {
15090
right: right.left
15191
}, precision);
15292
node.right = right.right;
153-
node.operator = op === '-' ? flip(right.operator) : right.operator;
93+
node.operator = op === '-' ? flip(right.operator) : right.operator;
15494
return reduce(node, precision);
15595
}
15696
// value + (something + value) => (value + value) + something
@@ -172,7 +112,7 @@ function reduceAddSubExpression(node, precision) {
172112

173113
// (expr) <op> value
174114
if (
175-
left.type === 'MathExpression' &&
115+
left.type === 'MathExpression' &&
176116
(left.operator === '+' || left.operator === '-') &&
177117
isValueType(right.type)
178118
) {
@@ -288,17 +228,67 @@ function reduceMultiplicationExpression(node) {
288228
return node;
289229
}
290230

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;
302292
}
303293

304294
return node;

0 commit comments

Comments
 (0)