es-stl - v3.0.2
    Preparing search index...

    Class Collection<V, K>Abstract

    Base class for iterable collection data structures.

    Subclasses provide storage and traversal by implementing clear and traverse. The inherited iterator method caches one forward iterator and one reverse iterator for reuse.

    class Values extends Collection<string> {
    constructor(private readonly values: string[]) {
    super();
    this._size = values.length;
    }

    clear(): void {
    this.values.length = 0;
    this._size = 0;
    }

    traverse(
    breakFlag: boolean,
    reverse: boolean,
    cb: IterateCallback<string, number>
    ): boolean {
    const values = reverse ? [...this.values].reverse() : this.values;

    for (let i = 0; i < values.length; i += 1) {
    if (cb(values[i], i, i, values.length) === breakFlag) {
    return breakFlag;
    }
    }

    return !breakFlag;
    }
    }

    Type Parameters

    • V

      Value type yielded by the collection.

    • K = number

      Key type yielded with each value.

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    • Type Parameters

      • V

        Value type yielded by the collection.

      • K = number

        Key type yielded with each value.

      Returns Collection<V, K>

    Properties

    _iterators: [Nullable<Iterator<V, K>>, Nullable<Iterator<V, K>>] = ...
    _size: number = 0

    Accessors

    Methods

    • Checks whether the collection contains no values.

      Returns boolean

      true when the collection is empty.

    • Creates or reuses an iterator bound to this collection.

      Parameters

      • Optionalreverse: boolean

        Whether iteration should run in reverse order.

      Returns Iterator<V, K>

      An iterator bound to this collection.

      Calling iterator() repeatedly returns the same forward iterator instance. Calling iterator(true) repeatedly returns the same reverse iterator instance.

      const dict = new Dictionary({ a: 1, b: 2 });

      dict.iterator().map((value) => value * 2);
      // => [2, 4]
    • Visits collection values in implementation-defined key order.

      Parameters

      • breakFlag: boolean

        Callback result that should stop traversal early.

      • reverse: boolean

        Whether traversal should run in reverse order.

      • cb: IterateCallback<V, K>

        Callback invoked for each value.

      Returns boolean

      breakFlag when traversal stops early, otherwise !breakFlag.

      Iterator helpers use this method to implement every, some, forEach, map, filter, find, findIndex, and reduce. Implementations should return breakFlag as soon as the callback returns the same value.