ParenthesisNode.js (5130B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createParenthesisNode = void 0; 7 8 var _is = require("../../utils/is.js"); 9 10 var _factory = require("../../utils/factory.js"); 11 12 var name = 'ParenthesisNode'; 13 var dependencies = ['Node']; 14 var createParenthesisNode = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 15 var Node = _ref.Node; 16 17 /** 18 * @constructor ParenthesisNode 19 * @extends {Node} 20 * A parenthesis node describes manual parenthesis from the user input 21 * @param {Node} content 22 * @extends {Node} 23 */ 24 function ParenthesisNode(content) { 25 if (!(this instanceof ParenthesisNode)) { 26 throw new SyntaxError('Constructor must be called with the new operator'); 27 } // validate input 28 29 30 if (!(0, _is.isNode)(content)) { 31 throw new TypeError('Node expected for parameter "content"'); 32 } 33 34 this.content = content; 35 } 36 37 ParenthesisNode.prototype = new Node(); 38 ParenthesisNode.prototype.type = 'ParenthesisNode'; 39 ParenthesisNode.prototype.isParenthesisNode = true; 40 /** 41 * Compile a node into a JavaScript function. 42 * This basically pre-calculates as much as possible and only leaves open 43 * calculations which depend on a dynamic scope with variables. 44 * @param {Object} math Math.js namespace with functions and constants. 45 * @param {Object} argNames An object with argument names as key and `true` 46 * as value. Used in the SymbolNode to optimize 47 * for arguments from user assigned functions 48 * (see FunctionAssignmentNode) or special symbols 49 * like `end` (see IndexNode). 50 * @return {function} Returns a function which can be called like: 51 * evalNode(scope: Object, args: Object, context: *) 52 */ 53 54 ParenthesisNode.prototype._compile = function (math, argNames) { 55 return this.content._compile(math, argNames); 56 }; 57 /** 58 * Get the content of the current Node. 59 * @return {Node} content 60 * @override 61 **/ 62 63 64 ParenthesisNode.prototype.getContent = function () { 65 return this.content.getContent(); 66 }; 67 /** 68 * Execute a callback for each of the child nodes of this node 69 * @param {function(child: Node, path: string, parent: Node)} callback 70 */ 71 72 73 ParenthesisNode.prototype.forEach = function (callback) { 74 callback(this.content, 'content', this); 75 }; 76 /** 77 * Create a new ParenthesisNode having it's childs be the results of calling 78 * the provided callback function for each of the childs of the original node. 79 * @param {function(child: Node, path: string, parent: Node) : Node} callback 80 * @returns {ParenthesisNode} Returns a clone of the node 81 */ 82 83 84 ParenthesisNode.prototype.map = function (callback) { 85 var content = callback(this.content, 'content', this); 86 return new ParenthesisNode(content); 87 }; 88 /** 89 * Create a clone of this node, a shallow copy 90 * @return {ParenthesisNode} 91 */ 92 93 94 ParenthesisNode.prototype.clone = function () { 95 return new ParenthesisNode(this.content); 96 }; 97 /** 98 * Get string representation 99 * @param {Object} options 100 * @return {string} str 101 * @override 102 */ 103 104 105 ParenthesisNode.prototype._toString = function (options) { 106 if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') { 107 return '(' + this.content.toString(options) + ')'; 108 } 109 110 return this.content.toString(options); 111 }; 112 /** 113 * Get a JSON representation of the node 114 * @returns {Object} 115 */ 116 117 118 ParenthesisNode.prototype.toJSON = function () { 119 return { 120 mathjs: 'ParenthesisNode', 121 content: this.content 122 }; 123 }; 124 /** 125 * Instantiate an ParenthesisNode from its JSON representation 126 * @param {Object} json An object structured like 127 * `{"mathjs": "ParenthesisNode", "content": ...}`, 128 * where mathjs is optional 129 * @returns {ParenthesisNode} 130 */ 131 132 133 ParenthesisNode.fromJSON = function (json) { 134 return new ParenthesisNode(json.content); 135 }; 136 /** 137 * Get HTML representation 138 * @param {Object} options 139 * @return {string} str 140 * @override 141 */ 142 143 144 ParenthesisNode.prototype.toHTML = function (options) { 145 if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') { 146 return '<span class="math-parenthesis math-round-parenthesis">(</span>' + this.content.toHTML(options) + '<span class="math-parenthesis math-round-parenthesis">)</span>'; 147 } 148 149 return this.content.toHTML(options); 150 }; 151 /** 152 * Get LaTeX representation 153 * @param {Object} options 154 * @return {string} str 155 * @override 156 */ 157 158 159 ParenthesisNode.prototype._toTex = function (options) { 160 if (!options || options && !options.parenthesis || options && options.parenthesis === 'keep') { 161 return "\\left(".concat(this.content.toTex(options), "\\right)"); 162 } 163 164 return this.content.toTex(options); 165 }; 166 167 return ParenthesisNode; 168 }, { 169 isClass: true, 170 isNode: true 171 }); 172 exports.createParenthesisNode = createParenthesisNode;