ConstantNode.js (5468B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createConstantNode = void 0; 7 8 var _string = require("../../utils/string.js"); 9 10 var _is = require("../../utils/is.js"); 11 12 var _latex = require("../../utils/latex.js"); 13 14 var _factory = require("../../utils/factory.js"); 15 16 var name = 'ConstantNode'; 17 var dependencies = ['Node']; 18 var createConstantNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 19 var Node = _ref.Node; 20 21 /** 22 * A ConstantNode holds a constant value like a number or string. 23 * 24 * Usage: 25 * 26 * new ConstantNode(2.3) 27 * new ConstantNode('hello') 28 * 29 * @param {*} value Value can be any type (number, BigNumber, string, ...) 30 * @constructor ConstantNode 31 * @extends {Node} 32 */ 33 function ConstantNode(value) { 34 if (!(this instanceof ConstantNode)) { 35 throw new SyntaxError('Constructor must be called with the new operator'); 36 } 37 38 this.value = value; 39 } 40 41 ConstantNode.prototype = new Node(); 42 ConstantNode.prototype.type = 'ConstantNode'; 43 ConstantNode.prototype.isConstantNode = true; 44 /** 45 * Compile a node into a JavaScript function. 46 * This basically pre-calculates as much as possible and only leaves open 47 * calculations which depend on a dynamic scope with variables. 48 * @param {Object} math Math.js namespace with functions and constants. 49 * @param {Object} argNames An object with argument names as key and `true` 50 * as value. Used in the SymbolNode to optimize 51 * for arguments from user assigned functions 52 * (see FunctionAssignmentNode) or special symbols 53 * like `end` (see IndexNode). 54 * @return {function} Returns a function which can be called like: 55 * evalNode(scope: Object, args: Object, context: *) 56 */ 57 58 ConstantNode.prototype._compile = function (math, argNames) { 59 var value = this.value; 60 return function evalConstantNode() { 61 return value; 62 }; 63 }; 64 /** 65 * Execute a callback for each of the child nodes of this node 66 * @param {function(child: Node, path: string, parent: Node)} callback 67 */ 68 69 70 ConstantNode.prototype.forEach = function (callback) {// nothing to do, we don't have childs 71 }; 72 /** 73 * Create a new ConstantNode having it's childs be the results of calling 74 * the provided callback function for each of the childs of the original node. 75 * @param {function(child: Node, path: string, parent: Node) : Node} callback 76 * @returns {ConstantNode} Returns a clone of the node 77 */ 78 79 80 ConstantNode.prototype.map = function (callback) { 81 return this.clone(); 82 }; 83 /** 84 * Create a clone of this node, a shallow copy 85 * @return {ConstantNode} 86 */ 87 88 89 ConstantNode.prototype.clone = function () { 90 return new ConstantNode(this.value); 91 }; 92 /** 93 * Get string representation 94 * @param {Object} options 95 * @return {string} str 96 */ 97 98 99 ConstantNode.prototype._toString = function (options) { 100 return (0, _string.format)(this.value, options); 101 }; 102 /** 103 * Get HTML representation 104 * @param {Object} options 105 * @return {string} str 106 */ 107 108 109 ConstantNode.prototype.toHTML = function (options) { 110 var value = this._toString(options); 111 112 switch ((0, _is.typeOf)(this.value)) { 113 case 'number': 114 case 'BigNumber': 115 case 'Fraction': 116 return '<span class="math-number">' + value + '</span>'; 117 118 case 'string': 119 return '<span class="math-string">' + value + '</span>'; 120 121 case 'boolean': 122 return '<span class="math-boolean">' + value + '</span>'; 123 124 case 'null': 125 return '<span class="math-null-symbol">' + value + '</span>'; 126 127 case 'undefined': 128 return '<span class="math-undefined">' + value + '</span>'; 129 130 default: 131 return '<span class="math-symbol">' + value + '</span>'; 132 } 133 }; 134 /** 135 * Get a JSON representation of the node 136 * @returns {Object} 137 */ 138 139 140 ConstantNode.prototype.toJSON = function () { 141 return { 142 mathjs: 'ConstantNode', 143 value: this.value 144 }; 145 }; 146 /** 147 * Instantiate a ConstantNode from its JSON representation 148 * @param {Object} json An object structured like 149 * `{"mathjs": "SymbolNode", value: 2.3}`, 150 * where mathjs is optional 151 * @returns {ConstantNode} 152 */ 153 154 155 ConstantNode.fromJSON = function (json) { 156 return new ConstantNode(json.value); 157 }; 158 /** 159 * Get LaTeX representation 160 * @param {Object} options 161 * @return {string} str 162 */ 163 164 165 ConstantNode.prototype._toTex = function (options) { 166 var value = this._toString(options); 167 168 switch ((0, _is.typeOf)(this.value)) { 169 case 'string': 170 return '\\mathtt{' + (0, _latex.escapeLatex)(value) + '}'; 171 172 case 'number': 173 case 'BigNumber': 174 { 175 if (!isFinite(this.value)) { 176 return this.value.valueOf() < 0 ? '-\\infty' : '\\infty'; 177 } 178 179 var index = value.toLowerCase().indexOf('e'); 180 181 if (index !== -1) { 182 return value.substring(0, index) + '\\cdot10^{' + value.substring(index + 1) + '}'; 183 } 184 } 185 return value; 186 187 case 'Fraction': 188 return this.value.toLatex(); 189 190 default: 191 return value; 192 } 193 }; 194 195 return ConstantNode; 196 }, { 197 isClass: true, 198 isNode: true 199 }); 200 exports.createConstantNode = createConstantNode;