Options
All
  • Public
  • Public/Protected
  • All
Menu

Class IterableWrapper<T>

Wraps IterableIterator<T> to provide additional functionality. There may be performance benefits when you chain multiple calls together since the wrapped Array is not reconstructed.
Do not modify the wrapped collection as long as you are working with its wrapper object.

Type parameters

  • T

Hierarchy

  • IterableWrapper

Implements

  • Iterable<T>

Index

Constructors

constructor

  • new IterableWrapper<T>(openIteratorFn: () => T[] | IterableIterator<T>): IterableWrapper<T>
  • Creates a new instance.
    There is usually no need to create an instance directly. Prefer shortcut function iter to wrap an Array or other object supporting the iterable contract.

    Type parameters

    • T

    Parameters

    • openIteratorFn: () => T[] | IterableIterator<T>

      Functions that returns an Array or other ES 2015 iterable. The function is called lazily on each demand to start the iteration.

        • (): T[] | IterableIterator<T>
        • Returns T[] | IterableIterator<T>

    Returns IterableWrapper<T>

Methods

[iterator]

  • [iterator](): IterableIterator<T>
  • Iterator wrapped into this instance. Allows enumeration by using the for-of syntax.

    Returns IterableIterator<T>

concat

  • Similar to Array.concat.
    Concatenates another iterable after this one and returns a new IterableWrapper<T> that goes through both collections.

    Parameters

    • another: Iterable<T>

      Another iterable such as this object or a plain Array.

    Returns IterableWrapper<T>

    A new iterable containing elements from both collections.

contains

  • contains(item: T): boolean
  • Checks whether an item is present in the collection.

    Parameters

    • item: T

      Item to be found. Equality operator === is applied to compare with content of this collection.

    Returns boolean

    true if found. Otherwise false.

distinct

  • Creates antoher iterable with distinct elements contained in the wrapped collection (removes duplicates).

    Parameters

    • Optional keyMapper: (item: T) => any

      A function that produces key used to distinguish among elements. Elements with duplicate keys are considered as duplicates. If not specified, the function uses comparison by equality operator ===.

        • (item: T): any
        • Parameters

          • item: T

          Returns any

    Returns IterableWrapper<T>

    Another iterable based on this one without duplicate elements.

every

  • every(predicate: (item: T) => boolean): boolean
  • Similar to Array.every.
    Evaluates whether all elements meet a condition.

    Parameters

    • predicate: (item: T) => boolean

      Predicate that has to return true when passed element meets the condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

    true if all elements are accepted or the sequence is empty. false if one or more elements do not pass the given predicate.

except

  • Subtracts another collection from this one (performs set minus operation).
    Creates new IterableWrapper<T> which contains all source elements except elements in another collection.

    Parameters

    • anotherCollection: IterableWrapper<T> | T[]

      Another IterableWrapper<T> or Array<T>.

    • Optional equalsFn: (a: T, b: T) => boolean

      Function that compares two elements. If ommitted, comparison will use equality operator ===.

        • (a: T, b: T): boolean
        • Parameters

          • a: T
          • b: T

          Returns boolean

    Returns IterableWrapper<T>

    Copy of this iterable sequence wihout elements found in another collection.

filter

  • filter(predicate: (item: T, index: number) => boolean): IterableWrapper<T>
  • Similar to Array.filter.
    Creates a new IterableWrapper<T> on top of this one that filters elements using a predicate.

    Parameters

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

      Predicate that says whether to accept or reject an element. Predicate is supposed to return true to accept and false to reject it.

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

          • item: T
          • index: number

          Returns boolean

    Returns IterableWrapper<T>

    New IterableWrapper<T> with filtered content.

find

  • find(predicate: (item: T) => boolean): undefined | T
  • Similar to Array.find.
    Finds the first element that matches provided predicate.

    Parameters

    • predicate: (item: T) => boolean

      Predicate that has to return true when passed element is the desired one.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns undefined | T

    The found element or undefined if none is found.

findIndex

  • findIndex(predicate: (item: T) => boolean): number
  • Similar to Array.findIndex.
    Finds the first element that matches provided predicate and returns its index. Returns -1 if not found.

    Parameters

    • predicate: (item: T) => boolean

      Predicate that has to return true when passed element is the desired one.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    Zero-based index of the found element or -1 if it's not found.

