Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Peekable<T>

An iterator with a peek. method that can look one element in advance.

Do not instantiate this directly, instead use the peekable method in IterPlus or AsyncIterPlus.

Type parameters

  • T

    The item type of the iterator.

Hierarchy

Index

Constructors

  • new Peekable<T>(iter: Iterator<T, any, undefined>): Peekable<T>

Properties

internal: Iterator<T, any, undefined>

The internal iterator that this wraps around.

storedVal: { has: true; val: IteratorResult<T, any> } | { has: false; val: unknown }

Methods

  • [iterator](): Iterator<T, any, undefined>
  • allEqual(): boolean
  • Checks if every element in this iterator is equal.

    This function is short-circuiting, so it stops on the first inequality.

    Returns boolean

    If every element is equal, or true if the iterator has one or less elements.

  • allEqualBy(cmp: (first: T, second: T) => boolean): boolean
  • Checks if every element in this iterator is equal, using a comparison function.

    This function is short-circuiting, so it stops on the first inequality.

    Parameters

    • cmp: (first: T, second: T) => boolean

      A function that checks if elements are equal.

        • (first: T, second: T): boolean
        • Parameters

          • first: T
          • second: T

          Returns boolean

    Returns boolean

    If every element is equal, or true if the iterator has one or less elements.

  • allEqualWith<K>(key: (elem: T) => K): boolean
  • Checks if every element in this iterator is equal, using a key function.

    This function is short-circuiting, so it stops on the first inequality.

    Type parameters

    • K

      The type of the key.

    Parameters

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns boolean

    If every element is equal, or true if the iterator has one or less elements.

  • average(): T
  • Returns the average of all elements in the iterator.

    throws

    A RangeError on an empty iterator.

    Returns T

    The average.

  • chunks(chunkSize: number): IterPlus<T[]>
  • Returns an iterator yielding non-overlapping chunks of the iterator.

    If there aren't enough elements to fill a chunk, the last chunk will be smaller than the chunk size.

    If you want gaps between the chunks, consider using windows with the appropriate interval instead.

    Parameters

    • chunkSize: number

      The chunk size.

    Returns IterPlus<T[]>

    An iterator that yields the chunks.

  • chunksExact(chunkSize: number): IterPlus<T[]>
  • Returns an iterator yielding non-overlapping chunks of the iterator.

    If there aren't enough elements to fill a chunk, the extra elements will be omitted.

    If you want gaps between the chunks, consider using windows with the appropriate interval instead.

    Parameters

    • chunkSize: number

      The chunk size.

    Returns IterPlus<T[]>

    An iterator that yields the chunks.

  • collect(): T[]
  • Collects the items in this iterator into an array.

    Returns T[]

    An array with the items in the iterator.

  • collectWith<R>(collector: (iter: IterPlus<T>) => R): R
  • Calls a specified collector function with this iterator as its only argument.

    Type parameters

    • R

    Parameters

    Returns R

    The return value of the collector.

  • compare(other: Iterable<T>): number
  • Lexicographically compares this iterator with another.

    This function is short-circuiting, so it stops on the first inequality.

    Parameters

    • other: Iterable<T>

      Iterable to compare to.

    Returns number

    -1 if this is less than the other, 0 if it's equal, and 1 if it's greater than.

  • compareBy<O>(other: Iterable<O>, cmp: (first: T, second: O) => number): number
  • Lexicographically compares this iterator with another using a comparison function.

    This function is short-circuiting, so it stops on the first inequality.

    Type parameters

    • O

      The type of the other iterator.

    Parameters

    • other: Iterable<O>

      Iterable to compare to.

    • cmp: (first: T, second: O) => number

      A function that should return a negative for less than, zero for equal to, and positive for greater than.

        • (first: T, second: O): number
        • Parameters

          • first: T
          • second: O

          Returns number

    Returns number

    -1 if this is less than the other, 0 if it's equal, and 1 if it's greater than.

  • compareWith<K>(other: Iterable<T>, key: (elem: T) => K): number
  • Lexicographically compares this iterator with another using a key.

    This function is short-circuiting, so it stops on the first inequality.

    Type parameters

    • K

      The type of the key.

    Parameters

    • other: Iterable<T>

      Iterable to compare to.

    • key: (elem: T) => K

      Function to generate a key to compare with from an element.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns number

    -1 if this is less than the other, 0 if it's equal, and 1 if it's greater than.

  • concat(...iters: Iterable<T>[]): IterPlus<T>
  • Concatenates one or more iterables to this iterator, creating an iterator that yields their elements in sequentual order.

    Parameters

    • Rest ...iters: Iterable<T>[]

      The iterables to chain to this one.

    Returns IterPlus<T>

    The generated iterator.

  • construct<R>(ctor: new (item: IterPlus<T>) => R): R
  • Calls a specified constructor with this iterator as its only argument.

    Type parameters

    • R

    Parameters

    • ctor: new (item: IterPlus<T>) => R

      The constructor to use.

    Returns R

    The constructed value.

  • count(): number
  • Counts the number of items in this iterator.

    Returns number

    The number of items in the iterator.

  • countIf(pred: (elem: T) => boolean): number
  • Counts the number of items in this iterator that match a predicate.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns number

    The number of matched items in the iterator.

  • Removes elements of an iterator that are equal to the previous one.

    Returns IterPlus<T>

    An iterator with no consecutive duplicates.

  • dedupBy(cmp: (first: T, second: T) => boolean): IterPlus<T>
  • Removes elements of an iterator that are equal to the previous one with a comparison function.

    Parameters

    • cmp: (first: T, second: T) => boolean

      A function that checks if elements are equal.

        • (first: T, second: T): boolean
        • Parameters

          • first: T
          • second: T

          Returns boolean

    Returns IterPlus<T>

    An iterator with no consecutive duplicates.

  • dedupWith<K>(key: (elem: T) => K): IterPlus<T>
  • Removes elements of an iterator that are equal to the previous one with a key.

    Type parameters

    • K

      The type of the key.

    Parameters

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns IterPlus<T>

    An iterator with no consecutive duplicates.

  • Drops elements from the iterator from the end.

    This uses memory proportional to the number of elements dropped, as the iterator must look ahead and store elements to know that it has not reached the end.

    Parameters

    • n: number

      The number of elements to drop.

    Returns IterPlus<T>

    An iterator with the specified number of elements removed from the end.

  • equals(other: Iterable<T>): boolean
  • Checks if this iterator is equal to another.

    This function is short-circuiting, so it stops on the first inequality.

    Parameters

    • other: Iterable<T>

      Iterable to compare to.

    Returns boolean

    If the two iterators are equal.

  • equalsBy<O>(other: Iterable<O>, cmp: (first: T, second: O) => boolean): boolean
  • Checks if this iterator is equal to another using a comparison function.

    This function is short-circuiting, so it stops on the first inequality.

    Type parameters

    • O

      The type of the other iterable.

    Parameters

    • other: Iterable<O>

      Iterable to compare to.

    • cmp: (first: T, second: O) => boolean

      A function that checks if elements are equal.

        • (first: T, second: O): boolean
        • Parameters

          • first: T
          • second: O

          Returns boolean

    Returns boolean

    If the two iterators are equal.

  • equalsWith<K>(other: Iterable<T>, key: (elem: T) => K): boolean
  • Checks if this iterator is equal to another using a key.

    This function is short-circuiting, so it stops on the first inequality.

    Type parameters

    • K

      The type of the key.

    Parameters

    • other: Iterable<T>

      Iterable to compare to.

    • key: (elem: T) => K

      Function to generate a key to compare with from an element.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns boolean

    If the two iterators are equal.

  • every(pred: (elem: T) => boolean): boolean
  • Checks if every element in the iterator matches a predicate.

    This function is short-circuiting, so if any element returns false, the function immediately returns false.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns boolean

    If every element satisfies the predicate.

  • filter(pred: (elem: T) => boolean): IterPlus<T>
  • Generates an iterator that only yields elements that match a predicate.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns IterPlus<T>

    The generated iterator.

  • filterMap<K>(func: (elem: T) => null | K): IterPlus<K>
  • Generates a mapped iterator that yields non-null elements.

    Type parameters

    • K

      The resulting type.

    Parameters

    • func: (elem: T) => null | K

      The mapping function.

        • (elem: T): null | K
        • Parameters

          • elem: T

          Returns null | K

    Returns IterPlus<K>

    The generated iterator.

  • find(pred: (elem: T) => boolean): null | T
  • Finds an element that satisfies a predicate.

    This function is short-circuiting, so it stops on the first match.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns null | T

    The element, or null if none was found.

  • findIndex(pred: (elem: T) => boolean): number
  • Finds the index of an element that satisfies a predicate.

    If you want to find the value and the index, consider using enumerate then using find.

    This function is short-circuiting, so it stops on the first match.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns number

    The index, or -1 if none was found.

  • findMap<K>(func: (elem: T) => null | K): null | K
  • Runs a function on every element and returns the first non-null element.

    This function is short-circuiting, so it stops on the first match.

    Type parameters

    • K

      The resulting type.

    Parameters

    • func: (elem: T) => null | K

      The mapping function.

        • (elem: T): null | K
        • Parameters

          • elem: T

          Returns null | K

    Returns null | K

    The element, or null if none was found.

  • flatMap<K>(func: (elem: T) => Iterable<K>): IterPlus<K>
  • Maps then flattens an iterator.

    Type parameters

    • K

      The resulting type.

    Parameters

    • func: (elem: T) => Iterable<K>

      The mapping function.

        • (elem: T): Iterable<K>
        • Parameters

          • elem: T

          Returns Iterable<K>

    Returns IterPlus<K>

    The generated iterator.

  • Flattens an iterator of iterables, yielding an iterator that sequentially produces their elements.

    Type parameters

    • K

      The internal type.

    Returns IterPlus<K>

    The generated iterator.

  • forEach(func: (elem: T) => unknown): void
  • Runs a function on each element of an iterator.

    This is equivalent to running a for loop on the iterator. If you want to obtain the values, consider using .map(func).collect() instead.

    Parameters

    • func: (elem: T) => unknown

      The function to run.

        • (elem: T): unknown
        • Parameters

          • elem: T

          Returns unknown

    Returns void

  • Generates an iterator that is guaranteed to never yield a value after finishing.

    Returns IterPlus<T>

    The generated iterator.

  • Globs elements of an iterator together.

    Returns IterPlus<T[]>

    An iterator where every element is an array of consecutively equal elements.

  • globBy(cmp: (first: T, second: T) => boolean): IterPlus<T[]>
  • Globs elements of an iterator together, with a comparison function.

    Parameters

    • cmp: (first: T, second: T) => boolean

      A function that checks if elements are equal.

        • (first: T, second: T): boolean
        • Parameters

          • first: T
          • second: T

          Returns boolean

    Returns IterPlus<T[]>

    An iterator where every element is an array of consecutively equal elements.

  • globWith<K>(key: (elem: T) => K): IterPlus<T[]>
  • Globs elements of an iterator together, with a key function.

    Type parameters

    • K

      The type of the key.

    Parameters

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns IterPlus<T[]>

    An iterator where every element is an array of consecutively equal elements.

  • group<K>(key: (elem: T) => K): Record<K, T[]>
  • Groups elements of an iterator together with a key function.

    Type parameters

    • K: string | number | symbol

      The type of the key.

    Parameters

    • key: (elem: T) => K
        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns Record<K, T[]>

    An object mapping keys to arrays of matching items.

  • hasCached(): boolean
  • Checks if there's a value cached from a previous peek.

    Will return true even if the cached value is the end of the iterator.

    Returns boolean

    If there's a value cached.

  • hasPrefix(other: Iterable<T>): boolean
  • Checks if this iterator is equal to another, while the second iterator still yields elements.

    This function is short-circuiting, so it stops on the first inequality.

    Parameters

    • other: Iterable<T>

      Iterable to compare to.

    Returns boolean

    If the first iterator starts with the second iterator.

  • hasPrefixBy<O>(other: Iterable<O>, cmp: (first: T, second: O) => boolean): boolean
  • Checks if this iterator is equal to another, while the second iterator still yields elements, using a comparison function.

    This function is short-circuiting, so it stops on the first inequality.

    Type parameters

    • O

      The type of the other iterable.

    Parameters

    • other: Iterable<O>

      Iterable to compare to.

    • cmp: (first: T, second: O) => boolean

      A function that checks if elements are equal.

        • (first: T, second: O): boolean
        • Parameters

          • first: T
          • second: O

          Returns boolean

    Returns boolean

    If the first iterator starts with the second iterator.

  • hasPrefixWith<K>(other: Iterable<T>, key: (elem: T) => K): boolean
  • Checks if this iterator is equal to another, while the second iterator still yields elements, with a key function.

    This function is short-circuiting, so it stops on the first inequality.

    Type parameters

    • K

      The type of the key.

    Parameters

    • other: Iterable<T>

      Iterable to compare to.

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns boolean

    If the first iterator starts with the second iterator.

  • headEquals(other: Iterable<T>): boolean
  • Checks if this iterator is equal to another, while they both yield elements.

    This function is short-circuiting, so it stops on the first inequality.

    However, if the first iterator terminates, a value will still be yielded from the second so that headEquals is commutative.

    Parameters

    • other: Iterable<T>

      Iterable to compare to.

    Returns boolean

    If the two iterators are equal.

  • headEqualsBy<O>(other: Iterable<O>, cmp: (first: T, second: O) => boolean): boolean
  • Checks if this iterator is equal to another, while they both yield elements, using a comparison function.

    This function is short-circuiting, so it stops on the first inequality.

    However, if the first iterator terminates, a value will still be yielded from the second so that headEquals is commutative.

    Type parameters

    • O

      The type of the other iterable.

    Parameters

    • other: Iterable<O>

      Iterable to compare to.

    • cmp: (first: T, second: O) => boolean

      A function that checks if elements are equal.

        • (first: T, second: O): boolean
        • Parameters

          • first: T
          • second: O

          Returns boolean

    Returns boolean

    If the two iterators are equal.

  • headEqualsWith<K>(other: Iterable<T>, key: (elem: T) => K): boolean
  • Checks if this iterator is equal to another, while they both yield elements, using a key.

    This function is short-circuiting, so it stops on the first inequality.

    However, if the first iterator terminates, a value will still be yielded from the second so that headEquals is commutative.

    Type parameters

    • K

      The type of the key.

    Parameters

    • other: Iterable<T>

      Iterable to compare to.

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns boolean

    If the two iterators are equal.

  • inspect(func: (elem: T) => unknown): IterPlus<T>
  • Lazily runs functions on an iterator, returning a new iterator with unmodified elements.

    This function is primarily used as a debugging tool to inspect elements in the middle of an iterator function chain.

    Parameters

    • func: (elem: T) => unknown

      The function to call.

        • (elem: T): unknown
        • Parameters

          • elem: T

          Returns unknown

    Returns IterPlus<T>

    The generated iterator.

  • interleave(...iters: Iterable<T>[]): IterPlus<T>
  • Interleaves one or more iterables with this iterator.

    Parameters

    • Rest ...iters: Iterable<T>[]

      The iterables to interleave with this one.

    Returns IterPlus<T>

    The interleaved iterator, yielding elements in the iterators in order.

  • intersperseMultiple(elems: Iterable<T>): IterPlus<T>
  • isPartitioned(pred: (elem: T) => boolean): boolean
  • Determines if an iterator is partitioned by a predicate (Items that return true come before items that return false).

    This function is short-circuiting, so it stops on the first non-partitioned element.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns boolean

    If the iterator is partitioned.

  • isSorted(): boolean
  • Determines if an iterator is sorted increasingly.

    This function is short-circuiting, so it stops on the first non-sorted element.

    Returns boolean

    If the iterator is sorted.

  • isSortedBy(cmp: (first: T, second: T) => number): boolean
  • Determines if an iterator is sorted increasingly by a comparison function.

    This function is short-circuiting, so it stops on the first non-sorted element.

    Parameters

    • cmp: (first: T, second: T) => number

      A function that should return a negative for less than, zero for equal to, and positive for greater than.

        • (first: T, second: T): number
        • Parameters

          • first: T
          • second: T

          Returns number

    Returns boolean

    If the iterator is sorted.

  • isSortedWith<K>(key: (elem: T) => K): boolean
  • Determines if an iterator is sorted increasingly by a key.

    This function is short-circuiting, so it stops on the first non-sorted element.

    Type parameters

    • K

      The type of the key.

    Parameters

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns boolean

    If the iterator is sorted.

  • Joins an iterator of iterables with an element.

    Type parameters

    • K

      The internal type.

    Parameters

    • elem: K

      The element to join with.

    Returns IterPlus<K>

    The joined iterator.

  • joinMultiple<K>(elems: Iterable<K>): IterPlus<K>
  • Joins an iterator of iterables with multiple elements.

    Type parameters

    • K

      The internal type.

    Parameters

    • elems: Iterable<K>

      The elements to intersperse.

    Returns IterPlus<K>

    The joined iterator.

  • last(): null | T
  • Finds the last element in an iterator.

    Returns null | T

    The last element of the iterator, or null if the iterator is empty.

  • map<K>(func: (elem: T) => K): IterPlus<K>
  • Lazily maps an iterator, creating a new iterator where each element has been modified by a function.

    If you want to immediately run a function on all elements of the iterator, use forEach instead.

    Type parameters

    • K

      The resulting type.

    Parameters

    • func: (elem: T) => K

      The mapping function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns IterPlus<K>

    The generated iterator.

  • mapAccum<A, V>(func: (accum: A, elem: T) => [A, V], initializer: A): IterPlus<V>
  • Runs a function for every element of the iterator, keeping track of an accumulator.

    Type parameters

    • A

      The type of the accumulator.

    • V

      The resulting type.

    Parameters

    • func: (accum: A, elem: T) => [A, V]

      The mapping function.

        • (accum: A, elem: T): [A, V]
        • Parameters

          • accum: A
          • elem: T

          Returns [A, V]

    • initializer: A

      The initial accumulator.

    Returns IterPlus<V>

    The mapped iterator.

  • mapWhile<K>(func: (elem: T) => null | K): IterPlus<K>
  • Lazily maps an iterator until it encounters null.

    Type parameters

    • K

      The resulting type.

    Parameters

    • func: (elem: T) => null | K

      The mapping function.

        • (elem: T): null | K
        • Parameters

          • elem: T

          Returns null | K

    Returns IterPlus<K>

    The generated iterator.

  • max(overwrite?: boolean): null | T
  • Finds the maximum value of an iterator.

    Parameters

    • overwrite: boolean = false

      If true, elements will be counted as the new maximum if they are equal to the maximum. Defaults to false.

    Returns null | T

    The maximum element, or null if the iterator is empty.

  • maxBy(cmp: (first: T, second: T) => number, overwrite?: boolean): null | T
  • Finds the maximum value of an iterator with a comparison function.

    Parameters

    • cmp: (first: T, second: T) => number

      A function that should return a negative for less than, zero for equal to, and positive for greater than.

        • (first: T, second: T): number
        • Parameters

          • first: T
          • second: T

          Returns number

    • overwrite: boolean = false

      If true, elements will be counted as the new maximum if they are equal to the maximum. Defaults to false.

    Returns null | T

    The maximum element, or null if the iterator is empty.

  • maxWith<K>(key: (elem: T) => K, overwrite?: boolean): null | T
  • Finds the maximum value of an iterator with a key.

    Type parameters

    • K

      The type of the key.

    Parameters

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    • overwrite: boolean = false

      If true, elements will be counted as the new maximum if they are equal to the maximum. Defaults to false.

    Returns null | T

    The maximum element, or null if the iterator is empty.

  • min(overwrite?: boolean): null | T
  • Finds the minimum value of an iterator.

    Parameters

    • overwrite: boolean = false

      If true, elements will be counted as the new minimum if they are equal to the minimum. Defaults to false.

    Returns null | T

    The minimum element, or null if the iterator is empty.

  • minBy(cmp: (first: T, second: T) => number, overwrite?: boolean): null | T
  • Finds the minimum value of an iterator with a comparison function.

    Parameters

    • cmp: (first: T, second: T) => number

      A function that should return a negative for less than, zero for equal to, and positive for greater than.

        • (first: T, second: T): number
        • Parameters

          • first: T
          • second: T

          Returns number

    • overwrite: boolean = false

      If true, elements will be counted as the new minimum if they are equal to the minimum. Defaults to false.

    Returns null | T

    The minimum element, or null if the iterator is empty.

  • minWith<K>(key: (elem: T) => K, overwrite?: boolean): null | T
  • Finds the minimum value of an iterator with a key.

    Type parameters

    • K

      The type of the key.

    Parameters

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    • overwrite: boolean = false

      If true, elements will be counted as the new minimum if they are equal to the minimum. Defaults to false.

    Returns null | T

    The minimum element, or null if the iterator is empty.

  • next(): IteratorResult<T, any>
  • Yields the next element in the iterator.

    Returns IteratorResult<T, any>

    The next element.

  • nextVal(): null | T
  • Returns the next value, or null if the iterator ended.

    Returns null | T

    The next value, or null if the iterator ended.

  • nth(n: number): null | T
  • Finds the nth element in an iterator.

    Parameters

    • n: number

      The number element to get.

    Returns null | T

    The nth element of the iterator, or null if the iterator is too short.

  • nubBy(cmp: (first: T, second: T) => boolean): IterPlus<T>
  • Removes duplicates from an iterator, including non-consecutive ones, with a comparison function.

    Unlike nubWith and nub, this does not use a set, so it is significantly slower.

    Parameters

    • cmp: (first: T, second: T) => boolean

      A function that checks if elements are equal.

        • (first: T, second: T): boolean
        • Parameters

          • first: T
          • second: T

          Returns boolean

    Returns IterPlus<T>

    The nubbed iterator.

  • nubWith<K>(key: (elem: T) => K): IterPlus<T>
  • Removes duplicates from an iterator, including non-consecutive ones, with a key function.

    Type parameters

    • K

      The type of the key.

    Parameters

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns IterPlus<T>

    The nubbed iterator.

  • partition(pred: (elem: T) => boolean): [T[], T[]]
  • Partitions an iterator into two groups.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns [T[], T[]]

    An array with two elements:

    • The elements where the predicate returned true.
    • The elements where the predicate returned false.
  • peek(): IteratorResult<T, any>
  • Peeks the next element in the iterator and does not consume it.

    Returns IteratorResult<T, any>

    The next element as an iterator result.

  • peekVal(): null | T
  • Peeks the next element in the iterator and does not consume it.

    Nullable version of peek.

    Returns null | T

    The next element, or null if the iterator is finished.

  • product(empty?: number): number
  • product(empty: bigint): bigint
  • Returns the product of all elements in the iterator.

    Parameters

    • Optional empty: number

      The default value for an empty iterator. Defaults to 1.

    Returns number

    The product.

  • Returns the product of all elements in the iterator.

    Parameters

    • empty: bigint

      The default value for an empty iterator. For bigint iterators it's advised to explicitly set this to 1n or another bigint.

    Returns bigint

    The product.

  • reduce<A>(func: (accum: A, elem: T) => A, initializer: A): A
  • reduce(func: (accum: T, elem: T) => T): T
  • Runs a function for every element of the iterator, keeping track of an accumulator.

    Type parameters

    • A

      The type of the accumulator.

    Parameters

    • func: (accum: A, elem: T) => A

      The reducing function.

        • (accum: A, elem: T): A
        • Parameters

          • accum: A
          • elem: T

          Returns A

    • initializer: A

      The initial accumulator.

    Returns A

    The final accumulator.

  • Runs a function for every element of the iterator, keeping track of an accumulator.

    Uses the first element as the initial accumulator, and it will be skipped over in the reduction.

    throws

    If the iterator is empty, then an error will be thrown.

    Parameters

    • func: (accum: T, elem: T) => T

      The reducing function.

        • (accum: T, elem: T): T
        • Parameters

          • accum: T
          • elem: T

          Returns T

    Returns T

    The final accumulator.

  • Creates an iterator that repeats the contents of the current iterator a certain number of times.

    Parameters

    • n: number

      The number of times to repeat.

    Returns IterPlus<T>

    An iterator that repeats itself n times.

  • Consumes the iterator and reverses it.

    This has to immediately resolve every element in the iterator, so it is equivalent to collecting to an array and revsersing the array, so it is very inefficient on memory and should be avoided.

    Returns IterPlus<T>

    The reversed iterator.

  • Creates an iterator that's rotated left a certain amount, so elements at the start end up at the end.

    This does not handle negative numbers due to right rotation being significantly slower. If you want negatives, please do the checks yourself and use rotateRight when appropriate.

    throws

    A RangeError when the amount is negative.

    Parameters

    • amount: number

      Amount to rotate by.

    Returns IterPlus<T>

    The rotated iterator.

  • rotateRight(amount: number): IterPlus<T>
  • Creates an iterator that's rotated right a certain amount, so elements at the end end up at the start.

    Due to the one-directional nature of iterators, this is not lazy and therefore much slower than rotateLeft.

    This does not handle negative numbers to be consistent with rotateLeft. If you want negatives, please do the checks yourself and use rotateRight when appropriate.

    throws

    A RangeError when the amount is negative.

    Parameters

    • amount: number

      Amount to rotate by.

    Returns IterPlus<T>

    The rotated iterator.

  • scan<A>(func: (accum: A, elem: T) => A, initializer: A): IterPlus<A>
  • scan(func: (accum: T, elem: T) => T): IterPlus<T>
  • Runs a function for every element of the iterator, keeping track of an accumulator.

    Type parameters

    • A

      The type of the accumulator.

    Parameters

    • func: (accum: A, elem: T) => A

      The reducing function.

        • (accum: A, elem: T): A
        • Parameters

          • accum: A
          • elem: T

          Returns A

    • initializer: A

      The initial accumulator.

    Returns IterPlus<A>

    The iterator containing all intermediate accumulators.

  • Runs a function for every element of the iterator, keeping track of an accumulator.

    Uses the first element as the initial accumulator, and it will be skipped over in the scan.

    throws

    If the iterator is empty, then an error will be thrown.

    Parameters

    • func: (accum: T, elem: T) => T

      The reducing function.

        • (accum: T, elem: T): T
        • Parameters

          • accum: T
          • elem: T

          Returns T

    Returns IterPlus<T>

    The iterator containing all intermediate accumulators.

  • Skips the first n elements of an iterator.

    Parameters

    • n: number

      The number of elements to skip.

    Returns IterPlus<T>

    The generated iterator.

  • skipWhile(pred: (elem: T) => boolean): IterPlus<T>
  • Skips elements of an iterator while a predicate is met.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns IterPlus<T>

    The generated iterator.

  • some(pred: (elem: T) => boolean): boolean
  • Checks if some element in the iterator matches a predicate.

    This function is short-circuiting, so if any element returns true, the function immediately returns true.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns boolean

    If some element satisfies the predicate.

  • split(elem: T, limit?: number): IterPlus<T[]>
  • Splits an iterator on an element.

    Parameters

    • elem: T
    • limit: number = Infinity

      The maximum number of chunks to make.

    Returns IterPlus<T[]>

    The iterator with the split chunks.

  • splitInclusive(elem: T, limit?: number): IterPlus<T[]>
  • Splits an iterator on an element, including the matched element as the last element of the chunk.

    Unlike the exclusive split, this does not create an empty chunk on the end when ending with the matched element.

    Parameters

    • elem: T
    • limit: number = Infinity

      The maximum number of chunks to make.

    Returns IterPlus<T[]>

    The iterator with the split chunks.

  • splitPred(pred: (elem: T) => boolean, limit?: number): IterPlus<T[]>
  • Splits an iterator on a predicate.

    Parameters

    • pred: (elem: T) => boolean

      The predicate to split with.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    • limit: number = Infinity

      The maximum number of chunks to make.

    Returns IterPlus<T[]>

    The iterator with the split chunks.

  • splitPredInclusive(pred: (elem: T) => boolean, limit?: number): IterPlus<T[]>
  • Splits an iterator on a predicate, including the matched element as the last element of the chunk.

    Unlike the exclusive split, this does not create an empty chunk on the end when ending with the matched element.

    Parameters

    • pred: (elem: T) => boolean

      The predicate to split with.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    • limit: number = Infinity

      The maximum number of chunks to make.

    Returns IterPlus<T[]>

    The iterator with the split chunks.

  • starmap<K, R>(func: (...args: K[]) => R): IterPlus<R>
  • Maps an iterator of iterables, and calls a function with the contents of the iterable as the argument.

    Type parameters

    • K

      The iterable type.

    • R

      The resulting type.

    Parameters

    • func: (...args: K[]) => R

      The mapping function.

        • (...args: K[]): R
        • Parameters

          • Rest ...args: K[]

          Returns R

    Returns IterPlus<R>

    The generated iterator.

  • Steps through an iterator by a certain amount, starting from the first.

    A step of 2 would yield the first element, then the third, then the fifth, and so on.

    Parameters

    • step: number

      The step size.

    Returns IterPlus<T>

    An iterator that advances by the given step size.

  • sum(empty?: number): number
  • sum(empty: bigint): bigint
  • sum(empty: string): string
  • Returns the sum of all elements in the iterator.

    Parameters

    • Optional empty: number

      The default value for an empty iterator. Defaults to 0.

    Returns number

    The sum.

  • Returns the sum of all elements in the iterator.

    Parameters

    • empty: bigint

      The default value for an empty iterator. For bigint iterators it's advised to explicitly set this to 0n or another bigint.

    Returns bigint

    The sum.

  • Returns the sum of all elements in the iterator.

    Parameters

    • empty: string

      The default value for an empty iterator. For string iterators it's advised to explicitly set this to "" or another string.

    Returns string

    The sum.

  • Takes the first n elements of an iterator.

    Parameters

    • n: number

      The number of elements to take.

    Returns IterPlus<T>

    The generated iterator.

  • takeWhile(pred: (elem: T) => boolean): IterPlus<T>
  • Takes elements of an iterator while a predicate is met.

    Parameters

    • pred: (elem: T) => boolean

      The predicate function.

        • (elem: T): boolean
        • Parameters

          • elem: T

          Returns boolean

    Returns IterPlus<T>

    The generated iterator.

  • tally(): Record<string, number>
  • Tallies elements of an iterator together.

    Returns Record<string, number>

    An object mapping keys to the number of times they appeared.

  • tallyWith<K>(key: (elem: T) => K): Record<K, number>
  • Tallies elements of an iterator together with a key function.

    Type parameters

    • K: string | number | symbol

      The type of the key.

    Parameters

    • key: (elem: T) => K

      The key function.

        • (elem: T): K
        • Parameters

          • elem: T

          Returns K

    Returns Record<K, number>

    An object mapping keys to the number of times they appeared.

  • Splits an iterator into multiple, where advancing one iterator does not advance the others.

    Functions by storing old values and removing when no longer needed, so only tee as many iterators as you need in order for memory to be cleaned properly.

    The original iterator will still be advanced, so only used the iterators returned by tee.

    Parameters

    • count: number = 2

      The number of iterators to split into.

    Returns IterPlus<T>[]

    An array of length count with separate iterators.

  • toArray(): T[]
  • toMap<K, V>(duplicate: "overwrite" | "maintain" | "error"): Map<K, V>
  • Converts an iterator of key-value pairs into a map.

    throws

    A RangeError if duplicate is "error" and a duplicate key is encountered.

    Type parameters

    • K

      The key type.

    • V

      The value type.

    Parameters

    • duplicate: "overwrite" | "maintain" | "error"

      How to handle duplicate keys. "overwrite" replaces values with the new value. "maintain" maintains the old value. "error" throws an error. Defaults to "overwrite".

    Returns Map<K, V>

    The generated map.

  • toObject<K, V>(duplicate: "overwrite" | "maintain" | "error"): {}
  • Converts an iterator of key-value pairs into an object.

    throws

    A RangeError if duplicate is "error" and a duplicate key is encountered.

    Type parameters

    • K

      The key type.

    • V

      The value type.

    Parameters

    • duplicate: "overwrite" | "maintain" | "error"

      How to handle duplicate keys. "overwrite" replaces values with the new value. "maintain" maintains the old value. "error" throws an error. Defaults to "overwrite".

    Returns {}

    The generated object.

    • [key: string]: V
  • toSet(): Set<T>
  • unzip<K>(): ArrayMap<K>
  • "Unzips" an iterator of tuples into a tuple of arrays.

    Type parameters

    • K: unknown[]

      The tuple type.

    Returns ArrayMap<K>

    A tuple with the individual elements.

  • windows(windowSize: number, interval?: number): IterPlus<T[]>
  • Returns an iterator yielding overlapping windows of the iterator.

    If there aren't enough elements to fill a window, no windows will be yielded.

    Parameters

    • windowSize: number

      The window size.

    • interval: number = 1

      The increment between the starts of windows. Defaults to 1.

    Returns IterPlus<T[]>

    An iterator that yields the windows.

  • zip<K>(...iters: IterableMap<K>): IterPlus<[T, ...K[]]>
  • Zips one or more iterables with this iterator.

    Type parameters

    • K: unknown[]

      The types of the other iterables.

    Parameters

    • Rest ...iters: IterableMap<K>

      The iterables to zip with this one.

    Returns IterPlus<[T, ...K[]]>

    The generated iterator.

  • zipWith<K, R>(func: (...args: [T, ...K[]]) => R, ...iters: IterableMap<K>): IterPlus<R>
  • Zips one or more iterables with this iterator using a function.

    Stops once any one of the iterators stop.

    Type parameters

    • K: unknown[]

      The types of the other iterables.

    • R

      The resulting value.

    Parameters

    • func: (...args: [T, ...K[]]) => R

      The function to use when zipping.

        • (...args: [T, ...K[]]): R
        • Parameters

          • Rest ...args: [T, ...K[]]

          Returns R

    • Rest ...iters: IterableMap<K>

      The iterables to zip with this one.

    Returns IterPlus<R>

    The generated iterator.

  • combinations<T>(data: T[], count?: number): IterPlus<T[]>
  • Generates an iterator that iterates through lexicographically sorted combinations without repetition of a dataset.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • data: T[]

      The data to generate combinations from.

    • count: number = data.length

      The number of elements in each combination.

    Returns IterPlus<T[]>

    The generated iterator.

  • combinationsWithRepetition<T>(data: T[], count?: number): IterPlus<T[]>
  • Generates an iterator that iterates through lexicographically sorted combinations with repetition of a dataset.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • data: T[]

      The data to generate combinations from.

    • count: number = data.length

      The number of elements in each combination.

    Returns IterPlus<T[]>

    The generated iterator.

  • cycle<T>(data: Iterable<T>): IterPlus<T>
  • Generates an iterator that cycles through an iterable.

    While this does work on infinite iterators, it should be avoided as it stores all elements, leading to an ever-growing memory usage.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • data: Iterable<T>

      The iterable to cycle through.

    Returns IterPlus<T>

    The generated iterator.

  • fromFunction<T>(func: () => null | T): IterPlus<T>
  • Generates an iterator that yields values from a function and ends once the function returns null.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • func: () => null | T

      The function to yield values, or null to end the iterator.

        • (): null | T
        • Returns null | T

    Returns IterPlus<T>

    The generated iterator.

  • Generates an iterator that yields a single value.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • val: T

      The value to yield.

    Returns IterPlus<T>

    The generated iterator.

  • Generates an iterator that lazily yields a single value.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • func: () => T

      The function to generate a single value.

        • (): T
        • Returns T

    Returns IterPlus<T>

    The generated iterator.

  • permutations<T>(data: T[], count?: number): IterPlus<T[]>
  • Generates an iterator that iterates through lexicographically sorted permutations without repetition of a dataset.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • data: T[]

      The data to generate permutations from.

    • count: number = data.length

      The number of elements in each permutations.

    Returns IterPlus<T[]>

    The generated iterator.

  • permutationsWithRepetition<T>(data: T[], count?: number): IterPlus<T[]>
  • Generates an iterator that iterates through lexicographically sorted permutations with repetition of a dataset.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • data: T[]

      The data to generate permutations from.

    • count: number = data.length

      The number of elements in each permutations.

    Returns IterPlus<T[]>

    The generated iterator.

  • Generates an iterator that iterates through the lexicographically sorted powerset of a dataset.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • data: T[]

      The data to get the powerset of.

    Returns IterPlus<T[]>

    The generated iterator.

  • product<T>(...data: ArrayMap<T>): IterPlus<T>
  • Generates an iterator that generates a lexicographically sorted cartesian product.

    Type parameters

    • T: unknown[]

      The item type of the iterator.

    Parameters

    • Rest ...data: ArrayMap<T>

      The iterators to take the product of.

    Returns IterPlus<T>

    The generated iterator.

  • Generates an iterator that endlessly repeats a value.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • val: T

      The value to yield.

    Returns IterPlus<T>

    The generated iterator.

  • repeatWith<T>(func: () => T): IterPlus<T>
  • Generates an iterator that endlessly calls a function.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • func: () => T

      The function to generate values.

        • (): T
        • Returns T

    Returns IterPlus<T>

    The generated iterator.

  • successors<T>(first: null | T, func: (prev: T) => null | T): IterPlus<T>
  • Generates an iterator that generates values based on the previous value.

    Type parameters

    • T

      The item type of the iterator.

    Parameters

    • first: null | T

      The initial value.

    • func: (prev: T) => null | T

      The function to generate new values.

        • (prev: T): null | T
        • Parameters

          • prev: T

          Returns null | T

    Returns IterPlus<T>

    The generated iterator.

  • unfold<T, A>(func: (accum: A) => null | [T, A], init: A): IterPlus<T>
  • Type parameters

    • T

    • A

    Parameters

    • func: (accum: A) => null | [T, A]
        • (accum: A): null | [T, A]
        • Parameters

          • accum: A

          Returns null | [T, A]

    • init: A

    Returns IterPlus<T>

Generated using TypeDoc