1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const concat_1 = require("./concat");
|
4 | const filter_1 = require("./filter");
|
5 | const flatten_1 = require("./flatten");
|
6 | const map_1 = require("./map");
|
7 | const slice_1 = require("./slice");
|
8 | const utils_1 = require("./utils");
|
9 | const zip_1 = require("./zip");
|
10 | class IteratorWithOperators {
|
11 | |
12 |
|
13 |
|
14 | constructor(source) {
|
15 | this.source = source;
|
16 | }
|
17 | |
18 |
|
19 |
|
20 | next() {
|
21 | return this.source.next();
|
22 | }
|
23 | |
24 |
|
25 |
|
26 |
|
27 | [Symbol.iterator]() {
|
28 | return this;
|
29 | }
|
30 | |
31 |
|
32 |
|
33 | map(iteratee) {
|
34 | return new IteratorWithOperators(new map_1.MapIterator(this.source, iteratee));
|
35 | }
|
36 | filter(predicate) {
|
37 | return new IteratorWithOperators(new filter_1.FilterIterator(this.source, predicate));
|
38 | }
|
39 | |
40 |
|
41 |
|
42 | concat(collection) {
|
43 | return new IteratorWithOperators(new concat_1.ConcatIterator([this.source, utils_1.toIterator(collection)]));
|
44 | }
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | take(limit) {
|
51 | return new IteratorWithOperators(new slice_1.SliceIterator(this.source, 0, limit + 1));
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 | drop(n) {
|
59 | return new IteratorWithOperators(new slice_1.SliceIterator(this.source, n, Infinity));
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | slice(start, end = Infinity) {
|
68 | return new IteratorWithOperators(new slice_1.SliceIterator(this.source, start, end));
|
69 | }
|
70 | |
71 |
|
72 |
|
73 | flatten() {
|
74 | return new IteratorWithOperators(new flatten_1.FlattenIterator(this.source));
|
75 | }
|
76 | reduce(iteratee, accumulator) {
|
77 | let result;
|
78 | if (accumulator === undefined) {
|
79 | result = this.source.next();
|
80 | if (result.done) {
|
81 | throw new TypeError('Reduce of empty Iterator with no initial value');
|
82 | }
|
83 | accumulator = result.value;
|
84 | }
|
85 | while (true) {
|
86 | result = this.source.next();
|
87 | if (result.done) {
|
88 | break;
|
89 | }
|
90 | accumulator = iteratee(accumulator, result.value);
|
91 | }
|
92 | return accumulator;
|
93 | }
|
94 | find(predicate) {
|
95 | let result;
|
96 | while (true) {
|
97 | result = this.source.next();
|
98 | if (result.done) {
|
99 | return undefined;
|
100 | }
|
101 | if (predicate(result.value)) {
|
102 | return result.value;
|
103 | }
|
104 | }
|
105 | }
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 | includes(value) {
|
112 | let result;
|
113 | do {
|
114 | result = this.source.next();
|
115 | if (!result.done && result.value === value) {
|
116 | return true;
|
117 | }
|
118 | } while (!result.done);
|
119 | return false;
|
120 | }
|
121 | |
122 |
|
123 |
|
124 | some(predicate) {
|
125 | let result;
|
126 | do {
|
127 | result = this.source.next();
|
128 | if (!result.done && predicate(result.value)) {
|
129 | return true;
|
130 | }
|
131 | } while (!result.done);
|
132 | return false;
|
133 | }
|
134 | |
135 |
|
136 |
|
137 | every(predicate) {
|
138 | let result;
|
139 | do {
|
140 | result = this.source.next();
|
141 | if (!result.done && !predicate(result.value)) {
|
142 | return false;
|
143 | }
|
144 | } while (!result.done);
|
145 | return true;
|
146 | }
|
147 | |
148 |
|
149 |
|
150 | forEach(iteratee) {
|
151 | let result;
|
152 | while (true) {
|
153 | result = this.source.next();
|
154 | if (result.done) {
|
155 | break;
|
156 | }
|
157 | iteratee(result.value);
|
158 | }
|
159 | }
|
160 | |
161 |
|
162 |
|
163 | join(separator = ',') {
|
164 | let joined = '';
|
165 | let result;
|
166 | while (true) {
|
167 | result = this.source.next();
|
168 | if (result.done) {
|
169 | break;
|
170 | }
|
171 | joined += separator + result.value;
|
172 | }
|
173 | return joined.substr(separator.length);
|
174 | }
|
175 | |
176 |
|
177 |
|
178 |
|
179 | toArray() {
|
180 | return Array.from(this);
|
181 | }
|
182 | |
183 |
|
184 |
|
185 |
|
186 | toSet() {
|
187 | const set = new Set();
|
188 | while (true) {
|
189 | const { value, done } = this.next();
|
190 | if (done) {
|
191 | return set;
|
192 | }
|
193 | set.add(value);
|
194 | }
|
195 | }
|
196 | /**
|
197 | * Iterates and returns all `[key, value]` paris emitted by the Iterator as an ES6 Map.
|
198 | * Equivalent to passing the Iterator to `new Map()`
|
199 | */
|
200 | toMap() {
|
201 | return new Map(this);
|
202 | }
|
203 | }
|
204 | exports.IteratorWithOperators = IteratorWithOperators;
|
205 |
|
206 |
|
207 |
|
208 | function iterate(collection) {
|
209 | return new IteratorWithOperators(utils_1.toIterator(collection));
|
210 | }
|
211 | exports.iterate = iterate;
|
212 |
|
213 |
|
214 |
|
215 | function zip(a, b) {
|
216 | return new IteratorWithOperators(new zip_1.ZipIterator(utils_1.toIterator(a), utils_1.toIterator(b)));
|
217 | }
|
218 | exports.zip = zip;
|
219 | exports.default = iterate;
|
220 |
|
\ | No newline at end of file |