UNPKG

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