1 | import { format } from '../../utils/string'
|
2 | import { typeOf } from '../../utils/is'
|
3 | import { escapeLatex } from '../../utils/latex'
|
4 | import { factory } from '../../utils/factory'
|
5 |
|
6 | const name = 'ConstantNode'
|
7 | const dependencies = [
|
8 | 'Node'
|
9 | ]
|
10 |
|
11 | export const createConstantNode = factory(name, dependencies, ({ Node }) => {
|
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 | if (arguments.length === 2) {
|
30 |
|
31 | 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.')
|
32 | }
|
33 |
|
34 | this.value = value
|
35 | }
|
36 |
|
37 | ConstantNode.prototype = new Node()
|
38 |
|
39 | ConstantNode.prototype.type = 'ConstantNode'
|
40 |
|
41 | ConstantNode.prototype.isConstantNode = true
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | ConstantNode.prototype._compile = function (math, argNames) {
|
57 | const value = this.value
|
58 |
|
59 | return function evalConstantNode () {
|
60 | return value
|
61 | }
|
62 | }
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 | ConstantNode.prototype.forEach = function (callback) {
|
69 |
|
70 | }
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | ConstantNode.prototype.map = function (callback) {
|
79 | return this.clone()
|
80 | }
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 | ConstantNode.prototype.clone = function () {
|
87 | return new ConstantNode(this.value)
|
88 | }
|
89 |
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 | ConstantNode.prototype._toString = function (options) {
|
96 | return format(this.value, options)
|
97 | }
|
98 |
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 | ConstantNode.prototype.toHTML = function (options) {
|
105 | const value = this._toString(options)
|
106 |
|
107 | switch (typeOf(this.value)) {
|
108 | case 'number':
|
109 | case 'BigNumber':
|
110 | case 'Fraction':
|
111 | return '<span class="math-number">' + value + '</span>'
|
112 | case 'string':
|
113 | return '<span class="math-string">' + value + '</span>'
|
114 | case 'boolean':
|
115 | return '<span class="math-boolean">' + value + '</span>'
|
116 | case 'null':
|
117 | return '<span class="math-null-symbol">' + value + '</span>'
|
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 | ConstantNode.prototype.toJSON = function () {
|
131 | return {
|
132 | mathjs: 'ConstantNode',
|
133 | value: this.value
|
134 | }
|
135 | }
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | ConstantNode.fromJSON = function (json) {
|
145 | return new ConstantNode(json.value)
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 | ConstantNode.prototype._toTex = function (options) {
|
154 | const value = this._toString(options)
|
155 |
|
156 | switch (typeOf(this.value)) {
|
157 | case 'string':
|
158 | return '\\mathtt{' + escapeLatex(value) + '}'
|
159 |
|
160 | case 'number':
|
161 | case 'BigNumber':
|
162 | {
|
163 | const index = value.toLowerCase().indexOf('e')
|
164 | if (index !== -1) {
|
165 | return value.substring(0, index) + '\\cdot10^{' +
|
166 | value.substring(index + 1) + '}'
|
167 | }
|
168 | }
|
169 | return value
|
170 | case 'Fraction':
|
171 | return this.value.toLatex()
|
172 |
|
173 | default:
|
174 | return value
|
175 | }
|
176 | }
|
177 |
|
178 | return ConstantNode
|
179 | }, { isClass: true, isNode: true })
|