Skip to main content

Function: every()

every(iterable, callbackFn)

every<ElementType, FilteredType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => element is FilteredType): iterable is IterableIterator<FilteredType>

Tests whether all elements in the iterable pass the test implemented by the provided function.

Type parameters

Type parameter
ElementType
FilteredType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterator to check.
callbackFn(element: ElementType, index: number) => element is FilteredTypeA function to execute for each element produced by the iterator. It should return a truthy value to indicate the element passes the test, and a falsy value otherwise.

Returns

iterable is IterableIterator<FilteredType>

true if callbackFn returns a truthy value for every element. Otherwise, false.

Example

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

const iterable = [1, 2, 3, 4, 5];
console.log(every(iterable, (value) => value < 10));
// Output: true

console.log(every(iterable, (value) => value < 3));
// Output: false

Remarks

This function consumes the entire iterator.

Source

projects/utilities/packages/iterator-utilities/src/lib/every.ts:28

every(iterable, callbackFn)

every<ElementType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => boolean): boolean

Type parameters

Type parameter
ElementType

Parameters

ParameterType
iterableIterableResolvable<ElementType>
callbackFn(element: ElementType, index: number) => boolean

Returns

boolean

Source

projects/utilities/packages/iterator-utilities/src/lib/every.ts:32