Skip to content

Predicates Reference

Coverage License NPM Version Open Issues Size

🧐 Predicate util functions

Usage

📦 Node

Install @lou.codes/predicates as a dependency:

Terminal window
1
pnpm add @lou.codes/predicates
2
# or
3
npm install @lou.codes/predicates
4
# or
5
yarn add @lou.codes/predicates

Import it and use it:

1
import { isBoolean } from "@lou.codes/predicates";
2
3
isBoolean(true); // true
4
isBoolean(false); // true
5
isBoolean(undefined); // false

🦕 Deno

Import @lou.codes/predicates using the npm: prefix, and use it directly:

1
import { isBoolean } from "npm:@lou.codes/predicates";
2
3
isBoolean(true); // true
4
isBoolean(false); // true
5
isBoolean(undefined); // false

🌎 Browser

Import @lou.codes/predicates using esm.sh, and use it directly:

1
<script type="module">
2
import { isBoolean } from "https://esm.sh/@lou.codes/predicates";
3
4
isBoolean(true); // true
5
isBoolean(false); // true
6
isBoolean(undefined); // false
7
</script>

Common

is

is<Expected>(expected): (actual: unknown) => actual is Expected

Curried wrapper for Object.is. Given and expected value and an actual value, returns true if those values are equal, or false if not.

Type parameters

Name
Expected

Parameters

NameType
expectedExpected

Returns

fn

Curried function with expected in context.

▸ (actual): actual is Expected

Parameters
NameType
actualunknown
Returns

actual is Expected

Example

1
const is2 = is(2);
2
3
is2(2); // true
4
is2(8); // false

View source


isFalsy

isFalsy(input): input is Falsy

Check if given input is falsy (0, NaN, "", false, or nullish).

Parameters

NameTypeDescription
inputunknownValue to check.

Returns

input is Falsy

Returns true if falsy, false otherwise.

Example

1
isFalsy(false); // true
2
isFalsy(0); // true
3
isFalsy(NaN); // true
4
isFalsy(""); // true
5
isFalsy(null); // true
6
isFalsy(undefined); // true
7
isFalsy(42); // false

View source


isTruthy

isTruthy<Input>(input): input is Truthy<ReadOnly<Input>>

Check if given input is truthy (so not 0, NaN, "", false, or nullish).

Type parameters

Name
Input

Parameters

NameTypeDescription
inputInput | Truthy<ReadOnly<Input>>Value to check.

Returns

input is Truthy<ReadOnly<Input>>

Returns true if truthy, false otherwise.

Example

1
isTruthy(42); // true
2
isTruthy(true); // true
3
isTruthy(false); // false
4
isTruthy(0); // false

View source

Iterables

hasAsyncIteratorSymbol

hasAsyncIteratorSymbol(object): object is Readonly<Record<typeof asyncIterator, unknown>>

Check if given object has the Symbol.asyncIterator symbol.

Parameters

NameType
objectunknown

Returns

object is Readonly<Record<typeof asyncIterator, unknown>>

true when given object has the Symbol.asyncIterator symbol, false otherwise.

Example

1
hasAsyncIteratorSymbol({ [Symbol.asyncIterator]() {} }); // true
2
hasAsyncIteratorSymbol({ bar: "bar" }); // false

View source


hasIteratorSymbol

hasIteratorSymbol(object): object is Readonly<Record<typeof iterator, unknown>>

Check if given object has the Symbol.iterator symbol.

Parameters

NameType
objectunknown

Returns

object is Readonly<Record<typeof iterator, unknown>>

true when given object has the Symbol.iterator symbol, false otherwise.

Example

1
hasIteratorSymbol({ [Symbol.iterator]() {} }); // true
2
hasIteratorSymbol({ bar: "bar" }); // false

View source


isArray

isArray<Item>(input): input is ReadOnlyArray<Item>

Check if given input is an instance of Array.

Type parameters

Name
Item

Parameters

NameType
inputunknown

Returns

input is ReadOnlyArray<Item>

true if the given value is an array, false otherwise.

Example

1
isArray([]); // true
2
isArray({ length: 42 }); // false

View source


isAsyncIterable

isAsyncIterable<Item>(input): input is Object

Check if given value is AsyncIterable.

Not to be confused with isAsynchronousIterable which checks for both AsyncIterable and Iterable.

Type parameters

Name
Item

Parameters

NameTypeDescription
inputunknownValue to check.

Returns

input is Object

true when is an AsyncIterable, false otherwise.

Example

1
isAsyncIterable(
2
(async function* () {
3
yield* [];
4
})(),
5
); // true
6
isAsyncIterable([]); // false
7
isAsyncIterable({}); // false

View source


isIsomorphicIterable

isIsomorphicIterable<Item>(input): input is IsomorphicIterable<Item>