flatMap

  • flatMap<TItem>(nestedAccessor: (item: T) => TItem[]): IterableWrapper<TItem>
  • Accesses nested collections as a single iterable. Provide a mapper function that is supposed to return the nested Array contained within the element. This function makes a new IterableWrapper<TChildItem> over all nested elements.

    Type parameters

    • TItem

    Parameters

    • nestedAccessor: (item: T) => TItem[]

      Returns nested collection for each element.

        • (item: T): TItem[]
        • Parameters

          • item: T

          Returns TItem[]

    Returns IterableWrapper<TItem>

    Iterable<TChildItem> over all items in nested collections.

flatten

  • flatten(nestedAccessor?: (item: T, level: number) => undefined | T[] | IterableIterator<T>): IterableWrapper<T>
  • Flattens a tree-like structure.
    Creates an IterableWrapper<T> that recursivelly walks through all elements.

    Parameters

    • Optional nestedAccessor: (item: T, level: number) => undefined | T[] | IterableIterator<T>

      Function that is supposed to return nested collection. Each element in the nested collection may contain another nested collection which will be processed in the same way.
      If the function returns undefined, it means that the gived element has no nested items or you want to skip them.

        • (item: T, level: number): undefined | T[] | IterableIterator<T>
        • Parameters

          • item: T
          • level: number

          Returns undefined | T[] | IterableIterator<T>

    Returns IterableWrapper<T>

    IterableWrapper<T> over the full tree-like structure.

flattenAndMap

  • flattenAndMap<TResult>(nestedAccessor: (item: T, level: number) => undefined | T[] | IterableIterator<T>, mapper: (item: T, level: number) => TResult): IterableWrapper<TResult>
  • Flattens a tree-like structure and creates new elements based on the source.
    Creates an IterableWrapper<T> that recursivelly walks through all elements. Additionally maps each element passing it to a mapper function.

    Type parameters

    • TResult

    Parameters

    • nestedAccessor: (item: T, level: number) => undefined | T[] | IterableIterator<T>

      Function that is supposed to return nested collection. Each element in the nested collection may contain another nested collection which will be processed in the same way.
      If the function returns undefined, it means that the gived element has no nested items or you want to skip them.

        • (item: T, level: number): undefined | T[] | IterableIterator<T>
        • Parameters

          • item: T
          • level: number

          Returns undefined | T[] | IterableIterator<T>

    • mapper: (item: T, level: number) => TResult

      Function that converts each item. Called lazily on demand.

        • (item: T, level: number): TResult
        • Parameters

          • item: T
          • level: number

          Returns TResult

    Returns IterableWrapper<TResult>

    IterableWrapper<T> over the full tree-like structure.

forEach

  • forEach(action: (item: T, index: number) => void): void
  • Similar to Array.forEach.
    Executes a function on each element in the iterable sequence.

    Parameters

    • action: (item: T, index: number) => void

      Function executed on each element.

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

          • item: T
          • index: number

          Returns void

    Returns void

getAt

  • getAt(index: number): T
  • Retrieves an element at given index.

    see

    tryGetAt

    Parameters

    • index: number

      0-based index

    Returns T

    Element at the index. Throws an error if the index is out of range.

groupBy

  • Groups content of the collection into another IterableWrapper. Each item is a group object with key and items.
    key is the value generated by given keyMapper functions. Equality === is used to determine what keys are the same. items is array of elements belonging to the group distinguished by key.

    see

    toMap

    Type parameters

    • TKey

    Parameters

    • keyMapper: (item: T) => TKey

      Makes a key for each element in the collection.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    Returns IterableWrapper<{ items: IterableWrapper<T>; key: TKey }>

    Collection of groups with key and items associated with the key.

head

  • head(): T
  • Returns the first element in the iterable sequence. Throws an error in case the iterable is empty.

    see

    tryGetHead

    Returns T

    The first element.

intersect

  • Returns new IterableWrapper<T> which is contains intersection of this instance and another one or an Array.

    Parameters

    • anotherCollection: IterableWrapper<T> | T[]

      Another IterableWrapper<T> or Array<T>.

    • Optional equalsFn: (a: T, b: T) => boolean

      Function that compares two elements. If ommitted, comparison will use equality operator ===.

        • (a: T, b: T): boolean
        • Parameters

          • a: T
          • b: T

          Returns boolean

    Returns IterableWrapper<T>

    IterableWrapper<T> containing intersection of these iterables or arrays.

