-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Node.js
384 lines (342 loc) · 11.5 KB
/
Node.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import { isNode } from '../../utils/is.js'
import { keywords } from '../keywords.js'
import { deepStrictEqual } from '../../utils/object.js'
import { factory } from '../../utils/factory.js'
import { createMap } from '../../utils/map.js'
const name = 'Node'
const dependencies = ['mathWithTransform']
export const createNode = /* #__PURE__ */ factory(name, dependencies, ({ mathWithTransform }) => {
/**
* Validate the symbol names of a scope.
* Throws an error when the scope contains an illegal symbol.
* @param {Object} scope
*/
function _validateScope (scope) {
for (const symbol of [...keywords]) {
if (scope.has(symbol)) {
throw new Error('Scope contains an illegal symbol, "' + symbol + '" is a reserved keyword')
}
}
}
class Node {
get type () { return 'Node' }
get isNode () { return true }
/**
* Evaluate the node
* @param {Object} [scope] Scope to read/write variables
* @return {*} Returns the result
*/
evaluate (scope) {
return this.compile().evaluate(scope)
}
/**
* Compile the node into an optimized, evauatable JavaScript function
* @return {{evaluate: function([Object])}} object
* Returns an object with a function 'evaluate',
* which can be invoked as expr.evaluate([scope: Object]),
* where scope is an optional object with
* variables.
*/
compile () {
const expr = this._compile(mathWithTransform, {})
const args = {}
const context = null
function evaluate (scope) {
const s = createMap(scope)
_validateScope(s)
return expr(s, args, context)
}
return {
evaluate
}
}
/**
* 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: *)
*/
_compile (math, argNames) {
throw new Error('Method _compile must be implemented by type ' + this.type)
}
/**
* Execute a callback for each of the child nodes of this node
* @param {function(child: Node, path: string, parent: Node)} callback
*/
forEach (callback) {
// must be implemented by each of the Node implementations
throw new Error('Cannot run forEach on a Node interface')
}
/**
* Create a new Node whose children are the results of calling the
* provided callback function for each child of the original node.
* @param {function(child: Node, path: string, parent: Node): Node} callback
* @returns {OperatorNode} Returns a transformed copy of the node
*/
map (callback) {
// must be implemented by each of the Node implementations
throw new Error('Cannot run map on a Node interface')
}
/**
* Validate whether an object is a Node, for use with map
* @param {Node} node
* @returns {Node} Returns the input if it's a node, else throws an Error
* @protected
*/
_ifNode (node) {
if (!isNode(node)) {
throw new TypeError('Callback function must return a Node')
}
return node
}
/**
* Recursively traverse all nodes in a node tree. Executes given callback for
* this node and each of its child nodes.
* @param {function(node: Node, path: string, parent: Node)} callback
* A callback called for every node in the node tree.
*/
traverse (callback) {
// execute callback for itself
// eslint-disable-next-line
callback(this, null, null)
// recursively traverse over all children of a node
function _traverse (node, callback) {
node.forEach(function (child, path, parent) {
callback(child, path, parent)
_traverse(child, callback)
})
}
_traverse(this, callback)
}
/**
* Recursively transform a node tree via a transform function.
*
* For example, to replace all nodes of type SymbolNode having name 'x' with
* a ConstantNode with value 2:
*
* const res = Node.transform(function (node, path, parent) {
* if (node && node.isSymbolNode) && (node.name === 'x')) {
* return new ConstantNode(2)
* }
* else {
* return node
* }
* })
*
* @param {function(node: Node, path: string, parent: Node) : Node} callback
* A mapping function accepting a node, and returning
* a replacement for the node or the original node. The "signature"
* of the callback must be:
* callback(node: Node, index: string, parent: Node) : Node
* @return {Node} Returns the original node or its replacement
*/
transform (callback) {
function _transform (child, path, parent) {
const replacement = callback(child, path, parent)
if (replacement !== child) {
// stop iterating when the node is replaced
return replacement
}
return child.map(_transform)
}
return _transform(this, null, null)
}
/**
* Find any node in the node tree matching given filter function. For
* example, to find all nodes of type SymbolNode having name 'x':
*
* const results = Node.filter(function (node) {
* return (node && node.isSymbolNode) && (node.name === 'x')
* })
*
* @param {function(node: Node, path: string, parent: Node) : Node} callback
* A test function returning true when a node matches, and false
* otherwise. Function signature:
* callback(node: Node, index: string, parent: Node) : boolean
* @return {Node[]} nodes
* An array with nodes matching given filter criteria
*/
filter (callback) {
const nodes = []
this.traverse(function (node, path, parent) {
if (callback(node, path, parent)) {
nodes.push(node)
}
})
return nodes
}
/**
* Create a shallow clone of this node
* @return {Node}
*/
clone () {
// must be implemented by each of the Node implementations
throw new Error('Cannot clone a Node interface')
}
/**
* Create a deep clone of this node
* @return {Node}
*/
cloneDeep () {
return this.map(function (node) {
return node.cloneDeep()
})
}
/**
* Deep compare this node with another node.
* @param {Node} other
* @return {boolean} Returns true when both nodes are of the same type and
* contain the same values (as do their childs)
*/
equals (other) {
return other
? this.type === other.type && deepStrictEqual(this, other)
: false
}
/**
* Get string representation. (wrapper function)
*
* This function can get an object of the following form:
* {
* handler: //This can be a callback function of the form
* // "function callback(node, options)"or
* // a map that maps function names (used in FunctionNodes)
* // to callbacks
* parenthesis: "keep" //the parenthesis option (This is optional)
* }
*
* @param {Object} [options]
* @return {string}
*/
toString (options) {
const customString = this._getCustomString(options)
if (typeof customString !== 'undefined') {
return customString
}
return this._toString(options)
}
/**
* Internal function to generate the string output.
* This has to be implemented by every Node
*
* @throws {Error}
*/
_toString () {
// must be implemented by each of the Node implementations
throw new Error('_toString not implemented for ' + this.type)
}
/**
* Get a JSON representation of the node
* Both .toJSON() and the static .fromJSON(json) should be implemented by all
* implementations of Node
* @returns {Object}
*/
toJSON () {
throw new Error(
'Cannot serialize object: toJSON not implemented by ' + this.type)
}
/**
* Get HTML representation. (wrapper function)
*
* This function can get an object of the following form:
* {
* handler: //This can be a callback function of the form
* // "function callback(node, options)" or
* // a map that maps function names (used in FunctionNodes)
* // to callbacks
* parenthesis: "keep" //the parenthesis option (This is optional)
* }
*
* @param {Object} [options]
* @return {string}
*/
toHTML (options) {
const customString = this._getCustomString(options)
if (typeof customString !== 'undefined') {
return customString
}
return this._toHTML(options)
}
/**
* Internal function to generate the HTML output.
* This has to be implemented by every Node
*
* @throws {Error}
*/
_toHTML () {
// must be implemented by each of the Node implementations
throw new Error('_toHTML not implemented for ' + this.type)
}
/**
* Get LaTeX representation. (wrapper function)
*
* This function can get an object of the following form:
* {
* handler: //This can be a callback function of the form
* // "function callback(node, options)"or
* // a map that maps function names (used in FunctionNodes)
* // to callbacks
* parenthesis: "keep" //the parenthesis option (This is optional)
* }
*
* @param {Object} [options]
* @return {string}
*/
toTex (options) {
const customString = this._getCustomString(options)
if (typeof customString !== 'undefined') {
return customString
}
return this._toTex(options)
}
/**
* Internal function to generate the LaTeX output.
* This has to be implemented by every Node
*
* @param {Object} [options]
* @throws {Error}
*/
_toTex (options) {
// must be implemented by each of the Node implementations
throw new Error('_toTex not implemented for ' + this.type)
}
/**
* Helper used by `to...` functions.
*/
_getCustomString (options) {
if (options && typeof options === 'object') {
switch (typeof options.handler) {
case 'object':
case 'undefined':
return
case 'function':
return options.handler(this, options)
default:
throw new TypeError('Object or function expected as callback')
}
}
}
/**
* Get identifier.
* @return {string}
*/
getIdentifier () {
return this.type
}
/**
* Get the content of the current Node.
* @return {Node} node
**/
getContent () {
return this
}
}
return Node
}, { isClass: true, isNode: true })