Check if given value is IsomorphicIterable (either Iterable or AsyncIterable).

Not to be confused with isAsyncIterable which only checks for AsyncIterable.

Type parameters

Name
Item

Parameters

NameTypeDescription
inputunknownValue to check.

Returns

input is IsomorphicIterable<Item>

true when is an IsomorphicIterable, false otherwise.

Example

1
isIterable([]); // true
2
isIterable({}); // false

View source


isIterable

isIterable<Item>(input): input is Object

Check if given value is Iterable.

Type parameters

Name
Item

Parameters

NameTypeDescription
inputunknownValue to check.

Returns

input is Object

true when is an Iterable, false otherwise.

Example

1
isIterable([]); // true
2
isIterable({}); // false

View source

Objects

has

has<Property>(property): (object: unknown) => object is Readonly<Record<Property, unknown>>

Curried wrapper for the in operator. Given a property name and an object, returns true the object contains that property, false otherwise.

Type parameters

NameType
Propertyextends PropertyKey

Parameters

NameType
propertyProperty

Returns

fn

Curried function with property in context.

▸ (object): object is Readonly<Record<Property, unknown>>

Parameters
NameType
objectunknown
Returns

object is Readonly<Record<Property, unknown>>

Example

1
const hasCircle = has("🟢");
2
3
hasCircle({ "🟢": "🟩" }); // true
4
hasCircle({ "🟩": "🟢" }); // false

View source


hasAsyncIteratorSymbol

hasAsyncIteratorSymbol(object): object is Readonly<Record<typeof asyncIterator, unknown>>

Check if given object has the Symbol.asyncIterator symbol.

Parameters

NameType
objectunknown

Returns

object is Readonly<Record<typeof asyncIterator, unknown>>

true when given object has the Symbol.asyncIterator symbol, false otherwise.

Example

1
hasAsyncIteratorSymbol({ [Symbol.asyncIterator]() {} }); // true
2
hasAsyncIteratorSymbol({ bar: "bar" }); // false

View source


hasIteratorSymbol

hasIteratorSymbol(object): object is Readonly<Record<typeof iterator, unknown>>

Check if given object has the Symbol.iterator symbol.

Parameters

NameType
objectunknown

Returns

object is Readonly<Record<typeof iterator, unknown>>

true when given object has the Symbol.iterator symbol, false otherwise.

Example

1
hasIteratorSymbol({ [Symbol.iterator]() {} }); // true
2
hasIteratorSymbol({ bar: "bar" }); // false

View source


isDate

isDate(input): input is Date

instanceof Date alias.

Parameters

NameType
inputunknown

Returns

input is Date

true if the given value is a Date, false otherwise.

Example

1
isBigInt(1n); // true
2
isBigInt(1); // false

View source


isFunction

isFunction(input): input is Function

typeof “function” alias.

Parameters

NameType
inputunknown

Returns

input is Function

true if the given value is a function, false otherwise.

Example

1
isFunction(() => {}); // true
2
isFunction(function () {}); // true
3
isFunction(function* () {}); // true
4
isFunction(class {}); // true
5
isFunction(null); // false

View source


isInstanceOf

isInstanceOf<Expected>(constructor): (input: unknown) => input is InstanceType<Expected>

Takes a constructor and checks if given input is an instance of it.

Type parameters

NameType
Expectedextends Class<never>

Parameters

NameType
constructorExpected

Returns

fn

Returns a curried function with constructor in context.

▸ (input): input is InstanceType<Expected>

Parameters
NameType
inputunknown
Returns

input is InstanceType<Expected>

Example

1
const instanceOfArray = instanceOf(Array);
2
3
instanceOfArray([]); // true
4
instanceOfArray({}); // false

View source


isObject

isObject(input): input is object

typeof “object” alias.

Parameters

NameType
inputunknown

Returns

input is object

true if the given value is an object, false otherwise.

Example

1
isObject({}); // true
2
isObject([]); // true
3
isObject(new Date()); // true
4
isObject(null); // false

View source


isPromise

isPromise(input): input is Promise<unknown>

instanceof Promise alias.

Parameters

NameType
inputunknown

Returns

input is Promise<unknown>

true if the given value is an instance of Promise, false otherwise.

Example

1
isPromise(new Promise()); // true
2
isPromise((async () => {})()); // true
3
isPromise(fetch("/")); // true
4
isPromise(Promise.resolve("Lou")); // true
5
isPromise("Lou"); // false

View source


isPropertyKey

isPropertyKey(input): input is PropertyKey

Checks if the given value is a valid PropertyKey of an object (string, symbol, or number).

Parameters

NameTypeDescription
inputunknownValue to check.

Returns

input is PropertyKey

true if the given value is a valid PropertyKey of an object, false otherwise.

Example

