Predicates Reference
๐ง Predicate util functions
Usage
๐ฆ Node
Install @lou.codes/predicates
as a dependency:
1pnpm add @lou.codes/predicates2# or3npm install @lou.codes/predicates4# or5yarn add @lou.codes/predicates
Import it and use it:
1import { isBoolean } from "@lou.codes/predicates";2
3isBoolean(true); // true4isBoolean(false); // true5isBoolean(undefined); // false
๐ฆ Deno
Import @lou.codes/predicates
using the npm:
prefix, and use it directly:
1import { isBoolean } from "npm:@lou.codes/predicates";2
3isBoolean(true); // true4isBoolean(false); // true5isBoolean(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); // true5 isBoolean(false); // true6 isBoolean(undefined); // false7</script>
Useful links
- ๐ Documentation: TypeDoc generated documentation.
- โณ Changelog: List of changes between versions.
- โ Tests Coverage: Coveralls page with tests coverage.
Common
is()
1function is<Expected>(2 expected: Expected,3): (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
Type parameter |
---|
Expected |
Parameters
Parameter | Type |
---|---|
expected | Expected |
Returns
Function
Curried function with expected
in context.
Parameters
Parameter | Type |
---|---|
actual | unknown |
Returns
actual is Expected
Example
1const is2 = is(2);2
3is2(2); // true4is2(8); // false
isFalsy()
1function isFalsy(input: unknown): input is Falsy;
Check if given input
is falsy (0, NaN, "", false, or nullish).
Parameters
Parameter | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is Falsy
Returns true
if falsy, false
otherwise.
Example
1isFalsy(false); // true2isFalsy(0); // true3isFalsy(NaN); // true4isFalsy(""); // true5isFalsy(null); // true6isFalsy(undefined); // true7isFalsy(42); // false
isTruthy()
1function isTruthy<Input>(2 input: Input | Truthy<Readonly<Input>>,3): input is Truthy<Readonly<Input>>;
Check if given input
is truthy (so not 0, NaN, "", false, or nullish).
Type parameters
Type parameter |
---|
Input |
Parameters
Parameter | Type | Description |
---|---|---|
input | Input | Truthy <Readonly <Input >> | Value to check. |
Returns
input is Truthy<Readonly<Input>>
Returns true
if truthy, false
otherwise.
Example
1isTruthy(42); // true2isTruthy(true); // true3isTruthy(false); // false4isTruthy(0); // false
Iterables
hasAsyncIteratorSymbol()
1function hasAsyncIteratorSymbol(2 object: unknown,3): object is Readonly<Record<typeof asyncIterator, unknown>>;
Check if given object has the Symbol.asyncIterator
symbol.
Parameters
Parameter | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<typeof asyncIterator, unknown>>
true
when given object has the Symbol.asyncIterator
symbol, false
otherwise.
Example
1hasAsyncIteratorSymbol({ [Symbol.asyncIterator]() {} }); // true2hasAsyncIteratorSymbol({ bar: "bar" }); // false
hasIteratorSymbol()
1function hasIteratorSymbol(2 object: unknown,3): object is Readonly<Record<typeof iterator, unknown>>;
Check if given object has the Symbol.iterator
symbol.
Parameters
Parameter | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<typeof iterator, unknown>>
true
when given object has the Symbol.iterator
symbol, false
otherwise.
Example
1hasIteratorSymbol({ [Symbol.iterator]() {} }); // true2hasIteratorSymbol({ bar: "bar" }); // false
isArray()
1function isArray<Item>(input: unknown): input is ReadOnlyArray<Item>;
Check if given input
is an instance of Array
.
Type parameters
Type parameter |
---|
Item |
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is ReadOnlyArray<Item>
true
if the given value is an array, false
otherwise.
Example
1isArray([]); // true2isArray({ length: 42 }); // false
isAsyncIterable()
1function isAsyncIterable<Item>(2 input: unknown,3): input is Readonly<AsyncIterable<Item, any, any>>;
Check if given value is AsyncIterable
.
Not to be confused with isAsynchronousIterable
which checks for both
AsyncIterable
and Iterable
.
Type parameters
Type parameter |
---|
Item |
Parameters
Parameter | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is Readonly<AsyncIterable<Item, any, any>>
true
when is an AsyncIterable
, false
otherwise.
Example
1isAsyncIterable(2 (async function* () {3 yield* [];4 })(),5); // true6isAsyncIterable([]); // false7isAsyncIterable({}); // false
isIsomorphicIterable()
1function isIsomorphicIterable<Item>(2 input: unknown,3): 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
Type parameter |
---|
Item |
Parameters
Parameter | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is IsomorphicIterable<Item>
true
when is an IsomorphicIterable
, false
otherwise.
Example
1isIterable([]); // true2isIterable({}); // false
isIterable()
1function isIterable<Item>(2 input: unknown,3): input is Readonly<Iterable<Item, any, any>>;
Check if given value is Iterable
.
Type parameters
Type parameter |
---|
Item |
Parameters
Parameter | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is Readonly<Iterable<Item, any, any>>
true
when is an Iterable
, false
otherwise.
Example
1isIterable([]); // true2isIterable({}); // false
Objects
has()
1function has<Property>(2 property: Property,3): (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
Type parameter |
---|
Property extends PropertyKey |
Parameters
Parameter | Type |
---|---|
property | Property |
Returns
Function
Curried function with property
in context.
Parameters
Parameter | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<Property, unknown>>
Example
1const hasCircle = has("๐ข");2
3hasCircle({ "๐ข": "๐ฉ" }); // true4hasCircle({ "๐ฉ": "๐ข" }); // false
hasAsyncIteratorSymbol()
1function hasAsyncIteratorSymbol(2 object: unknown,3): object is Readonly<Record<typeof asyncIterator, unknown>>;
Check if given object has the Symbol.asyncIterator
symbol.
Parameters
Parameter | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<typeof asyncIterator, unknown>>
true
when given object has the Symbol.asyncIterator
symbol, false
otherwise.
Example
1hasAsyncIteratorSymbol({ [Symbol.asyncIterator]() {} }); // true2hasAsyncIteratorSymbol({ bar: "bar" }); // false
hasIteratorSymbol()
1function hasIteratorSymbol(2 object: unknown,3): object is Readonly<Record<typeof iterator, unknown>>;
Check if given object has the Symbol.iterator
symbol.
Parameters
Parameter | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<typeof iterator, unknown>>
true
when given object has the Symbol.iterator
symbol, false
otherwise.
Example
1hasIteratorSymbol({ [Symbol.iterator]() {} }); // true2hasIteratorSymbol({ bar: "bar" }); // false
isDate()
1function isDate(input: unknown): input is Date;
instanceof Date
alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is Date
true
if the given value is a Date
, false
otherwise.
Example
1isBigInt(1n); // true2isBigInt(1); // false
isFunction()
1function isFunction(input: unknown): input is Function;
typeof
โfunctionโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is Function
true
if the given value is a function, false
otherwise.
Example
1isFunction(() => {}); // true2isFunction(function () {}); // true3isFunction(function* () {}); // true4isFunction(class {}); // true5isFunction(null); // false
isInstanceOf()
1function isInstanceOf<Expected>(2 constructor: Expected,3): (input: unknown) => input is InstanceType<Expected>;
Takes a constructor
and checks if given input
is an instance of it.
Type parameters
Type parameter |
---|
Expected extends Class <never > |
Parameters
Parameter | Type |
---|---|
constructor | Expected |
Returns
Function
Returns a curried function with constructor
in context.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is InstanceType<Expected>
Example
1const instanceOfArray = instanceOf(Array);2
3instanceOfArray([]); // true4instanceOfArray({}); // false
isObject()
1function isObject(input: unknown): input is object;
typeof
โobjectโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is object
true
if the given value is an object
, false
otherwise.
Example
1isObject({}); // true2isObject([]); // true3isObject(new Date()); // true4isObject(null); // false
isPromise()
1function isPromise(input: unknown): input is Promise<unknown>;
instanceof Promise
alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is Promise<unknown>
true
if the given value is an instance of Promise
, false
otherwise.
Example
1isPromise(new Promise()); // true2isPromise((async () => {})()); // true3isPromise(fetch("/")); // true4isPromise(Promise.resolve("Lou")); // true5isPromise("Lou"); // false
isPropertyKey()
1function isPropertyKey(input: unknown): input is PropertyKey;
Checks if the given value is a valid PropertyKey of an object (string
,
symbol
, or number
).
Parameters
Parameter | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is PropertyKey
true
if the given value is a valid PropertyKey of an object, false
otherwise.
Example
1isPropertyKey("Lou"); // true2isPropertyKey(1); // true3isPropertyKey(Symbol("Lou")); // true4isPropertyKey({}); // false
isPropertyOf()
1function isPropertyOf<Key>(2 object: Readonly<Record<Key, unknown>>,3): (key: Key) => boolean;
Check if the given key is present in the given object (not inherited).
Type parameters
Type parameter |
---|
Key extends PropertyKey |
Parameters
Parameter | Type | Description |
---|---|---|
object | Readonly <Record <Key , unknown >> | Object to check. |
Returns
Function
Curried function with context
set.
Parameters
Parameter | Type |
---|---|
key | Key |
Returns
boolean
Example
1const isPropertyOfFoo = isPropertyOf({ "๐ข": "๐ฉ" });2isPropertyOfFoo("๐ข"); // true3isPropertyOfFoo("๐ฉ"); // false
isPrototypeOf()
1function isPrototypeOf<Constructor>(2 constructor: Constructor,3): <Input>(object: Input) => boolean;
Checks if given input
โs prototype comes directly from given constructor
.
Type parameters
Type parameter |
---|
Constructor extends Class |
Parameters
Parameter | Type | Description |
---|---|---|
constructor | Constructor | Constructor to check. |
Returns
Function
Returns a curried function with constructor
in context.
Type parameters
Type parameter |
---|
Input extends object |
Parameters
Parameter | Type |
---|---|
object | Input |
Returns
boolean
Example
1const isPrototypeOfObject = isPrototypeOf(Object);2isPrototypeOfObject({}); // true3isPrototypeOfObject(/./); // false
isRegExp()
1function isRegExp(input: unknown): input is RegExp;
instanceof RegExp
alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is RegExp
true
if the given value is an instance of RegExp
, false
otherwise.
Example
1isRegExp(new RegExp("-")); // true2isRegExp(/-/); // true3isRegExp("Lou"); // false
Predicates
isPropertyOf()
1function isPropertyOf<Key>(2 object: Readonly<Record<Key, unknown>>,3): (key: Key) => boolean;
Check if the given key is present in the given object (not inherited).
Type parameters
Type parameter |
---|
Key extends PropertyKey |
Parameters
Parameter | Type | Description |
---|---|---|
object | Readonly <Record <Key , unknown >> | Object to check. |
Returns
Function
Curried function with context
set.
Parameters
Parameter | Type |
---|---|
key | Key |
Returns
boolean
Example
1const isPropertyOfFoo = isPropertyOf({ "๐ข": "๐ฉ" });2isPropertyOfFoo("๐ข"); // true3isPropertyOfFoo("๐ฉ"); // false
isPrototypeOf()
1function isPrototypeOf<Constructor>(2 constructor: Constructor,3): <Input>(object: Input) => boolean;
Checks if given input
โs prototype comes directly from given constructor
.
Type parameters
Type parameter |
---|
Constructor extends Class |
Parameters
Parameter | Type | Description |
---|---|---|
constructor | Constructor | Constructor to check. |
Returns
Function
Returns a curried function with constructor
in context.
Type parameters
Type parameter |
---|
Input extends object |
Parameters
Parameter | Type |
---|---|
object | Input |
Returns
boolean
Example
1const isPrototypeOfObject = isPrototypeOf(Object);2isPrototypeOfObject({}); // true3isPrototypeOfObject(/./); // false
isPrototypeOfObject()
1function isPrototypeOfObject<Input>(object: Input): boolean;
Given input
โs prototype comes directly from Object.
Type parameters
Type parameter |
---|
Input extends object |
Parameters
Parameter | Type |
---|---|
object | Input |
Returns
boolean
true
if the given value is an object inheriting directly from Object
,
false
otherwise.
Example
1isPrototypeOfObject({}); // true2isPrototypeOfObject(/./); // false
isType()
1function isType<Type>(2 type: Type,3): (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
Type parameter |
---|
Type extends keyof TypeOfDictionary |
Parameters
Parameter | Type |
---|---|
type | Type |
Returns
Function
Curried function with type
in context that returns true
if input
is of
typeof
type
, false
otherwise.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is TypeOfDictionary[Type]
Example
1const isString = isType("string");2
3isString("value"); // true4isString(1); // false
Primitives
between()
1function between(2 start: string | Numeric,3): (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
Parameter | Type | Description |
---|---|---|
start | string | Numeric | Minimum boundary. |
Returns
Function
Curried function with start
in context.
Parameters
Parameter | Type |
---|---|
end | string | Numeric |
Returns
Function
Parameters
Parameter | Type |
---|---|
value | string | Numeric |
Returns
boolean
Example
1const between0 = between(0);2const between0And10 = between0(10);3
4between0And10(5); // true5between0And10(-1); // false6between0And10(11); // false
isBigInt()
1function isBigInt(input: unknown): input is bigint;
typeof
โbigintโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is bigint
true
if the given value is a bigint
, false
otherwise.
Example
1isBigInt(1n); // true2isBigInt(1); // false
isBoolean()
1function isBoolean(input: unknown): input is boolean;
typeof
โbooleanโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is boolean
true
if the given value is a boolean
, false
otherwise.
Example
1isBoolean(true); // true2isBoolean(false); // true3isBoolean(null); // false
isNull()
1function isNull(input: unknown): 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
Parameter | Type |
---|---|
input | unknown |
Returns
input is null
true
if the given value is a null
, false
otherwise.
Example
1isNull(null); // true2isNull(undefined); // false
isNullish()
1function isNullish(input: unknown): input is Nullish;
Check if input
is undefined
or null
.
Parameters
Parameter | Type | Default value |
---|---|---|
input | unknown | null |
Returns
input is Nullish
true
if nullish, false
otherwise.
Example
1isNullish(undefined); // true2isNullish(null); // true3isNullish(""); // false4isNullish(42); // false
isNumber()
1function isNumber(input: unknown): input is number;
typeof
โnumberโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is number
true
if the given value is a number
, false
otherwise.
Example
1isNumber(1); // true2isNumber(Infinity); // true3isNumber(NaN); // true4isNumber(1n); // false
isSafeInteger()
1function isSafeInteger(input: number): boolean;
Check if value passed is a safe integer.
Parameters
Parameter | Type |
---|---|
input | number |
Returns
boolean
Example
1isSafeInteger(13); // true2isSafeInteger(10.13); // false
isString()
1function isString(input: unknown): input is string;
typeof
โstringโ alias.
Parameters
Parameter | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is string
Returns true
if value is a string
, false
otherwise.
Example
1isString("Lou"); // true2isString(`Lou`); // true3isString(Symbol("Lou")); // false
isSymbol()
1function isSymbol(input: unknown): input is symbol;
typeof
โsymbolโ alias.
Parameters
Parameter | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is symbol
Returns true
if value is a symbol
, false
otherwise.
Example
1isSymbol(Symbol("Lou")); // true2isSymbol(Symbol.iterator); // true3isSymbol("Lou"); // false
isUndefined()
1function isUndefined(input: unknown): input is undefined;
typeof
โundefinedโ alias.
Parameters
Parameter | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is undefined
Returns true
if value is undefined
, false
otherwise.
Example
1isUndefined(undefined); // true2isUndefined(null); // false
match()
1function match(2 regularExpression:3 | Readonly<RegExp>4 | `/${string}/u`5 | `/${string}/su`6 | `/${string}/mu`7 | `/${string}/msu`8 | `/${string}/iu`9 | `/${string}/isu`10 | `/${string}/imu`11 | `/${string}/imsu`12 | `/${string}/gu`13 | `/${string}/gsu`14 | `/${string}/gmu`15 | `/${string}/gmsu`16 | `/${string}/giu`17 | `/${string}/gisu`18 | `/${string}/gimu`19 | `/${string}/gimsu`,20): (text: string) => boolean;
Given a regular expression and a string, returns true
if the string matches
the regular expression.
Parameters
Parameter | Type | Description |
---|---|---|
regularExpression | | Readonly <RegExp > | `/${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
Function
true
if the string matches the regular expression, false
otherwise.
Parameters
Parameter | Type |
---|---|
text | string |
Returns
boolean
Example
1const matchNumbers = match(/\d+/u);2
3matchNumbers("123"); // true4matchNumbers("abc"); // false