Skip to content

Commit 77514d2

Browse files
committed
refactor code and format
1 parent 109ff0b commit 77514d2

7 files changed

Lines changed: 28 additions & 35 deletions

File tree

src/__tests__/compare-compound-inequations.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ const atm = new AstToMathJs();
88

99
describe("splitInequality", () => {
1010
it.each`
11-
compoundInequality | leftPart | rightPart
12-
${"20>x>7"} | ${"20>x"} | ${"x>7"}
13-
${" 5 ≥ -4 + x > -1 - 1"} | ${" 5 ≥ -4 + x"} | ${"-4 + x > -1 - 1"}
14-
${"x/x<x+y≤1.5*2"} | ${"x/x<x+y"} | ${"x+y≤1.5*2"}
15-
${ "2 < 4x > 20"} | ${"2<4x"} | ${"4x>20"}
16-
${ "-3 < 2x+5 < 17"} | ${"-3<2x+5"} | ${"2x+5 < 17"}
17-
${ "-3 = 6x = -3"} | ${"-3 = 6x"} | ${"6x = -3"}
18-
${ "a≥b≥c "} | ${"a≥b"} | ${"b≥c"}
19-
${ "a+2≥b-10≥c-100 "} | ${"a+2≥b-10"} | ${"b-10≥c-100"}
20-
${ "a≠b≠c "} | ${"a≠b"} | ${"b≠c"}
11+
compoundInequality | leftPart | rightPart
12+
${"20>x>7"} | ${"20>x"} | ${"x>7"}
13+
${" 5 ≥ -4 + x > -1 - 1"} | ${" 5 ≥ -4 + x"} | ${"-4 + x > -1 - 1"}
14+
${"x/x<x+y≤1.5*2"} | ${"x/x<x+y"} | ${"x+y≤1.5*2"}
15+
${"2 < 4x > 20"} | ${"2<4x"} | ${"4x>20"}
16+
${"-3 < 2x+5 < 17"} | ${"-3<2x+5"} | ${"2x+5 < 17"}
17+
${"-3 = 6x = -3"} | ${"-3 = 6x"} | ${"6x = -3"}
18+
${"a≥b≥c "} | ${"a≥b"} | ${"b≥c"}
19+
${"a+2≥b-10≥c-100 "} | ${"a+2≥b-10"} | ${"b-10≥c-100"}
20+
${"a≠b≠c "} | ${"a≠b"} | ${"b≠c"}
2121
`(
2222
"$compoundInequality => $leftPart, $rightPart",
2323
({ compoundInequality, leftPart, rightPart }) => {
24-
const equation = atm.convert(lta.convert(compoundInequality));
25-
const broken = splitInequality(equation);
24+
const inequality = atm.convert(lta.convert(compoundInequality));
25+
const broken = splitInequality(inequality);
2626

27-
const leftPartExpression = atm.convert(lta.convert(leftPart));
28-
const rightPartExpression = atm.convert(lta.convert(rightPart));
27+
const leftSide = atm.convert(lta.convert(leftPart));
28+
const rightSide = atm.convert(lta.convert(rightPart));
2929

30-
expect(broken.left).toEqual(leftPartExpression);
31-
expect(broken.right).toEqual(rightPartExpression);
30+
expect(broken.left).toEqual(leftSide);
31+
expect(broken.right).toEqual(rightSide);
3232
}
3333
);
3434
});

src/__tests__/utils.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
setXToOne,
99
solveLinearEquation,
1010
expressionsCanBeCompared,
11-
transformEqualityInExpression
11+
transformEqualityInExpression,
1212
} from "../symbolic/utils";
1313

