UNPKG

5.88 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createArrayNode = void 0;
7
8var _is = require("../../utils/is");
9
10var _array = require("../../utils/array");
11
12var _factory = require("../../utils/factory");
13
14var name = 'ArrayNode';
15var dependencies = ['Node'];
16var createArrayNode =
17/* #__PURE__ */
18(0, _factory.factory)(name, dependencies, function (_ref) {
19 var Node = _ref.Node;
20
21 /**
22 * @constructor ArrayNode
23 * @extends {Node}
24 * Holds an 1-dimensional array with items
25 * @param {Node[]} [items] 1 dimensional array with items
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 || []; // validate input
33
34 if (!Array.isArray(this.items) || !this.items.every(_is.isNode)) {
35 throw new TypeError('Array containing Nodes expected');
36 } // TODO: deprecated since v3, remove some day
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 * Execute a callback for each of the child nodes of this node
89 * @param {function(child: Node, path: string, parent: Node)} callback
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 * Create a new ArrayNode having it's childs be the results of calling
101 * the provided callback function for each of the childs of the original node.
102 * @param {function(child: Node, path: string, parent: Node): Node} callback
103 * @returns {ArrayNode} Returns a transformed copy of the node
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 * Create a clone of this node, a shallow copy
118 * @return {ArrayNode}
119 */
120
121
122 ArrayNode.prototype.clone = function () {
123 return new ArrayNode(this.items.slice(0));
124 };
125 /**
126 * Get string representation
127 * @param {Object} options
128 * @return {string} str
129 * @override
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 * Get a JSON representation of the node
141 * @returns {Object}
142 */
143
144
145 ArrayNode.prototype.toJSON = function () {
146 return {
147 mathjs: 'ArrayNode',
148 items: this.items
149 };
150 };
151 /**
152 * Instantiate an ArrayNode from its JSON representation
153 * @param {Object} json An object structured like
154 * `{"mathjs": "ArrayNode", items: [...]}`,
155 * where mathjs is optional
156 * @returns {ArrayNode}
157 */
158
159
160 ArrayNode.fromJSON = function (json) {
161 return new ArrayNode(json.items);
162 };
163 /**
164 * Get HTML representation
165 * @param {Object} options
166 * @return {string} str
167 * @override
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 * Get LaTeX representation
179 * @param {Object} options
180 * @return {string} str
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 } // new line
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});
207exports.createArrayNode = createArrayNode;
\No newline at end of file