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 = factory(name, dependencies, (_ref) => {
|
8 | var {
|
9 | Node
|
10 | } = _ref;
|
11 |
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
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 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
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 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | ConstantNode.prototype.forEach = function (callback) {
|
62 | };
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | ConstantNode.prototype.map = function (callback) {
|
72 | return this.clone();
|
73 | };
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | ConstantNode.prototype.clone = function () {
|
81 | return new ConstantNode(this.value);
|
82 | };
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | ConstantNode.prototype._toString = function (options) {
|
91 | return format(this.value, options);
|
92 | };
|
93 | |
94 |
|
95 |
|
96 |
|
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 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | ConstantNode.prototype.toJSON = function () {
|
132 | return {
|
133 | mathjs: 'ConstantNode',
|
134 | value: this.value
|
135 | };
|
136 | };
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | ConstantNode.fromJSON = function (json) {
|
147 | return new ConstantNode(json.value);
|
148 | };
|
149 | |
150 |
|
151 |
|
152 |
|
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 | }); |
\ | No newline at end of file |