Options
All
  • Public
  • Public/Protected
  • All
Menu

Ordered sequence of elements

Type parameters

  • TItem

Hierarchy

Implemented by

Index

Methods

__@iterator

  • __@iterator(): Iterator<TItem>
  • Returns Iterator<TItem>

concat

  • concat<TOther>(...others: Iterable<TOther>[]): Sequence<TItem | TOther>
  • Returns a new sequence that contains the items in the current sequence and items from the given iterable.

    example
    // Returns sequence with values 1, 2, 3, 4
    from([1, 2]).concat([3, 4]);

    Type parameters

    • TOther

    Parameters

    • Rest ...others: Iterable<TOther>[]

    Returns Sequence<TItem | TOther>

distinct

  • Returns unique values in the sequence. Uniqueness is checked using the '===' operator.

    example
    // Returns a sequence with the values 4, 5
    from([4, 4, 5, 4]).distinct();

    Returns Sequence<TItem>

every

  • Checks that all items in the sequence pass the test implemented by the provided function.

    example
    // Returns false
    from([-1, 4, 5, 6]).every(x => x >= 0);

    Parameters

    Returns boolean

filter

  • Returns a new sequence where items are filtered out for which the predicate function returns a falsy value.

    example
    // Returns a squence with the value -1
    from([-1, 4, 5, 6]).filter(x => x < 0);

    Parameters

    Returns Sequence<TItem>

find

  • find(predicate: PredicateFn<TItem>): TItem | undefined
  • Returns the value of the first element in the sequence that satisfies the provided testing function. Otherwise undefined is returned.

    example
    // Returns 4
    from([2, 4, 6]).find(x => x === 4);

    Parameters

    Returns TItem | undefined

first

  • first(): TItem | undefined
  • Returns the first element of the sequence or undefined if the sequence is empty.

    example
    // Returns 1
    from([1, 3, 5]).first();

    Returns TItem | undefined

flatMap

  • flatMap<TResultItem>(mapperFn: MapFn<TItem, TResultItem[]>): Sequence<TResultItem>
  • First maps each element of the sequence using the given mapping function, then flattens the result into a new sequence.

    example
    // Returns [1, 2, 3, 4, 5, 6]
    from([1, 3, 5]).flatMap(x => [x, x + 1]).toArray();

    Type parameters

    • TResultItem

    Parameters

    • mapperFn: MapFn<TItem, TResultItem[]>

    Returns Sequence<TResultItem>

forEach

  • Calls the given callback function with each item in the sequence.

    example
    // Logs 1, 2 and 3 to console
    from([1, 2, 3]).forEach(i => console.log(i));

    Parameters

    Returns void

groupBy

  • Groups the items in the sequence using the given item's key

    example
    from([
      { name: "John", gender: "M" },
      { name: "Mike", gender: "M" },
      { name: "Lisa", gender: "F" },
      { name: "Mary", gender: "F" }
    ]).groupBy("gender");
    // Returns a sequence with two groupings:
    // {
    //   key: "M",
    //   items: [
    //     { name: "John", gender: "M" },
    //     { name: "Mike", gender: "M" }
    //   ]
    // },
    // {
    //   key: "F",
    //   items: [
    //     { name: "Lisa", gender: "F" },
    //     { name: "Mary", gender: "F" }
    //   ]
    // }

    Type parameters

    • TKey: keyof TItem

    Parameters

    • key: TKey

      Key to be used for the grouping

    Returns Sequence<Grouping<TItem[TKey], TItem>>

  • Groups the items in the sequence by keys returned by the given keySelector function.

    example
    from([
      { name: "John", gender: "M" },
      { name: "Mike", gender: "M" },
      { name: "Lisa", gender: "F" },
      { name: "Mary", gender: "F" }
    ]).groupBy(user => user.gender);
    // Returns a sequence with two groupings:
    // {
    //   key: "M",
    //   items: [
    //     { name: "John", gender: "M" },
    //     { name: "Mike", gender: "M" }
    //   ]
    // },
    // {
    //   key: "F",
    //   items: [
    //     { name: "Lisa", gender: "F" },
    //     { name: "Mary", gender: "F" }
    //   ]
    // }

    Type parameters

    • TKey

    Parameters

    • keySelector: KeySelectorFn<TItem, TKey>

      A function to extract the key for each element.

    Returns Sequence<Grouping<TKey, TItem>>

  • Groups the items of a sequence according to a specified key and projects the elements for each group by using a specified function.

    example
    from([
      { name: "John", gender: "M" },
      { name: "Mike", gender: "M" },
      { name: "Lisa", gender: "F" },
      { name: "Mary", gender: "F" }
    ]).groupBy("gender", user => user.name);
    // Returns a sequence with two groupings:
    // {
    //   key: "M",
    //   items: ["John", "Mike"]
    // },
    // {
    //   key: "F",
    //   items: ["Lisa", "Mary"]
    // }

    Type parameters

    • TKey: keyof TItem

    • TElement

    Parameters

    • key: TKey

      Key to be used for the grouping

    • elementSelector: MapFn<TItem, TElement>

      A function to map each source element to an element in an Grouping<TKey,TElement>.

    Returns Sequence<Grouping<TItem[TKey], TItem>>

  • Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.

    example
    from([
      { name: "John", gender: "M" },
      { name: "Mike", gender: "M" },
      { name: "Lisa", gender: "F" },
      { name: "Mary", gender: "F" }
    ]).groupBy("gender", user => user.name);
    // Returns a sequence with two groupings:
    // {
    //   key: "M",
    //   items: ["John", "Mike"]
    // },
    // {
    //   key: "F",
    //   items: ["Lisa", "Mary"]
    // }

    Type parameters

    • TKey

    • TElement

    Parameters

    • keySelector: KeySelectorFn<TItem, TKey>

      A function to extract the key for each element.

    • elementSelector: MapFn<TItem, TElement>

      A function to map each source element to an element in an Grouping<TKey,TElement>.

    Returns Sequence<Grouping<TKey, TElement>>

