Skip to main content

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 parameterDescription
ElementTypeThe type of elements in the iterable.

Parameters

ParameterTypeDescription
iterableIterableResolvable<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 }

Source

projects/utilities/packages/iterator-utilities/src/lib/peekable.ts:30