1
isPropertyKey("Lou"); // true
2
isPropertyKey(1); // true
3
isPropertyKey(Symbol("Lou")); // true
4
isPropertyKey({}); // false

View source


isPropertyOf

isPropertyOf<Key>(object): (key: Key) => boolean

Check if the given key is present in the given object (not inherited).

Type parameters

NameType
Keyextends PropertyKey

Parameters

NameTypeDescription
objectReadOnlyRecord<Key>Object to check.

Returns

fn

Curried function with context set.

▸ (key): boolean

Parameters
NameType
keyKey
Returns

boolean

Example

1
const isPropertyOfFoo = isPropertyOf({ "🟢": "🟩" });
2
isPropertyOfFoo("🟢"); // true
3
isPropertyOfFoo("🟩"); // false

View source


isPrototypeOf

isPrototypeOf<Constructor>(constructor): <Input>(object: Input) => boolean

Checks if given input’s prototype comes directly from given constructor.

Type parameters

NameType
Constructorextends Class

Parameters

NameTypeDescription
constructorConstructorConstructor to check.

Returns

fn

Returns a curried function with constructor in context.

▸ <Input>(object): boolean

Type parameters
NameType
Inputextends object
Parameters
NameType
objectInput
Returns

boolean

Example

1
const isPrototypeOfObject = isPrototypeOf(Object);
2
isPrototypeOfObject({}); // true
3
isPrototypeOfObject(/./); // false

View source


isRegExp

isRegExp(input): input is RegExp

instanceof RegExp alias.

Parameters

NameType
inputunknown

Returns

input is RegExp

true if the given value is an instance of RegExp, false otherwise.

Example

1
isRegExp(new RegExp("-")); // true
2
isRegExp(/-/); // true
3
isRegExp("Lou"); // false

View source

Predicates

isPropertyOf

isPropertyOf<Key>(object): (key: Key) => boolean

Check if the given key is present in the given object (not inherited).

Type parameters

NameType
Keyextends PropertyKey

Parameters

NameTypeDescription
objectReadOnlyRecord<Key>Object to check.

Returns

fn

Curried function with context set.

▸ (key): boolean

Parameters
NameType
keyKey
Returns

boolean

Example

1
const isPropertyOfFoo = isPropertyOf({ "🟢": "🟩" });
2
isPropertyOfFoo("🟢"); // true
3
isPropertyOfFoo("🟩"); // false

View source


isPrototypeOf

isPrototypeOf<Constructor>(constructor): <Input>(object: Input) => boolean

Checks if given input’s prototype comes directly from given constructor.

Type parameters

NameType
Constructorextends Class

Parameters

NameTypeDescription
constructorConstructorConstructor to check.

Returns

fn

Returns a curried function with constructor in context.

▸ <Input>(object): boolean

Type parameters
NameType
Inputextends object
Parameters
NameType
objectInput
Returns

boolean

Example

1
const isPrototypeOfObject = isPrototypeOf(Object);
2
isPrototypeOfObject({}); // true
3
isPrototypeOfObject(/./); // false

View source


isPrototypeOfObject

isPrototypeOfObject<Input>(object): boolean

Given input’s prototype comes directly from Object.

Type parameters

NameType
Inputextends object

Parameters

NameType
objectInput

Returns

boolean

true if the given value is an object inheriting directly from Object, false otherwise.

Example

1
isPrototypeOfObject({}); // true
2
isPrototypeOfObject(/./); // false

View source


isType

isType<Type>(type): (input: unknown) => input is TypeOfDictionary[Type]

Takes a type string and checks if given input is of that typeof. This “patches” typeof so null is not "object" but "null" instead (rejected proposal for lack of backwards compatibility, more details here).

Type parameters

NameType
Typeextends keyof TypeOfDictionary

Parameters

NameType
typeType

Returns

fn

Curried function with type in context that returns true if input is of typeof type, false otherwise.

▸ (input): input is TypeOfDictionary[Type]

Parameters
NameType
inputunknown
Returns

input is TypeOfDictionary[Type]

Example

1
const isString = isType("string");
2
3
isString("value"); // true
4
isString(1); // false

View source

Primitives

asyncIteratorSymbol

Const asyncIteratorSymbol: typeof Symbol.asyncIterator

Symbol.asyncIterator shorthand (useful for minification/bundling).

See

Symbol.asyncIterator

View source


iteratorSymbol

Const iteratorSymbol: typeof Symbol.iterator

Symbol.iterator shorthand (useful for minification/bundling).

See

Symbol.iterator

View source


between

between(start): (end: string | Numeric) => (value: string | Numeric) => boolean

Takes a start and end and returns a boolean if value number or string is between them.

Parameters

NameTypeDescription
startstring | NumericMinimum boundary.

Returns

fn

Curried function with start in context.

