@nevware21/ts-utils
    Preparing search index...

    Function createIterableIterator

    • Create an iterable iterator which conforms to both the Iterator and Iterable protocols, it uses the provided ctx to manage moving to the next value and can be used directly in for...of loops or consumed via .next() calls.

      The returned object satisfies both protocols: its [Symbol.iterator]() method returns itself, making it usable anywhere an Iterable is expected, while also exposing the next() method of the Iterator protocol.

      Type Parameters

      • T

        Identifies the type that will be returned by the iterator

      Parameters

      Returns IterableIterator<T>

      A new IterableIterator instance

      0.14.0

      let idx = -1;
      let theValues = [ 5, 10, 15, 20, 25, 30 ];

      let theIterator = createIterableIterator<number>({
      n: function() {
      idx++;
      let isDone = idx >= theValues.length;
      if (!isDone) {
      this.v = theValues[idx];
      }
      return isDone;
      }
      });

      // Can be consumed as an iterator
      theIterator.next(); // { value: 5, done: false }

      // Or used in a for...of loop (Iterable protocol)
      let values: number[] = [];
      for (const value of theIterator) {
      values.push(value);
      }
      // Values: [10, 15, 20, 25, 30]