-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum-range.js
More file actions
47 lines (39 loc) · 1.15 KB
/
num-range.js
File metadata and controls
47 lines (39 loc) · 1.15 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
/*! NumRange v1.0.1 | (c) 2022 Kieran Barker | MIT License | https://github.com/kieranbarker/num-range */
class NumRange {
constructor({ start = 0, stop = undefined, step = 1 } = {}) {
if (typeof stop == "undefined") {
throw new TypeError("NumRange: Stop value cannot be undefined.");
}
if (step == 0) {
throw new RangeError("NumRange: Step value cannot be zero.");
}
this.start = Number(start);
this.stop = Number(stop);
this.step = Number(step);
}
get length() {
const length = Math.ceil((this.stop - this.start) / this.step);
return Math.max(length, 0);
}
*[Symbol.iterator]() {
let num = this.start;
while (this.step > 0 ? num < this.stop : num > this.stop) {
yield num;
num += this.step;
}
}
at(index) {
if (index >= 0 && index > this.length - 1) return;
if (index < 0 && Math.abs(index) > this.length) return;
return (index >= 0 ? this.start : this.stop) + this.step * index;
}
indexOf(num) {
const index = (num - this.start) / this.step;
if (index < 0 || index > this.length - 1) return -1;
if (!Number.isInteger(index)) return -1;
return index;
}
includes(num) {
return this.indexOf(num) > -1;
}
}