Skip to main content

either

TS JS Deno

Returns true if at least one function returns true for a given set of arguments, false otherwise.

Use the logical or (||) operator on the result of calling the two functions with the supplied args.

typescript
export const either =
(f: Function, g: Function) =>
(...args: any[]) =>
f(...args) || g(...args);
typescript
const isEven = (num: number) => num % 2 === 0;
const isPositive = (num: number) => num > 0;
const isPositiveOrEven = either(isPositive, isEven);

isPositiveOrEven(4); // true
isPositiveOrEven(3); // true

interface User {
name: string;
age: number;
}
const user1: User = {
name: "deepak",
age: 18,
};
const user2: User = {
name: "Martha",
age: 21,
};
const isDeepak = (u: User) => u.name === "deepak";
const isAdult = (minAge: number, u: User) => u.age > minAge;