UNPKG

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