Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/iter/es6.js
4083 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Shims between goog.iter.Iterator and ES6 iterator.
9
*/
10
11
goog.module('goog.iter.es6');
12
goog.module.declareLegacyNamespace();
13
14
const GoogIterable = goog.require('goog.iter.Iterable');
15
const GoogIterator = goog.require('goog.iter.Iterator');
16
17
18
/**
19
* Common interface extending both `goog.iter.Iterable` and ES6 `Iterable`,
20
* and providing `toGoog()` and `toEs6()` methods to get either kind
21
* of iterator. `ShimIterable.of()` is the primary entry point for
22
* this library. If it is given an iterable that is *not* also an
23
* iterator, then it will inherit any reusability from its argument
24
* (i.e. `ShimIterable.of(mySet)` will be reusable, since mySet makes
25
* a fresh Iterator every time, whereas `ShimIterable.of(myIterator)`
26
* will be one-shot).
27
*
28
* `ShimGoogIterator` and `ShimEs6Iterator` extend `ShimIterable` and
29
* also implement one or the other iterator API. Since they extend
30
* `ShimIterable`, it is easy to convert back and forth between the two
31
* APIs. Any such conversion will expose a view to the same underlying
32
* iterator, so elements pulled via one API will not be available from
33
* the other.
34
*
35
* @interface
36
* @extends {Iterable<VALUE>}
37
* @template VALUE
38
*/
39
class ShimIterable {
40
/** @return {!GoogIterator<VALUE>} */
41
__iterator__() {}
42
43
/** @return {!ShimGoogIterator<VALUE>} */
44
toGoog() {}
45
46
/** @return {!ShimEs6Iterator<VALUE>} */
47
toEs6() {}
48
49
/**
50
* @param {!Iterable<VALUE>|!Iterator<VALUE>|
51
* !GoogIterator<VALUE>|!GoogIterable} iter
52
* @return {!ShimIterable}
53
* @template VALUE
54
*/
55
static of(iter) {
56
if (iter instanceof ShimIterableImpl || iter instanceof ShimGoogIterator ||
57
iter instanceof ShimEs6Iterator) {
58
return iter;
59
} else if (typeof iter.next == 'function') {
60
return new ShimIterableImpl(
61
() => /** @type {!Iterator|!GoogIterator} */ (iter));
62
} else if (typeof iter[Symbol.iterator] == 'function') {
63
return new ShimIterableImpl(() => iter[Symbol.iterator]());
64
} else if (typeof iter.__iterator__ == 'function') {
65
return new ShimIterableImpl(
66
() => /** @type {{__iterator__:function(this:?, boolean=)}} */ (iter)
67
.__iterator__());
68
}
69
throw new Error('Not an iterator or iterable.');
70
}
71
}
72
73
74
/**
75
* Concrete (private) implementation of a non-iterator iterable. This is
76
* separate from the iterator versions since it supports iterables that
77
* are not "one-shot".
78
* @implements {ShimIterable<VALUE>}
79
* @template VALUE
80
*/
81
class ShimIterableImpl {
82
/** @param {function(): !Iterator<VALUE>} func */
83
constructor(func) {
84
/** @const @private */
85
this.func_ = func;
86
}
87
88
/** @override */
89
__iterator__() {
90
return new ShimGoogIterator(this.func_());
91
}
92
93
/** @override */
94
toGoog() {
95
return new ShimGoogIterator(this.func_());
96
}
97
98
/** @override */
99
[Symbol.iterator]() {
100
return new ShimEs6Iterator(this.func_());
101
}
102
103
/** @override */
104
toEs6() {
105
return new ShimEs6Iterator(this.func_());
106
}
107
}
108
109
110
/**
111
* Concrete `goog.iter.Iterator` subclass that also implements `ShimIterable`.
112
* @extends {GoogIterator<VALUE>}
113
* @implements {ShimIterable<VALUE>}
114
* @template VALUE
115
*/
116
class ShimGoogIterator extends GoogIterator {
117
/** @param {!Iterator<VALUE>} iter */
118
constructor(iter) {
119
super();
120
this.iter_ = iter;
121
}
122
123
/**
124
* @override @see {!goog.iter.Iterator}
125
* @return {!IIterableResult<VALUE>}
126
*/
127
next() {
128
return this.iter_.next();
129
}
130
131
132
/** @override */
133
toGoog() {
134
return this;
135
}
136
137
/** @override */
138
[Symbol.iterator]() {
139
return new ShimEs6Iterator(this.iter_);
140
}
141
142
/** @override */
143
toEs6() {
144
return new ShimEs6Iterator(this.iter_);
145
}
146
}
147
148
149
/**
150
* Concrete ES6 `Iterator` that also implements `ShimIterable`.
151
* @implements {IteratorIterable<VALUE>}
152
* @extends {ShimIterableImpl<VALUE>}
153
* @template VALUE
154
*/
155
class ShimEs6Iterator extends ShimIterableImpl {
156
/** @param {!Iterator<VALUE>} iter */
157
constructor(iter) {
158
super(() => iter);
159
/** @const @private */
160
this.iter_ = iter;
161
}
162
163
/** @override */
164
next() {
165
return this.iter_.next();
166
}
167
}
168
169
170
exports = {
171
ShimIterable,
172
ShimEs6Iterator,
173
ShimGoogIterator,
174
};
175
176