Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EnhancedGenerator<T, TReturn, TNext>

Utilities to create and use generators which can be manipulated in various ways.

Most methods come both as instance (prototype) methods and as static methods. They provide equivalent functionality, but the static methods allow use on Iterator and Iterable objects without first converting to a generator.

The EnhancedGenerator.enhance method will add additional instance methods to an ordinary generator's prototype (a new prototype, not modifying any global prototype!). It can also be used to convert Iterator and Iterable objects to EnhancedGenerator.

For methods which return a EnhancedGenerator, care is take to propagate any Generator.throw and Generator.return calls to any supplied generators, so they can properly terminate.

The exception is EnhancedGenerator.flat (and by extension, EnhancedGenerator.flatMap), which cannot know what nested generators they might encounter in the future. Any generators encountered so far will be terminated, however.

Type parameters

  • T

    the type of values returned in the iteration result.

  • TReturn

    the type of values returned in the iteration result when the generator terminates

  • TNext

    the type of value which can be passed to .next(val).

Hierarchy

  • EnhancedGenerator

Implements

  • Generator<T, TReturn, TNext>
  • Iterable<T>

Index

Properties

Optional returning

returning: any

Methods

Abstract [Symbol.iterator]

  • [Symbol.iterator](): Generator<T>

asArray

  • asArray(): T[]

concat

  • Concatenates generators (or iterators or iterables).

    Ensures that any supplied generators are terminated when this is terminated.

    Type parameters

    Parameters

    • Rest ...gens: N

      zero or more additional Genable to provide values.

    Returns EnhancedGenerator<T, any, unknown>

every

  • every(p: IndexedPredicate<T>, thisArg?: any): boolean
  • Returns false and terminates this generator if the predicate is false for any of the generator's yielded values.

    If the generator terminates without having failed the predicate, true is returned.

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    • Optional thisArg: any

      Optional value to supply as context (this) for the predicate

    Returns boolean

filter

  • Return a new EnhancedGenerator that yields only the values that satisfy the predicate f.

    f receives the value and a sequential index.

    Parameters

    • f: (v: T) => boolean
        • (v: T): boolean
        • Parameters

          • v: T

          Returns boolean

    • Optional thisArg: any

      Optional context to be passed as this to the predicate.

    Returns EnhancedGenerator<T, TReturn, TNext>

flat

  • Flatten the values yielded by this generator to level depth. Produces a generator that yields the individual values at each level in depth-first order. Any iterable (including Array) or iterator will be traversed and its values yielded.

    The return type is currently over-broad

    Type parameters

    • D: number

    Parameters

    • Default value depth: D = 1 as D

      (default = 1)

    Returns EnhancedGenerator<FlatGen<T, D>>

flatMap

  • Flatten the values yielded by applying the function to the values yielded by the generator to level depth. Produces a generator that yields the individual values at each level in depth-first order. Any iterable (including Array) or iterator will be traversed and its values yielded.

    The return type is currently over-broad

    Type parameters

    • D: number

    • R

    Parameters

    • f: IndexedFn<T, R>
    • Default value depth: D = 1 as D

    Returns EnhancedGenerator<FlatGen<R, D>>

forEach

  • forEach(f: IndexedFn<T>, thisArg?: any): void
  • Operate on each value produced by this generator. f is called with two values, the value yielded by this generator and a sequential index.

    Parameters

    • f: IndexedFn<T>
    • Optional thisArg: any

      Value to be supplied as context this for function f.

    Returns void

join

  • join(sep?: undefined | string): string

limit

  • limit(max: number): Generator<T, any, unknown> | Iterator<T, any, undefined> | Iterable<T>
  • Limit the number of values that can be generated. A RangeError is thrown if this limit is exceeded. See EnhancedGenerator.slice if you want to truncate.

    Parameters

    • max: number

    Returns Generator<T, any, unknown> | Iterator<T, any, undefined> | Iterable<T>

map

  • Apply the function to each value yielded by this generator. It is called with two arguments, the value yielded, and a sequential index. The return value is a generator that yields the values produced by the function.

    Type parameters

    • V

    Parameters

    • f: IndexedFn<T, V>
    • Optional thisArg: any

      Optional value to be supplied as context this for function f.

    Returns EnhancedGenerator<V>

Abstract next

  • next(): IteratorResult<T>

reduce

  • reduce<A>(f: Reducer<A, T, T>): A
  • reduce<A>(f: Reducer<A, T, A>, init: A): A
  • Like Array.prototype.reduce, but the 3rd argument to the reducing function ("array") is omitted because there is no array.

    Type parameters

    • A

    Parameters

    Returns A

  • Like Array.prototype.reduce, but the 3rd argument to the reducing function ("array") is omitted because there is no array.

    Type parameters

    • A

    Parameters

    Returns A