isEmpty

  • isEmpty(): boolean
  • Determines whether the iterable is empty.

    Returns boolean

    true if there are no elements in the wrapped iterable.

Private iterate

  • iterate(): IterableIterator<T>

last

  • last(): T
  • Returns the last element in the iterable sequence. Returns undefined in case the iterable is empty.
    Optimized if the underlying iterable is an Array.

    see

    tryGetLast

    Returns T

    The last element or undefined.

length

  • length(): number
  • Calculates lenght of the underlying iterable.
    Optimized if the underlying iterable is an Array.

    Returns number

    Number of elements in the iterable.

map

  • map<TResult>(mapper: (item: T, index: number) => TResult): IterableWrapper<TResult>
  • Similar to Array.map.
    Creates a new IterableWrapper<T> on top of this one. All elements are passed to a mapper function which is supposed to convert them and/or create another object based on the original.

    Type parameters

    • TResult

    Parameters

    • mapper: (item: T, index: number) => TResult

      Function that converts each item. Called lazily on demand.

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

          • item: T
          • index: number

          Returns TResult

    Returns IterableWrapper<TResult>

    New IterableWrapper<T> with mapped content.

max

  • max(mapper: (item: T) => number): undefined | number
  • Finds maximum using a mapper function that produces a number for each element.

    Parameters

    • mapper: (item: T) => number

      Function that returns a number for each element.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns undefined | number

    The biggest of all numbers returned by the mapper function. Undefined if iterable sequence is empty.

min

  • min(mapper: (item: T) => number): undefined | number
  • Finds minimum using a mapper function that produces a number for each element.

    Parameters

    • mapper: (item: T) => number

      Function that returns a number for each element.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns undefined | number

    The smalllest of all numbers returned by the mapper function. Undefined if iterable sequence is empty.

reduce

  • reduce<TResult>(aggregator: (acc: TResult, item: T) => TResult, initialValue: TResult): TResult
  • The same functionality as Array.reduce.
    Passes all elements into a function to produce a single-value result.

    Type parameters

    • TResult

    Parameters

    • aggregator: (acc: TResult, item: T) => TResult
        • (acc: TResult, item: T): TResult
        • Parameters

          • acc: TResult
          • item: T

          Returns TResult

    • initialValue: TResult

    Returns TResult

    Aggregated value produced by the aggregator function.

reverse

  • Similar to Array,reverse.
    Creates a reversed iterable sequence based on this one.
    Optimized if the input iterable is an Array. Constructs a temporary Array in case it's other kind of iterable.

    Returns IterableWrapper<T>

    Reversed iterable IterableWrapper<T>

sequenceEquals

  • sequenceEquals(anotherCollection: Iterable<T>, equalsFn?: (a: T, b: T) => boolean): boolean
  • Checks whether two collections are the same. I.e. they have the same length and elements at equal positions.

    Parameters

    • anotherCollection: Iterable<T>

      Another iterable collectio

    • Optional equalsFn: (a: T, b: T) => boolean

      Equality check function. If omitted, equals operator === is applied.

        • (a: T, b: T): boolean
        • Parameters

          • a: T
          • b: T

          Returns boolean

    Returns boolean

skip

  • Skips a given number of elements in the collection.
    Optimized if the underlying collection is an Array.

    Parameters

    • numElements: number

      Number of skipped elements.

    Returns IterableWrapper<T>

    Another IterableWrapper<T> based on this one without the first N elements.

some

  • some(predicate: (item: T) => boolean): boolean
  • Similar to Array.some.
    Evaluates whether at least one element is accepted by provided predicate.

    Parameters

    • predicate: (item: T) => boolean

      Predicate that has to return true to accept the passed element.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

    true if at least one element is accepted. false if none is accepted or the sequence is empty.

sort

  • Similar to Array.sort.
    Out-of-place sort function. Sorts the collection and returns a new IteratorWrapper<T> over ordered results. Beware that this function constructs a temporary Array that might affect performance negatively.

    Parameters

    • Optional compareFn: (a: T, b: T) => number

      Comparison function (same as Array.sort comparison function). It has to return negative value if a < b, 0 if a === b, positive value if a > b.

        • (a: T, b: T): number
        • Parameters

          • a: T
          • b: T

          Returns number

    Returns IterableWrapper<T>

    Sorted sequence.

