1 | import { isNode, isSymbolNode } from '../../utils/is';
|
2 | import { factory } from '../../utils/factory';
|
3 | import { getPrecedence } from '../operators';
|
4 | var name = 'RangeNode';
|
5 | var dependencies = ['Node'];
|
6 | export var createRangeNode = factory(name, dependencies, function (_ref) {
|
7 | var Node = _ref.Node;
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function RangeNode(start, end, step) {
|
18 | if (!(this instanceof RangeNode)) {
|
19 | throw new SyntaxError('Constructor must be called with the new operator');
|
20 | }
|
21 |
|
22 |
|
23 | if (!isNode(start)) throw new TypeError('Node expected');
|
24 | if (!isNode(end)) throw new TypeError('Node expected');
|
25 | if (step && !isNode(step)) throw new TypeError('Node expected');
|
26 | if (arguments.length > 3) throw new Error('Too many arguments');
|
27 | this.start = start;
|
28 |
|
29 | this.end = end;
|
30 |
|
31 | this.step = step || null;
|
32 | }
|
33 |
|
34 | RangeNode.prototype = new Node();
|
35 | RangeNode.prototype.type = 'RangeNode';
|
36 | RangeNode.prototype.isRangeNode = true;
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | RangeNode.prototype.needsEnd = function () {
|
44 |
|
45 | var endSymbols = this.filter(function (node) {
|
46 | return isSymbolNode(node) && node.name === 'end';
|
47 | });
|
48 | return endSymbols.length > 0;
|
49 | };
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | RangeNode.prototype._compile = function (math, argNames) {
|
66 | var range = math.range;
|
67 |
|
68 | var evalStart = this.start._compile(math, argNames);
|
69 |
|
70 | var evalEnd = this.end._compile(math, argNames);
|
71 |
|
72 | if (this.step) {
|
73 | var evalStep = this.step._compile(math, argNames);
|
74 |
|
75 | return function evalRangeNode(scope, args, context) {
|
76 | return range(evalStart(scope, args, context), evalEnd(scope, args, context), evalStep(scope, args, context));
|
77 | };
|
78 | } else {
|
79 | return function evalRangeNode(scope, args, context) {
|
80 | return range(evalStart(scope, args, context), evalEnd(scope, args, context));
|
81 | };
|
82 | }
|
83 | };
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | RangeNode.prototype.forEach = function (callback) {
|
91 | callback(this.start, 'start', this);
|
92 | callback(this.end, 'end', this);
|
93 |
|
94 | if (this.step) {
|
95 | callback(this.step, 'step', this);
|
96 | }
|
97 | };
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | RangeNode.prototype.map = function (callback) {
|
107 | return new RangeNode(this._ifNode(callback(this.start, 'start', this)), this._ifNode(callback(this.end, 'end', this)), this.step && this._ifNode(callback(this.step, 'step', this)));
|
108 | };
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | RangeNode.prototype.clone = function () {
|
116 | return new RangeNode(this.start, this.end, this.step && this.step);
|
117 | };
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function calculateNecessaryParentheses(node, parenthesis) {
|
128 | var precedence = getPrecedence(node, parenthesis);
|
129 | var parens = {};
|
130 | var startPrecedence = getPrecedence(node.start, parenthesis);
|
131 | parens.start = startPrecedence !== null && startPrecedence <= precedence || parenthesis === 'all';
|
132 |
|
133 | if (node.step) {
|
134 | var stepPrecedence = getPrecedence(node.step, parenthesis);
|
135 | parens.step = stepPrecedence !== null && stepPrecedence <= precedence || parenthesis === 'all';
|
136 | }
|
137 |
|
138 | var endPrecedence = getPrecedence(node.end, parenthesis);
|
139 | parens.end = endPrecedence !== null && endPrecedence <= precedence || parenthesis === 'all';
|
140 | return parens;
|
141 | }
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | RangeNode.prototype._toString = function (options) {
|
150 | var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
|
151 | var parens = calculateNecessaryParentheses(this, parenthesis);
|
152 |
|
153 | var str;
|
154 | var start = this.start.toString(options);
|
155 |
|
156 | if (parens.start) {
|
157 | start = '(' + start + ')';
|
158 | }
|
159 |
|
160 | str = start;
|
161 |
|
162 | if (this.step) {
|
163 | var step = this.step.toString(options);
|
164 |
|
165 | if (parens.step) {
|
166 | step = '(' + step + ')';
|
167 | }
|
168 |
|
169 | str += ':' + step;
|
170 | }
|
171 |
|
172 | var end = this.end.toString(options);
|
173 |
|
174 | if (parens.end) {
|
175 | end = '(' + end + ')';
|
176 | }
|
177 |
|
178 | str += ':' + end;
|
179 | return str;
|
180 | };
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | RangeNode.prototype.toJSON = function () {
|
188 | return {
|
189 | mathjs: 'RangeNode',
|
190 | start: this.start,
|
191 | end: this.end,
|
192 | step: this.step
|
193 | };
|
194 | };
|
195 | |
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | RangeNode.fromJSON = function (json) {
|
205 | return new RangeNode(json.start, json.end, json.step);
|
206 | };
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | RangeNode.prototype.toHTML = function (options) {
|
215 | var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
|
216 | var parens = calculateNecessaryParentheses(this, parenthesis);
|
217 |
|
218 | var str;
|
219 | var start = this.start.toHTML(options);
|
220 |
|
221 | if (parens.start) {
|
222 | start = '<span class="math-parenthesis math-round-parenthesis">(</span>' + start + '<span class="math-parenthesis math-round-parenthesis">)</span>';
|
223 | }
|
224 |
|
225 | str = start;
|
226 |
|
227 | if (this.step) {
|
228 | var step = this.step.toHTML(options);
|
229 |
|
230 | if (parens.step) {
|
231 | step = '<span class="math-parenthesis math-round-parenthesis">(</span>' + step + '<span class="math-parenthesis math-round-parenthesis">)</span>';
|
232 | }
|
233 |
|
234 | str += '<span class="math-operator math-range-operator">:</span>' + step;
|
235 | }
|
236 |
|
237 | var end = this.end.toHTML(options);
|
238 |
|
239 | if (parens.end) {
|
240 | end = '<span class="math-parenthesis math-round-parenthesis">(</span>' + end + '<span class="math-parenthesis math-round-parenthesis">)</span>';
|
241 | }
|
242 |
|
243 | str += '<span class="math-operator math-range-operator">:</span>' + end;
|
244 | return str;
|
245 | };
|
246 | |
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 | RangeNode.prototype._toTex = function (options) {
|
254 | var parenthesis = options && options.parenthesis ? options.parenthesis : 'keep';
|
255 | var parens = calculateNecessaryParentheses(this, parenthesis);
|
256 | var str = this.start.toTex(options);
|
257 |
|
258 | if (parens.start) {
|
259 | str = "\\left(".concat(str, "\\right)");
|
260 | }
|
261 |
|
262 | if (this.step) {
|
263 | var step = this.step.toTex(options);
|
264 |
|
265 | if (parens.step) {
|
266 | step = "\\left(".concat(step, "\\right)");
|
267 | }
|
268 |
|
269 | str += ':' + step;
|
270 | }
|
271 |
|
272 | var end = this.end.toTex(options);
|
273 |
|
274 | if (parens.end) {
|
275 | end = "\\left(".concat(end, "\\right)");
|
276 | }
|
277 |
|
278 | str += ':' + end;
|
279 | return str;
|
280 | };
|
281 |
|
282 | return RangeNode;
|
283 | }, {
|
284 | isClass: true,
|
285 | isNode: true
|
286 | }); |
\ | No newline at end of file |