repeat

  • Returns a new generator that repeats the supplied value after this generator completes.

    Type parameters

    • N

    Parameters

    • value: N

      the value to repeat

    • Default value repetitions: number = Number.POSITIVE_INFINITY

      The number repetitions; the default is infinite.

    Returns EnhancedGenerator<T | N>

repeatLast

  • Returns a new generator that repeats the last value returned by this (or undefined if this did not return any values).

    Parameters

    • Default value max: number = Number.POSITIVE_INFINITY

    Returns EnhancedGenerator<T | undefined>

Abstract return

  • return(value: any): IteratorResult<T>

slice

  • Return a new EnhancedGenerator that only yields the indicated values, skipping start initial values and continuing until the end.

    Parameters

    • Default value start: number = 0
    • Default value end: number = Number.POSITIVE_INFINITY

    Returns EnhancedGenerator<T>

some

  • some(p: IndexedPredicate<T>, thisArg?: any): boolean
  • Returns true and terminates this generator if the predicate is true for any of the generator's yielded values.

    If the generator terminates without having satisfied the predicate, false is returned.

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    • Optional thisArg: any

      Optional value to supply as context (this) for the predicate

    Returns boolean

Abstract throw

  • throw(e: any): IteratorResult<T>

zip

  • zip<G>(...gens: G): any

Static asArray

Static concat

  • Concatenates generators (or iterators or iterables).

    Ensures that any supplied generators are terminated when this is terminated.

    Type parameters

    Parameters

    • Rest ...gens: T

      zero or more additional Genable to provide values.

    Returns EnhancedGenerator<GenUnion<T>>

Static enhance

Static every

  • every<T>(p: IndexedPredicate<T>, thisArg?: any): (gen: Genable<T>) => boolean
  • every<T>(p: IndexedPredicate<T>): (gen: Genable<T>, thisArg?: any) => boolean
  • every<T>(p: IndexedPredicate<T>, gen: Genable<T>): boolean
  • every<T>(p: IndexedPredicate<T>, thisArg: any, gen: Genable<T>): boolean
  • Returns false and terminates the generator if the predicate is false for any of the generator's yielded values.

    If the generator terminates without having failed the predicate, true is returned.

    Type parameters

    • T

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    • Optional thisArg: any

      Optional value to supply as context (this) for the predicate

    Returns (gen: Genable<T>) => boolean

      • Parameters

        Returns boolean

  • Returns false and terminates this generator if the predicate is false for any of the generator's yielded values.

    If the generator terminates without having failed the predicate, true is returned.

    Type parameters

    • T

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    Returns (gen: Genable<T>, thisArg?: any) => boolean

      • (gen: Genable<T>, thisArg?: any): boolean
      • Parameters

        • gen: Genable<T>
        • Optional thisArg: any

        Returns boolean

  • Returns false and terminates this generator if the predicate is false for any of the generator's yielded values.

    If the generator terminates without having failed the predicate, true is returned.

    Type parameters

    • T

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    • gen: Genable<T>

      the generator

    Returns boolean

  • Returns false and terminates this generator if the predicate is false for any of the generator's yielded values.

    If the generator terminates without having failed the predicate, true is returned.

    Type parameters

    • T

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    • thisArg: any

      Optional value to supply as context (this) for the predicate

    • gen: Genable<T>

      the generator

    Returns boolean

Static filter

Static flat

  • Flatten the values yielded by the generator to level depth. Produces a generator that yields the individual values at each level in depth-first order. Any iterable (including Array) or iterator will be traversed and its values yielded.

    The return type is currently over-broad

    Type parameters

    • T

    • D: number

    Parameters

    • depth: D

    Returns <X>(gen: Genable<X>) => EnhancedGenerator<FlatGen<X, D>>

  • Flatten the values yielded by the generator to level depth. Produces a generator that yields the individual values at each level in depth-first order. Any iterable (including Array) or iterator will be traversed and its values yielded.

    The return type is currently over-broad

    Type parameters

    • T

    • D: number

    Parameters

    Returns EnhancedGenerator<FlatGen<T, D>>

  • Flatten the values yielded by the generator to level depth. Produces a generator that yields the individual values at each level in depth-first order. Any iterable (including Array) or iterator will be traversed and its values yielded.

    The return type is currently over-broad

    Type parameters

    • T

    • D: number

    Parameters

    • gen: Genable<T>
    • Optional depth: D

      default = 1

    Returns EnhancedGenerator<FlatGen<T, D>>

