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");
|
9 |
|
10 | var _is = require("../../utils/is");
|
11 |
|
12 | var _latex = require("../../utils/latex");
|
13 |
|
14 | var _factory = require("../../utils/factory");
|
15 |
|
16 | var name = 'ConstantNode';
|
17 | var dependencies = ['Node'];
|
18 | var createConstantNode =
|
19 |
|
20 | (0, _factory.factory)(name, dependencies, function (_ref) {
|
21 | var Node = _ref.Node;
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
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 |
|
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 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
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 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | ConstantNode.prototype.forEach = function (callback) {}
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | ;
|
86 |
|
87 | ConstantNode.prototype.map = function (callback) {
|
88 | return this.clone();
|
89 | };
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | ConstantNode.prototype.clone = function () {
|
97 | return new ConstantNode(this.value);
|
98 | };
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | ConstantNode.prototype._toString = function (options) {
|
107 | return (0, _string.format)(this.value, options);
|
108 | };
|
109 | |
110 |
|
111 |
|
112 |
|
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 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | ConstantNode.prototype.toJSON = function () {
|
148 | return {
|
149 | mathjs: 'ConstantNode',
|
150 | value: this.value
|
151 | };
|
152 | };
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | ConstantNode.fromJSON = function (json) {
|
163 | return new ConstantNode(json.value);
|
164 | };
|
165 | |
166 |
|
167 |
|
168 |
|
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 | });
|
203 | exports.createConstantNode = createConstantNode; |
\ | No newline at end of file |