▸ (end): (value: string | Numeric) => boolean

Parameters
NameType
endstring | Numeric
Returns

fn

▸ (value): boolean

Parameters
NameType
valuestring | Numeric
Returns

boolean

Example

1
const between0 = between(0);
2
const between0And10 = between0(10);
3
4
between0And10(5); // true
5
between0And10(-1); // false
6
between0And10(11); // false

View source


isBigInt

isBigInt(input): input is bigint

typeof “bigint” alias.

Parameters

NameType
inputunknown

Returns

input is bigint

true if the given value is a bigint, false otherwise.

Example

1
isBigInt(1n); // true
2
isBigInt(1); // false

View source


isBoolean

isBoolean(input): input is boolean

typeof “boolean” alias.

Parameters

NameType
inputunknown

Returns

input is boolean

true if the given value is a boolean, false otherwise.

Example

1
isBoolean(true); // true
2
isBoolean(false); // true
3
isBoolean(null); // false

View source


isNull

isNull(input): input is null

typeof “null” alias. This “patches” typeof so null is not "object" but "null" instead (rejected proposal for lack of backwards compatibility, more details here).

Parameters

NameType
inputunknown

Returns

input is null

true if the given value is a null, false otherwise.

Example

1
isNull(null); // true
2
isNull(undefined); // false

View source


isNullish

isNullish(input?): input is Nullish

Check if input is undefined or null.

Parameters

NameTypeDefault value
inputunknownnull

Returns

input is Nullish

true if nullish, false otherwise.

Example

1
isNullish(undefined); // true
2
isNullish(null); // true
3
isNullish(""); // false
4
isNullish(42); // false

View source


isNumber

isNumber(input): input is number

typeof “number” alias.

Parameters

NameType
inputunknown

Returns

input is number

true if the given value is a number, false otherwise.

Example

1
isNumber(1); // true
2
isNumber(Infinity); // true
3
isNumber(NaN); // true
4
isNumber(1n); // false

View source


isSafeInteger

isSafeInteger(...input): boolean

Check if value passed is a safe integer.

Parameters

NameType
...inputSingle<number>

Returns

boolean

Example

1
isSafeInteger(13); // true
2
isSafeInteger(10.13); // false

View source


isString

isString(input): input is string

typeof “string” alias.

Parameters

NameTypeDescription
inputunknownValue to check.

Returns

input is string

Returns true if value is a string, false otherwise.

Example

1
isString("Lou"); // true
2
isString(`Lou`); // true
3
isString(Symbol("Lou")); // false

View source


isSymbol

isSymbol(input): input is symbol

typeof “symbol” alias.

Parameters

NameTypeDescription
inputunknownValue to check.

Returns

input is symbol

Returns true if value is a symbol, false otherwise.

Example

1
isSymbol(Symbol("Lou")); // true
2
isSymbol(Symbol.iterator); // true
3
isSymbol("Lou"); // false

View source


isUndefined

isUndefined(input): input is undefined

typeof “undefined” alias.

Parameters

NameTypeDescription
inputunknownValue to check.

Returns

input is undefined

Returns true if value is undefined, false otherwise.

Example

1
isUndefined(undefined); // true
2
isUndefined(null); // false

View source


match

match(regularExpression): (text: string) => boolean

Given a regular expression and a string, returns true if the string matches the regular expression.

Parameters

NameTypeDescription
regularExpression{ dotAll: boolean ; flags: string ; global: boolean ; hasIndices: boolean ; ignoreCase: boolean ; lastIndex: number ; multiline: boolean ; source: string ; sticky: boolean ; unicode: boolean ; [matchAll]: (str: string) => IterableIterator<RegExpMatchArray> ; [match]: (string: string) => null | RegExpMatchArray ; [replace]: (string: string, replaceValue: string) => string(string: string, replacer: (substring: string, …args: any[]) => string) => string ; [search]: (string: string) => number ; [split]: (string: string, limit?: number) => string[] ; compile: (pattern: string, flags?: string) => this ; exec: (string: string) => null | RegExpExecArray ; test: (string: string) => boolean } | `/${string}/u` | `/${string}/su` | `/${string}/mu` | `/${string}/msu` | `/${string}/iu` | `/${string}/isu` | `/${string}/imu` | `/${string}/imsu` | `/${string}/gu` | `/${string}/gsu` | `/${string}/gmu` | `/${string}/gmsu` | `/${string}/giu` | `/${string}/gisu` | `/${string}/gimu` | `/${string}/gimsu`Instance of RegExp or a string.

Returns

fn

true if the string matches the regular expression, false otherwise.

▸ (text): boolean

Parameters
NameType
textstring
Returns

boolean

Example

1
const matchNumbers = match(/\d+/u);
2
3
matchNumbers("123"); // true
4
matchNumbers("abc"); // false

View source