50 lines
982 B
TypeScript
50 lines
982 B
TypeScript
/**
|
|
* Predicate that always returns true
|
|
*/
|
|
export const always = (...args: any[]) => true;
|
|
|
|
/**
|
|
* Predicate that always returns false
|
|
*/
|
|
export const never = (...args: any[]) => false;
|
|
|
|
/**
|
|
* Negate a predicate
|
|
*/
|
|
export function not<A extends any[]>(
|
|
predicate: (...args: A) => boolean,
|
|
): (...args: A) => boolean {
|
|
return (...args) => !predicate(...args);
|
|
}
|
|
|
|
/**
|
|
* Apply a boolean "and" to merge predicates
|
|
*/
|
|
export function and<A extends any[]>(
|
|
...predicates: ((...args: A) => boolean)[]
|
|
): (...args: A) => boolean {
|
|
return (...args) => {
|
|
for (let p of predicates) {
|
|
if (!p(...args)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Apply a boolean "or" to merge predicates
|
|
*/
|
|
export function or<A extends any[]>(
|
|
...predicates: ((...args: A) => boolean)[]
|
|
): (...args: A) => boolean {
|
|
return (...args) => {
|
|
for (let p of predicates) {
|
|
if (p(...args)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
}
|