UNPKG

2.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/*-----------------------------------------------------------------------------
4| Copyright (c) 2014-2017, PhosphorJS Contributors
5|
6| Distributed under the terms of the BSD 3-Clause License.
7|
8| The full license is in the file LICENSE, distributed with this software.
9|----------------------------------------------------------------------------*/
10var iter_1 = require("./iter");
11/**
12 * Enumerate an iterable object.
13 *
14 * @param object - The iterable or array-like object of interest.
15 *
16 * @param start - The starting enum value. The default is `0`.
17 *
18 * @returns An iterator which yields the enumerated values.
19 *
20 * #### Example
21 * ```typescript
22 * import { enumerate, toArray } from '@phosphor/algorithm';
23 *
24 * let data = ['foo', 'bar', 'baz'];
25 *
26 * let stream = enumerate(data, 1);
27 *
28 * toArray(stream); // [[1, 'foo'], [2, 'bar'], [3, 'baz']]
29 * ```
30 */
31function enumerate(object, start) {
32 if (start === void 0) { start = 0; }
33 return new EnumerateIterator(iter_1.iter(object), start);
34}
35exports.enumerate = enumerate;
36/**
37 * An iterator which enumerates the source values.
38 */
39var EnumerateIterator = /** @class */ (function () {
40 /**
41 * Construct a new enumerate iterator.
42 *
43 * @param source - The iterator of values of interest.
44 *
45 * @param start - The starting enum value.
46 */
47 function EnumerateIterator(source, start) {
48 this._source = source;
49 this._index = start;
50 }
51 /**
52 * Get an iterator over the object's values.
53 *
54 * @returns An iterator which yields the object's values.
55 */
56 EnumerateIterator.prototype.iter = function () {
57 return this;
58 };
59 /**
60 * Create an independent clone of the iterator.
61 *
62 * @returns A new independent clone of the iterator.
63 */
64 EnumerateIterator.prototype.clone = function () {
65 return new EnumerateIterator(this._source.clone(), this._index);
66 };
67 /**
68 * Get the next value from the iterator.
69 *
70 * @returns The next value from the iterator, or `undefined`.
71 */
72 EnumerateIterator.prototype.next = function () {
73 var value = this._source.next();
74 if (value === undefined) {
75 return undefined;
76 }
77 return [this._index++, value];
78 };
79 return EnumerateIterator;
80}());
81exports.EnumerateIterator = EnumerateIterator;