UNPKG

nerdamer-ts

Version:

javascript light-weight symbolic math expression evaluator

35 lines 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Node = void 0; class Node { constructor(token) { this.type = token.type; this.value = token.value; //the incoming token may already be a Node type if (token instanceof Node) { this.left = token.left; this.right = token.right; } } toString() { let left = this.left ? this.left.toString() + '---' : ''; let right = this.right ? '---' + this.right.toString() : ''; return left + '(' + this.value + ')' + right; } toHTML(depth = 0, indent = 4) { let tab = function (n) { return ' '.repeat(indent * n); }; let html = ''; let left = this.left ? tab(depth + 1) + '<li>\n' + this.left.toHTML(depth + 2, indent) + tab(depth + 1) + '</li> \n' : ''; let right = this.right ? tab(depth + 1) + '<li>\n' + this.right.toHTML(depth + 2, indent) + tab(depth + 1) + '</li>\n' : ''; html = tab(depth) + '<div class="' + this.type.toLowerCase() + '"><span>' + this.value + '</span></div>' + tab(depth) + '\n'; if (left || right) { html += tab(depth) + '<ul>\n' + left + right + tab(depth) + '</ul>\n'; } html += ''; return html; } } exports.Node = Node; //# sourceMappingURL=Node.js.map