Path: blob/trunk/third_party/closure/goog/iter/es6.js
4083 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Shims between goog.iter.Iterator and ES6 iterator.8*/910goog.module('goog.iter.es6');11goog.module.declareLegacyNamespace();1213const GoogIterable = goog.require('goog.iter.Iterable');14const GoogIterator = goog.require('goog.iter.Iterator');151617/**18* Common interface extending both `goog.iter.Iterable` and ES6 `Iterable`,19* and providing `toGoog()` and `toEs6()` methods to get either kind20* of iterator. `ShimIterable.of()` is the primary entry point for21* this library. If it is given an iterable that is *not* also an22* iterator, then it will inherit any reusability from its argument23* (i.e. `ShimIterable.of(mySet)` will be reusable, since mySet makes24* a fresh Iterator every time, whereas `ShimIterable.of(myIterator)`25* will be one-shot).26*27* `ShimGoogIterator` and `ShimEs6Iterator` extend `ShimIterable` and28* also implement one or the other iterator API. Since they extend29* `ShimIterable`, it is easy to convert back and forth between the two30* APIs. Any such conversion will expose a view to the same underlying31* iterator, so elements pulled via one API will not be available from32* the other.33*34* @interface35* @extends {Iterable<VALUE>}36* @template VALUE37*/38class ShimIterable {39/** @return {!GoogIterator<VALUE>} */40__iterator__() {}4142/** @return {!ShimGoogIterator<VALUE>} */43toGoog() {}4445/** @return {!ShimEs6Iterator<VALUE>} */46toEs6() {}4748/**49* @param {!Iterable<VALUE>|!Iterator<VALUE>|50* !GoogIterator<VALUE>|!GoogIterable} iter51* @return {!ShimIterable}52* @template VALUE53*/54static of(iter) {55if (iter instanceof ShimIterableImpl || iter instanceof ShimGoogIterator ||56iter instanceof ShimEs6Iterator) {57return iter;58} else if (typeof iter.next == 'function') {59return new ShimIterableImpl(60() => /** @type {!Iterator|!GoogIterator} */ (iter));61} else if (typeof iter[Symbol.iterator] == 'function') {62return new ShimIterableImpl(() => iter[Symbol.iterator]());63} else if (typeof iter.__iterator__ == 'function') {64return new ShimIterableImpl(65() => /** @type {{__iterator__:function(this:?, boolean=)}} */ (iter)66.__iterator__());67}68throw new Error('Not an iterator or iterable.');69}70}717273/**74* Concrete (private) implementation of a non-iterator iterable. This is75* separate from the iterator versions since it supports iterables that76* are not "one-shot".77* @implements {ShimIterable<VALUE>}78* @template VALUE79*/80class ShimIterableImpl {81/** @param {function(): !Iterator<VALUE>} func */82constructor(func) {83/** @const @private */84this.func_ = func;85}8687/** @override */88__iterator__() {89return new ShimGoogIterator(this.func_());90}9192/** @override */93toGoog() {94return new ShimGoogIterator(this.func_());95}9697/** @override */98[Symbol.iterator]() {99return new ShimEs6Iterator(this.func_());100}101102/** @override */103toEs6() {104return new ShimEs6Iterator(this.func_());105}106}107108109/**110* Concrete `goog.iter.Iterator` subclass that also implements `ShimIterable`.111* @extends {GoogIterator<VALUE>}112* @implements {ShimIterable<VALUE>}113* @template VALUE114*/115class ShimGoogIterator extends GoogIterator {116/** @param {!Iterator<VALUE>} iter */117constructor(iter) {118super();119this.iter_ = iter;120}121122/**123* @override @see {!goog.iter.Iterator}124* @return {!IIterableResult<VALUE>}125*/126next() {127return this.iter_.next();128}129130131/** @override */132toGoog() {133return this;134}135136/** @override */137[Symbol.iterator]() {138return new ShimEs6Iterator(this.iter_);139}140141/** @override */142toEs6() {143return new ShimEs6Iterator(this.iter_);144}145}146147148/**149* Concrete ES6 `Iterator` that also implements `ShimIterable`.150* @implements {IteratorIterable<VALUE>}151* @extends {ShimIterableImpl<VALUE>}152* @template VALUE153*/154class ShimEs6Iterator extends ShimIterableImpl {155/** @param {!Iterator<VALUE>} iter */156constructor(iter) {157super(() => iter);158/** @const @private */159this.iter_ = iter;160}161162/** @override */163next() {164return this.iter_.next();165}166}167168169exports = {170ShimIterable,171ShimEs6Iterator,172ShimGoogIterator,173};174175176