Predicates by Lou
🧐 Predicate util functions
Usage
📦 Node
Install @lou.codes/predicates
as a dependency:
pnpm add @lou.codes/predicates# ornpm install @lou.codes/predicates# oryarn add @lou.codes/predicates
Import it and use it:
import { isBoolean } from "@lou.codes/predicates";
isBoolean(true); // trueisBoolean(false); // trueisBoolean(undefined); // false
🦕 Deno
Import @lou.codes/predicates
using the npm:
prefix, and use it directly:
import { isBoolean } from "npm:@lou.codes/predicates";
isBoolean(true); // trueisBoolean(false); // trueisBoolean(undefined); // false
🌎 Browser
Import @lou.codes/predicates
using esm.sh, and use it directly:
<script type="module"> import { isBoolean } from "https://esm.sh/@lou.codes/predicates";
isBoolean(true); // true isBoolean(false); // true isBoolean(undefined); // false</script>
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
const is2 = is(2);
is2(2); // trueis2(8); // false
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
isFalsy(false); // trueisFalsy(0); // trueisFalsy(NaN); // trueisFalsy(""); // trueisFalsy(null); // trueisFalsy(undefined); // trueisFalsy(42); // false
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
isTruthy(42); // trueisTruthy(true); // trueisTruthy(false); // falseisTruthy(0); // false
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
hasAsyncIteratorSymbol({ [Symbol.asyncIterator]() {} }); // truehasAsyncIteratorSymbol({ bar: "bar" }); // false
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
hasIteratorSymbol({ [Symbol.iterator]() {} }); // truehasIteratorSymbol({ bar: "bar" }); // false
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
isArray([]); // trueisArray({ length: 42 }); // false
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
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is Object
true
when is an AsyncIterable
, false
otherwise.
Example
isAsyncIterable((async function * () { yield* [] })()); // trueisAsyncIterable([]); // falseisAsyncIterable({}); // false
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([]); // trueisIterable({}); // false
isIterable
▸ isIterable<Item
>(input
): input is Object
Check if given value is Iterable
.
Type parameters
Name |
---|
Item |
Parameters
Name | Type | Description |
---|---|---|
input | unknown | Value to check. |
Returns
input is Object
true
when is an Iterable
, false
otherwise.
Example
isIterable([]); // trueisIterable({}); // false
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
const hasCircle = has("🟢");
hasCircle({ "🟢": "🟩" }); // truehasCircle({ "🟩": "🟢" }); // false
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
hasAsyncIteratorSymbol({ [Symbol.asyncIterator]() {} }); // truehasAsyncIteratorSymbol({ bar: "bar" }); // false
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
hasIteratorSymbol({ [Symbol.iterator]() {} }); // truehasIteratorSymbol({ bar: "bar" }); // false
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
isBigInt(1n); // trueisBigInt(1); // false
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
isFunction(() => {}); // trueisFunction(function () {}); // trueisFunction(function* () {}); // trueisFunction(class {}); // trueisFunction(null); // false
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
const instanceOfArray = instanceOf(Array);
instanceOfArray([]); // trueinstanceOfArray({}); // false
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
isObject({}); // trueisObject([]); // trueisObject(new Date()); // trueisObject(null); // false
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
isPromise(new Promise()); // trueisPromise((async () => {})()); // trueisPromise(fetch("/")); // trueisPromise(Promise.resolve("Lou")); // trueisPromise("Lou"); // false
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
isPropertyKey("Lou"); // trueisPropertyKey(1); // trueisPropertyKey(Symbol("Lou")); // trueisPropertyKey({}); // false
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 | ReadOnlyRecord <Key > | Object to check. |
Returns
fn
Curried function with context
set.
▸ (key
): boolean
Parameters
Name | Type |
---|---|
key | Key |
Returns
boolean
Example
const isPropertyOfFoo = isPropertyOf({ "🟢": "🟩" });isPropertyOfFoo("🟢"); // trueisPropertyOfFoo("🟩"); // false
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
const isPrototypeOfObject = isPrototypeOf(Object);isPrototypeOfObject({}); // trueisPrototypeOfObject(/./); // false
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
isRegExp(new RegExp("-")); // trueisRegExp(/-/); // trueisRegExp("Lou"); // false
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 | ReadOnlyRecord <Key > | Object to check. |
Returns
fn
Curried function with context
set.
▸ (key
): boolean
Parameters
Name | Type |
---|---|
key | Key |
Returns
boolean
Example
const isPropertyOfFoo = isPropertyOf({ "🟢": "🟩" });isPropertyOfFoo("🟢"); // trueisPropertyOfFoo("🟩"); // false
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
const isPrototypeOfObject = isPrototypeOf(Object);isPrototypeOfObject({}); // trueisPrototypeOfObject(/./); // false
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
isPrototypeOfObject({}); // trueisPrototypeOfObject(/./); // false
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
const isString = isType("string");
isString("value"); // trueisString(1); // false
Primitives
asyncIteratorSymbol
• Const
asyncIteratorSymbol: typeof Symbol.asyncIterator
Symbol.asyncIterator
shorthand (useful for minification/bundling).
See
iteratorSymbol
• Const
iteratorSymbol: typeof Symbol.iterator
Symbol.iterator
shorthand (useful for minification/bundling).
See
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
const between0 = between(0);const between0And10 = between0(10);
between0And10(5); // truebetween0And10(-1); // falsebetween0And10(11); // false
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
isBigInt(1n); // trueisBigInt(1); // false
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
isBoolean(true); // trueisBoolean(false); // trueisBoolean(null); // false
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
isNull(null); // trueisNull(undefined); // false
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
isNullish(undefined); // trueisNullish(null); // trueisNullish(""); // falseisNullish(42); // false
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
isNumber(1); // trueisNumber(Infinity); // trueisNumber(NaN); // trueisNumber(1n); // false
isSafeInteger
▸ isSafeInteger(...input
): boolean
Check if value passed is a safe integer.
Parameters
Name | Type |
---|---|
...input | Single <number > |
Returns
boolean
Example
isSafeInteger(13); // trueisSafeInteger(10.13); // false
Defined in
packages/@lou.codes/predicates/node_modules/@lou.codes/types/dist/Function.d.ts:20
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
isString("Lou"); // trueisString(`Lou`); // trueisString(Symbol("Lou")); // false
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
isSymbol(Symbol("Lou")); // trueisSymbol(Symbol.iterator); // trueisSymbol("Lou"); // false
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
isUndefined(undefined); // trueisUndefined(null); // false
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 | { 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 ) => RegExp ; 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
Name | Type |
---|---|
text | string |
Returns
boolean
Example
const matchNumbers = match(/\d+/u);
matchNumbers("123"); // truematchNumbers("abc"); // false