Options
All
  • Public
  • Public/Protected
  • All
Menu

Type parameters

  • T

Hierarchy

  • ChainableAsyncIterable

Implements

  • AsyncIterable<T>

Index

Constructors

constructor

Properties

Private source

source: AsyncIterable<T>

Methods

[Symbol.asyncIterator]

  • [Symbol.asyncIterator](): AsyncIterator<T, any, undefined>

append

  • Wraps the two given iterables as a single concatenated iterable.

    example

    init({ from: 1, to: 3 }) .append(init({ from: 8, to: 10 })) // Yields: 1, 2, 3, 8, 9, 10

    Parameters

    • second: AsyncIterable<T>

      The second iterable.

    Returns ChainableAsyncIterable<T>

choose

  • choose<U>(chooser: (item: T, index: number) => U | undefined | Promise<U | undefined>): ChainableAsyncIterable<U>
  • Applies the given function to each element of the sequence and returns a new sequence comprised of the results for each element where the function returns a value. This can be thought of as doing both a filter and a map at the same time.

    example

    init({ start: 1, count: 3 }) .choose((x) => (x % 2 === 1 ? x * 2 : undefined)) // Yields: 2, 6

    Type parameters

    • U

    Parameters

    • chooser: (item: T, index: number) => U | undefined | Promise<U | undefined>

      A function to transform items from the input collection to a new value to be included, or undefined to be excluded.

        • (item: T, index: number): U | undefined | Promise<U | undefined>
        • Parameters

          • item: T
          • index: number

          Returns U | undefined | Promise<U | undefined>

    Returns ChainableAsyncIterable<U>

collect

  • Applies the given function to each element of the source iterable and concatenates all the results.

    example

    init({ start: 1, count: 3 }).collect(function* (x) { yield x yield x }) // Yields: 1, 1, 2, 2, 3, 3 // You can also just return an array from your mapping function init({ start: 1, count: 3 }) .collect((x) => [x, x])

    Type parameters

    • U

    Parameters

    • mapping: (item: T, index: number) => Iterable<U> | AsyncIterable<U>

      A function to transform elements of the input collection into collections that are concatenated.

        • (item: T, index: number): Iterable<U> | AsyncIterable<U>
        • Parameters

          • item: T
          • index: number

          Returns Iterable<U> | AsyncIterable<U>

    Returns ChainableAsyncIterable<U>

count

  • count(): Promise<number>
  • Returns the number of items in the collection.

    example

    await init(5).count() // Returns: 5

    Returns Promise<number>

distinct

  • Returns a iterable that contains no duplicate entries according to the equality comparisons on the elements. If an element occurs multiple times in the sequence then the later occurrences are discarded.

    example

    async function* source() { yield 'bob' yield 'cat' yield 'bob' yield 'amy' } chain(source()).distinct() // Yields: 'bob', 'cat', 'amy'

    Returns ChainableAsyncIterable<T>

distinctBy

  • Returns a iterable that contains no duplicate entries according to the equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the sequence then the later occurrences are discarded.

    example

    async function* source() { yield { name: 'amy', id: 1 } yield { name: 'bob', id: 2 } yield { name: 'bob', id: 3 } yield { name: 'cat', id: 3 } } chain(source()).distinctBy((x) => x.name) // Yields: // { name: 'amy', id: 1 } // { name: 'bob', id: 2 } // { name: 'cat', id: 3 }

    Type parameters

    • Key

    Parameters

    • selector: (item: T, index: number) => Key | Promise<Key>

      A function that transforms the collection items into comparable keys.

        • (item: T, index: number): Key | Promise<Key>
        • Parameters

          • item: T
          • index: number

          Returns Key | Promise<Key>

    Returns ChainableAsyncIterable<T>

every

  • every(predicate: (item: T, index: number) => boolean | Promise<boolean>): Promise<boolean>
  • Tests if every element of the collection satisfies the given predicate.

    example

    await init({ from: 1, to: 3 }).every((x) => x > 0) // Returns: true await init({ from: 1, to: 3 }).every((x) => x < 2) // Returns: false

    Parameters

    • predicate: (item: T, index: number) => boolean | Promise<boolean>

      A function to test against each item of the input collection.

        • (item: T, index: number): boolean | Promise<boolean>
        • Parameters

          • item: T
          • index: number

          Returns boolean | Promise<boolean>

    Returns Promise<boolean>

exists

  • exists(predicate: (item: T, index: number) => boolean | Promise<boolean>): Promise<boolean>
  • Tests if any element of the collection satisfies the given predicate.

    example

    await init({ from: 1, to: 3 }).exists((x) => x === 2) // Returns: true await init({ from: 1, to: 3 }).exists((x) => x === 4) // Returns: false

    Parameters

    • predicate: (item: T, index: number) => boolean | Promise<boolean>

      A function to test each item of the input collection.

        • (item: T, index: number): boolean | Promise<boolean>
        • Parameters

          • item: T
          • index: number

          Returns boolean | Promise<boolean>

    Returns Promise<boolean>

