Packages@accelint/coreArray
every
Tests whether all elements in the array pass the given comparator.
Usage
import { every } from '@accelint/core/array/every';
// Check if all numbers are even
const allEven = every((x: number) => x % 2 === 0)([2, 4, 6, 8]);
// true
const notAllEven = every((x: number) => x % 2 === 0)([2, 3, 4]);
// falseReference
function every<T>(
comparator: (element: T) => boolean
): (arr: T[]) => booleanParameters
| Parameter | Type | Description |
|---|---|---|
comparator | (element: T) => boolean | Function called for each element. Returns true if element passes test, false otherwise. |
Returns
Returns a curried function that accepts an array and returns true if all elements pass the comparator, false otherwise.
Type Parameters
T- The type of elements in the array
Examples
Example: Validating objects
import { every } from '@accelint/core/array/every';
interface User {
id: number;
verified: boolean;
}
const users: User[] = [
{ id: 1, verified: true },
{ id: 2, verified: true }
];
const allVerified = every((u: User) => u.verified)(users);
// trueExample: Type guards
import { every } from '@accelint/core/array/every';
const values: Array<string | number> = ['a', 'b', 'c'];
const allStrings = every((v: string | number) => typeof v === 'string')(values);
// trueGood to know: Returns
truefor an empty array. Short-circuits on first failing element, providing early exit optimization. This is a pure function with no side effects.