Skip to main content

negate()

negate<Args>(predicate): (...args) => boolean

Creates a function that negates the result of the predicate.


Type Parametersโ€‹

Args: Args extends unknown[]โ€‹

The argument types of the predicate.


Parametersโ€‹

predicate: (...args) => unknownโ€‹

The predicate to negate.


Returnsโ€‹

The new negated function.


Sinceโ€‹

2.0.0


Also known asโ€‹

complement (Ramda) ยท negate (Lodash, es-toolkit) ยท not (Effect) ยท โŒ (Remeda, Radashi, Modern Dash, Antfu)


Exampleโ€‹

const isEven = (n: number) => n % 2 === 0;
const isOdd = negate(isEven);

isOdd(1); // => true
isOdd(2); // => false

// Use with array methods
[1, 2, 3, 4, 5].filter(negate(isEven));
// => [1, 3, 5]

// With multiple arguments
const includes = (arr: number[], val: number) => arr.includes(val);
const excludes = negate(includes);
excludes([1, 2, 3], 4); // => true

How it works?โ€‹

Creates a function that returns the logical negation of the predicate result.

Transformationโ€‹

Array Filter Exampleโ€‹


Use Casesโ€‹

Filter Inversion for array operations ๐Ÿ“Œโ€‹

Invert predicates for filtering operations. Essential for creating opposite filter conditions.

import { negate } from "pithos/arkhe/function/negate";

const isEven = (n: number) => n % 2 === 0;
const isOdd = negate(isEven);

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

console.log(numbers.filter(isEven)); // [2, 4, 6, 8, 10]
console.log(numbers.filter(isOdd)); // [1, 3, 5, 7, 9]

Validation Negation for form checks ๐Ÿ“Œโ€‹

Create opposite validation conditions. Critical for form validation logic.

import { negate } from "pithos/arkhe/function/negate";

const isEmpty = (value: string) => value.trim().length === 0;
const isNotEmpty = negate(isEmpty);

const hasErrors = (errors: string[]) => errors.length > 0;
const isValid = negate(hasErrors);

// Use in form validation
const formFields = ["username", "email", "password"];
const values = { username: "john", email: "", password: "secret" };

const emptyFields = formFields.filter(field => isEmpty(values[field]));
const filledFields = formFields.filter(field => isNotEmpty(values[field]));

console.log(emptyFields); // ["email"]
console.log(filledFields); // ["username", "password"]

Access Control logicโ€‹

Invert permission checks for access control. Important for authorization systems.

import { negate } from "pithos/arkhe/function/negate";

interface User {
id: string;
role: string;
suspended: boolean;
}

const isAdmin = (user: User) => user.role === "admin";
const isSuspended = (user: User) => user.suspended;

const isNotAdmin = negate(isAdmin);
const isActive = negate(isSuspended);

const users: User[] = [
{ id: "1", role: "admin", suspended: false },
{ id: "2", role: "user", suspended: true },
{ id: "3", role: "user", suspended: false },
];

// Find active non-admin users
const regularActiveUsers = users.filter(u => isNotAdmin(u) && isActive(u));
console.log(regularActiveUsers); // [{ id: "3", role: "user", suspended: false }]