sum

  • sum(mapper: (item: T) => number): number
  • Calculates sum using a mapper funcation that produces a number for each element.

    Parameters

    • mapper: (item: T) => number

      Function that returns a number for each element.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

    Sum of all numbers returned by mapper function.

take

  • Takes at most N elements and then stops the iteration.

    Parameters

    • maxElements: number

      Maximum number of elements.

    Returns IterableWrapper<T>

    Another IterableWrapper<T> limited to maximum of N elements.

takeWhile

  • Returns elements in the collection as long as a condition is met. Stops iteration when given predicate returns false for the first time.

    Parameters

    • predicate: (item: T) => boolean

      Predicate that accepts an element and return true to continue / false to stop.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns IterableWrapper<T>

    New iterable that is limited by given condition.

toArray

  • toArray(): T[]
  • Constructs a new Array out of the iterable collection.

    Returns T[]

toMap

  • toMap<TKey>(keyMapper: (item: T) => TKey): Map<TKey, T[]>
  • Constructs a new Map<TKey, T[]> (built-in JavaScript object) out of all elements in the iterable collection.

    Type parameters

    • TKey

    Parameters

    • keyMapper: (item: T) => TKey

      Function that produces a key for each individual element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    Returns Map<TKey, T[]>

    A new Map<TKey, T[]>.

toReadonlyArray

  • toReadonlyArray(): readonly T[]
  • Constructs a new ReadonlyArray out of the iterable collection.
    Note that ReadonlyArray<T> is TypeScript-specific interface. No such object exists in JS. It's an Array protected from writing by the TS compiler.

    Returns readonly T[]

toReadonlyMap

  • toReadonlyMap<TKey>(keyMapper: (item: T) => TKey): ReadonlyMap<TKey, T[]>
  • Constructs a new ReadonlyMap<TKey, T[]> out of all elements in the iterable collection.

    Type parameters

    • TKey

    Parameters

    • keyMapper: (item: T) => TKey

      Function that produces a key for each individual element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    Returns ReadonlyMap<TKey, T[]>

    A new ReadonlyMap<TKey, T[]>. ReadonlyMap is TypeScript-specific version of Map protected from writing.

toReadonlySet

  • toReadonlySet(): ReadonlySet<T>
  • Constructs a ReadonlySet<T> out of the iterable collection. Duplicated elements are removed. Note that ReadonlySet<T> is TypeScript-specific interface. No such object exists in JS. It's a Set<T> protected from writing by TS compiler.

    Returns ReadonlySet<T>

    ReadonlySet<T> of unique elements in the iterable sequence.

toSeparatedString

  • toSeparatedString(separator?: string, convert?: (item: T) => undefined | string): string
  • Converts all elements into strings and joins them together into single string separated by coma or other separator.

    Parameters

    • separator: string = ', '

      Separator among elements

    • Optional convert: (item: T) => undefined | string

      Function that converts element to text. The function may return undefined to skip an element. If ommitted x.toString() is applied.

        • (item: T): undefined | string
        • Parameters

          • item: T

          Returns undefined | string

    Returns string

toSet

  • toSet(): Set<T>
  • Creates Set<T> (built-in JavaScript object) out of the iterable sequence. Duplicated elements are removed.

    Returns Set<T>

    Set<T> of unique elements in the iterable sequence.

tryGetAt

  • tryGetAt(index: number): undefined | T
  • Attempts to retrieve an element at given index.

    Parameters

    • index: number

      0-based index

    Returns undefined | T

    Element at the index or undefined if the index is out of range.

tryGetHead

  • tryGetHead(): undefined | T
  • Returns the first element in the iterable sequence. Throws an error in case the iterable is empty.

    see

    head

    Returns undefined | T

    The last element.

tryGetLast

  • tryGetLast(): undefined | T
  • Returns the last element in the iterable sequence. Returns undefined in case the iterable is empty.
    Optimized if the underlying iterable is an Array.

    see

    last

    Returns undefined | T

    The last element or undefined.

Generated using TypeDoc