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