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