Skip to content

Predicate

Predicate: object

Defined in: Core/Predicate.ts:29

all: <A>(predicates) => Predicate<A>

Combines an array of predicates with AND: passes only when every predicate holds. Returns true for an empty array (vacuous truth).

A

readonly Predicate<A>[]

Predicate<A>

const checks: Predicate<string>[] = [
  s => s.length > 0,
  s => s.length <= 100,
  s => !s.includes("<"),
];

Predicate.all(checks)("hello");  // true
Predicate.all(checks)("");       // false — too short
Predicate.all(checks)("<b>");    // false — contains "<"
Predicate.all([])("anything");   // true

and: <A>(second) => (first) => Predicate<A>

Combines two predicates with logical AND: passes only when both hold.

Data-last — the first predicate is the data being piped.

A

Predicate<A>

(first) => Predicate<A>

const isPositive: Predicate<number> = n => n > 0;
const isEven: Predicate<number> = n => n % 2 === 0;

const isPositiveEven: Predicate<number> = pipe(isPositive, Predicate.and(isEven));

isPositiveEven(4);   // true
isPositiveEven(3);   // false — positive but odd
isPositiveEven(-2);  // false — even but not positive

any: <A>(predicates) => Predicate<A>

Combines an array of predicates with OR: passes when at least one holds. Returns false for an empty array.

A

readonly Predicate<A>[]

Predicate<A>

const acceptedFormats: Predicate<string>[] = [
  s => s.endsWith(".jpg"),
  s => s.endsWith(".png"),
  s => s.endsWith(".webp"),
];

Predicate.any(acceptedFormats)("photo.jpg");   // true
Predicate.any(acceptedFormats)("photo.gif");   // false
Predicate.any([])("anything");                 // false

from: object

Refinement: <A, B>(r) => Predicate<A> = fromRefinement

Converts a Refinement<A, B> into a Predicate<A>, discarding the compile-time narrowing. Use this when you want to combine a type guard with plain predicates using and, or, or all.

This is a zero-cost runtime type cast.

A

B

Refinement<A, B>

Predicate<A>

const isString: Refinement<unknown, string> =
  Refinement.from.predicate(x => typeof x === "string");

const isShortString: Predicate<unknown> = pipe(
  Predicate.from.Refinement(isString),
  Predicate.and(x => (x as string).length < 10)
);

isShortString("hi");            // true
isShortString("a very long string that exceeds ten characters");  // false
isShortString(42);              // false

match: <A, B>(branches, fallback) => (a) => B

Performs declarative conditional branching over [predicate, handler] pairs, returning the handler result of the first matching predicate or evaluating the fallback.

A

B

readonly readonly [Predicate<A>, (a) => B][]

(a) => B

(a) => B

const classifyNumber = Predicate.match(
  [
    [(n: number) => n < 0, () => "negative"],
    [(n: number) => n === 0, () => "zero"],
  ],
  () => "positive",
);
classifyNumber(-5); // "negative"

not: <A>(p) => Predicate<A>

Negates a predicate: the result passes exactly when the original fails.

A

Predicate<A>

Predicate<A>

const isBlank: Predicate<string> = s => s.trim().length === 0;
const isNotBlank = Predicate.not(isBlank);

isNotBlank("hello");  // true
isNotBlank("   ");    // false

or: <A>(second) => (first) => Predicate<A>

Combines two predicates with logical OR: passes when either holds.

Data-last — the first predicate is the data being piped.

A

Predicate<A>

(first) => Predicate<A>

const isChild: Predicate<number> = n => n < 13;
const isSenior: Predicate<number> = n => n >= 65;

const getsDiscount: Predicate<number> = pipe(isChild, Predicate.or(isSenior));

getsDiscount(8);   // true
getsDiscount(70);  // true
getsDiscount(30);  // false

using: <A, B>(f) => (p) => Predicate<B>

Adapts a Predicate<A> to work on a different input type B by applying f to extract the relevant A from a B before running the check.

Data-last — the predicate is the data being piped; f is the extractor.

A

B

(b) => A

(p) => Predicate<B>

type User = { name: string; age: number };

const isAdult: Predicate<number> = n => n >= 18;

// Lift isAdult to work on Users by extracting the age field
const isAdultUser: Predicate<User> = pipe(
  isAdult,
  Predicate.using((u: User) => u.age)
);

isAdultUser({ name: "Alice", age: 30 });  // true
isAdultUser({ name: "Bob",   age: 15 });  // false