UNPKG

6.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const concat_1 = require("./concat");
4const filter_1 = require("./filter");
5const flatten_1 = require("./flatten");
6const map_1 = require("./map");
7const slice_1 = require("./slice");
8const utils_1 = require("./utils");
9const zip_1 = require("./zip");
10class IteratorWithOperators {
11 /**
12 * @param source Iterator to wrap
13 */
14 constructor(source) {
15 this.source = source;
16 }
17 /**
18 * Returns a `{ value, done }` object that adheres to the Iterator protocol
19 */
20 next() {
21 return this.source.next();
22 }
23 /**
24 * The presence of this method makes the Iterator itself Iterable.
25 * This makes it possible to pass it to `for of` and Iterable-accepting functions like `Array.from()`
26 */
27 [Symbol.iterator]() {
28 return this;
29 }
30 /**
31 * Returns a new Iterator by running each element thru iteratee
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 * Returns a new Iterator concatenating the Iterator with an additional Iterator or Iterable
41 */
42 concat(collection) {
43 return new IteratorWithOperators(new concat_1.ConcatIterator([this.source, utils_1.toIterator(collection)]));
44 }
45 /**
46 * Returns a new Iterator that emits slice of the source with n elements taken from the beginning
47 *
48 * @param limit The number of elements to take.
49 */
50 take(limit) {
51 return new IteratorWithOperators(new slice_1.SliceIterator(this.source, 0, limit + 1));
52 }
53 /**
54 * Returns a new Iterator that emits slice of the source with n elements dropped from the beginning
55 *
56 * @param n The number of elements to drop.
57 */
58 drop(n) {
59 return new IteratorWithOperators(new slice_1.SliceIterator(this.source, n, Infinity));
60 }
61 /**
62 * Returns a new Iterator that emits a slice of the source
63 *
64 * @param {number} start Zero-based positive start index, inclusive
65 * @param {number} end Zero-based positive end index, exclusive, defaults to end of iterator
66 */
67 slice(start, end = Infinity) {
68 return new IteratorWithOperators(new slice_1.SliceIterator(this.source, start, end));
69 }
70 /**
71 * Returns a new Iterator that flattens items emitted by the Iterator a single level deep
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 * Iterates and checks if `value` is emitted by the Iterator
108 *
109 * @param value The value to search
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 * Iterates and checks if `predicate` returns truthy for any element emitted by the Iterator
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 * Iterates and checks if `predicate` returns truthy for all elements emitted by the Iterator
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 * Iterates and invokes `iteratee` for every element emitted by the Iterator
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 * Iterates and joins all elements emitted by the Iterator together as a string separated by an optional separator
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 * Iterates and returns all items emitted by the Iterator as an array.
177 * Equivalent to passing the Iterator to `Array.from()`
178 */
179 toArray() {
180 return Array.from(this);
181 }
182 /**
183 * Iterates and returns all items emitted by the Iterator as an ES6 Set.
184 * Equivalent to passing the Iterator to `new Set()`
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}
204exports.IteratorWithOperators = IteratorWithOperators;
205/**
206 * Creates an Iterator with advanced chainable operator methods for any Iterable or Iterator
207 */
208function iterate(collection) {
209 return new IteratorWithOperators(utils_1.toIterator(collection));
210}
211exports.iterate = iterate;
212/**
213 * Creates an Iterator that emits pairs of values from the two passed Iterators
214 */
215function zip(a, b) {
216 return new IteratorWithOperators(new zip_1.ZipIterator(utils_1.toIterator(a), utils_1.toIterator(b)));
217}
218exports.zip = zip;
219exports.default = iterate;
220//# sourceMappingURL=iterate.js.map
\No newline at end of file