UNPKG

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