-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (47 loc) · 1.13 KB
/
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
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
'use strict'
const {XIterable, Root} = require('x-iterable-base')
const {iterator} = Symbol
const createYielder = (base, callback) => {
const iterable = base.map(callback)
return {
[iterator]: () => iterable[iterator](),
__proto__: base
}
}
class RangeIterable extends XIterable(Root) {
constructor (begin, end) {
super()
begin >>= 0
end >>= 0
return begin <= end && {
* [iterator] () {
for (let i = begin; i !== end; ++i) {
yield i
}
},
reverse () {
const endMinusOne = end - 1
return createYielder(this, value => endMinusOne - value)
},
__proto__: this
}
}
* [iterator] () {}
shift (delta) {
return createYielder(this, value => value + delta)
}
multiply (factor) {
return createYielder(this, value => value * factor)
}
static range (...args) {
switch (args.length) {
case 1:
return new Range(0, args[0])
case 2:
return new Range(...args)
}
throw new RangeError('Number of provided arguments must be 1 or 2')
}
}
class Range extends RangeIterable {}
module.exports = Range