includes

  • includes(searchItem: TItem): boolean
  • Determines whether the sequence includes the given element, returning true or false as appropriate. The check is done using '==='.

    example
    // Returns true
    from([1, 2, 3]).includes(3);

    Parameters

    • searchItem: TItem

    Returns boolean

isEmpty

  • isEmpty(): boolean
  • Returns true if the sequence is empty, false otherwise.

    example
    // Returns true
    from([]).isEmpty();

    Returns boolean

last

  • last(): TItem | undefined
  • Returns the first element of the sequence or undefined if the sequence is empty.

    example
    // Returns 5
    from([1, 3, 5]).last();

    Returns TItem | undefined

map

  • map<TResultItem>(mapFn: MapFn<TItem, TResultItem>): Sequence<TResultItem>
  • Maps the sequence to a new sequence where each item is converted to a new value using the given mapper function.

    example
    // Returns [2, 4, 6]
    from([1, 2, 3]).map(x => x * 2);

    Type parameters

    • TResultItem

    Parameters

    • mapFn: MapFn<TItem, TResultItem>

    Returns Sequence<TResultItem>

pick

  • pick<TKeys>(...keys: TKeys[]): Sequence<object>
  • Maps each item in the sequence to an object composed of the picked object properties.

    example
    const users = [
    { id: 1, name: "John", age: 31, active: true },
    { id: 2, name: "Jane", age: 32, active: false },
    { id: 3, name: "Luke", age: 33, active: false },
    { id: 4, name: "Mary", age: 34, active: true },
    ];
    
    // Returns a Sequence of { name: 'John' }, { name: 'Jane' }, { name: 'Luke' }, { name: 'Mary' }
    from(users).pick("name");

    Type parameters

    • TKeys: keyof TItem

    Parameters

    • Rest ...keys: TKeys[]

    Returns Sequence<object>

prepend

  • prepend(...items: Iterable<TItem>[]): Sequence<TItem>
  • This method yields the elements from the provided items first, followed by the items in the underlying sequence.

    example
    // returns [4, 5, 6, 1, 2, 3]
    from([1, 2, 3])
      .prepend([4, 5, 6])
      .toArray();

    Parameters

    • Rest ...items: Iterable<TItem>[]

      The provided set of items that should be in the prepended to the Sequence.

    Returns Sequence<TItem>

reduce

  • reduce<TResult>(callback: ReduceCallbackFn<TResult, TItem>, accumulator: TResult): TResult
  • Executes a reducer function on each item in the sequence resulting in a single output value.

    example
    // Returns a 15
    from([1, 2, 3, 4, 5]).reduce((x, acc) => acc+x, 0)

    Type parameters

    • TResult

    Parameters

    Returns TResult

reverse

  • Reverses the order of the items in the sequence

    example
    // Returns [3, 2, 1]
    from([1, 2, 3]).reverse().toArray();

    Returns Sequence<TItem>

skip

  • skip(howMany: number): Sequence<TItem>
  • Skips the first N items in the sequence

    example
    // Returns [3, 4]
    from([1, 2, 3, 4]).skip(2);

    Parameters

    • howMany: number

    Returns Sequence<TItem>

skipWhile

  • Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

    example
    // Returns [3, 4, 5]
    from([1, 2, 3, 4, 5])
      .skipWhile(i => i < 3)
      .toArray();

    Parameters

    • predicate: PredicateFn<TItem>

      A function to test each element for a condition.

    Returns Sequence<TItem>

some

  • Returns true if sequence contains an element for which the given predicate returns a truthy value.

    example
    // Returns true
    from([1, 2, 3]).some(x => x === 1)

    Parameters

    Returns boolean

sortBy

  • example
    // Returns a Sequence of 1, 2, 3
    from([1, 3, 2]).sortBy()

    Returns OrderedSequence<TItem>

  • example
    const users = [
    { id: 2, name: "Jane" },
    { id: 4, name: "Mary" },
    { id: 1, name: "John" },
    { id: 3, name: "Luke" },
    ];
    
    // Returns a Sequence of
    //  { id: 1, name: 'John' },
    //  { id: 2, name: 'Jane' },
    //  { id: 3, name: 'Luke' },
    //  { id: 4, name: 'Mary' }
    from(users).sortBy(user => user.id)

    Type parameters

    • TKey

    Parameters

    Returns OrderedSequence<TItem>

