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