Options
All
  • Public
  • Public/Protected
  • All
Menu

Type parameters

  • T

Hierarchy

  • ChainableArray

Implements

  • Iterable<T>

Index

Constructors

constructor

Properties

Private source

source: T[]

Methods

[Symbol.iterator]

  • [Symbol.iterator](): Iterator<T, any, undefined>

append

  • Wraps the two given arrays as a single concatenated array.

    example

    chain([1]).append([2]).toArray() // [1, 2]

    Parameters

    • second: ReadonlyArray<T>

      The second array.

    Returns ChainableArray<T>

choose

  • choose<U>(chooser: (item: T, index: number) => U | undefined): ChainableArray<U>
  • Applies the given function to each element of the array and returns a new array comprised of the results for each element where the function returns a value.

    example

    chain([1, 2, 3]) .choose(x => (x % 2 === 1 ? x * 2 : undefined) .toArray() // [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 ChainableArray<U>

collect

  • collect<U>(mapping: (item: T, index: number) => Iterable<U>): ChainableArray<U>
  • Applies the given function to each element of the source array and concatenates all the results.

    example

    chain([1, 2]).collect(x => [x, x]).toArray() // [1, 1, 2, 2]

    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 ChainableArray<U>

count

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

    example

    chain([1, 2, 3, 4, 5]).count() // 5

    Returns number

distinct

  • Returns an array 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

    chain(['amy', 'bob', 'bob', 'cat']).distinct().toArray() // ['amy', 'bob', 'cat']

    Returns ChainableArray<T>

distinctBy

  • distinctBy<Key>(selector: (item: T, index: number) => Key): ChainableArray<T>
  • Returns an array 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

    chain([ { name: 'amy', id: 1 }, { name: 'bob', id: 2 }, { name: 'bob', id: 3 }, { name: 'cat', id: 3 } ]) .distinctBy(x => x.name) .toArray() // [{ 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 array items into comparable keys.

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

          • item: T
          • index: number

          Returns Key

    Returns ChainableArray<T>

every

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

    example

    chain([1, 2]).every(x => x >= 1).toArray() // true

    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 array satisfies the given predicate.

    example

    chain([1, 2]).exists(x => x === 1).toArray() // true

    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

  • filter(predicate: (item: T, index: number) => boolean): ChainableArray<T>
  • Returns a new array containing only the elements of the collection for which the given predicate returns true.

    example

    chain([1, 2, 3, 4]).filter(x => x % 2 === 0).toArray() // [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 ChainableArray<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

    chain([ { name: 'amy', id: 1 }, { name: 'bob', id: 2 } ]) .find(x => x.name === 'bob') .toArray() // { name: 'bob', id: 2 }

    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 or throws if not found. If you don't want exceptions, use find instead.

    throws

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

    example

    chain([ { name: 'amy', id: 1 }, { name: 'bob', id: 2 } ]) .get(x => x.name === 'bob') .toArray() // { name: 'bob', id: 2 }

    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): ChainableArray<[Key, T[]]>
  • Applies a key-generating function to each element of an array and yields an array of unique keys and an array of all elements that have each key.

    example

    chain([{ name: 'amy', age: 1 }, { name: 'bob', age: 2 }, { name: 'cat', age: 2 }]) .groupBy(x => x.age) .toArray() // [[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 ChainableArray<[Key, T[]]>

length

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

    example

    chain([1, 2, 3, 4, 5]).length() // 5

    Returns number

map

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

    example

    chain([1, 2]).map(x => x * 2).toArray() // [2, 4]

    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 ChainableArray<U>

maxBy

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

    throws

    If the collection is empty.

    example

    chain([{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }]) .maxBy(x => x.age) .toArray() // 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 array.

    throws

    If the collection is empty.

    example

    chain([{ name: 'amy', age: 21 }, { name: 'bob', age: 3 }, { name: 'cat', age: 18 }]) .meanBy(x => x.age) .toArray() // 14

    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 array.

    throws

    If the collection is empty.

    example

    chain([{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }]) .minBy(x => x.age) .toArray() // 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 array of each element in the input collection and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.

    example

    chain([1, 2, 3]).pairwise().toArray() // [[1, 2], [2, 3]]

    Returns ChainableArray<[T, T]>

reverse

  • Returns a new array with the order of elements reversed.

    example

    chain([8, 3, 5]).reverse().toArray() // [5, 3, 8]

    Returns ChainableArray<T>

sort

  • sort<Key>(selector?: undefined | ((item: T) => Key)): ChainableArray<T>
  • Returns a new array ordered by the selected key. If no selector is specified, the elements will be compared directly.

    example

    chain([{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }]) .sort(x => x.age) .toArray() // [{ name: 'bob', age: 2 }, { name: 'cat', age: 18 }, { name: 'amy', age: 21 }]

    Type parameters

    • Key

    Parameters

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

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

    Returns ChainableArray<T>

sortBy

  • Applies a key-generating function to each element of the array and returns a new array ordered by the keys.

    example

    chain([{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }]) .sortBy(x => x.age) .toArray() // [{ name: 'bob', age: 2 }, { name: 'cat', age: 18 }, { name: 'amy', age: 21 }]

    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 ChainableArray<T>

sortByDescending

  • sortByDescending<Key>(selector: (item: T) => Key): ChainableArray<T>
  • Applies a key-generating function to each element of the array and returns a new array ordered by the keys, descending.

    example

    chain([{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }]) .sortByDescending(x => x.age) .toArray() // [{ name: 'amy', age: 21 }, { name: 'cat', age: 18 }, { name: 'bob', age: 2 }]

    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 ChainableArray<T>

sortDescending

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

    example

    chain([{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }]) .sortDescending(x => x.age) .toArray() // [{ name: 'amy', age: 21 }, { name: 'cat', age: 18 }, { name: 'bob', age: 2 }]

    Type parameters

    • Key

    Parameters

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

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

    Returns ChainableArray<T>

sumBy

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

    example

    chain([{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }]) .sumBy(x => x.age) .toArray() // 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

toArray

  • toArray(): T[]
  • Returns a native array of the current value.

    Returns T[]

Legend

  • Constructor
  • Method
  • Property
  • Private property

Generated using TypeDoc