|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | import { isBigNumber, isComplex, isNode, isUnit } from '../../utils/is' |
| 4 | +import { factory } from '../../utils/factory' |
| 5 | +import { getPrecedence } from '../operators' |
4 | 6 |
|
5 | | -const operators = require('../operators') |
6 | | - |
7 | | -function factory (type, config, load, typed) { |
8 | | - const Node = load(require('./Node')) |
9 | | - const mathTypeOf = load(require('../../function/utils/typeOf')) |
| 7 | +const name = 'expression.node.ConditionalNode' |
| 8 | +const dependencies = [ |
| 9 | + 'typeOf', |
| 10 | + 'expression.node.Node' |
| 11 | +] |
10 | 12 |
|
| 13 | +export const createConditionalNode = factory(name, dependencies, ({ typeOf, expression: { node: { Node } } }) => { |
11 | 14 | /** |
12 | 15 | * A lazy evaluating conditional operator: 'condition ? trueExpr : falseExpr' |
13 | 16 | * |
@@ -101,30 +104,30 @@ function factory (type, config, load, typed) { |
101 | 104 | */ |
102 | 105 | ConditionalNode.prototype._toString = function (options) { |
103 | 106 | const parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep' |
104 | | - const precedence = operators.getPrecedence(this, parenthesis) |
| 107 | + const precedence = getPrecedence(this, parenthesis) |
105 | 108 |
|
106 | 109 | // Enclose Arguments in parentheses if they are an OperatorNode |
107 | 110 | // or have lower or equal precedence |
108 | 111 | // NOTE: enclosing all OperatorNodes in parentheses is a decision |
109 | 112 | // purely based on aesthetics and readability |
110 | 113 | let condition = this.condition.toString(options) |
111 | | - const conditionPrecedence = operators.getPrecedence(this.condition, parenthesis) |
| 114 | + const conditionPrecedence = getPrecedence(this.condition, parenthesis) |
112 | 115 | if ((parenthesis === 'all') || |
113 | 116 | (this.condition.type === 'OperatorNode') || |
114 | 117 | ((conditionPrecedence !== null) && (conditionPrecedence <= precedence))) { |
115 | 118 | condition = '(' + condition + ')' |
116 | 119 | } |
117 | 120 |
|
118 | 121 | let trueExpr = this.trueExpr.toString(options) |
119 | | - const truePrecedence = operators.getPrecedence(this.trueExpr, parenthesis) |
| 122 | + const truePrecedence = getPrecedence(this.trueExpr, parenthesis) |
120 | 123 | if ((parenthesis === 'all') || |
121 | 124 | (this.trueExpr.type === 'OperatorNode') || |
122 | 125 | ((truePrecedence !== null) && (truePrecedence <= precedence))) { |
123 | 126 | trueExpr = '(' + trueExpr + ')' |
124 | 127 | } |
125 | 128 |
|
126 | 129 | let falseExpr = this.falseExpr.toString(options) |
127 | | - const falsePrecedence = operators.getPrecedence(this.falseExpr, parenthesis) |
| 130 | + const falsePrecedence = getPrecedence(this.falseExpr, parenthesis) |
128 | 131 | if ((parenthesis === 'all') || |
129 | 132 | (this.falseExpr.type === 'OperatorNode') || |
130 | 133 | ((falsePrecedence !== null) && (falsePrecedence <= precedence))) { |
@@ -164,30 +167,30 @@ function factory (type, config, load, typed) { |
164 | 167 | */ |
165 | 168 | ConditionalNode.prototype.toHTML = function (options) { |
166 | 169 | const parenthesis = (options && options.parenthesis) ? options.parenthesis : 'keep' |
167 | | - const precedence = operators.getPrecedence(this, parenthesis) |
| 170 | + const precedence = getPrecedence(this, parenthesis) |
168 | 171 |
|
169 | 172 | // Enclose Arguments in parentheses if they are an OperatorNode |
170 | 173 | // or have lower or equal precedence |
171 | 174 | // NOTE: enclosing all OperatorNodes in parentheses is a decision |
172 | 175 | // purely based on aesthetics and readability |
173 | 176 | let condition = this.condition.toHTML(options) |
174 | | - const conditionPrecedence = operators.getPrecedence(this.condition, parenthesis) |
| 177 | + const conditionPrecedence = getPrecedence(this.condition, parenthesis) |
175 | 178 | if ((parenthesis === 'all') || |
176 | 179 | (this.condition.type === 'OperatorNode') || |
177 | 180 | ((conditionPrecedence !== null) && (conditionPrecedence <= precedence))) { |
178 | 181 | condition = '<span class="math-parenthesis math-round-parenthesis">(</span>' + condition + '<span class="math-parenthesis math-round-parenthesis">)</span>' |
179 | 182 | } |
180 | 183 |
|
181 | 184 | let trueExpr = this.trueExpr.toHTML(options) |
182 | | - const truePrecedence = operators.getPrecedence(this.trueExpr, parenthesis) |
| 185 | + const truePrecedence = getPrecedence(this.trueExpr, parenthesis) |
183 | 186 | if ((parenthesis === 'all') || |
184 | 187 | (this.trueExpr.type === 'OperatorNode') || |
185 | 188 | ((truePrecedence !== null) && (truePrecedence <= precedence))) { |
186 | 189 | trueExpr = '<span class="math-parenthesis math-round-parenthesis">(</span>' + trueExpr + '<span class="math-parenthesis math-round-parenthesis">)</span>' |
187 | 190 | } |
188 | 191 |
|
189 | 192 | let falseExpr = this.falseExpr.toHTML(options) |
190 | | - const falsePrecedence = operators.getPrecedence(this.falseExpr, parenthesis) |
| 193 | + const falsePrecedence = getPrecedence(this.falseExpr, parenthesis) |
191 | 194 | if ((parenthesis === 'all') || |
192 | 195 | (this.falseExpr.type === 'OperatorNode') || |
193 | 196 | ((falsePrecedence !== null) && (falsePrecedence <= precedence))) { |
@@ -239,12 +242,8 @@ function factory (type, config, load, typed) { |
239 | 242 | return false |
240 | 243 | } |
241 | 244 |
|
242 | | - throw new TypeError('Unsupported type of condition "' + mathTypeOf(condition) + '"') |
| 245 | + throw new TypeError('Unsupported type of condition "' + typeOf(condition) + '"') |
243 | 246 | } |
244 | 247 |
|
245 | 248 | return ConditionalNode |
246 | | -} |
247 | | - |
248 | | -exports.name = 'ConditionalNode' |
249 | | -exports.path = 'expression.node' |
250 | | -exports.factory = factory |
| 249 | +}) |
0 commit comments