Predicates Reference
🧐 Predicate util functions
Usage
📦 Node
Install @lou.codes/predicates
as a dependency:
Import it and use it:
🦕 Deno
Import @lou.codes/predicates
using the npm:
prefix, and use it directly:
🌎 Browser
Import @lou.codes/predicates
using esm.sh, and use it directly:
Useful links
- 📝 Documentation: TypeDoc generated documentation.
- ⏳ Changelog: List of changes between versions.
- ✅ Tests Coverage: Coveralls page with tests coverage.
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
Name | Type |
---|---|
expected | Expected |
Returns
fn
Curried function with expected
in context.
▸ (actual
): actual is Expected
Parameters
Name | Type |
---|---|
actual | unknown |
Returns
actual is Expected
Example
isFalsy
▸ isFalsy(input
): input is Falsy
Check if given input
is falsy (0, NaN, "", false, or nullish).
Parameters
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is Falsy
Returns true
if falsy, false
otherwise.
Example
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
Name | Type | Description |
---|---|---|
input | Input | Truthy <Readonly <Input >> | Value to check. |
Returns
input is Truthy<Readonly<Input>>
Returns true
if truthy, false
otherwise.
Example
Iterables
hasAsyncIteratorSymbol
▸ hasAsyncIteratorSymbol(object
): object is Readonly<Record<typeof
asyncIterator, unknown>>
Check if given object has the Symbol.asyncIterator
symbol.
Parameters
Name | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<typeof asyncIterator, unknown>>
true
when given object has the Symbol.asyncIterator
symbol, false
otherwise.
Example
hasIteratorSymbol
▸ hasIteratorSymbol(object
): object is Readonly<Record<typeof iterator,
unknown>>
Check if given object has the Symbol.iterator
symbol.
Parameters
Name | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<typeof iterator, unknown>>
true
when given object has the Symbol.iterator
symbol, false
otherwise.
Example
isArray
▸ isArray<Item
>(input
): input is ReadOnlyArray<Item>
Check if given input
is an instance of Array
.
Type parameters
Name |
---|
Item |
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is ReadOnlyArray<Item>
true
if the given value is an array, false
otherwise.
Example
isAsyncIterable
▸ isAsyncIterable<Item
>(input
): input is
Readonly<AsyncIterable<Item>>
Check if given value is AsyncIterable
.
Not to be confused with isAsynchronousIterable
which checks for both
AsyncIterable
and Iterable
.
Type parameters
Name |
---|
Item |
Parameters
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is Readonly<AsyncIterable<Item>>
true
when is an AsyncIterable
, false
otherwise.
Example
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
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is IsomorphicIterable<Item>
true
when is an IsomorphicIterable
, false
otherwise.
Example
isIterable
▸ isIterable<Item
>(input
): input is Readonly<Iterable<Item>>
Check if given value is Iterable
.
Type parameters
Name |
---|
Item |
Parameters
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is Readonly<Iterable<Item>>
true
when is an Iterable
, false
otherwise.
Example
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
Name | Type |
---|---|
Property | extends PropertyKey |
Parameters
Name | Type |
---|---|
property | Property |
Returns
fn
Curried function with property
in context.
▸ (object
): object is Readonly<Record<Property, unknown>>
Parameters
Name | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<Property, unknown>>
Example
hasAsyncIteratorSymbol
▸ hasAsyncIteratorSymbol(object
): object is Readonly<Record<typeof
asyncIterator, unknown>>
Check if given object has the Symbol.asyncIterator
symbol.
Parameters
Name | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<typeof asyncIterator, unknown>>
true
when given object has the Symbol.asyncIterator
symbol, false
otherwise.
Example
hasIteratorSymbol
▸ hasIteratorSymbol(object
): object is Readonly<Record<typeof iterator,
unknown>>
Check if given object has the Symbol.iterator
symbol.
Parameters
Name | Type |
---|---|
object | unknown |
Returns
object is Readonly<Record<typeof iterator, unknown>>
true
when given object has the Symbol.iterator
symbol, false
otherwise.
Example
isDate
▸ isDate(input
): input is Date
instanceof Date
alias.
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is Date
true
if the given value is a Date
, false
otherwise.
Example
isFunction
▸ isFunction(input
): input is Function
typeof
“function” alias.
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is Function
true
if the given value is a function, false
otherwise.
Example
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
Name | Type |
---|---|
Expected | extends Class <never > |
Parameters
Name | Type |
---|---|
constructor | Expected |
Returns
fn
Returns a curried function with constructor
in context.
▸ (input
): input is InstanceType<Expected>
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is InstanceType<Expected>
Example
isObject
▸ isObject(input
): input is object
typeof
“object” alias.
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is object
true
if the given value is an object
, false
otherwise.
Example
isPromise
▸ isPromise(input
): input is Promise<unknown>
instanceof Promise
alias.
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is Promise<unknown>
true
if the given value is an instance of Promise
, false
otherwise.
Example
isPropertyKey
▸ isPropertyKey(input
): input is PropertyKey
Checks if the given value is a valid PropertyKey of an object (string
,
symbol
, or number
).
Parameters
Name | 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
isPropertyOf
▸ isPropertyOf<Key
>(object
): (key
: Key
) => boolean
Check if the given key is present in the given object (not inherited).
Type parameters
Name | Type |
---|---|
Key | extends PropertyKey |
Parameters
Name | Type | Description |
---|---|---|
object | Readonly <Record <Key , unknown >> | Object to check. |
Returns
fn
Curried function with context
set.
▸ (key
): boolean
Parameters
Name | Type |
---|---|
key | Key |
Returns
boolean
Example
isPrototypeOf
▸ isPrototypeOf<Constructor
>(constructor
): <Input>(object
:
Input
) => boolean
Checks if given input
’s prototype comes directly from given constructor
.
Type parameters
Name | Type |
---|---|
Constructor | extends Class |
Parameters
Name | Type | Description |
---|---|---|
constructor | Constructor | Constructor to check. |
Returns
fn
Returns a curried function with constructor
in context.
▸ <Input
>(object
): boolean
Type parameters
Name | Type |
---|---|
Input | extends object |
Parameters
Name | Type |
---|---|
object | Input |
Returns
boolean
Example
isRegExp
▸ isRegExp(input
): input is RegExp
instanceof RegExp
alias.
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is RegExp
true
if the given value is an instance of RegExp
, false
otherwise.
Example
Predicates
isPropertyOf
▸ isPropertyOf<Key
>(object
): (key
: Key
) => boolean
Check if the given key is present in the given object (not inherited).
Type parameters
Name | Type |
---|---|
Key | extends PropertyKey |
Parameters
Name | Type | Description |
---|---|---|
object | Readonly <Record <Key , unknown >> | Object to check. |
Returns
fn
Curried function with context
set.
▸ (key
): boolean
Parameters
Name | Type |
---|---|
key | Key |
Returns
boolean
Example
isPrototypeOf
▸ isPrototypeOf<Constructor
>(constructor
): <Input>(object
:
Input
) => boolean
Checks if given input
’s prototype comes directly from given constructor
.
Type parameters
Name | Type |
---|---|
Constructor | extends Class |
Parameters
Name | Type | Description |
---|---|---|
constructor | Constructor | Constructor to check. |
Returns
fn
Returns a curried function with constructor
in context.
▸ <Input
>(object
): boolean
Type parameters
Name | Type |
---|---|
Input | extends object |
Parameters
Name | Type |
---|---|
object | Input |
Returns
boolean
Example
isPrototypeOfObject
▸ isPrototypeOfObject<Input
>(object
): boolean
Given input
’s prototype comes directly from Object.
Type parameters
Name | Type |
---|---|
Input | extends object |
Parameters
Name | Type |
---|---|
object | Input |
Returns
boolean
true
if the given value is an object inheriting directly from Object
,
false
otherwise.
Example
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
Name | Type |
---|---|
Type | extends keyof TypeOfDictionary |
Parameters
Name | Type |
---|---|
type | Type |
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
Name | Type |
---|---|
input | unknown |
Returns
input is TypeOfDictionary[Type]
Example
Primitives
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
Name | Type | Description |
---|---|---|
start | string | Numeric | Minimum boundary. |
Returns
fn
Curried function with start
in context.
▸ (end
): (value
: string
| Numeric
) => boolean
Parameters
Name | Type |
---|---|
end | string | Numeric |
Returns
fn
▸ (value
): boolean
Parameters
Name | Type |
---|---|
value | string | Numeric |
Returns
boolean
Example
isBigInt
▸ isBigInt(input
): input is bigint
typeof
“bigint” alias.
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is bigint
true
if the given value is a bigint
, false
otherwise.
Example
isBoolean
▸ isBoolean(input
): input is boolean
typeof
“boolean” alias.
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is boolean
true
if the given value is a boolean
, false
otherwise.
Example
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
Name | Type |
---|---|
input | unknown |
Returns
input is null
true
if the given value is a null
, false
otherwise.
Example
isNullish
▸ isNullish(input?
): input is Nullish
Check if input
is undefined
or null
.
Parameters
Name | Type | Default value |
---|---|---|
input | unknown | null |
Returns
input is Nullish
true
if nullish, false
otherwise.
Example
isNumber
▸ isNumber(input
): input is number
typeof
“number” alias.
Parameters
Name | Type |
---|---|
input | unknown |
Returns
input is number
true
if the given value is a number
, false
otherwise.
Example
isSafeInteger
▸ isSafeInteger(input
): boolean
Check if value passed is a safe integer.
Parameters
Name | Type |
---|---|
input | number |
Returns
boolean
Example
isString
▸ isString(input
): input is string
typeof
“string” alias.
Parameters
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is string
Returns true
if value is a string
, false
otherwise.
Example
isSymbol
▸ isSymbol(input
): input is symbol
typeof
“symbol” alias.
Parameters
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is symbol
Returns true
if value is a symbol
, false
otherwise.
Example
isUndefined
▸ isUndefined(input
): input is undefined
typeof
“undefined” alias.
Parameters
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is undefined
Returns true
if value is undefined
, false
otherwise.
Example
match
▸ match(regularExpression
): (text
: string
) => boolean
Given a regular expression and a string, returns true
if the string matches
the regular expression.
Parameters
Name | 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
fn
true
if the string matches the regular expression, false
otherwise.
▸ (text
): boolean
Parameters
Name | Type |
---|---|
text | string |
Returns
boolean
Example