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()
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
isFalsy()
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
isTruthy()
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
Iterables
hasAsyncIteratorSymbol()
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
hasIteratorSymbol()
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
isArray()
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
isAsyncIterable()
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>>
true
when is an AsyncIterable
, false
otherwise.
Example
isIsomorphicIterable()
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
isIterable()
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>>
true
when is an Iterable
, false
otherwise.
Example
Objects
has()
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
hasAsyncIteratorSymbol()
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
hasIteratorSymbol()
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
isDate()
instanceof Date
alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is Date
true
if the given value is a Date
, false
otherwise.
Example
isFunction()
typeof
โfunctionโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is Function
true
if the given value is a function, false
otherwise.
Example
isInstanceOf()
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
isObject()
typeof
โobjectโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is object
true
if the given value is an object
, false
otherwise.
Example
isPromise()
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
isPropertyKey()
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
isPropertyOf()
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
isPrototypeOf()
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
isRegExp()
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
Predicates
isPropertyOf()
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
isPrototypeOf()
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
isPrototypeOfObject()
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
isType()
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
Primitives
between()
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
isBigInt()
typeof
โbigintโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is bigint
true
if the given value is a bigint
, false
otherwise.
Example
isBoolean()
typeof
โbooleanโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is boolean
true
if the given value is a boolean
, false
otherwise.
Example
isNull()
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
isNullish()
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
isNumber()
typeof
โnumberโ alias.
Parameters
Parameter | Type |
---|---|
input | unknown |
Returns
input is number
true
if the given value is a number
, false
otherwise.
Example
isSafeInteger()
Check if value passed is a safe integer.
Parameters
Parameter | Type |
---|---|
input | number |
Returns
boolean
Example
isString()
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
isSymbol()
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
isUndefined()
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
match()
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