Options
All
  • Public
  • Public/Protected
  • All
Menu

Type parameters

  • T

Hierarchy

  • ChainableIterable

Implements

  • Iterable<T>

Index

Constructors

constructor

Properties

Private source

source: Iterable<T>

Methods

[Symbol.iterator]

  • [Symbol.iterator](): Iterator<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: Iterable<T>

      The second iterable.

    Returns ChainableIterable<T>

choose

  • choose<U>(chooser: (item: T, index: number) => U | undefined): ChainableIterable<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

      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
        • Parameters

          • item: T
          • index: number

          Returns U | undefined

    Returns ChainableIterable<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>

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

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

          • item: T
          • index: number

          Returns Iterable<U>

    Returns ChainableIterable<U>

count

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

    example

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

    Returns 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

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

    Returns ChainableIterable<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

    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

      A function that transforms the collection items into comparable keys.

        • (item: T, index: number): Key
        • Parameters

          • item: T
          • index: number

          Returns Key

    Returns ChainableIterable<T>

every

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

    example

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

    Parameters

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

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

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns boolean

exists

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

    example

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

    Parameters

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

      A function to test each item of the input collection.

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns 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

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

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns ChainableIterable<T>

find

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

    example

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

    Parameters

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

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

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns T | undefined

get

  • get(predicate: (item: T, index: number) => boolean): 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

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

    Parameters

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

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

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns T

groupBy

  • groupBy<Key>(selector: (item: T, index: number) => Key): ChainableIterable<[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

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

    Type parameters

    • Key

    Parameters

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

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

        • (item: T, index: number): Key
        • Parameters

          • item: T
          • index: number

          Returns Key

    Returns ChainableIterable<[Key, Iterable<T>]>

length

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

    example

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

    Returns 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

      A function to transform items from the input collection.

        • (item: T, index: number): U
        • Parameters

          • item: T
          • index: number

          Returns U

    Returns ChainableIterable<U>

maxBy

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

    throws

    If the collection is empty.

    example

    function* source() { yield { name: 'amy', age: 21 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 18 } } 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 number

meanBy

  • meanBy(selector: (item: T) => number): 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

    function* source() { yield { name: 'amy', age: 21 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 18 } yield { name: 'dot', age: 39 } } 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 number

minBy

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

    throws

    If the collection is empty.

    example

    function* source() { yield { name: 'amy', age: 21 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 18 } } 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 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 ChainableIterable<[T, T]>

reverse

  • Yields each element of the iterable in reverse order.

    example

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

    Returns ChainableIterable<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 ChainableIterable<T>

sort

  • 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

    function* source() { yield 'Cat' yield 'amy' yield 'BOB' } chain(source()).sort((n) => n.toLowerCase()) // Yields: '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 ChainableIterable<T>

sortBy

  • Applies a key-generating function to each element of the collection and yields an iterable ordered by keys.

    example

    function* source() { yield 'Cat' yield 'amy' yield 'BOB' } chain(source()).sortBy((n) => n.toLowerCase()) // Yields: '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 ChainableIterable<T>

sortByDescending

  • Applies a key-generating function to each element of the collection and yields an iterable ordered by keys, descending.

    example

    function* source() { yield 'Cat' yield 'amy' yield 'BOB' } chain(source()).sortByDescending((n) => n.toLowerCase()) // Yields: '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 ChainableIterable<T>

sortDescending

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

    example

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

    function* source() { yield 'Cat' yield 'amy' yield 'BOB' } chain(source()).sortDescending((n) => n.toLowerCase()) // Yields: '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 ChainableIterable<T>

sumBy

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

    example

    function* source() { yield { name: 'amy', age: 21 } yield { name: 'bob', age: 2 } yield { name: 'cat', age: 18 } } 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 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 ChainableIterable<T>

toArray

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

    example

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

    Returns T[]

Legend

  • Constructor
  • Method
  • Property
  • Private property

Generated using TypeDoc