Options
All
• Public
• Public/Protected
• All

Ordered sequence of elements

## Methods

### __@iterator

• __@iterator(): 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]);``````

### 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();``````

### 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);``````

### 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);``````

### 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);``````

### 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();``````

### 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();``````

### 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));``````

### 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" }
//   ]
// }``````

#### 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" }
//   ]
// }``````

#### 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"]
// }``````

#### 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"]
// }``````

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

### 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);``````

### isEmpty

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

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

### 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();``````

### 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);``````

### 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");``````

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

### 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)``````

### reverse

• Reverses the order of the items in the sequence

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

### 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);``````

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

### 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)``````

### 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)``````

### 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()``````

#### 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

### 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();``````

#### 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();``````

#### 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);``````

#### 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");``````

#### Parameters

• ##### valueSelector: MapFn<TItem, TResult>

A function to select a value from an element.

### 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);``````

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

### toArray

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

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

### 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);``````

### toObject

• Converts the sequence to an object using the given keySelectorFn and possible elementSelectorFn.

example
``````// Returns an object:
// {
//   "John": { id: 1, name: "John" },
//   "Jane": { id: 2, name: "Jane"}
// }
const users = [{ id: 1, name: "John" }, { id: 2, name: "Jane"}]
from(users).toObject(u => u.name);``````

### 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();``````

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

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

## 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