UNPKG

7.56 kBJavaScriptView Raw
1import { getPrecedence } from '../operators'
2import { escape } from '../../utils/string'
3import { getSafeProperty } from '../../utils/customs'
4import { latexOperators } from '../../utils/latex'
5import { factory } from '../../utils/factory'
6
7const name = 'RelationalNode'
8const dependencies = [
9 'Node'
10]
11
12export const createRelationalNode = /* #__PURE__ */ factory(name, dependencies, ({ Node }) => {
13 /**
14 * A node representing a chained conditional expression, such as 'x > y > z'
15 *
16 * @param {String[]} conditionals An array of conditional operators used to compare the parameters
17 * @param {Node[]} params The parameters that will be compared
18 *
19 * @constructor RelationalNode
20 * @extends {Node}
21 */
22 function RelationalNode (conditionals, params) {
23 if (!(this instanceof RelationalNode)) {
24 throw new SyntaxError('Constructor must be called with the new operator')
25 }
26 if (!Array.isArray(conditionals)) throw new TypeError('Parameter conditionals must be an array')
27 if (!Array.isArray(params)) throw new TypeError('Parameter params must be an array')
28 if (conditionals.length !== params.length - 1) throw new TypeError('Parameter params must contain exactly one more element than parameter conditionals')
29
30 this.conditionals = conditionals
31 this.params = params
32 }
33
34 RelationalNode.prototype = new Node()
35
36 RelationalNode.prototype.type = 'RelationalNode'
37
38 RelationalNode.prototype.isRelationalNode = true
39
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 RelationalNode.prototype._compile = function (math, argNames) {
54 const self = this
55
56 const compiled = this.params.map(p => p._compile(math, argNames))
57
58 return function evalRelationalNode (scope, args, context) {
59 let evalLhs
60 let evalRhs = compiled[0](scope, args, context)
61
62 for (let i = 0; i < self.conditionals.length; i++) {
63 evalLhs = evalRhs
64 evalRhs = compiled[i + 1](scope, args, context)
65 const condFn = getSafeProperty(math, self.conditionals[i])
66 if (!condFn(evalLhs, evalRhs)) {
67 return false
68 }
69 }
70 return true
71 }
72 }
73
74 /**
75 * Execute a callback for each of the child nodes of this node
76 * @param {function(child: Node, path: string, parent: Node)} callback
77 */
78 RelationalNode.prototype.forEach = function (callback) {
79 this.params.forEach((n, i) => callback(n, 'params[' + i + ']', this), this)
80 }
81
82 /**
83 * Create a new RelationalNode having its childs be the results of calling
84 * the provided callback function for each of the childs of the original node.
85 * @param {function(child: Node, path: string, parent: Node): Node} callback
86 * @returns {RelationalNode} Returns a transformed copy of the node
87 */
88 RelationalNode.prototype.map = function (callback) {
89 return new RelationalNode(this.conditionals.slice(), this.params.map((n, i) => this._ifNode(callback(n, 'params[' + i + ']', this)), this))
90 }
91
92 /**
93 * Create a clone of this node, a shallow copy
94 * @return {RelationalNode}
95 */
96 RelationalNode.prototype.clone = function () {
97 return new RelationalNode(this.conditionals, this.params)
98 }
99
100 /**
101 * Get string representation.
102 * @param {Object} options
103 * @return {string} str
104 */
105 RelationalNode.prototype._toString = function (options) {
106 const parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'
107 const precedence = getPrecedence(this, parenthesis)
108
109 const paramStrings = this.params.map(function (p, index) {
110 const paramPrecedence = getPrecedence(p, parenthesis)
111 return (parenthesis === 'all' || (paramPrecedence !== null && paramPrecedence <= precedence))
112 ? '(' + p.toString(options) + ')'
113 : p.toString(options)
114 })
115
116 const operatorMap = {
117 equal: '==',
118 unequal: '!=',
119 smaller: '<',
120 larger: '>',
121 smallerEq: '<=',
122 largerEq: '>='
123 }
124
125 let ret = paramStrings[0]
126 for (let i = 0; i < this.conditionals.length; i++) {
127 ret += ' ' + operatorMap[this.conditionals[i]] + ' ' + paramStrings[i + 1]
128 }
129
130 return ret
131 }
132
133 /**
134 * Get a JSON representation of the node
135 * @returns {Object}
136 */
137 RelationalNode.prototype.toJSON = function () {
138 return {
139 mathjs: 'RelationalNode',
140 conditionals: this.conditionals,
141 params: this.params
142 }
143 }
144
145 /**
146 * Instantiate a RelationalNode from its JSON representation
147 * @param {Object} json An object structured like
148 * `{"mathjs": "RelationalNode", "condition": ..., "trueExpr": ..., "falseExpr": ...}`,
149 * where mathjs is optional
150 * @returns {RelationalNode}
151 */
152 RelationalNode.fromJSON = function (json) {
153 return new RelationalNode(json.conditionals, json.params)
154 }
155
156 /**
157 * Get HTML representation
158 * @param {Object} options
159 * @return {string} str
160 */
161 RelationalNode.prototype.toHTML = function (options) {
162 const parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'
163 const precedence = getPrecedence(this, parenthesis)
164
165 const paramStrings = this.params.map(function (p, index) {
166 const paramPrecedence = getPrecedence(p, parenthesis)
167 return (parenthesis === 'all' || (paramPrecedence !== null && paramPrecedence <= precedence))
168 ? '<span class="math-parenthesis math-round-parenthesis">(</span>' + p.toHTML(options) + '<span class="math-parenthesis math-round-parenthesis">)</span>'
169 : p.toHTML(options)
170 })
171
172 const operatorMap = {
173 equal: '==',
174 unequal: '!=',
175 smaller: '<',
176 larger: '>',
177 smallerEq: '<=',
178 largerEq: '>='
179 }
180
181 let ret = paramStrings[0]
182 for (let i = 0; i < this.conditionals.length; i++) {
183 ret += '<span class="math-operator math-binary-operator math-explicit-binary-operator">' + escape(operatorMap[this.conditionals[i]]) + '</span>' + paramStrings[i + 1]
184 }
185
186 return ret
187 }
188
189 /**
190 * Get LaTeX representation
191 * @param {Object} options
192 * @return {string} str
193 */
194 RelationalNode.prototype._toTex = function (options) {
195 const parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep'
196 const precedence = getPrecedence(this, parenthesis)
197
198 const paramStrings = this.params.map(function (p, index) {
199 const paramPrecedence = getPrecedence(p, parenthesis)
200 return (parenthesis === 'all' || (paramPrecedence !== null && paramPrecedence <= precedence))
201 ? '\\left(' + p.toTex(options) + '\right)'
202 : p.toTex(options)
203 })
204
205 let ret = paramStrings[0]
206 for (let i = 0; i < this.conditionals.length; i++) {
207 ret += latexOperators[this.conditionals[i]] + paramStrings[i + 1]
208 }
209
210 return ret
211 }
212
213 return RelationalNode
214}, { isClass: true, isNode: true })