'use strict'; var format = require('../../utils/string').format; var escapeLatex = require('../../utils/latex').escape; function factory (type, config, load, typed) { var Node = load(require('./Node')); var getType = load(require('../../function/utils/typeof')); /** * A ConstantNode holds a constant value like a number or string. * * Usage: * * new ConstantNode(2.3); * new ConstantNode('hello'); * * @param {*} value Value can be any type (number, BigNumber, string, ...) * @constructor ConstantNode * @extends {Node} */ function ConstantNode(value) { if (!(this instanceof ConstantNode)) { throw new SyntaxError('Constructor must be called with the new operator'); } if (arguments.length === 2) { // TODO: remove deprecation error some day (created 2018-01-23) throw new SyntaxError('new ConstantNode(valueStr, valueType) is not supported anymore since math v4.0.0. Use new ConstantNode(value) instead, where value is a non-stringified value.'); } this.value = value; } ConstantNode.prototype = new Node(); ConstantNode.prototype.type = 'ConstantNode'; ConstantNode.prototype.isConstantNode = true; /** * Compile a node into a JavaScript function. * This basically pre-calculates as much as possible and only leaves open * calculations which depend on a dynamic scope with variables. * @param {Object} math Math.js namespace with functions and constants. * @param {Object} argNames An object with argument names as key and `true` * as value. Used in the SymbolNode to optimize * for arguments from user assigned functions * (see FunctionAssignmentNode) or special symbols * like `end` (see IndexNode). * @return {function} Returns a function which can be called like: * evalNode(scope: Object, args: Object, context: *) */ ConstantNode.prototype._compile = function (math, argNames) { var value = this.value; return function evalConstantNode() { return value; } } /** * Execute a callback for each of the child nodes of this node * @param {function(child: Node, path: string, parent: Node)} callback */ ConstantNode.prototype.forEach = function (callback) { // nothing to do, we don't have childs }; /** * Create a new ConstantNode 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 {ConstantNode} Returns a clone of the node */ ConstantNode.prototype.map = function (callback) { return this.clone(); }; /** * Create a clone of this node, a shallow copy * @return {ConstantNode} */ ConstantNode.prototype.clone = function () { return new ConstantNode(this.value); }; /** * Get string representation * @param {Object} options * @return {string} str */ ConstantNode.prototype._toString = function (options) { return format (this.value, options); }; /** * Get HTML representation * @param {Object} options * @return {string} str */ ConstantNode.prototype.toHTML = function (options) { var value = this._toString(options); switch (getType(this.value)) { case 'number': case 'BigNumber': case 'Fraction': return '' + value + ''; case 'string': return '' + value + ''; case 'boolean': return '' + value + ''; case 'null': return '' + value + ''; case 'undefined': return '' + value + ''; default: return '' + value + ''; } }; /** * Get LaTeX representation * @param {Object} options * @return {string} str */ ConstantNode.prototype._toTex = function (options) { var value = this._toString(options); switch (getType(this.value)) { case 'string': return '\\mathtt{' + escapeLatex(value) + '}'; case 'number': case 'BigNumber': var index = value.toLowerCase().indexOf('e'); if (index !== -1) { return value.substring(0, index) + '\\cdot10^{' + value.substring(index + 1) + '}'; } return value; case 'Fraction': return this.value.toLatex(); default: return value; } }; return ConstantNode; } exports.name = 'ConstantNode'; exports.path = 'expression.node'; exports.factory = factory;