UNPKG

6.09 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createBlockNode = void 0;
7
8var _is = require("../../utils/is");
9
10var _array = require("../../utils/array");
11
12var _factory = require("../../utils/factory");
13
14var name = 'BlockNode';
15var dependencies = ['ResultSet', 'Node'];
16var createBlockNode =
17/* #__PURE__ */
18(0, _factory.factory)(name, dependencies, function (_ref) {
19 var ResultSet = _ref.ResultSet,
20 Node = _ref.Node;
21
22 /**
23 * @constructor BlockNode
24 * @extends {Node}
25 * Holds a set with blocks
26 * @param {Array.<{node: Node} | {node: Node, visible: boolean}>} blocks
27 * An array with blocks, where a block is constructed as an Object
28 * with properties block, which is a Node, and visible, which is
29 * a boolean. The property visible is optional and is true by default
30 */
31 function BlockNode(blocks) {
32 if (!(this instanceof BlockNode)) {
33 throw new SyntaxError('Constructor must be called with the new operator');
34 } // validate input, copy blocks
35
36
37 if (!Array.isArray(blocks)) throw new Error('Array expected');
38 this.blocks = blocks.map(function (block) {
39 var node = block && block.node;
40 var visible = block && block.visible !== undefined ? block.visible : true;
41 if (!(0, _is.isNode)(node)) throw new TypeError('Property "node" must be a Node');
42 if (typeof visible !== 'boolean') throw new TypeError('Property "visible" must be a boolean');
43 return {
44 node: node,
45 visible: visible
46 };
47 });
48 }
49
50 BlockNode.prototype = new Node();
51 BlockNode.prototype.type = 'BlockNode';
52 BlockNode.prototype.isBlockNode = true;
53 /**
54 * Compile a node into a JavaScript function.
55 * This basically pre-calculates as much as possible and only leaves open
56 * calculations which depend on a dynamic scope with variables.
57 * @param {Object} math Math.js namespace with functions and constants.
58 * @param {Object} argNames An object with argument names as key and `true`
59 * as value. Used in the SymbolNode to optimize
60 * for arguments from user assigned functions
61 * (see FunctionAssignmentNode) or special symbols
62 * like `end` (see IndexNode).
63 * @return {function} Returns a function which can be called like:
64 * evalNode(scope: Object, args: Object, context: *)
65 */
66
67 BlockNode.prototype._compile = function (math, argNames) {
68 var evalBlocks = (0, _array.map)(this.blocks, function (block) {
69 return {
70 evaluate: block.node._compile(math, argNames),
71 visible: block.visible
72 };
73 });
74 return function evalBlockNodes(scope, args, context) {
75 var results = [];
76 (0, _array.forEach)(evalBlocks, function evalBlockNode(block) {
77 var result = block.evaluate(scope, args, context);
78
79 if (block.visible) {
80 results.push(result);
81 }
82 });
83 return new ResultSet(results);
84 };
85 };
86 /**
87 * Execute a callback for each of the child blocks of this node
88 * @param {function(child: Node, path: string, parent: Node)} callback
89 */
90
91
92 BlockNode.prototype.forEach = function (callback) {
93 for (var i = 0; i < this.blocks.length; i++) {
94 callback(this.blocks[i].node, 'blocks[' + i + '].node', this);
95 }
96 };
97 /**
98 * Create a new BlockNode having it's childs be the results of calling
99 * the provided callback function for each of the childs of the original node.
100 * @param {function(child: Node, path: string, parent: Node): Node} callback
101 * @returns {BlockNode} Returns a transformed copy of the node
102 */
103
104
105 BlockNode.prototype.map = function (callback) {
106 var blocks = [];
107
108 for (var i = 0; i < this.blocks.length; i++) {
109 var block = this.blocks[i];
110
111 var node = this._ifNode(callback(block.node, 'blocks[' + i + '].node', this));
112
113 blocks[i] = {
114 node: node,
115 visible: block.visible
116 };
117 }
118
119 return new BlockNode(blocks);
120 };
121 /**
122 * Create a clone of this node, a shallow copy
123 * @return {BlockNode}
124 */
125
126
127 BlockNode.prototype.clone = function () {
128 var blocks = this.blocks.map(function (block) {
129 return {
130 node: block.node,
131 visible: block.visible
132 };
133 });
134 return new BlockNode(blocks);
135 };
136 /**
137 * Get string representation
138 * @param {Object} options
139 * @return {string} str
140 * @override
141 */
142
143
144 BlockNode.prototype._toString = function (options) {
145 return this.blocks.map(function (param) {
146 return param.node.toString(options) + (param.visible ? '' : ';');
147 }).join('\n');
148 };
149 /**
150 * Get a JSON representation of the node
151 * @returns {Object}
152 */
153
154
155 BlockNode.prototype.toJSON = function () {
156 return {
157 mathjs: 'BlockNode',
158 blocks: this.blocks
159 };
160 };
161 /**
162 * Instantiate an BlockNode from its JSON representation
163 * @param {Object} json An object structured like
164 * `{"mathjs": "BlockNode", blocks: [{node: ..., visible: false}, ...]}`,
165 * where mathjs is optional
166 * @returns {BlockNode}
167 */
168
169
170 BlockNode.fromJSON = function (json) {
171 return new BlockNode(json.blocks);
172 };
173 /**
174 * Get HTML representation
175 * @param {Object} options
176 * @return {string} str
177 * @override
178 */
179
180
181 BlockNode.prototype.toHTML = function (options) {
182 return this.blocks.map(function (param) {
183 return param.node.toHTML(options) + (param.visible ? '' : '<span class="math-separator">;</span>');
184 }).join('<span class="math-separator"><br /></span>');
185 };
186 /**
187 * Get LaTeX representation
188 * @param {Object} options
189 * @return {string} str
190 */
191
192
193 BlockNode.prototype._toTex = function (options) {
194 return this.blocks.map(function (param) {
195 return param.node.toTex(options) + (param.visible ? '' : ';');
196 }).join('\\;\\;\n');
197 };
198
199 return BlockNode;
200}, {
201 isClass: true,
202 isNode: true
203});
204exports.createBlockNode = createBlockNode;
\No newline at end of file