Skip to main content

Function: reverse()

reverse<ElementType>(iterable: IterableResolvable<ElementType>): IterableIterator<ElementType>

Consumes the iterable and returns a new iterable with the elements in reverse order.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterator to reverse.

Returns

IterableIterator<ElementType>

An iterator whose element correspond to the elements of the provided iterator in reverse order.

Example

import { reverse } from '@sapphire/iterator-utilities';

console.log([...reverse([1, 2, 3, 4, 5])]);
// Output: [5, 4, 3, 2, 1]

console.log([...reverse('hello')]);
// Output: ['o', 'l', 'l', 'e', 'h']

Remarks

This function collects all elements of the provided iterator into an array before yielding them in reverse order.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/reverse.ts:25