filter

  • Returns a new iterable containing only the elements of the collection for which the given predicate returns true.

    example

    init({ start: 1, count: 4 }) .filter((x) => x % 2 === 0) // Yields: 2, 4

    Parameters

    • predicate: (item: T, index: number) => boolean | Promise<boolean>

      A function to test whether each item in the input collection should be included in the output.

        • (item: T, index: number): boolean | Promise<boolean>
        • Parameters

          • item: T
          • index: number

          Returns boolean | Promise<boolean>

    Returns ChainableAsyncIterable<T>

find

  • find(predicate: (item: T, index: number) => boolean | Promise<boolean>): Promise<T | undefined>
  • Returns the first element for which the given function returns true, otherwise undefined.

    example

    async function* source() { yield { name: 'amy', id: 1 } yield { name: 'bob', id: 2 } } await chain(source()).find((p) => p.name === 'bob') // Returns: { name: 'bob', id: 2 } await chain(source()).find((p) => p.name === 'cat') // Returns: undefined

    Parameters

    • predicate: (item: T, index: number) => boolean | Promise<boolean>

      A function to test whether an item in the collection should be returned.

        • (item: T, index: number): boolean | Promise<boolean>
        • Parameters

          • item: T
          • index: number

          Returns boolean | Promise<boolean>

    Returns Promise<T | undefined>

get

  • get(predicate: (item: T, index: number) => boolean | Promise<boolean>): Promise<T>
  • Returns the first element for which the given function returns true.

    throws

    If no item is found matching the criteria of the predicate.

    example

    async function* source() { yield { name: 'amy', id: 1 } yield { name: 'bob', id: 2 } } await chain(source()).get((p) => p.name === 'bob') // Returns: { name: 'bob', id: 2 } await chain(source()).get((p) => p.name === 'cat') // Throws: Element not found matching criteria

    Parameters

    • predicate: (item: T, index: number) => boolean | Promise<boolean>

      A function to test whether an item in the collection should be returned.

        • (item: T, index: number): boolean | Promise<boolean>
        • Parameters

          • item: T
          • index: number

          Returns boolean | Promise<boolean>

    Returns Promise<T>

groupBy

  • groupBy<Key>(selector: (item: T, index: number) => Key | Promise<Key>): Promise<Map<Key, Iterable<T>>>
  • Applies a key-generating function to each element of a collection and yields a iterable of unique keys and an array of all elements that have each key.

    example

    async function* source() { yield { name: 'amy', age: 1 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 2 } } await chain(source()).groupBy((x) => x.age) // Returns the Map: // 1, [{ name: 'amy', age: 1 }] // 2, [{ name: 'bob', age: 2 }, { name: 'cat', age: 2 }]

    Type parameters

    • Key

    Parameters

    • selector: (item: T, index: number) => Key | Promise<Key>

      A function that transforms an element of the collection into a comparable key.

        • (item: T, index: number): Key | Promise<Key>
        • Parameters

          • item: T
          • index: number

          Returns Key | Promise<Key>

    Returns Promise<Map<Key, Iterable<T>>>

length

  • length(): Promise<number>
  • Returns the number of items in the collection.

    example

    await init(5).length() // Returns: 5

    Returns Promise<number>

map

  • Creates a new iterable whose elements are the results of applying the specified mapping to each of the elements of the source collection.

    example

    init({ start: 1, count: 3 }) .map((x) => x * 2) // Yields: 2, 4, 6

    Type parameters

    • U

    Parameters

    • mapping: (item: T, index: number) => U | Promise<U>

      A function to transform items from the input collection.

        • (item: T, index: number): U | Promise<U>
        • Parameters

          • item: T
          • index: number

          Returns U | Promise<U>

    Returns ChainableAsyncIterable<U>

maxBy

  • maxBy(selector: (item: T) => number): Promise<number>
  • Returns the maximum of the values returned by the selector for each element in the collection.

    throws

    If the collection is empty.

    example

    async function* source() { yield { name: 'amy', age: 21 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 18 } } await chain(source()).maxBy((x) => x.age) // Returns: 21

    Parameters

    • selector: (item: T) => number

      A function to transform each element into a comparable value.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns Promise<number>

meanBy

  • meanBy(selector: (item: T) => number): Promise<number>
  • Returns the mean (average) of the values returned by the selector for each element in the collection.

    throws

    If the collection is empty.

    example

    async function* source() { yield { name: 'amy', age: 21 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 18 } yield { name: 'dot', age: 39 } } await chain(source()).meanBy((x) => x.age) // Returns: 20

    Parameters

    • selector: (item: T) => number

      A function to transform each element into a summable value.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns Promise<number>

