A world-class math input for the web
1import { CaretNode, h, t, VNode } from "@caret-js/core";
2import { VariableNode } from "./variable";
3
4export class FunctionDefinitionNode extends CaretNode {
5 constructor(
6 public name: string,
7 public args: VariableNode[] = [],
8 public body: CaretNode
9 ) {
10 super();
11 }
12
13 static from(
14 name: string,
15 args: VariableNode[] = [],
16 body: CaretNode
17 ): FunctionDefinitionNode {
18 return new FunctionDefinitionNode(name, args, body);
19 }
20
21 toDebugMathML(): VNode {
22 return h(
23 "mrow",
24 {},
25 h("mi", {}, t(this.name)),
26 h("mo", { stretchy: "true" }, t("(")),
27 ...this.args.flatMap((arg, index) => [
28 arg.toDebugMathML(),
29 index < this.args.length - 1 ? h("mo", {}, t(",")) : null,
30 ]),
31 h("mo", { stretchy: "true" }, t(")")),
32 h("mo", {}, t("=")),
33 this.body.toDebugMathML()
34 );
35 }
36}