Static flatMap

  • Flatten the values yielded by applying the function to the values yielded by the generator to level depth. Produces a function that accepts a generator, and returns another generator that yields the individual value at each level in depth-first order. Any iterable (including Array) or iterator will be traversed and its values yielded.

    The return type is currently over-broad

    Type parameters

    • T

    • D: number

    • R

    Parameters

    • f: IndexedFn<T, R>
    • depth: D

    Returns (gen: Genable<T>) => EnhancedGenerator<FlatGen<R, D>>

  • Flatten the values yielded by applying the function to the values yielded by the generator to level depth. Produces a function that accepts a generator, and returns another generator that yields the individual value at each level in depth-first order. Any iterable (including Array) or iterator will be traversed and its values yielded.

    The return type is currently over-broad

    Type parameters

    • T

    • D: number

    • R

    Parameters

    • f: IndexedFn<T, R>

    Returns (gen: Genable<T>, depth?: D) => EnhancedGenerator<FlatGen<R, D>>

  • Flatten the values yielded by applying the function to the values yielded by the generator to level depth. Produces a generator that yields the individual values at each level in depth-first order. Any iterable (including Array) or iterator will be traversed and its values yielded.

    The return type is currently over-broad

    Type parameters

    • T

    • D: number

    • R

    Parameters

    • f: IndexedFn<T, R>
    • gen: Genable<T>

    Returns EnhancedGenerator<FlatGen<R, D>>

  • Type parameters

    • T

    • D: number

    • R

    Parameters

    • f: IndexedFn<T, R>
    • depth: D
    • gen: Genable<T>

    Returns EnhancedGenerator<FlatGen<R, D>>

Static forEach

  • forEach<T>(f: IndexedFn<T>, thisArg: any, gen: Genable<T>): void
  • forEach<T>(f: IndexedFn<T>, gen: Genable<T>): void
  • forEach<T>(f: IndexedFn<T>, thisArg?: any): (gen: Genable<T>) => void
  • forEach<T>(f: IndexedFn<T>): (gen: Genable<T>, thisArg?: any) => void
  • Operate on each value produced by the generator. f is called with two values, the value yielded by this generator and a sequential index.

    Type parameters

    • T

      the type of value produced by the generator.

    Parameters

    • f: IndexedFn<T>
    • thisArg: any

      Optional value to be supplied as context this for function f.

    • gen: Genable<T>

      the generator.

    Returns void

  • Operate on each value produced by the generator. f is called with two values, the value yielded by this generator and a sequential index.

    Type parameters

    • T

      the type of value produced by the generator.

    Parameters

    • f: IndexedFn<T>
    • gen: Genable<T>

      the generator.

    Returns void

  • Operate on each value produced by the generator. f is called with two values, the value yielded by this generator and a sequential index.

    Type parameters

    • T

      the type of value produced by the generator.

    Parameters

    • f: IndexedFn<T>
    • Optional thisArg: any

      Optional value to be supplied as context this for function f.

    Returns (gen: Genable<T>) => void

  • Operate on each value produced by the generator. f is called with two values, the value yielded by this generator and a sequential index.

    Type parameters

    • T

      the type of value produced by the generator.

    Parameters

    • f: IndexedFn<T>

    Returns (gen: Genable<T>, thisArg?: any) => void

      • (gen: Genable<T>, thisArg?: any): void
      • Parameters

        • gen: Genable<T>
        • Optional thisArg: any

        Returns void

Static join

  • join(sep: string): <T>(gen: Genable<T>) => string
  • join<T>(gen: Genable<T>, sep?: undefined | string): string
  • Returns a function that joins the elements produced by a Genable, analogous to Array.prototype.join.

    Parameters

    • sep: string

      (default = ',')

    Returns <T>(gen: Genable<T>) => string

      • Type parameters

        • T

        Parameters

        Returns string

  • Joins the elements produced by a Genable, analogous to Array.prototype.join.

    Type parameters

    • T

    Parameters

    • gen: Genable<T>
    • Optional sep: undefined | string

    Returns string

Static limit

Static map

  • Apply the function to each value yielded by the generator. It is called with two arguments, the value yielded, and a sequential index. The return value is a generator that yields the values produced by the function.

    Type parameters

    • T

      the type of value produced by the generator.

    • V

    Parameters

    • f: IndexedFn<T, V>

    Returns (iter: Genable<T>, thisArg?: any) => EnhancedGenerator<V>

  • Apply the function to each value yielded by the generator. It is called with two arguments, the value yielded, and a sequential index. The return value is a generator that yields the values produced by the function.

    Type parameters

    • T

      the type of value produced by the generator.

    • V

    Parameters

    • f: IndexedFn<T, V>
    • thisArg: any

      Optional value to be supplied as context this for function f.

    Returns (iter: Genable<T>) => EnhancedGenerator<V>

  • Apply the function to each value yielded by the generator. It is called with two arguments, the value yielded, and a sequential index. The return value is a generator that yields the values produced by the function.

    Type parameters

    • T

      the type of value produced by the generator.

    • V

    Parameters

    • f: IndexedFn<T, V>
    • iter: Genable<T>

      the generator

    Returns EnhancedGenerator<V>

  • Apply the function to each value yielded by the generator. It is called with two arguments, the value yielded, and a sequential index. The return value is a generator that yields the values produced by the function.

    Type parameters

    • T

      the type of value produced by the generator.

    • V

    Parameters

    • f: IndexedFn<T, V>
    • thisArg: any

      Optional value to be supplied as context this for function f.

    • iter: Genable<T>

      the generator

    Returns EnhancedGenerator<V>