sortByDescending

  • Sorts the elements of a sequence in descending order according to a key by using a specified comparer.

    example
    // Returns a Sequence of 3, 2, 1
    from([1, 3, 2]).sortByDescending()

    Type parameters

    • TKey

    Parameters

    • Optional keySelector: KeySelectorFn<TItem, TKey>

      A function to extract a key from an element.

    • Optional comparer: ComparerFn<TKey>

      A function to compare the keys

    Returns OrderedSequence<TItem>

sum

  • sum<TItem>(): number
  • sum<TItem>(): string
  • sum<TResult>(valueSelector: MapFn<TItem, TResult>): number
  • sum<TResult>(valueSelector: MapFn<TItem, TResult>): string
  • Sums the elements in the sequence. NOTE! If the sequence is empty, 0 is returned.

    example
    // Returns 6
    from([1, 2, 3]).sum();

    Type parameters

    • TItem: number

    Returns number

  • Sums the elements in the sequence NOTE! If the sequence is empty, 0 is returned.

    example
    // Returns "abc"
    from(["a", "b", "c"]).sum();

    Type parameters

    • TItem: string

    Returns string

  • Maps the elements in the sequence using the valueSelector and sums them together. NOTE! If the sequence is empty, 0 is returned.

    example
    // Returns 2
    from([true, false, true]).sum(x => x ? 1 : 0);

    Type parameters

    • TResult: number

    Parameters

    • valueSelector: MapFn<TItem, TResult>

      A function to select a value from an element.

    Returns number

  • Maps the elements in the sequence using the valueSelector and sums them together. NOTE! If the sequence is empty, 0 is returned.

    example
    // Returns "101"
    from([true, false, true]).sum(x => x ? "1" : "0");

    Type parameters

    • TResult: string

    Parameters

    • valueSelector: MapFn<TItem, TResult>

      A function to select a value from an element.

    Returns string

take

  • take(howMany: number): Sequence<TItem>
  • Takes the firt N items from the sequence

    example
    // Returns [1, 2]
    from([1, 2, 3, 4]).take(2);

    Parameters

    • howMany: number

    Returns Sequence<TItem>

takeWhile

  • Returns elements from a sequence as long as a specified condition is true, and then skips the remaining elements.

    example
    // Returns [1, 2]
    from([1, 2, 3, 4, 5])
      .takeWhile(i => i < 3)
      .toArray();

    Parameters

    • predicate: PredicateFn<TItem>

      A function to test each element for a condition.

    Returns Sequence<TItem>

thenBy

thenByDescending

toArray

  • toArray(): TItem[]
  • Converts the sequence to an array

    example
    // Return [1, 2, 3]
    from([1, 2, 3]).toArray();

    Returns TItem[]

toMap

  • toMap<TKey, TElement>(keySelectorFn: MapFn<TItem, TKey>, elementSelectorFn?: MapFn<TItem, TElement>): Map<TKey, TElement>
  • Converts the sequence to a Map using the given keySelectorFn and possible elementSelectorFn.

    example
    // Returns map with elements:
    // 1 -> { id: 1, name: "John" }
    // 2 -> { id: 2, name: "Jane"}
    const users = [{ id: 1, name: "John" }, { id: 2, name: "Jane"}]
    from(users).toMap(u => u.id);

    Type parameters

    • TKey

    • TElement

    Parameters

    • keySelectorFn: MapFn<TItem, TKey>
    • Optional elementSelectorFn: MapFn<TItem, TElement>

    Returns Map<TKey, TElement>

toObject

toSet

  • toSet(): Set<TItem>
  • Converts the sequence to a Set

    example
    // Return a Set with elements 1, 2, 3
    from([1, 1, 2, 3]).toSet();

    Returns Set<TItem>

toString

  • toString(separator?: undefined | string): string
  • Converts the sequence to a string using the given separator

    example
    // Returns a string "1,2,3"
    from([1, 2, 3]).toString();
    
    // Returns a string "1 - 2 - 3"
    from([1, 2, 3]).toString(" - ");

    Parameters

    • Optional separator: undefined | string

      A string used to separate one element of a sequence from the next in the resulting String. If omitted, the elements are separated with a comma.

    Returns string

without

  • Returns elements from a sequence as long as they don't exist in the specified iterable items.

    example
    // returns [2, 4, 6]
    from([1, 2, 3, 4, 5, 6])
      .without([1, 3, 5])
      .toArray();
    
    // returns [{ id: 1 }, { id: 3 }]
    from([{ id: 1 }, { id: 2 }, { id: 3 }])
      .without([{ id: 2 }], (a, b) => a.id === b.id)
      .toArray();

    Parameters

    • items: Iterable<TItem>

      The provided set of items that should not be in the returned Sequence.

    • Optional predicate: ComparePredicate<TItem>

      The optional predicate that determines if two TItem items are equal.

    Returns Sequence<TItem>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc