import { CaretNode, h, t, VNode } from "@caret-js/core"; import { MathExpressionTag } from "../tags/mathExpression"; import { CanBeAdjacentMultRightSideTag } from "../parselets/adjacentMultiplication"; import { ParenthesesChildTag } from "../parselets/parentheses"; export class DecimalNode extends CaretNode { constructor(public value: string) { super(); this.addTag(new MathExpressionTag()).addTag( new CanBeAdjacentMultRightSideTag( decimalAdjacentMultiplicationRightSideCheck ) ); } static from(value: string): DecimalNode { return new DecimalNode(value); } toFloat(): number { return parseFloat(this.value); } toDebugMathML(): VNode { return h("mn", {}, t(this.value)); } } function decimalAdjacentMultiplicationRightSideCheck( left: CaretNode, right: DecimalNode ): boolean { if (right.hasTag(ParenthesesChildTag)) { return true; } // Decimal nodes on the right side of adjacent multiplication must be non-negative // For example, "x2" is "x multiplied by 2", but "x-2" is "x minus 2" if (right.toFloat() < 0) { return false; } if (left.hasTag(ParenthesesChildTag)) { return true; } if (left instanceof DecimalNode) { return false; } return false; }