1 | // Copyright (c) Jupyter Development Team.
|
2 | // Distributed under the terms of the Modified BSD License.
|
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 | |----------------------------------------------------------------------------*/
|
10 |
|
11 | /**
|
12 | * Enumerate an iterable object.
|
13 | *
|
14 | * @param object - The iterable 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 } from '@lumino/algorithm';
|
23 | *
|
24 | * let data = ['foo', 'bar', 'baz'];
|
25 | *
|
26 | * let stream = enumerate(data, 1);
|
27 | *
|
28 | * Array.from(stream); // [[1, 'foo'], [2, 'bar'], [3, 'baz']]
|
29 | * ```
|
30 | */
|
31 | export function* enumerate<T>(
|
32 | object: Iterable<T>,
|
33 | start = 0
|
34 | ): IterableIterator<[number, T]> {
|
35 | for (const value of object) {
|
36 | yield [start++, value];
|
37 | }
|
38 | }
|