mathjs/lib/expression/node/ArrayNode.js
2014-01-07 21:07:43 +01:00

73 lines
1.7 KiB
JavaScript

var Node = require('./Node'),
object = require('../../util/object'),
string = require('../../util/string'),
collection = require('../../type/collection'),
Matrix = require('../../type/Matrix');
/**
* @constructor ArrayNode
* @extends {Node}
* Holds an 1-dimensional array with nodes
* @param {Array} nodes 1 dimensional array with nodes
*/
function ArrayNode(nodes) {
this.nodes = nodes || [];
}
ArrayNode.prototype = new Node();
/**
* Compile the node to javascript code
* @param {Object} defs Object which can be used to define functions
* or constants globally available for the compiled
* expression
* @private
*/
ArrayNode.prototype._compile = function (defs) {
var asMatrix = (defs.math.config().matrix !== 'array');
var nodes = this.nodes.map(function (node) {
return node._compile(defs);
});
return (asMatrix ? 'math.matrix([' : '[') +
nodes.join(',') +
(asMatrix ? '])' : ']');
};
/**
* Find all nodes matching given filter
* @param {Object} filter See Node.find for a description of the filter settings
* @returns {Node[]} nodes
*/
ArrayNode.prototype.find = function (filter) {
var results = [];
// check itself
if (this.match(filter)) {
results.push(this);
}
// search in all nodes
var nodes = this.nodes;
for (var r = 0, rows = nodes.length; r < rows; r++) {
var nodes_r = nodes[r];
for (var c = 0, cols = nodes_r.length; c < cols; c++) {
results = results.concat(nodes_r[c].find(filter));
}
}
return results;
};
/**
* Get string representation
* @return {String} str
* @override
*/
ArrayNode.prototype.toString = function() {
return string.format(this.nodes);
};
module.exports = ArrayNode;