Repository navigation
Expand file tree
/
Copy pathRange.js
More file actions
316 lines (286 loc) · 7.99 KB
/
Copy pathRange.js
File metadata and controls
316 lines (286 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
'use strict';
var number = require('../../utils/number');
function factory (type, config, load, typed) {
/**
* Create a range. A range has a start, step, and end, and contains functions
* to iterate over the range.
*
* A range can be constructed as:
* var range = new Range(start, end);
* var range = new Range(start, end, step);
*
* To get the result of the range:
* range.forEach(function (x) {
* console.log(x);
* });
* range.map(function (x) {
* return math.sin(x);
* });
* range.toArray();
*
* Example usage:
* var c = new Range(2, 6); // 2:1:5
* c.toArray(); // [2, 3, 4, 5]
* var d = new Range(2, -3, -1); // 2:-1:-2
* d.toArray(); // [2, 1, 0, -1, -2]
*
* @class Range
* @constructor Range
* @param {number} start included lower bound
* @param {number} end excluded upper bound
* @param {number} [step] step size, default value is 1
*/
function Range(start, end, step) {
if (!(this instanceof Range)) {
throw new SyntaxError('Constructor must be called with the new operator');
}
if (start != null) {
if (type.isBigNumber(start))
start = start.toNumber();
else if (typeof start !== 'number')
throw new TypeError('Parameter start must be a number');
}
if (end != null) {
if (type.isBigNumber(end))
end = end.toNumber();
else if (typeof end !== 'number')
throw new TypeError('Parameter end must be a number');
}
if (step != null) {
if (type.isBigNumber(step))
step = step.toNumber();
else if (typeof step !== 'number')
throw new TypeError('Parameter step must be a number');
}
this.start = (start != null) ? parseFloat(start) : 0;
this.end = (end != null) ? parseFloat(end) : 0;
this.step = (step != null) ? parseFloat(step) : 1;
}
/**
* Attach type information
*/
Range.prototype.type = 'Range';
Range.prototype.isRange = true;
/**
* Parse a string into a range,
* The string contains the start, optional step, and end, separated by a colon.
* If the string does not contain a valid range, null is returned.
* For example str='0:2:11'.
* @memberof Range
* @param {string} str
* @return {Range | null} range
*/
Range.parse = function (str) {
if (typeof str !== 'string') {
return null;
}
var args = str.split(':');
var nums = args.map(function (arg) {
return parseFloat(arg);
});
var invalid = nums.some(function (num) {
return isNaN(num);
});
if (invalid) {
return null;
}
switch (nums.length) {
case 2:
return new Range(nums[0], nums[1]);
case 3:
return new Range(nums[0], nums[2], nums[1]);
default:
return null;
}
};
/**
* Create a clone of the range
* @return {Range} clone
*/
Range.prototype.clone = function () {
return new Range(this.start, this.end, this.step);
};
/**
* Retrieve the size of the range.
* Returns an array containing one number, the number of elements in the range.
* @memberof Range
* @returns {number[]} size
*/
Range.prototype.size = function () {
var len = 0,
start = this.start,
step = this.step,
end = this.end,
diff = end - start;
if (number.sign(step) == number.sign(diff)) {
len = Math.ceil((diff) / step);
}
else if (diff == 0) {
len = 0;
}
if (isNaN(len)) {
len = 0;
}
return [len];
};
/**
* Calculate the minimum value in the range
* @memberof Range
* @return {number | undefined} min
*/
Range.prototype.min = function () {
var size = this.size()[0];
if (size > 0) {
if (this.step > 0) {
// positive step
return this.start;
}
else {
// negative step
return this.start + (size - 1) * this.step;
}
}
else {
return undefined;
}
};
/**
* Calculate the maximum value in the range
* @memberof Range
* @return {number | undefined} max
*/
Range.prototype.max = function () {
var size = this.size()[0];
if (size > 0) {
if (this.step > 0) {
// positive step
return this.start + (size - 1) * this.step;
}
else {
// negative step
return this.start;
}
}
else {
return undefined;
}
};
/**
* Execute a callback function for each value in the range.
* @memberof Range
* @param {function} callback The callback method is invoked with three
* parameters: the value of the element, the index
* of the element, and the Range being traversed.
*/
Range.prototype.forEach = function (callback) {
var x = this.start;
var step = this.step;
var end = this.end;
var i = 0;
if (step > 0) {
while (x < end) {
callback(x, [i], this);
x += step;
i++;
}
}
else if (step < 0) {
while (x > end) {
callback(x, [i], this);
x += step;
i++;
}
}
};
/**
* Execute a callback function for each value in the Range, and return the
* results as an array
* @memberof Range
* @param {function} callback The callback method is invoked with three
* parameters: the value of the element, the index
* of the element, and the Matrix being traversed.
* @returns {Array} array
*/
Range.prototype.map = function (callback) {
var array = [];
this.forEach(function (value, index, obj) {
array[index[0]] = callback(value, index, obj);
});
return array;
};
/**
* Create an Array with a copy of the Ranges data
* @memberof Range
* @returns {Array} array
*/
Range.prototype.toArray = function () {
var array = [];
this.forEach(function (value, index) {
array[index[0]] = value;
});
return array;
};
/**
* Get the primitive value of the Range, a one dimensional array
* @memberof Range
* @returns {Array} array
*/
Range.prototype.valueOf = function () {
// TODO: implement a caching mechanism for range.valueOf()
return this.toArray();
};
/**
* Get a string representation of the range, with optional formatting options.
* Output is formatted as 'start:step:end', for example '2:6' or '0:0.2:11'
* @memberof Range
* @param {Object | number | function} [options] Formatting options. See
* lib/utils/number:format for a
* description of the available
* options.
* @returns {string} str
*/
Range.prototype.format = function (options) {
var str = number.format(this.start, options);
if (this.step != 1) {
str += ':' + number.format(this.step, options);
}
str += ':' + number.format(this.end, options);
return str;
};
/**
* Get a string representation of the range.
* @memberof Range
* @returns {string}
*/
Range.prototype.toString = function () {
return this.format();
};
/**
* Get a JSON representation of the range
* @memberof Range
* @returns {Object} Returns a JSON object structured as:
* `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}`
*/
Range.prototype.toJSON = function () {
return {
mathjs: 'Range',
start: this.start,
end: this.end,
step: this.step
};
};
/**
* Instantiate a Range from a JSON object
* @memberof Range
* @param {Object} json A JSON object structured as:
* `{"mathjs": "Range", "start": 2, "end": 4, "step": 1}`
* @return {Range}
*/
Range.fromJSON = function (json) {
return new Range(json.start, json.end, json.step);
};
return Range;
}
exports.name = 'Range';
exports.path = 'type';
exports.factory = factory;