forked from google/mathsteps
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEquation.ts
60 lines (49 loc) · 1.84 KB
/
Equation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as math from "mathjs";
import { printAscii, printLatex } from "../util/print";
/**
* This represents an equation, made up of the leftNode (LHS), the
* rightNode (RHS) and a comparator (=, <, >, <=, or >=)
* */
export class Equation {
constructor(public leftNode, public rightNode, public comparator) {
this.leftNode = leftNode;
this.rightNode = rightNode;
this.comparator = comparator;
}
// Prints an Equation properly using the print module
ascii(showPlusMinus = false) {
console.log(`============`);
console.log(JSON.stringify(this.leftNode));
console.log(JSON.stringify(this.rightNode));
console.log(`============`);
const leftSide = printAscii(this.leftNode, showPlusMinus);
const rightSide = printAscii(this.rightNode, showPlusMinus);
const comparator = this.comparator;
return `${leftSide} ${comparator} ${rightSide}`;
}
// Prints an Equation properly using LaTeX
latex(showPlusMinus = false) {
const leftSide = printLatex(this.leftNode, showPlusMinus);
const rightSide = printLatex(this.rightNode, showPlusMinus);
const comparator = this.comparator;
return `${leftSide} ${comparator} ${rightSide}`;
}
clone() {
const newLeft = this.leftNode.cloneDeep();
const newRight = this.rightNode.cloneDeep();
return new Equation(newLeft, newRight, this.comparator);
}
// Splits a string on the given comparator and returns a new Equation object
// from the left and right hand sides
static createEquationFromString(str, comparator) {
const sides = str.split(comparator);
if (sides.length !== 2) {
throw Error(
"Expected two sides of an equation using comparator: " + comparator
);
}
const leftNode = math.parse(sides[0]);
const rightNode = math.parse(sides[1]);
return new Equation(leftNode, rightNode, comparator);
}
}