-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
28 lines (23 loc) · 809 Bytes
/
index.js
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
const validate = (name, value) => {
if (!Number.isSafeInteger(value)) {
throw new TypeError(`Expected \`${name}\` to be a safe integer.`);
}
};
const lt = (left, right) => left < right;
const gt = (left, right) => left > right;
const lte = (left, right) => left <= right;
const gte = (left, right) => left >= right;
export default function * getRange({start = 0, end, step = 1, inclusive = false}) {
if (step === 0) {
throw new TypeError('The `step` option cannot be zero.');
}
validate('start', start);
validate('end', end);
validate('step', step);
const compareExclusive = step < 0 ? gt : lt;
const compareInclusive = step < 0 ? gte : lte;
const compare = inclusive ? compareInclusive : compareExclusive;
for (let index = start; compare(index, end); index += step) {
yield index;
}
}