Standard Toolkit
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]);
// false

Reference

function every<T>(
  comparator: (element: T) => boolean
): (arr: T[]) => boolean

Parameters

ParameterTypeDescription
comparator(element: T) => booleanFunction 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);
// true

Example: 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);
// true

Good to know: Returns true for an empty array. Short-circuits on first failing element, providing early exit optimization. This is a pure function with no side effects.

  • some - Test if any element passes comparator
  • filter - Get all elements matching predicate
  • find - Find first element matching predicate

On this page