mathjs/lib/expression/node/AssignmentNode.js
Niels Heisterkamp b7fe0bfbf1 Added LaTeX conversion of expressions using toTex() on expression.
Also added some basic tests, which cover some use cases; but should be expanded.
2014-05-19 08:20:06 +02:00

83 lines
2.1 KiB
JavaScript

var Node = require('./Node'),
ArrayNode = require('./ArrayNode'),
latex = require('../../util/latex'),
isString = require('../../util/string').isString;
/**
* @constructor AssignmentNode
* @extends {Node}
* Define a symbol, like "a = 3.2"
*
* @param {String} name Symbol name
* @param {Node} expr The expression defining the symbol
*/
function AssignmentNode(name, expr) {
if (!(this instanceof AssignmentNode)) {
throw new SyntaxError('Constructor must be called with the new operator');
}
// validate input
if (!isString(name)) throw new TypeError('String expected for parameter "name"');
if (!(expr instanceof Node)) throw new TypeError('Node expected for parameter "expr"');
this.name = name;
this.expr = expr;
}
AssignmentNode.prototype = new Node();
AssignmentNode.prototype.type = 'AssignmentNode';
/**
* 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
*/
AssignmentNode.prototype._compile = function (defs) {
return 'scope["' + this.name + '"] = ' + this.expr._compile(defs) + '';
};
/**
* Find all nodes matching given filter
* @param {Object} filter See Node.find for a description of the filter options
* @returns {Node[]} nodes
*/
AssignmentNode.prototype.find = function (filter) {
var nodes = [];
// check itself
if (this.match(filter)) {
nodes.push(this);
}
// search in expression
nodes = nodes.concat(this.expr.find(filter));
return nodes;
};
/**
* Get string representation
* @return {String}
*/
AssignmentNode.prototype.toString = function() {
return this.name + ' = ' + this.expr.toString();
};
/**
* Get LaTeX representation
* @return {String}
*/
AssignmentNode.prototype.toTex = function() {
var brace;
if (this.expr instanceof ArrayNode) {
brace = ['\\textbf{', '}'];
}
return latex.addBraces(latex.toSymbol(this.name), brace) + '=' +
latex.addBraces(this.expr.toTex());
};
module.exports = AssignmentNode;