Skip to main content

Function: compress()

compress<ElementType>(iterable: IterableResolvable<ElementType>, selectors: IterableResolvable<boolean>): IterableIterator<ElementType>

Creates a new iterable of the first iterable based on the truthiness of the corresponding element in the second iterable.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator that contains elements to be compressed.
selectorsIterableResolvable<boolean>The selectors that determine which elements to include in the result.

Returns

IterableIterator<ElementType>

An iterator that contains only the elements from the input iterator that correspond to true values in the selectors iterator.

Example

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

const iterable = compress([1, 2, 3, 4, 5], [true, false, true, false, true]);
console.log([...iterable]);
// Output: [1, 3, 5]

Remarks

This function consumes both input iterators until either is exhausted.

Defined in

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