Function: peekable()
peekable<
ElementType
>(iterable
:IterableResolvable
<ElementType
>):Peekable
<ElementType
>
Creates an iterator that allows you to peek at the next element without advancing the iterator.
Type Parameters
Type Parameter | Description |
---|---|
ElementType | The type of elements in the iterable. |
Parameters
Parameter | Type | Description |
---|---|---|
iterable | IterableResolvable <ElementType > | The iterable to create a peekable iterator from. |
Returns
Peekable
<ElementType
>
A new peekable iterator.
Example
import { peekable } from '@sapphire/iterator-utilities';
const iterable = [1, 2, 3, 4, 5];
const peekableIterator = peekable(iterable);
console.log(peekableIterator.next());
// Output: { value: 1, done: false }
console.log(peekableIterator.peek());
// Output: { value: 2, done: false }
console.log(peekableIterator.next());
// Output: { value: 2, done: false }
console.log(peekableIterator.next());
// Output: { value: 3, done: false }
Defined in
projects/utilities/packages/iterator-utilities/src/lib/peekable.ts:30