mathjs/lib/expression/node/ConditionalNode.js

174 lines
5.9 KiB
JavaScript

'use strict';
var latex = require('../../util/latex');
var operators = require('../operators');
function factory (type, config, load, typed) {
var Node = load(require('./Node'));
var BigNumber = load(require('../../type/BigNumber'));
var Complex = load(require('../../type/Complex'));
var Unit = load(require('../../type/Unit'));
/**
* A lazy evaluating conditional operator: 'condition ? trueExpr : falseExpr'
*
* @param {Node} condition Condition, must result in a boolean
* @param {Node} trueExpr Expression evaluated when condition is true
* @param {Node} falseExpr Expression evaluated when condition is true
*
* @constructor ConditionalNode
* @extends {Node}
*/
function ConditionalNode(condition, trueExpr, falseExpr) {
if (!(this instanceof ConditionalNode)) {
throw new SyntaxError('Constructor must be called with the new operator');
}
if (!(condition && condition.isNode)) throw new TypeError('Parameter condition must be a Node');
if (!(trueExpr && trueExpr.isNode)) throw new TypeError('Parameter trueExpr must be a Node');
if (!(falseExpr && falseExpr.isNode)) throw new TypeError('Parameter falseExpr must be a Node');
this.condition = condition;
this.trueExpr = trueExpr;
this.falseExpr = falseExpr;
}
ConditionalNode.prototype = new Node();
ConditionalNode.prototype.type = 'ConditionalNode';
ConditionalNode.prototype.isConditionalNode = true;
/**
* 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
* @return {String} js
* @private
*/
ConditionalNode.prototype._compile = function (defs) {
/**
* Test whether a condition is met
* @param {*} condition
* @returns {boolean} true if condition is true or non-zero, else false
*/
defs.testCondition = function (condition) {
if (typeof condition === 'number'
|| typeof condition === 'boolean'
|| typeof condition === 'string') {
return condition ? true : false;
}
if (condition instanceof BigNumber) {
return condition.isZero() ? false : true;
}
if (condition instanceof Complex) {
return (condition.re || condition.im) ? true : false;
}
if (condition instanceof Unit) {
return condition.value ? true : false;
}
if (condition === null || condition === undefined) {
return false;
}
throw new TypeError('Unsupported type of condition "' + defs.math['typeof'](condition) + '"');
};
return (
'testCondition(' + this.condition._compile(defs) + ') ? ' +
'( ' + this.trueExpr._compile(defs) + ') : ' +
'( ' + this.falseExpr._compile(defs) + ')'
);
};
/**
* Execute a callback for each of the child nodes of this node
* @param {function(child: Node, path: string, parent: Node)} callback
*/
ConditionalNode.prototype.forEach = function (callback) {
callback(this.condition, 'condition', this);
callback(this.trueExpr, 'trueExpr', this);
callback(this.falseExpr, 'falseExpr', this);
};
/**
* Create a new ConditionalNode having it's childs be the results of calling
* the provided callback function for each of the childs of the original node.
* @param {function(child: Node, path: string, parent: Node): Node} callback
* @returns {ConditionalNode} Returns a transformed copy of the node
*/
ConditionalNode.prototype.map = function (callback) {
return new ConditionalNode(
this._ifNode(callback(this.condition, 'condition', this)),
this._ifNode(callback(this.trueExpr, 'trueExpr', this)),
this._ifNode(callback(this.falseExpr, 'falseExpr', this))
);
};
/**
* Create a clone of this node, a shallow copy
* @return {ConditionalNode}
*/
ConditionalNode.prototype.clone = function () {
return new ConditionalNode(this.condition, this.trueExpr, this.falseExpr);
};
/**
* Get string representation
* @return {String} str
*/
ConditionalNode.prototype.toString = function () {
var precedence = operators.getPrecedence(this);
//Enclose Arguments in parentheses if they are an OperatorNode
//or have lower or equal precedence
//NOTE: enclosing all OperatorNodes in parentheses is a decision
//purely based on aesthetics and readability
var condition = this.condition.toString();
var conditionPrecedence = operators.getPrecedence(this.condition);
if ((this.condition.type === 'OperatorNode')
|| ((conditionPrecedence !== null) && (conditionPrecedence <= precedence))) {
condition = '(' + condition + ')';
}
var trueExpr = this.trueExpr.toString();
var truePrecedence = operators.getPrecedence(this.trueExpr);
if ((this.trueExpr.type === 'OperatorNode')
|| ((truePrecedence !== null) && (truePrecedence <= precedence))) {
trueExpr = '(' + trueExpr + ')';
}
var falseExpr = this.falseExpr.toString();
var falsePrecedence = operators.getPrecedence(this.falseExpr);
if ((this.falseExpr.type === 'OperatorNode')
|| ((falsePrecedence !== null) && (falsePrecedence <= precedence))) {
falseExpr = '(' + falseExpr + ')';
}
return condition + ' ? ' + trueExpr + ' : ' + falseExpr;
};
/**
* Get LaTeX representation
* @param {Object|function} callback(s)
* @return {String} str
*/
ConditionalNode.prototype._toTex = function (callbacks) {
return '\\left\\{\\begin{array}{l l}{'
+ this.trueExpr.toTex(callbacks) + '}, &\\quad{\\text{if}\\;'
+ this.condition.toTex(callbacks)
+ '}\\\\{' + this.falseExpr.toTex(callbacks)
+ '}, &\\quad{\\text{otherwise}}\\end{array}\\right.';
};
return ConditionalNode;
}
exports.name = 'ConditionalNode';
exports.path = 'expression.node';
exports.factory = factory;