1 | import { getPrecedence } from '../operators'
|
2 | import { escape } from '../../utils/string'
|
3 | import { getSafeProperty } from '../../utils/customs'
|
4 | import { latexOperators } from '../../utils/latex'
|
5 | import { factory } from '../../utils/factory'
|
6 |
|
7 | const name = 'RelationalNode'
|
8 | const dependencies = [
|
9 | 'Node'
|
10 | ]
|
11 |
|
12 | export const createRelationalNode = factory(name, dependencies, ({ Node }) => {
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
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 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
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 |
|
76 |
|
77 |
|
78 | RelationalNode.prototype.forEach = function (callback) {
|
79 | this.params.forEach((n, i) => callback(n, 'params[' + i + ']', this), this)
|
80 | }
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
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 |
|
94 |
|
95 |
|
96 | RelationalNode.prototype.clone = function () {
|
97 | return new RelationalNode(this.conditionals, this.params)
|
98 | }
|
99 |
|
100 | |
101 |
|
102 |
|
103 |
|
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 |
|
135 |
|
136 |
|
137 | RelationalNode.prototype.toJSON = function () {
|
138 | return {
|
139 | mathjs: 'RelationalNode',
|
140 | conditionals: this.conditionals,
|
141 | params: this.params
|
142 | }
|
143 | }
|
144 |
|
145 | |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | RelationalNode.fromJSON = function (json) {
|
153 | return new RelationalNode(json.conditionals, json.params)
|
154 | }
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
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 |
|
191 |
|
192 |
|
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 })
|