UNPKG

14.4 kBJavaScriptView Raw
1"use strict";
2/*-----------------------------------------------------------------------------
3| Copyright (c) 2014-2017, PhosphorJS Contributors
4|
5| Distributed under the terms of the BSD 3-Clause License.
6|
7| The full license is in the file LICENSE, distributed with this software.
8|----------------------------------------------------------------------------*/
9Object.defineProperty(exports, "__esModule", { value: true });
10/**
11 * Create an iterator for an iterable object.
12 *
13 * @param object - The iterable or array-like object of interest.
14 *
15 * @returns A new iterator for the given object.
16 *
17 * #### Notes
18 * This function allows iteration algorithms to operate on user-defined
19 * iterable types and builtin array-like objects in a uniform fashion.
20 */
21function iter(object) {
22 var it;
23 if (typeof object.iter === 'function') {
24 it = object.iter();
25 }
26 else {
27 it = new ArrayIterator(object);
28 }
29 return it;
30}
31exports.iter = iter;
32/**
33 * Create an iterator for the keys in an object.
34 *
35 * @param object - The object of interest.
36 *
37 * @returns A new iterator for the keys in the given object.
38 *
39 * #### Complexity
40 * Linear.
41 *
42 * #### Example
43 * ```typescript
44 * import { each, keys } from '@phosphor/algorithm';
45 *
46 * let data = { one: 1, two: 2, three: 3 };
47 *
48 * each(keys(data), key => { console.log(key); }); // 'one', 'two', 'three'
49 * ```
50 */
51function iterKeys(object) {
52 return new KeyIterator(object);
53}
54exports.iterKeys = iterKeys;
55/**
56 * Create an iterator for the values in an object.
57 *
58 * @param object - The object of interest.
59 *
60 * @returns A new iterator for the values in the given object.
61 *
62 * #### Complexity
63 * Linear.
64 *
65 * #### Example
66 * ```typescript
67 * import { each, values } from '@phosphor/algorithm';
68 *
69 * let data = { one: 1, two: 2, three: 3 };
70 *
71 * each(values(data), value => { console.log(value); }); // 1, 2, 3
72 * ```
73 */
74function iterValues(object) {
75 return new ValueIterator(object);
76}
77exports.iterValues = iterValues;
78/**
79 * Create an iterator for the items in an object.
80 *
81 * @param object - The object of interest.
82 *
83 * @returns A new iterator for the items in the given object.
84 *
85 * #### Complexity
86 * Linear.
87 *
88 * #### Example
89 * ```typescript
90 * import { each, items } from '@phosphor/algorithm';
91 *
92 * let data = { one: 1, two: 2, three: 3 };
93 *
94 * each(items(data), value => { console.log(value); }); // ['one', 1], ['two', 2], ['three', 3]
95 * ```
96 */
97function iterItems(object) {
98 return new ItemIterator(object);
99}
100exports.iterItems = iterItems;
101/**
102 * Create an iterator for an iterator-like function.
103 *
104 * @param fn - A function which behaves like an iterator `next` method.
105 *
106 * @returns A new iterator for the given function.
107 *
108 * #### Notes
109 * The returned iterator **cannot** be cloned.
110 *
111 * #### Example
112 * ```typescript
113 * import { each, iterFn } from '@phosphor/algorithm';
114 *
115 * let it = iterFn((() => {
116 * let i = 0;
117 * return () => i > 3 ? undefined : i++;
118 * })());
119 *
120 * each(it, v => { console.log(v); }); // 0, 1, 2, 3
121 * ```
122 */
123function iterFn(fn) {
124 return new FnIterator(fn);
125}
126exports.iterFn = iterFn;
127/**
128 * Invoke a function for each value in an iterable.
129 *
130 * @param object - The iterable or array-like object of interest.
131 *
132 * @param fn - The callback function to invoke for each value.
133 *
134 * #### Notes
135 * Iteration can be terminated early by returning `false` from the
136 * callback function.
137 *
138 * #### Complexity
139 * Linear.
140 *
141 * #### Example
142 * ```typescript
143 * import { each } from '@phosphor/algorithm';
144 *
145 * let data = [5, 7, 0, -2, 9];
146 *
147 * each(data, value => { console.log(value); });
148 * ```
149 */
150function each(object, fn) {
151 var index = 0;
152 var it = iter(object);
153 var value;
154 while ((value = it.next()) !== undefined) {
155 if (fn(value, index++) === false) {
156 return;
157 }
158 }
159}
160exports.each = each;
161/**
162 * Test whether all values in an iterable satisfy a predicate.
163 *
164 * @param object - The iterable or array-like object of interest.
165 *
166 * @param fn - The predicate function to invoke for each value.
167 *
168 * @returns `true` if all values pass the test, `false` otherwise.
169 *
170 * #### Notes
171 * Iteration terminates on the first `false` predicate result.
172 *
173 * #### Complexity
174 * Linear.
175 *
176 * #### Example
177 * ```typescript
178 * import { every } from '@phosphor/algorithm';
179 *
180 * let data = [5, 7, 1];
181 *
182 * every(data, value => value % 2 === 0); // false
183 * every(data, value => value % 2 === 1); // true
184 * ```
185 */
186function every(object, fn) {
187 var index = 0;
188 var it = iter(object);
189 var value;
190 while ((value = it.next()) !== undefined) {
191 if (!fn(value, index++)) {
192 return false;
193 }
194 }
195 return true;
196}
197exports.every = every;
198/**
199 * Test whether any value in an iterable satisfies a predicate.
200 *
201 * @param object - The iterable or array-like object of interest.
202 *
203 * @param fn - The predicate function to invoke for each value.
204 *
205 * @returns `true` if any value passes the test, `false` otherwise.
206 *
207 * #### Notes
208 * Iteration terminates on the first `true` predicate result.
209 *
210 * #### Complexity
211 * Linear.
212 *
213 * #### Example
214 * ```typescript
215 * import { some } from '@phosphor/algorithm';
216 *
217 * let data = [5, 7, 1];
218 *
219 * some(data, value => value === 7); // true
220 * some(data, value => value === 3); // false
221 * ```
222 */
223function some(object, fn) {
224 var index = 0;
225 var it = iter(object);
226 var value;
227 while ((value = it.next()) !== undefined) {
228 if (fn(value, index++)) {
229 return true;
230 }
231 }
232 return false;
233}
234exports.some = some;
235/**
236 * Create an array from an iterable of values.
237 *
238 * @param object - The iterable or array-like object of interest.
239 *
240 * @returns A new array of values from the given object.
241 *
242 * #### Example
243 * ```typescript
244 * import { iter, toArray } from '@phosphor/algorithm';
245 *
246 * let data = [1, 2, 3, 4, 5, 6];
247 *
248 * let stream = iter(data);
249 *
250 * toArray(stream); // [1, 2, 3, 4, 5, 6];
251 * ```
252 */
253function toArray(object) {
254 var index = 0;
255 var result = [];
256 var it = iter(object);
257 var value;
258 while ((value = it.next()) !== undefined) {
259 result[index++] = value;
260 }
261 return result;
262}
263exports.toArray = toArray;
264/**
265 * Create an object from an iterable of key/value pairs.
266 *
267 * @param object - The iterable or array-like object of interest.
268 *
269 * @returns A new object mapping keys to values.
270 *
271 * #### Example
272 * ```typescript
273 * import { toObject } from '@phosphor/algorithm';
274 *
275 * let data = [['one', 1], ['two', 2], ['three', 3]];
276 *
277 * toObject(data); // { one: 1, two: 2, three: 3 }
278 * ```
279 */
280function toObject(object) {
281 var it = iter(object);
282 var pair;
283 var result = {};
284 while ((pair = it.next()) !== undefined) {
285 result[pair[0]] = pair[1];
286 }
287 return result;
288}
289exports.toObject = toObject;
290/**
291 * An iterator for an array-like object.
292 *
293 * #### Notes
294 * This iterator can be used for any builtin JS array-like object.
295 */
296var ArrayIterator = /** @class */ (function () {
297 /**
298 * Construct a new array iterator.
299 *
300 * @param source - The array-like object of interest.
301 */
302 function ArrayIterator(source) {
303 this._index = 0;
304 this._source = source;
305 }
306 /**
307 * Get an iterator over the object's values.
308 *
309 * @returns An iterator which yields the object's values.
310 */
311 ArrayIterator.prototype.iter = function () {
312 return this;
313 };
314 /**
315 * Create an independent clone of the iterator.
316 *
317 * @returns A new independent clone of the iterator.
318 */
319 ArrayIterator.prototype.clone = function () {
320 var result = new ArrayIterator(this._source);
321 result._index = this._index;
322 return result;
323 };
324 /**
325 * Get the next value from the iterator.
326 *
327 * @returns The next value from the iterator, or `undefined`.
328 */
329 ArrayIterator.prototype.next = function () {
330 if (this._index >= this._source.length) {
331 return undefined;
332 }
333 return this._source[this._index++];
334 };
335 return ArrayIterator;
336}());
337exports.ArrayIterator = ArrayIterator;
338/**
339 * An iterator for the keys in an object.
340 *
341 * #### Notes
342 * This iterator can be used for any JS object.
343 */
344var KeyIterator = /** @class */ (function () {
345 /**
346 * Construct a new key iterator.
347 *
348 * @param source - The object of interest.
349 *
350 * @param keys - The keys to iterate, if known.
351 */
352 function KeyIterator(source, keys) {
353 if (keys === void 0) { keys = Object.keys(source); }
354 this._index = 0;
355 this._source = source;
356 this._keys = keys;
357 }
358 /**
359 * Get an iterator over the object's values.
360 *
361 * @returns An iterator which yields the object's values.
362 */
363 KeyIterator.prototype.iter = function () {
364 return this;
365 };
366 /**
367 * Create an independent clone of the iterator.
368 *
369 * @returns A new independent clone of the iterator.
370 */
371 KeyIterator.prototype.clone = function () {
372 var result = new KeyIterator(this._source, this._keys);
373 result._index = this._index;
374 return result;
375 };
376 /**
377 * Get the next value from the iterator.
378 *
379 * @returns The next value from the iterator, or `undefined`.
380 */
381 KeyIterator.prototype.next = function () {
382 if (this._index >= this._keys.length) {
383 return undefined;
384 }
385 var key = this._keys[this._index++];
386 if (key in this._source) {
387 return key;
388 }
389 return this.next();
390 };
391 return KeyIterator;
392}());
393exports.KeyIterator = KeyIterator;
394/**
395 * An iterator for the values in an object.
396 *
397 * #### Notes
398 * This iterator can be used for any JS object.
399 */
400var ValueIterator = /** @class */ (function () {
401 /**
402 * Construct a new value iterator.
403 *
404 * @param source - The object of interest.
405 *
406 * @param keys - The keys to iterate, if known.
407 */
408 function ValueIterator(source, keys) {
409 if (keys === void 0) { keys = Object.keys(source); }
410 this._index = 0;
411 this._source = source;
412 this._keys = keys;
413 }
414 /**
415 * Get an iterator over the object's values.
416 *
417 * @returns An iterator which yields the object's values.
418 */
419 ValueIterator.prototype.iter = function () {
420 return this;
421 };
422 /**
423 * Create an independent clone of the iterator.
424 *
425 * @returns A new independent clone of the iterator.
426 */
427 ValueIterator.prototype.clone = function () {
428 var result = new ValueIterator(this._source, this._keys);
429 result._index = this._index;
430 return result;
431 };
432 /**
433 * Get the next value from the iterator.
434 *
435 * @returns The next value from the iterator, or `undefined`.
436 */
437 ValueIterator.prototype.next = function () {
438 if (this._index >= this._keys.length) {
439 return undefined;
440 }
441 var key = this._keys[this._index++];
442 if (key in this._source) {
443 return this._source[key];
444 }
445 return this.next();
446 };
447 return ValueIterator;
448}());
449exports.ValueIterator = ValueIterator;
450/**
451 * An iterator for the items in an object.
452 *
453 * #### Notes
454 * This iterator can be used for any JS object.
455 */
456var ItemIterator = /** @class */ (function () {
457 /**
458 * Construct a new item iterator.
459 *
460 * @param source - The object of interest.
461 *
462 * @param keys - The keys to iterate, if known.
463 */
464 function ItemIterator(source, keys) {
465 if (keys === void 0) { keys = Object.keys(source); }
466 this._index = 0;
467 this._source = source;
468 this._keys = keys;
469 }
470 /**
471 * Get an iterator over the object's values.
472 *
473 * @returns An iterator which yields the object's values.
474 */
475 ItemIterator.prototype.iter = function () {
476 return this;
477 };
478 /**
479 * Create an independent clone of the iterator.
480 *
481 * @returns A new independent clone of the iterator.
482 */
483 ItemIterator.prototype.clone = function () {
484 var result = new ItemIterator(this._source, this._keys);
485 result._index = this._index;
486 return result;
487 };
488 /**
489 * Get the next value from the iterator.
490 *
491 * @returns The next value from the iterator, or `undefined`.
492 */
493 ItemIterator.prototype.next = function () {
494 if (this._index >= this._keys.length) {
495 return undefined;
496 }
497 var key = this._keys[this._index++];
498 if (key in this._source) {
499 return [key, this._source[key]];
500 }
501 return this.next();
502 };
503 return ItemIterator;
504}());
505exports.ItemIterator = ItemIterator;
506/**
507 * An iterator for an iterator-like function.
508 */
509var FnIterator = /** @class */ (function () {
510 /**
511 * Construct a new function iterator.
512 *
513 * @param fn - The iterator-like function of interest.
514 */
515 function FnIterator(fn) {
516 this._fn = fn;
517 }
518 /**
519 * Get an iterator over the object's values.
520 *
521 * @returns An iterator which yields the object's values.
522 */
523 FnIterator.prototype.iter = function () {
524 return this;
525 };
526 /**
527 * Create an independent clone of the iterator.
528 *
529 * @returns A new independent clone of the iterator.
530 */
531 FnIterator.prototype.clone = function () {
532 throw new Error('An `FnIterator` cannot be cloned.');
533 };
534 /**
535 * Get the next value from the iterator.
536 *
537 * @returns The next value from the iterator, or `undefined`.
538 */
539 FnIterator.prototype.next = function () {
540 return this._fn.call(undefined);
541 };
542 return FnIterator;
543}());
544exports.FnIterator = FnIterator;