-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathArrayNode.js
More file actions
178 lines (163 loc) · 5.44 KB
/
ArrayNode.js
File metadata and controls
178 lines (163 loc) · 5.44 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
import { isArrayNode, isNode } from '../../utils/is.js'
import { map } from '../../utils/array.js'
import { factory } from '../../utils/factory.js'
const name = 'ArrayNode'
const dependencies = [
'Node'
]
export const createArrayNode = /* #__PURE__ */ factory(name, dependencies, ({ Node }) => {
class ArrayNode extends Node {
/**
* @constructor ArrayNode
* @extends {Node}
* Holds an 1-dimensional array with items
* @param {Node[]} [items] 1 dimensional array with items
*/
constructor (items) {
super()
this.items = items || []
// validate input
if (!Array.isArray(this.items) || !this.items.every(isNode)) {
throw new TypeError('Array containing Nodes expected')
}
}
static name = name
get type () { return name }
get isArrayNode () { return 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: *)
*/
_compile (math, argNames) {
const evalItems = map(this.items, function (item) {
return item._compile(math, argNames)
})
const asMatrix = (math.config.matrix !== 'Array')
if (asMatrix) {
const matrix = math.matrix
return function evalArrayNode (scope, args, context) {
return matrix(map(evalItems, function (evalItem) {
return evalItem(scope, args, context)
}))
}
} else {
return function evalArrayNode (scope, args, context) {
return map(evalItems, function (evalItem) {
return evalItem(scope, args, context)
})
}
}
}
/**
* Execute a callback for each of the child nodes of this node
* @param {function(child: Node, path: string, parent: Node)} callback
*/
forEach (callback) {
for (let i = 0; i < this.items.length; i++) {
const node = this.items[i]
callback(node, 'items[' + i + ']', this)
}
}
/**
* Create a new ArrayNode 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 {ArrayNode} Returns a transformed copy of the node
*/
map (callback) {
const items = []
for (let i = 0; i < this.items.length; i++) {
items[i] = this._ifNode(callback(this.items[i], 'items[' + i + ']', this))
}
return new ArrayNode(items)
}
/**
* Create a clone of this node, a shallow copy
* @return {ArrayNode}
*/
clone () {
return new ArrayNode(this.items.slice(0))
}
/**
* Get string representation
* @param {Object} options
* @return {string} str
* @override
*/
_toString (options) {
const items = this.items.map(function (node) {
return node.toString(options)
})
return '[' + items.join(', ') + ']'
}
/**
* Get a JSON representation of the node
* @returns {Object}
*/
toJSON () {
return {
mathjs: name,
items: this.items
}
}
/**
* Instantiate an ArrayNode from its JSON representation
* @param {Object} json An object structured like
* `{"mathjs": "ArrayNode", items: [...]}`,
* where mathjs is optional
* @returns {ArrayNode}
*/
static fromJSON (json) {
return new ArrayNode(json.items)
}
/**
* Get HTML representation
* @param {Object} options
* @return {string} str
* @override
*/
_toHTML (options) {
const items = this.items.map(function (node) {
return node.toHTML(options)
})
return '<span class="math-parenthesis math-square-parenthesis">[</span>' +
items.join('<span class="math-separator">,</span>') +
'<span class="math-parenthesis math-square-parenthesis">]</span>'
}
/**
* Get LaTeX representation
* @param {Object} options
* @return {string} str
*/
_toTex (options) {
function itemsToTex (items, nested) {
const mixedItems = items.some(isArrayNode) && !items.every(isArrayNode)
const itemsFormRow = nested || mixedItems
const itemSep = itemsFormRow ? '&' : '\\\\'
const itemsTex = items
.map(function (node) {
if (node.items) {
return itemsToTex(node.items, !nested)
} else {
return node.toTex(options)
}
})
.join(itemSep)
return mixedItems || !itemsFormRow || (itemsFormRow && !nested)
? '\\begin{bmatrix}' + itemsTex + '\\end{bmatrix}'
: itemsTex
}
return itemsToTex(this.items, false)
}
}
return ArrayNode
}, { isClass: true, isNode: true })