1 | import { isNode } from '../../utils/is'
|
2 | import { map } from '../../utils/array'
|
3 | import { factory } from '../../utils/factory'
|
4 |
|
5 | const name = 'ArrayNode'
|
6 | const dependencies = [
|
7 | 'Node'
|
8 | ]
|
9 |
|
10 | export const createArrayNode = factory(name, dependencies, ({ Node }) => {
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function ArrayNode (items) {
|
18 | if (!(this instanceof ArrayNode)) {
|
19 | throw new SyntaxError('Constructor must be called with the new operator')
|
20 | }
|
21 |
|
22 | this.items = items || []
|
23 |
|
24 |
|
25 | if (!Array.isArray(this.items) || !this.items.every(isNode)) {
|
26 | throw new TypeError('Array containing Nodes expected')
|
27 | }
|
28 |
|
29 |
|
30 | const deprecated = function () {
|
31 | throw new Error('Property `ArrayNode.nodes` is deprecated, use `ArrayNode.items` instead')
|
32 | }
|
33 | Object.defineProperty(this, 'nodes', { get: deprecated, set: deprecated })
|
34 | }
|
35 |
|
36 | ArrayNode.prototype = new Node()
|
37 |
|
38 | ArrayNode.prototype.type = 'ArrayNode'
|
39 |
|
40 | ArrayNode.prototype.isArrayNode = true
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | ArrayNode.prototype._compile = function (math, argNames) {
|
56 | const evalItems = map(this.items, function (item) {
|
57 | return item._compile(math, argNames)
|
58 | })
|
59 |
|
60 | const asMatrix = (math.config.matrix !== 'Array')
|
61 | if (asMatrix) {
|
62 | const matrix = math.matrix
|
63 | return function evalArrayNode (scope, args, context) {
|
64 | return matrix(map(evalItems, function (evalItem) {
|
65 | return evalItem(scope, args, context)
|
66 | }))
|
67 | }
|
68 | } else {
|
69 | return function evalArrayNode (scope, args, context) {
|
70 | return map(evalItems, function (evalItem) {
|
71 | return evalItem(scope, args, context)
|
72 | })
|
73 | }
|
74 | }
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 | ArrayNode.prototype.forEach = function (callback) {
|
82 | for (let i = 0; i < this.items.length; i++) {
|
83 | const node = this.items[i]
|
84 | callback(node, 'items[' + i + ']', this)
|
85 | }
|
86 | }
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | ArrayNode.prototype.map = function (callback) {
|
95 | const items = []
|
96 | for (let i = 0; i < this.items.length; i++) {
|
97 | items[i] = this._ifNode(callback(this.items[i], 'items[' + i + ']', this))
|
98 | }
|
99 | return new ArrayNode(items)
|
100 | }
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 | ArrayNode.prototype.clone = function () {
|
107 | return new ArrayNode(this.items.slice(0))
|
108 | }
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | ArrayNode.prototype._toString = function (options) {
|
117 | const items = this.items.map(function (node) {
|
118 | return node.toString(options)
|
119 | })
|
120 | return '[' + items.join(', ') + ']'
|
121 | }
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 | ArrayNode.prototype.toJSON = function () {
|
128 | return {
|
129 | mathjs: 'ArrayNode',
|
130 | items: this.items
|
131 | }
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | ArrayNode.fromJSON = function (json) {
|
142 | return new ArrayNode(json.items)
|
143 | }
|
144 |
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | ArrayNode.prototype.toHTML = function (options) {
|
152 | const items = this.items.map(function (node) {
|
153 | return node.toHTML(options)
|
154 | })
|
155 | return '<span class="math-parenthesis math-square-parenthesis">[</span>' + items.join('<span class="math-separator">,</span>') + '<span class="math-parenthesis math-square-parenthesis">]</span>'
|
156 | }
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 | ArrayNode.prototype._toTex = function (options) {
|
164 | let s = '\\begin{bmatrix}'
|
165 |
|
166 | this.items.forEach(function (node) {
|
167 | if (node.items) {
|
168 | s += node.items.map(function (childNode) {
|
169 | return childNode.toTex(options)
|
170 | }).join('&')
|
171 | } else {
|
172 | s += node.toTex(options)
|
173 | }
|
174 |
|
175 |
|
176 | s += '\\\\'
|
177 | })
|
178 | s += '\\end{bmatrix}'
|
179 | return s
|
180 | }
|
181 |
|
182 | return ArrayNode
|
183 | }, { isClass: true, isNode: true })
|