kollectionjs - v2.0.0
    Preparing search index...

    Class Sequence<T>

    A lazy sequence of elements that wraps a native Iterator<T>.

    Operations on a Sequence are evaluated lazily — intermediate operations (e.g. filter, map) build up a pipeline and produce no results until a terminal operation (e.g. toArray, fold, count) consumes the iterator.

    asSequence([1, 2, 3, 4, 5])
    .filter(n => n % 2 === 0)
    .map(n => n * 10)
    .toArray(); // [20, 40]

    Type Parameters

    • T

      The element type.

    Hierarchy

    • Iterator<T>
      • Sequence
    Index

    Constructors

    • Creates a Sequence wrapping the given Iterator.

      Type Parameters

      • T

        The element type.

      Parameters

      • iterator: Iterator<T>

        The underlying iterator to wrap.

      Returns Sequence<T>

    Properties

    "[toStringTag]": string

    Methods

    • Returns void

    • Returns IteratorObject<T, undefined, unknown>

    • Returns this iterator.

      Returns IteratorObject<T, undefined, unknown>

    • Returns a Map built by applying transform to each element and using the resulting [key, value] pair. If two elements produce the same key, the last value wins.

      Type Parameters

      • K

        The key type.

      • V

        The value type.

      Parameters

      • transform: (value: T) => [K, V]

        Returns a [key, value] tuple for each element.

      Returns Map<K, V>

    • Returns a Map keyed by the result of keySelector (or a property name). An optional valueTransformer can transform the stored value. If two elements produce the same key, the last value wins.

      Type Parameters

      • K

      Parameters

      • keySelector: (value: T) => K

        A key-selector function or a property name of T.

      Returns Map<K, T>

      sequenceOf({ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' })
      .associateBy('id'); // Map { 1 => { id: 1, name: 'Alice' }, 2 => { id: 2, name: 'Bob' } }
    • Returns a Map keyed by the result of keySelector (or a property name). An optional valueTransformer can transform the stored value. If two elements produce the same key, the last value wins.

      Type Parameters

      • K extends string | number | symbol

      Parameters

      • key: K

      Returns Map<T[K], T>

      sequenceOf({ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' })
      .associateBy('id'); // Map { 1 => { id: 1, name: 'Alice' }, 2 => { id: 2, name: 'Bob' } }
    • Returns a Map keyed by the result of keySelector (or a property name). An optional valueTransformer can transform the stored value. If two elements produce the same key, the last value wins.

      Type Parameters

      • K
      • V

      Parameters

      • keySelector: (value: T) => K

        A key-selector function or a property name of T.

      • valueTransformer: (value: T) => V

      Returns Map<K, V>

      sequenceOf({ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' })
      .associateBy('id'); // Map { 1 => { id: 1, name: 'Alice' }, 2 => { id: 2, name: 'Bob' } }
    • Returns a Map keyed by the result of keySelector (or a property name). An optional valueTransformer can transform the stored value. If two elements produce the same key, the last value wins.

      Type Parameters

      • K extends string | number | symbol
      • V

      Parameters

      • key: K
      • valueTransformer: (value: T) => V

      Returns Map<T[K], V>

      sequenceOf({ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' })
      .associateBy('id'); // Map { 1 => { id: 1, name: 'Alice' }, 2 => { id: 2, name: 'Bob' } }
    • Returns a Map where each element is the key and valueSelector provides the value.

      Type Parameters

      • V

        The value type.

      Parameters

      • valueSelector: (value: T) => V

        Called with each element to produce its associated value.

      Returns Map<T, V>

    • Returns the arithmetic mean of a numeric sequence. Returns NaN for an empty sequence.

      Parameters

      Returns number

    • Splits the sequence into chunks of at most chunkSize elements. The last chunk may be smaller if the sequence length is not divisible by chunkSize.

      Parameters

      • chunkSize: number

        Maximum number of elements per chunk. Must be ≥ 1.

      Returns Sequence<T[]>

      If chunkSize is less than 1.

    • Returns true if the sequence contains the given element (using ===).

      Parameters

      • element: T

        The value to search for.

      Returns boolean

    • Returns the number of elements that satisfy the predicate. When called with no argument, returns the total number of elements.

      Parameters

      • predicate: (value: T) => boolean = TruePredicate

        Called with each element. Defaults to always-true.

      Returns number

    • Returns a sequence containing only distinct elements (first occurrence wins). Equality is determined using a Set (i.e. SameValueZero).

      Returns Sequence<T>

    • Returns a sequence containing only elements with distinct keys as computed by selector. First occurrence wins when two elements produce the same key.

      Type Parameters

      • K

        The key type.

      Parameters

      • selector: (value: T) => K

        Extracts the key used for comparison.

      Returns Sequence<T>

    • Returns a sequence that skips the first num elements.

      Parameters

      • num: number = 1

        Number of elements to skip. Defaults to 1.

      Returns Sequence<T>

    • Returns a sequence that skips elements from the start while the predicate holds. Once the predicate returns false, all remaining elements are included.

      Parameters

      • predicate: (value: T) => boolean

        Called with each element.

      Returns Sequence<T>

    • Returns the element at the given zero-based index, or undefined if the index is out of bounds.

      Parameters

      • index: number

        Zero-based position.

      Returns T | undefined

    • Returns the element at the given zero-based index, or the result of defaultValue if the index is out of bounds.

      Parameters

      • index: number

        Zero-based position.

      • defaultValue: (index: number) => T

        Called with the requested index when it is out of bounds.

      Returns T

    • Determines whether all the members of this iterator satisfy the specified test.

      Parameters

      • predicate: (value: T, index: number) => unknown

        A function that accepts up to two arguments. The every method calls the predicate function for each element in this iterator until the predicate returns false, or until the end of this iterator.

      Returns boolean

    • Returns a sequence containing only the elements that satisfy the predicate.

      Parameters

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

        Called with (value, index) for each element.

      Returns Sequence<T>

    • Returns a sequence containing only the [key, value] pairs whose key satisfies the predicate.

      Type Parameters

      • K

        The key type.

      • V

        The value type.

      Parameters

      • this: Sequence<[K, V]>
      • predicate: (key: K) => boolean

        Called with each key.

      Returns Sequence<[K, V]>

    • Returns a sequence containing only the elements that do not satisfy the predicate.

      Parameters

      • predicate: (value: T) => boolean

        Called with each element.

      Returns Sequence<T>

    • Returns a sequence with all null and undefined elements removed. The return type is narrowed to Sequence<NonNullable<T>>.

      Returns Sequence<NonNullable<T>>

    • Returns a sequence containing only the [key, value] pairs whose value satisfies the predicate.

      Type Parameters

      • K

        The key type.

      • V

        The value type.

      Parameters

      • this: Sequence<[K, V]>
      • predicate: (value: V) => boolean

        Called with each value.

      Returns Sequence<[K, V]>

    • Returns the first element matching the predicate, or undefined if none match. When called with no argument, returns the first element or undefined for an empty sequence.

      Parameters

      • predicate: (value: T, index: number) => unknown = TruePredicate

        Called with (value, index) for each element. Defaults to always-true.

      Returns T | undefined

    • Returns the first element matching the predicate, or undefined if none match. When called with no argument, returns the first element or undefined for an empty sequence.

      Parameters

      • predicate: (value: T) => boolean = TruePredicate

        Test applied to each element. Defaults to always-true.

      Returns T | undefined

    • Maps each element to an iterable and flattens the results into a single sequence.

      Type Parameters

      • U

        The element type of the produced iterables.

      Parameters

      • transform: (value: T, index: number) => Iterable<U>

        Called with (value, index); must return an Iterable.

      Returns Sequence<U>

    • Flattens a sequence of iterables into a single sequence.

      Type Parameters

      • U

        The element type of the nested iterables.

      Parameters

      Returns Sequence<U>

      sequenceOf([1, 2], [3, 4]).flatten().toArray(); // [1, 2, 3, 4]
      
    • Accumulates a value by applying operation left-to-right, starting from initial.

      Type Parameters

      • R

        The accumulator type.

      Parameters

      • initial: R

        The starting accumulator value.

      • operation: (acc: R, value: T, index: number) => R

        Called with (accumulator, value) for each element.

      Returns R

    • Performs the specified action for each element in the iterator.

      Parameters

      • callbackfn: (value: T, index: number) => void

        A function that accepts up to two arguments. forEach calls the callbackfn function one time for each element in the iterator.

      Returns void

    • Groups elements into a Map by the key returned by keySelector. An optional valueTransform can be supplied to transform each element before storing.

      Type Parameters

      • K

        The key type.

      Parameters

      • keySelector: (value: T) => K

        Extracts the group key from each element.

      Returns Map<K, T[]>

    • Groups elements into a Map by the key returned by keySelector. An optional valueTransform can be supplied to transform each element before storing.

      Type Parameters

      • K

        The key type.

      • V

        The value type (when valueTransform is provided).

      Parameters

      • keySelector: (value: T) => K

        Extracts the group key from each element.

      • valueTransform: (value: T) => V

      Returns Map<K, V[]>

    • Returns the zero-based index of the first occurrence of element (using ===), or -1 if it is not found.

      Parameters

      • element: T

        The value to search for.

      Returns number

    • Returns the zero-based index of the first element matching the predicate, or -1 if none match.

      Parameters

      • predicate: (value: T) => boolean

        Called with each element.

      Returns number

    • Returns the zero-based index of the last element matching the predicate, or -1 if none match.

      Parameters

      • predicate: (value: T) => boolean

        Called with each element.

      Returns number

    • Returns true if the sequence contains no elements.

      Returns boolean

    • Returns true if the sequence contains at least one element.

      Returns boolean

    • Joins all elements into a single string.

      Parameters

      • Optionaloptions: {
            postfix?: string;
            prefix?: string;
            separator?: string;
            transform?: (value: T) => string;
        }
        • Optionalpostfix?: string

          String appended to the result. Defaults to "".

        • Optionalprefix?: string

          String prepended to the result. Defaults to "".

        • Optionalseparator?: string

          String placed between elements. Defaults to ", ".

        • Optionaltransform?: (value: T) => string

          Optional function to convert each element to a string. Defaults to String(value).

      Returns string

    • Returns the last element matching the predicate, or undefined if none match. When called with no argument, returns the last element or undefined for an empty sequence.

      Parameters

      • predicate: (value: T) => boolean = TruePredicate

        Test applied to each element. Defaults to always-true.

      Returns T | undefined

    • Returns a sequence where each element is transformed by transform.

      Type Parameters

      • S

        The type of the transformed elements.

      Parameters

      • transform: (value: T, index: number) => S

        Called with (value, index) for each element.

      Returns Sequence<S>

    • Returns a sequence of [key, value] pairs where each key is transformed by transform.

      Type Parameters

      • K

        The original key type.

      • V

        The value type.

      • K2

        The transformed key type.

      Parameters

      • this: Sequence<[K, V]>
      • transform: (key: K) => K2

        Called with each key.

      Returns Sequence<[K2, V]>

    • Returns a sequence of transformed elements, skipping any null or undefined results.

      Type Parameters

      • R

        The non-nullable return type of transform.

      Parameters

      • transform: (value: T) => R | null | undefined

        Called with each element; results of null/undefined are excluded.

      Returns Sequence<R>

    • Returns a sequence of [key, value] pairs where each value is transformed by transform.

      Type Parameters

      • K

        The key type.

      • V

        The original value type.

      • V2

        The transformed value type.

      Parameters

      • this: Sequence<[K, V]>
      • transform: (value: V) => V2

        Called with each value.

      Returns Sequence<[K, V2]>

    • Returns the maximum element using the natural > ordering, or undefined if the sequence is empty.

      Returns T | undefined

    • Returns the element for which selector returns the largest value, or undefined if the sequence is empty.

      Type Parameters

      • R

        The type of the selected key.

      Parameters

      • selector: (value: T) => R

        Extracts a comparable key from each element.

      Returns T | undefined

    • Returns the maximum element according to the given compare function, or undefined if the sequence is empty.

      Parameters

      • compare: (a: T, b: T) => number

        Returns a positive number when the first argument is greater.

      Returns T | undefined

    • Returns the minimum element using the natural < ordering, or undefined if the sequence is empty.

      Returns T | undefined

    • Returns the element for which selector returns the smallest value, or undefined if the sequence is empty.

      Type Parameters

      • R

        The type of the selected key.

      Parameters

      • selector: (value: T) => R

        Extracts a comparable key from each element.

      Returns T | undefined

    • Returns a sequence with the given element(s) excluded. When data is an Iterable, all elements it contains are excluded. When data is a single value, only that exact value (by ===) is excluded.

      Parameters

      • data: T | Iterable<T, any, any>

        A single element or an iterable of elements to exclude.

      Returns Sequence<T>

    • Returns the minimum element according to the given compare function, or undefined if the sequence is empty.

      Parameters

      • compare: (a: T, b: T) => number

        Returns a negative number when the first argument is smaller.

      Returns T | undefined

    • Returns IteratorResult<T>

    • Returns true if no element satisfies the predicate. When called with no argument, returns true if the sequence is empty.

      Parameters

      • predicate: (value: T) => boolean = TruePredicate

        Test applied to each element. Defaults to always-true.

      Returns boolean

    • Performs the given action on each element and returns the same sequence unchanged. Useful for side-effects (e.g. logging) inside a pipeline.

      Parameters

      • action: (value: T) => void

        Called with each element.

      Returns Sequence<T>

    • Splits the sequence into two arrays: the first contains elements that satisfy the predicate, the second contains the rest.

      Parameters

      • predicate: (value: T) => boolean

        Called with each element.

      Returns [T[], T[]]

      A tuple [matching, nonMatching].

    • Returns a sequence that contains all elements of this sequence followed by the given element or all elements of the given iterable.

      Parameters

      • element: T

        A single element or an iterable to append.

      Returns Sequence<T>

    • Returns a sequence that contains all elements of this sequence followed by the given element or all elements of the given iterable.

      Parameters

      • other: Iterable<T>

      Returns Sequence<T>

    • Calls the specified callback function for all the elements in this iterator. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

      Parameters

      • callbackfn: (previousValue: T, currentValue: T, currentIndex: number) => T

        A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the iterator.

      Returns T

    • Parameters

      • callbackfn: (previousValue: T, currentValue: T, currentIndex: number) => T
      • initialValue: T

      Returns T

    • Calls the specified callback function for all the elements in this iterator. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

      Type Parameters

      • U

      Parameters

      • callbackfn: (previousValue: U, currentValue: T, currentIndex: number) => U

        A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the iterator.

      • initialValue: U

        If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of a value from the iterator.

      Returns U

    • Parameters

      • Optionalvalue: undefined

      Returns IteratorResult<T, undefined>

    • Returns a sequence of running accumulator values, starting with initial. The first element of the returned sequence is always initial.

      Type Parameters

      • R

        The accumulator type.

      Parameters

      • initial: R

        The starting accumulator value.

      • operation: (acc: R, value: T) => R

        Called with (accumulator, value) for each element.

      Returns Sequence<R>

      sequenceOf(1, 2, 3).scan(0, (acc, n) => acc + n).toArray(); // [0, 1, 3, 6]
      
    • Returns the single element matching the predicate, or undefined if there is no match or more than one match.

      Parameters

      • predicate: (value: T) => boolean = TruePredicate

        Called with each element. Defaults to always-true.

      Returns T | undefined

    • Returns true if any element satisfies the predicate. When called with no argument, returns true if the sequence is non-empty.

      Parameters

      • predicate: (value: T, index: number) => unknown = TruePredicate

        Test applied to each element. Defaults to always-true.

      Returns boolean

    • Returns a new sequence sorted in ascending order by the key returned by selector. Materializes the sequence.

      Type Parameters

      • R

        The key type.

      Parameters

      • selector: (value: T) => R

        Extracts a comparable sort key from each element.

      Returns Sequence<T>

    • Returns a new sequence with elements sorted in descending order using natural ordering. Materializes the sequence.

      Returns Sequence<T>

    • Returns a new sequence sorted in descending order by the key returned by selector. Materializes the sequence.

      Type Parameters

      • R

        The key type.

      Parameters

      • selector: (value: T) => R

        Extracts a comparable sort key from each element.

      Returns Sequence<T>

    • Returns a new sequence sorted using the given comparator. Materializes the sequence.

      Parameters

      • comparator: (a: T, b: T) => number

        Standard comparator: negative when a < b, positive when a > b.

      Returns Sequence<T>

    • Returns the sum of all elements in a numeric sequence. Returns 0 for an empty sequence.

      Parameters

      Returns number

    • Returns the sum of the values returned by selector for each element. Returns 0 for an empty sequence.

      Parameters

      • selector: (value: T) => number

        Extracts a numeric value from each element.

      Returns number

    • Returns a sequence containing the first num elements.

      Parameters

      • num: number = 1

        Number of elements to take. Defaults to 1.

      Returns Sequence<T>

    • Returns a sequence of elements taken from the start while the predicate holds. Stops at the first element that does not satisfy the predicate.

      Parameters

      • predicate: (value: T) => boolean

        Called with each element.

      Returns Sequence<T>

    • Parameters

      • Optionale: any

      Returns IteratorResult<T, undefined>

    • Creates a new array from the values yielded by this iterator.

      Returns T[]

    • Collects a sequence of [key, value] pairs into a Map.

      Type Parameters

      • K

        The key type.

      • V

        The value type.

      Parameters

      Returns Map<K, V>

    • Collects a sequence of [key, value] pairs into a plain object.

      Type Parameters

      • E extends [PropertyKey, unknown]

      Parameters

      Returns FromEntries<E>

      entriesOf({ name: 'yoda' })
      .mapKeys(k => k.toUpperCase())
      .mapValues(v => v.toUpperCase())
      .toObject(); // { NAME: 'YODA' }
    • Collects a sequence of [key, value] pairs into a plain object.

      Type Parameters

      • K extends PropertyKey

        The key type (must be a valid property key).

      • V

        The value type.

      Parameters

      Returns Record<K, V>

      entriesOf({ name: 'yoda' })
      .mapKeys(k => k.toUpperCase())
      .mapValues(v => v.toUpperCase())
      .toObject(); // { NAME: 'YODA' }
    • Collects all elements into a Set.

      Parameters

      • Optionalset: Set<T>

        An existing Set to add elements to. A new Set is created when omitted.

      Returns Set<T>

    • Unzips a sequence of [A, B] pairs into a tuple of two arrays [A[], B[]].

      Type Parameters

      • A

        The first element type of each pair.

      • B

        The second element type of each pair.

      Parameters

      Returns [A[], B[]]

    • Returns a sequence of sliding windows of the given size.

      Parameters

      • size: number

        The number of elements in each window. Must be ≥ 1.

      • step: number = 1

        How many elements to advance the window each time. Defaults to 1.

      • partialWindows: boolean = false

        When true, windows smaller than size at the end are included. Defaults to false.

      Returns Sequence<T[]>

      If size or step is less than 1.

      sequenceOf(1, 2, 3, 4).windowed(2).toArray(); // [[1, 2], [2, 3], [3, 4]]
      
    • Returns a sequence of { index, value } pairs, where index is the zero-based position of each element.

      Returns Sequence<{ index: number; value: T }>

      sequenceOf('a', 'b', 'c').withIndex().toArray();
      // [{ index: 0, value: 'a' }, { index: 1, value: 'b' }, { index: 2, value: 'c' }]
    • Returns a sequence of pairs built by combining the elements of this sequence with elements from other. Stops when the shorter sequence is exhausted. An optional transform function can produce a different type from each pair.

      Type Parameters

      • S

        The element type of other.

      Parameters

      • other: Iterable<S>

        The iterable to zip with.

      Returns Sequence<[T, S]>

    • Returns a sequence of pairs built by combining the elements of this sequence with elements from other. Stops when the shorter sequence is exhausted. An optional transform function can produce a different type from each pair.

      Type Parameters

      • S

        The element type of other.

      • R

        The result type when transform is provided.

      Parameters

      • other: Iterable<S>

        The iterable to zip with.

      • transform: (a: T, b: S) => R

      Returns Sequence<R>

    • Returns a sequence of pairs of each consecutive element with the next one. An optional transform function can produce a different type from each pair. The returned sequence has one fewer element than the original.

      Returns Sequence<[T, T]>

      sequenceOf(1, 2, 3).zipWithNext().toArray(); // [[1, 2], [2, 3]]
      
    • Returns a sequence of pairs of each consecutive element with the next one. An optional transform function can produce a different type from each pair. The returned sequence has one fewer element than the original.

      Type Parameters

      • R

        The result type when transform is provided.

      Parameters

      • transform: (a: T, b: T) => R

      Returns Sequence<R>

      sequenceOf(1, 2, 3).zipWithNext().toArray(); // [[1, 2], [2, 3]]
      
    • Creates a native iterator from an iterator or iterable object. Returns its input if the input already inherits from the built-in Iterator class.

      Type Parameters

      • T

      Parameters

      • value: Iterator<T, unknown, undefined> | Iterable<T, unknown, undefined>

        An iterator or iterable object to convert a native iterator.

      Returns IteratorObject<T, undefined, unknown>