UNPKG

1.46 kBPlain TextView Raw
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|----------------------------------------------------------------------------*/
10import { IIterator } from './iter';
11
12/**
13 * Create an empty iterator.
14 *
15 * @returns A new iterator which yields nothing.
16 *
17 * #### Example
18 * ```typescript
19 * import { empty, toArray } from '@lumino/algorithm';
20 *
21 * let stream = empty<number>();
22 *
23 * toArray(stream); // []
24 * ```
25 */
26export function empty<T>(): IIterator<T> {
27 return new EmptyIterator<T>();
28}
29
30/**
31 * An iterator which is always empty.
32 */
33export class EmptyIterator<T> implements IIterator<T> {
34 /**
35 * Get an iterator over the object's values.
36 *
37 * @returns An iterator which yields the object's values.
38 */
39 iter(): IIterator<T> {
40 return this;
41 }
42
43 /**
44 * Create an independent clone of the iterator.
45 *
46 * @returns A new independent clone of the iterator.
47 */
48 clone(): IIterator<T> {
49 return new EmptyIterator<T>();
50 }
51
52 /**
53 * Get the next value from the iterator.
54 *
55 * @returns The next value from the iterator, or `undefined`.
56 */
57 next(): T | undefined {
58 return undefined;
59 }
60}