1414
const lta = new LatexToAst();
@@ -92,7 +92,7 @@ describe("getCoefficients", () => {
9292
it.each`
9393
expression | coefficients
9494
${"x+0"} | ${[0, 1]}
95-
${"2x^2 = 2x"} | ${[1, 0]}
95+
${"2x^2 = 2x"} | ${[1, 0]}
9696
${"x +1"} | ${[1, 1]}
9797
${"((x^2 + x) / x) - 1"} | ${[0, 0, 1]}
9898
${"1+2"} | ${[]}

src/conversion/latex-to-ast.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ export const latex_rules = [
302302
["\\\\neq(?![a-zA-Z])", "NE"],
303303
["\\\\ne(?![a-zA-Z])", "NE"],
304304
["\\\\not\\s*=", "NE"],
305-
306305
["\\\\leq(?![a-zA-Z])", "LE"],
307306
["\\\\le(?![a-zA-Z])", "LE"],
308307
["\\\\geq(?![a-zA-Z])", "GE"],
@@ -739,7 +738,6 @@ export class LatexToAst {
739738
case "ge":
740739
return "largerEq";
741740
case "NE":
742-
return "unequal";
743741
case "ne":
744742
return "unequal";
745743
}

src/symbolic/compare-compound-inequations.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mathjs } from "../mathjs";
2-
import { MathNode, unequal } from "mathjs";
2+
import { MathNode } from "mathjs";
33
import {
44
expressionsCanBeCompared,
55
equationsHaveTheSameVariables,
@@ -75,7 +75,6 @@ export const compareCompoundInequations = (
7575
secondInequation: any
7676
) => {
7777

78-
7978
if (!expressionsCanBeCompared(firstInequation, secondInequation)) {
8079
return false;
8180
}

src/symbolic/compare-equations.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,16 @@ export const compareEquations = (
8383
// we have one distinct case, when multiplying both parts of an inequality with a negative number, the sign must change direction
8484
if (equivalence && isInequality) {
8585
// check if direction should be changed
86-
let signShouldBeChanged =
86+
return !(
8787
(m.isPositive(firstEquationCoefficients[0]) &&
8888
m.isNegative(firstEquationCoefficients[1]) &&
8989
m.isNegative(secondEquationCoefficients[0]) &&
9090
m.isPositive(secondEquationCoefficients[1])) ||
9191
(m.isNegative(firstEquationCoefficients[0]) &&
9292
m.isPositive(firstEquationCoefficients[1]) &&
9393
m.isPositive(secondEquationCoefficients[0]) &&
94-
m.isNegative(secondEquationCoefficients[1]));
95-
96-
return !signShouldBeChanged;
94+
m.isNegative(secondEquationCoefficients[1]))
95+
);
9796
}
9897
}
9998

src/symbolic/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ export const isMathEqual = (a: any, b: any) => {
175175
//@ts-ignore
176176
as?.conditionals?.length === bs?.conditionals?.length &&
177177
//@ts-ignore
178-
as?.conditionals?.length === 2 && as?.conditionals?.toString() === bs?.conditionals?.toString()
178+
as?.conditionals?.length === 2 &&
179+
//@ts-ignore
180+
as?.conditionals?.toString() === bs?.conditionals?.toString()
179181
) {
180182
const params = [
181183
"smaller",

src/symbolic/utils.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ export const expressionsCanBeCompared = (
3232
};
3333

3434
// move the terms of the equations to the left hand side
35-
export const transformEqualityInExpression = (equality: MathNode) => {
36-
const expression = new m.OperatorNode("-", "subtract", equality.args);
37-
35+
export const transformEqualityInExpression = (equality: MathNode) =>
3836
// remove added/subtracted numbers/variables from both sides of the equation
39-
return customSimplify(expression);
40-
};
37+
customSimplify(new m.OperatorNode("-", "subtract", equality.args));
4138

4239
// check if equation is valid and find out the number of variables and their name
4340
export const getVariables = (equation: MathNode) => {
@@ -53,9 +50,7 @@ export const getVariables = (equation: MathNode) => {
5350
}
5451
});
5552

56-
variableNames.sort();
57-
58-
return variableNames;
53+
return variableNames.sort();
5954
};
6055

6156
export const getCoefficients = (equation: MathNode) => {

0 commit comments

Comments
 (0)