Created
January 27, 2019 05:08
-
-
Save hk-skit/582a779c9696e1acd956404c78c084ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RangeIterator { | |
constructor({ | |
start = 0, | |
end = 1000, | |
step = 1 | |
}) { | |
this.start = start; | |
this.end = end; | |
this.step = step; | |
} | |
[Symbol.iterator]() { | |
let start = this.start; | |
const iterable = { | |
// Next fn complying with Iterator protocol. | |
next: () => { | |
if (start <= this.end) { | |
const result = { | |
value: start, | |
done: false | |
}; | |
start += this.step; | |
return result; | |
} | |
return { | |
done: true | |
}; | |
} | |
} | |
return iterable;; | |
} | |
} | |
const range = new RangeIterator({ | |
start: 0, | |
end: 20, | |
step: 5 | |
}); | |
console.log(...range); // 0 5 10 15 20 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment