1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createArrayNode = void 0;
|
7 |
|
8 | var _is = require("../../utils/is");
|
9 |
|
10 | var _array = require("../../utils/array");
|
11 |
|
12 | var _factory = require("../../utils/factory");
|
13 |
|
14 | var name = 'ArrayNode';
|
15 | var dependencies = ['Node'];
|
16 | var createArrayNode =
|
17 |
|
18 | (0, _factory.factory)(name, dependencies, function (_ref) {
|
19 | var Node = _ref.Node;
|
20 |
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function ArrayNode(items) {
|
28 | if (!(this instanceof ArrayNode)) {
|
29 | throw new SyntaxError('Constructor must be called with the new operator');
|
30 | }
|
31 |
|
32 | this.items = items || [];
|
33 |
|
34 | if (!Array.isArray(this.items) || !this.items.every(_is.isNode)) {
|
35 | throw new TypeError('Array containing Nodes expected');
|
36 | }
|
37 |
|
38 |
|
39 | var deprecated = function deprecated() {
|
40 | throw new Error('Property `ArrayNode.nodes` is deprecated, use `ArrayNode.items` instead');
|
41 | };
|
42 |
|
43 | Object.defineProperty(this, 'nodes', {
|
44 | get: deprecated,
|
45 | set: deprecated
|
46 | });
|
47 | }
|
48 |
|
49 | ArrayNode.prototype = new Node();
|
50 | ArrayNode.prototype.type = 'ArrayNode';
|
51 | ArrayNode.prototype.isArrayNode = true;
|
52 | /**
|
53 | * Compile a node into a JavaScript function.
|
54 | * This basically pre-calculates as much as possible and only leaves open
|
55 | * calculations which depend on a dynamic scope with variables.
|
56 | * @param {Object} math Math.js namespace with functions and constants.
|
57 | * @param {Object} argNames An object with argument names as key and `true`
|
58 | * as value. Used in the SymbolNode to optimize
|
59 | * for arguments from user assigned functions
|
60 | * (see FunctionAssignmentNode) or special symbols
|
61 | * like `end` (see IndexNode).
|
62 | * @return {function} Returns a function which can be called like:
|
63 | * evalNode(scope: Object, args: Object, context: *)
|
64 | */
|
65 |
|
66 | ArrayNode.prototype._compile = function (math, argNames) {
|
67 | var evalItems = (0, _array.map)(this.items, function (item) {
|
68 | return item._compile(math, argNames);
|
69 | });
|
70 | var asMatrix = math.config.matrix !== 'Array';
|
71 |
|
72 | if (asMatrix) {
|
73 | var matrix = math.matrix;
|
74 | return function evalArrayNode(scope, args, context) {
|
75 | return matrix((0, _array.map)(evalItems, function (evalItem) {
|
76 | return evalItem(scope, args, context);
|
77 | }));
|
78 | };
|
79 | } else {
|
80 | return function evalArrayNode(scope, args, context) {
|
81 | return (0, _array.map)(evalItems, function (evalItem) {
|
82 | return evalItem(scope, args, context);
|
83 | });
|
84 | };
|
85 | }
|
86 | };
|
87 | |
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | ArrayNode.prototype.forEach = function (callback) {
|
94 | for (var i = 0; i < this.items.length; i++) {
|
95 | var node = this.items[i];
|
96 | callback(node, 'items[' + i + ']', this);
|
97 | }
|
98 | };
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | ArrayNode.prototype.map = function (callback) {
|
108 | var items = [];
|
109 |
|
110 | for (var i = 0; i < this.items.length; i++) {
|
111 | items[i] = this._ifNode(callback(this.items[i], 'items[' + i + ']', this));
|
112 | }
|
113 |
|
114 | return new ArrayNode(items);
|
115 | };
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | ArrayNode.prototype.clone = function () {
|
123 | return new ArrayNode(this.items.slice(0));
|
124 | };
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | ArrayNode.prototype._toString = function (options) {
|
134 | var items = this.items.map(function (node) {
|
135 | return node.toString(options);
|
136 | });
|
137 | return '[' + items.join(', ') + ']';
|
138 | };
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | ArrayNode.prototype.toJSON = function () {
|
146 | return {
|
147 | mathjs: 'ArrayNode',
|
148 | items: this.items
|
149 | };
|
150 | };
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | ArrayNode.fromJSON = function (json) {
|
161 | return new ArrayNode(json.items);
|
162 | };
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | ArrayNode.prototype.toHTML = function (options) {
|
172 | var items = this.items.map(function (node) {
|
173 | return node.toHTML(options);
|
174 | });
|
175 | return '<span class="math-parenthesis math-square-parenthesis">[</span>' + items.join('<span class="math-separator">,</span>') + '<span class="math-parenthesis math-square-parenthesis">]</span>';
|
176 | };
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | ArrayNode.prototype._toTex = function (options) {
|
185 | var s = '\\begin{bmatrix}';
|
186 | this.items.forEach(function (node) {
|
187 | if (node.items) {
|
188 | s += node.items.map(function (childNode) {
|
189 | return childNode.toTex(options);
|
190 | }).join('&');
|
191 | } else {
|
192 | s += node.toTex(options);
|
193 | }
|
194 |
|
195 |
|
196 | s += '\\\\';
|
197 | });
|
198 | s += '\\end{bmatrix}';
|
199 | return s;
|
200 | };
|
201 |
|
202 | return ArrayNode;
|
203 | }, {
|
204 | isClass: true,
|
205 | isNode: true
|
206 | });
|
207 | exports.createArrayNode = createArrayNode; |
\ | No newline at end of file |