minBy

  • minBy(selector: (item: T) => number): Promise<number>
  • Returns the minimum of the values returned by the selector for each element in the collection.

    throws

    If the collection is empty.

    example

    async function* source() { yield { name: 'amy', age: 21 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 18 } } await chain(source()).minBy((x) => x.age) // Returns: 2

    Parameters

    • selector: (item: T) => number

      A function to transform each element into a comparable value.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns Promise<number>

pairwise

  • Returns an interable of each element in the input sequence and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.

    example

    init({ from: 1, to: 4 }).pairwise() // Yields: [1, 2], [2, 3], [3, 4]

    Returns ChainableAsyncIterable<[T, T]>

reverse

  • reverse(): Promise<T[]>
  • Yields each element of the iterable in reverse order.

    example

    async function* source() { yield 'cat' yield 'amy' yield 'bob' } await chain(source()).reverse() // Returns: ['bob', 'amy', 'cat']

    Returns Promise<T[]>

skip

  • Returns the elements of the iterable after a specified count.

    example

    init({ from: 1, to: 5}).skip(2) // Yields: 3, 4, 5

    Parameters

    • count: number

      The number of items to skip.

    Returns ChainableAsyncIterable<T>

sort

  • sort<Key>(selector?: undefined | ((item: T) => Key)): Promise<T[]>
  • Yields an iterable ordered by the selected key. If no selector is specified, the elements will be compared directly.

    example

    init({ from 3, to: 1 }).sort() // Yields 1, 2, 3

    async function* source() { yield 'Cat' yield 'amy' yield 'BOB' } await chain(source()).sort((n) => n.toLowerCase()) // Returns: ['amy', 'BOB', 'Cat']

    Type parameters

    • Key

    Parameters

    • Optional selector: undefined | ((item: T) => Key)

      An optional function to transform items of the input sequence into comparable keys.

    Returns Promise<T[]>

sortBy

  • sortBy<Key>(selector: (item: T) => Key): Promise<T[]>
  • Applies a key-generating function to each element of the collection and yields an iterable ordered by keys.

    example

    async function* source() { yield 'Cat' yield 'amy' yield 'BOB' } await chain(source()).sortBy((n) => n.toLowerCase()) // Returns: ['amy', 'BOB', 'Cat']

    Type parameters

    • Key

    Parameters

    • selector: (item: T) => Key

      A function to transform items of the input sequence into comparable keys.

        • (item: T): Key
        • Parameters

          • item: T

          Returns Key

    Returns Promise<T[]>

sortByDescending

  • sortByDescending<Key>(selector: (item: T) => Key): Promise<T[]>
  • Applies a key-generating function to each element of the collection and yields an iterable ordered by keys, descending.

    example

    async function* source() { yield 'Cat' yield 'amy' yield 'BOB' } await chain(source()).sortByDescending((n) => n.toLowerCase()) // Returns: ['Cat', 'BOB', 'amy']

    Type parameters

    • Key

    Parameters

    • selector: (item: T) => Key

      A function to transform items of the input sequence into comparable keys.

        • (item: T): Key
        • Parameters

          • item: T

          Returns Key

    Returns Promise<T[]>

sortDescending

  • sortDescending<Key>(selector?: undefined | ((item: T) => Key)): Promise<T[]>
  • Yields an iterable ordered by the selected key descending. If no selector is specified, the elements will be compared directly.

    example

    await init({ from 1, to: 3 }).sortDescending() // Returns [3, 2, 1]

    async function* source() { yield 'Cat' yield 'amy' yield 'BOB' } await chain(source()).sortDescending((n) => n.toLowerCase()) // Returns: ['Cat', 'BOB', 'amy']

    Type parameters

    • Key

    Parameters

    • Optional selector: undefined | ((item: T) => Key)

      An optional function to transform items of the input sequence into comparable keys.

    Returns Promise<T[]>

sumBy

  • sumBy(selector: (item: T) => number): Promise<number>
  • Returns the sum of the values returned by the selector for each element in the collection.

    example

    async function* source() { yield { name: 'amy', age: 21 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 18 } } await chain(source()).sumBy((x) => x.age) // Returns: 41

    Parameters

    • selector: (item: T) => number

      A function to transform each element into a summable value.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns Promise<number>

take

  • Returns the elements of the iterable up to a specified count.

    example

    init({ from: 1, to: 4 }).take(2) // Yields: 1, 2

    Parameters

    • count: number

      The number of items to take.

    Returns ChainableAsyncIterable<T>

toArray

  • toArray(): Promise<T[]>
  • Creates an array from the source iterable object.

    example

    await init(3).toArray() // Returns: [0, 1, 2]

    Returns Promise<T[]>

Legend

  • Constructor
  • Method
  • Property
  • Private property

Generated using TypeDoc