Skip to content

negate

Creates a function that negates the result of predicate.

Available since v2.0.0

import { negate } from '@helpers4/function';
negate<T extends unknown[]>(predicate: function): function
ParameterTypeDescription
predicatefunctionA predicate function returning a boolean.

function — A new function that returns the logical negation of predicate.

Returns a function that inverts the boolean result of the given predicate.

const isEven = (n: number) => n % 2 === 0;
const isOdd = negate(isEven);
isOdd(3); // => true
isOdd(4); // => false

negate is ideal for inverting predicates passed to Array.filter.

const isEmpty = (arr: unknown[]) => arr.length === 0;
[[], [1], [], [2, 3]].filter(negate(isEmpty))
// => [[1], [2, 3]]

View source on GitHub