Static reduce

  • Reduces gen like Array.prototype.reduce, but the 3rd argument to the reducing function ("array") is omitted because there is no array.

    Type parameters

    • A

    • T

    Parameters

    Returns A

  • Returns a reducer function that, when applied to a Generator gen, reduces gen like Array.prototype.reduce. The 3rd argument to the reducing function ("array") is omitted because there is no array.

    Type parameters

    • A

    • T

    Parameters

    Returns (gen: Genable<T>) => A

  • Returns a reducer function that, when applied to a Generator gen, reduces gen like Array.prototype.reduce. The 3rd argument to the reducing function ("array") is omitted because there is no array.

    Type parameters

    • A

    • T

    Parameters

    Returns (init: A, gen: Genable<T>) => A

      • Parameters

        Returns A

  • Reduces gen like Array.prototype.reduce, but the 3rd argument to the reducing function ("array") is omitted because there is no array.

    Type parameters

    • A

    • T

    Parameters

    Returns A

  • Returns a reducer function that, when applied to a Generator gen, reduces gen like Array.prototype.reduce. The 3rd argument to the reducing function ("array") is omitted because there is no array.

    Alternatively, the init value can be supplied along with the generator as a second argument.

    Type parameters

    • A

    • T

    Parameters

    Returns (gen: Genable<T>) => A

Static repeat

  • Returns a new generator that repeats the supplied value after the supplied generator completes.

    Type parameters

    • T

    Parameters

    • value: T

      the value to repeat

    • Default value repetitions: number = Number.POSITIVE_INFINITY

      The number repetitions; the default is infinite.

    Returns EnhancedGenerator<T>

Static repeatLast

  • Returns a new generator that repeats the last value returned by gen (or undefined if gen did not return any values).

    Type parameters

    • T

    Parameters

    • gen: Genable<T>
    • Default value max: number = Number.POSITIVE_INFINITY

    Returns EnhancedGenerator<T | undefined>

Static slice

Static some

  • some<T>(p: IndexedPredicate<T>, thisArg?: any): (gen: Genable<T>) => boolean
  • some<T>(p: IndexedPredicate<T>): (gen: Genable<T>, thisArg?: any) => boolean
  • some<T>(p: IndexedPredicate<T>, gen: Genable<T>): boolean
  • some<T>(p: IndexedPredicate<T>, thisArg: any, gen: Genable<T>): boolean
  • Returns true and terminates the generator if the predicate is true for any of the generator's yielded values.

    If the generator terminates without having satisfied the predicate, false is returned.

    Type parameters

    • T

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    • Optional thisArg: any

      Optional value to supply as context (this) for the predicate

    Returns (gen: Genable<T>) => boolean

      • Parameters

        Returns boolean

  • Returns true and terminates the generator if the predicate is true for any of the generator's yielded values.

    If the generator terminates without having satisfied the predicate, false is returned.

    Type parameters

    • T

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    Returns (gen: Genable<T>, thisArg?: any) => boolean

      • (gen: Genable<T>, thisArg?: any): boolean
      • Parameters

        • gen: Genable<T>
        • Optional thisArg: any

        Returns boolean

  • Returns true and terminates the generator if the predicate is true for any of the generator's yielded values.

    If the generator terminates without having satisfied the predicate, false is returned.

    Type parameters

    • T

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    • gen: Genable<T>

      the generator

    Returns boolean

  • Returns true and terminates the generator if the predicate is true for any of the generator's yielded values.

    If the generator terminates without having satisfied the predicate, false is returned.

    Type parameters

    • T

    Parameters

    • p: IndexedPredicate<T>

      predicate to apply to each yielded value.

    • thisArg: any

      Optional value to supply as context (this) for the predicate

    • gen: Genable<T>

      the generator

    Returns boolean

Static zip

  • zip<G>(...gens: G): any
  • Combines generators, returning a generator that produces a tuple with each of their results.

    Terminates when the first generator terminates. To get other behaviors, use with EnhancedGenerator.repeat or EnhancedGenerator.repeatLast.

    Type parameters

    • G: (Generator<any, any, unknown> | Iterator<any, any, undefined> | Iterable<any>)[]

    Parameters

    • Rest ...gens: G

    Returns any

Generated using TypeDoc