Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generator utilities
- const Generator = Object.getPrototypeOf(function*() {})
- Generator.range = function*(start, end, step=1) {
- let number = start
- function stop() {
- return step >= 0 ? number >= end : number <=end
- }
- while (!stop()) {
- yield number
- number += step
- }
- }
- Generator.prototype.forEach = function(callback) {
- let index = 0
- for (const item of this) {
- callback(item, index++, this)
- }
- }
- Generator.prototype.until = function*(condition) {
- for (const item of this) {
- if (!condition(item)) {
- yield item
- } else {
- return
- }
- }
- }
- Generator.prototype.exhaust = function(n) {
- for (let i = 0; i < n; i++) {
- this.next()
- }
- }
- Generator.prototype.dropFirst = function*(n) {
- this.exhaust(n)
- yield* this
- }
- module.exports = Generator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement