Skip to main content

Module: @sapphire/utilities

Interfaces

References

filterNullish

Renames and re-exports filterNullAndUndefined


filterNullishAndEmpty

Renames and re-exports filterNullAndUndefinedAndEmpty


filterNullishAndZero

Renames and re-exports filterNullAndUndefinedAndZero


filterNullishOrEmpty

Renames and re-exports filterNullAndUndefinedAndEmpty


filterNullishOrZero

Renames and re-exports filterNullAndUndefinedAndZero


isNullish

Renames and re-exports isNullOrUndefined


isNullishOrEmpty

Renames and re-exports isNullOrUndefinedOrEmpty


isNullishOrZero

Renames and re-exports isNullOrUndefinedOrZero


parseURL

Renames and re-exports tryParseURL


tryParse

Renames and re-exports tryParseJSON

Type Aliases

AbstractConstructor

Ƭ AbstractConstructor<T>: (...args: any[]) => T

A generic abstract constructor without parameters

Type parameters

Name
T

Type declaration

• (...args): T

Parameters
NameType
...argsany[]
Returns

T

Defined in

lib/types.ts:64


AbstractCtor

Ƭ AbstractCtor<A, R>: (...args: A) => R

A generic abstract constructor with parameters

Type parameters

NameType
Aextends Arr = readonly any[]
Rany

Type declaration

• (...args): R

Parameters
NameType
...argsA
Returns

R

Defined in

lib/types.ts:54


AnyObject

Ƭ AnyObject<T>: { [K in keyof T]: T[K] }

An object that can have any structure, this is an alternative to NonNullObject for situations where that leads to unexpected type resolutions.

Note that this is still a strictly typed type, it is not simply aliasing any

Type parameters

Name
T

Defined in

lib/types.ts:106


ArgumentTypes

Ƭ ArgumentTypes<F>: F extends (...args: infer A) => any ? A : never

Type parameters

NameType
Fextends (...args: any[]) => unknown

Defined in

lib/types.ts:38


Arr

Ƭ Private Arr: readonly any[]

A readonly array of any values.

Defined in

lib/types.ts:44


ArrayElementType

Ƭ ArrayElementType<T>: T extends infer K[] ? K : T extends readonly infer RK[] ? RK : T

Gets a union type of all the keys that are in an array.

Example

const sample = [1, 2, '3', true];

type arrayUnion = ArrayElementType<typeof sample>;
// Expected: string | number | boolean

Type parameters

Name
T

Defined in

lib/types.ts:186


Awaitable

Ƭ Awaitable<T>: PromiseLike<T> | T

ReturnType for a function that can return either a value or a Promise with that value

Type parameters

Name
T

Defined in

lib/types.ts:79


Builtin

Ƭ Builtin: Primitive | Function | Date | Error | RegExp

Defined in

lib/types.ts:4


Constructor

Ƭ Constructor<T>: (...args: any[]) => T

A generic constructor without parameters

Type parameters

Name
T

Type declaration

• (...args): T

Parameters
NameType
...argsany[]
Returns

T

Defined in

lib/types.ts:59


Ctor

Ƭ Ctor<A, R>: (...args: A) => R

A generic constructor with parameters

Type parameters

NameType
Aextends Arr = readonly any[]
Rany

Type declaration

• (...args): R

Parameters
NameType
...argsA
Returns

R

Defined in

lib/types.ts:49


DeepPartial

Ƭ DeepPartial<T>: { [P in keyof T]?: T[P] extends (infer U)[] ? DeepPartial<U>[] : T[P] extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : DeepPartial<T[P]> }

Type parameters

Name
T

Defined in

lib/types.ts:30


DeepRequired

Ƭ DeepRequired<T>: T extends Builtin ? NonNullable<T> : T extends Map<infer K, infer V> ? Map<DeepRequired<K>, DeepRequired<V>> : T extends ReadonlyMap<infer K, infer V> ? ReadonlyMap<DeepRequired<K>, DeepRequired<V>> : T extends WeakMap<infer K, infer V> ? WeakMap<DeepRequired<K>, DeepRequired<V>> : T extends Set<infer U> ? Set<DeepRequired<U>> : T extends ReadonlySet<infer U> ? ReadonlySet<DeepRequired<U>> : T extends WeakSet<infer U> ? WeakSet<DeepRequired<U>> : T extends Promise<infer U> ? Promise<DeepRequired<U>> : T extends ? { [K in keyof (...)]-?: DeepRequired<(...)> } : NonNullable<T>

Type parameters

Name
T

Defined in

lib/types.ts:6


FirstArgument

Ƭ FirstArgument<T>: T extends (arg1: infer U, ...args: unknown[]) => unknown ? U : unknown

Gets the first argument of any given function

Type parameters

Name
T

Defined in

lib/types.ts:69


Mutable

Ƭ Mutable<T>: { -readonly [P in keyof T]: T[P] extends unknown[] | NonNullObject ? Mutable<T[P]> : T[P] }

Transforms a readonly type to be mutable

Example

interface Sample {
id: string;
hobbies: readonly string[];
}

type BB = Mutable<Sample>;
// Expected:
// {
// id: string;
// hobbies: string[];
// }

Type parameters

Name
T

Defined in

lib/types.ts:149


NonNullObject

Ƭ NonNullObject: & object

An object that is non nullable, to bypass TypeScript not easily working with Record<PropertyKey, unknown> in various instances.

Defined in

lib/types.ts:98


NonNullableProperties

Ƭ NonNullableProperties<T>: { [P in keyof T]: NonNullable<T[P]> }

Similar to the built in NonNullable, but properly removes null from all keys in the class or interface This does not recurse deeply, for that use DeepRequired

Type parameters

NameType
Tunknown

Defined in

lib/types.ts:90


Nullish

Ƭ Nullish: null | undefined

Type union for the full 2 billion dollar mistake in the JavaScript ecosystem

Defined in

lib/types.ts:84


PartialRequired

Ƭ PartialRequired<T, K>: Partial<Omit<T, K>> & Required<Pick<T, K>>

Type parameters

NameType
TT
Kextends keyof T

Defined in

lib/types.ts:28


PickByValue

Ƭ PickByValue<T, V>: { [P in keyof T]: T[P] extends V ? P : never }[keyof T] & keyof T

Gets all the keys (as a string union) from a type T that match value V

Example

interface Sample {
id: string;
name: string | null;
middleName?: string;
lastName: string;
hobbies: readonly string[];
}

type BB = PickByValue<Sample, string>;
// Expected:
// "id" | "lastName"

Type parameters

Name
T
V

Defined in

lib/types.ts:127


Primitive

Ƭ Primitive: string | number | boolean | bigint | symbol | undefined | null

Defined in

lib/types.ts:1


RequiredExcept

Ƭ RequiredExcept<T, K>: Partial<Pick<T, K>> & Required<Omit<T, K>>

Type parameters

NameType
TT
Kextends keyof T

Defined in

lib/types.ts:26


SecondArgument

Ƭ SecondArgument<T>: T extends (arg1: unknown, arg2: infer U, ...args: unknown[]) => unknown ? U : unknown

Gets the second argument of any given function

Type parameters

Name
T

Defined in

lib/types.ts:74


StrictRequired

Ƭ StrictRequired<T>: { [P in keyof T]-?: NonNullable<T[P]> }

Transforms every key in an object to be strictly required, essentially removing undefined and null from the type.

Example

interface Sample {
id: string;
name: string | null;
middleName?: string;
}

type BB = StrictRequired<Sample>;
// Expected:
// {
// id: string;
// name: string;
// middleName: string;
// }

Type parameters

Name
T

Defined in

lib/types.ts:172


ThrottleFn

Ƭ ThrottleFn<T>: T & { flush: () => void }

Type parameters

NameType
Textends (...args: any[]) => any

Defined in

lib/throttle.ts:1

Functions

arrayStrictEquals

arrayStrictEquals<T>(arr1, arr2): boolean

Compare if both arrays are strictly equal

Type parameters

NameType
Textends readonly unknown[]

Parameters

NameTypeDescription
arr1TThe array to compare to
arr2TThe array to compare with

Returns

boolean

Defined in

lib/arrayStrictEquals.ts:6


cast

cast<T>(value): T

Casts any value to T

Note that this function is not type-safe, and may cause runtime errors if used incorrectly. Also note that this function is effectively useless in a JavaScript project, it only serves a purpose for TypeScript projects.

Type parameters

Name
T

Parameters

NameTypeDescription
valueunknownThe value to cast to another type

Returns

T

The value but as type T

Defined in

lib/cast.ts:10


chunk

chunk<T>(array, chunkSize): T[][]

Splits up an array into chunks

Type parameters

Name
T

Parameters

NameTypeDescription
arrayreadonly T[]The array to chunk up
chunkSizenumberThe size of each individual chunk

Returns

T[][]

Defined in

lib/chunk.ts:6


classExtends

classExtends<T>(value, base): value is T

Checks whether or not the value class extends the base class.

Type parameters

NameType
Textends Ctor

Parameters

NameTypeDescription
valueCtorThe constructor to be checked against.
baseTThe base constructor.

Returns

value is T

Defined in

lib/classExtends.ts:8


codeBlock

codeBlock<C>(content): ```` ${C} ````

Wraps the content inside a codeblock with no language

Type parameters

NameType
Cextends string

Parameters

NameType
contentC

Returns

```` ${C} ````

Remark

If the provided content includes 3 backticks (```) then those backticks will be escaped by adding a Zero Width Space between the first and second backtick

@remark If the provided content ends with a backtick then a Zero Width Space will be added to the end of the content

@param content - The content to wrap

Defined in

lib/codeBlock.ts:14

codeBlock<L, C>(language, content): ````${L} ${C} ````

Wraps the content inside a codeblock with the specified language

Type parameters

NameType
Lextends string
Cextends string

Parameters

NameType
languageL
contentC

Returns

````${L} ${C} ````

Remark

If the provided content includes 3 backticks (```) then those backticks will be escaped by adding a Zero Width Space between the first and second backtick

@remark If the provided content ends with a backtick then a Zero Width Space will be added to the end of the content

@param language The codeblock language @param content The expression to be wrapped in the codeblock

Defined in

lib/codeBlock.ts:28


cutText

cutText(str, length): string

Split a text by its latest space character in a range from the character 0 to the selected one.

Parameters

NameTypeDescription
strstringThe text to split.
lengthnumberThe length of the desired string.

Returns

string

Copyright

2019 Aura Román

License

Apache-2.0

Defined in

lib/cutText.ts:10


debounce

debounce<FnArgumentsType, FnReturnType>(func, options?): DebouncedFunc<FnArgumentsType, FnReturnType>

Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked. The debounced function comes with a cancel method to cancel delayed invocations and a flush method to immediately invoke them. Provide an options object to indicate that func should be invoked on the leading and/or trailing edge of the wait timeout. Subsequent calls to the debounced function return the result of the last func invocation.

Note: If leading and trailing options are true, func is invoked on the trailing edge of the timeout only if the the debounced function is invoked more than once during the wait timeout.

See David Corbacho’s article for details over the differences between _.debounce and _.throttle.

Type parameters

NameType
FnArgumentsTypeextends any[]
FnReturnTypeFnReturnType

Parameters

NameTypeDescription
func(...args: FnArgumentsType) => FnReturnTypeThe function to debounce.
optionsDebounceSettingsThe options object.

Returns

DebouncedFunc<FnArgumentsType, FnReturnType>

Returns the new debounced function.

Defined in

lib/debounce/debounce.ts:68


deepClone

deepClone<T>(source): T

Deep clone an object

Type parameters

Name
T

Parameters

NameTypeDescription
sourceTThe object to clone

Returns

T

Defined in

lib/deepClone.ts:18


filterNullAndUndefined

filterNullAndUndefined<TValue>(value): value is TValue

Checks whether a value is not null nor undefined. This can be used in Array.filter to remove null and undefined from the array type

Type parameters

Name
TValue

Parameters

NameTypeDescription
valueNullish | TValueThe value to verify that is neither null nor undefined

Returns

value is TValue

A boolean that is true if the value is neither null nor undefined, false otherwise.

Example

// TypeScript Type: (string | undefined | null)[]
const someArray = ['one', 'two', undefined, null, 'five'];

// TypeScript Type: string[]
const filteredArray = someArray.filter(filterNullAndUndefined);
// Result: ['one', 'two', 'five']

Defined in

lib/filterNullAndUndefined.ts:19


filterNullAndUndefinedAndEmpty

filterNullAndUndefinedAndEmpty<TValue>(value): value is TValue

Checks whether a value is not null nor undefined nor '' (empty string). This can be used in Array.filter to remove null, undefined from the array type

Type parameters

Name
TValue

Parameters

NameTypeDescription
value"" | Nullish | TValueThe value to verify that is neither null, undefined nor '' (empty string)

Returns

value is TValue

A boolean that is true if the value is neither null, undefined nor '' (empty string), false otherwise.

Example

// TypeScript Type: (string | undefined | null)[]
const someArray = ['one', 'two', undefined, null, ''];

// TypeScript Type: string[]
const filteredArray = someArray.filter(filterNullAndUndefinedAndEmpty);
// Result: ['one', 'two']

Defined in

lib/filterNullAndUndefinedAndEmpty.ts:19


filterNullAndUndefinedAndZero

filterNullAndUndefinedAndZero<TValue>(value): value is TValue

Checks whether a value is not null nor undefined nor 0. This can be used in Array.filter to remove null, undefined from the array type

Type parameters

Name
TValue

Parameters

NameTypeDescription
value0 | Nullish | TValueThe value to verify that is neither null, undefined nor 0

Returns

value is TValue

A boolean that is true if the value is neither null, undefined nor 0, false otherwise.

Example

// TypeScript Type: (string | number | undefined | null)[]
const someArray = ['one', 'two', undefined, null, 0, 1];

// TypeScript Type: (string | number)[]
const filteredArray = someArray.filter(filterNullAndUndefinedAndZero);
// Result: ['one', 'two', 1]

Defined in

lib/filterNullAndUndefinedAndZero.ts:19


getDeepObjectKeys

getDeepObjectKeys<T>(obj, options?): string[]

Flattens an object to a list of its keys, traversing deeply into nested objects and arrays of objects.

Type parameters

Name
T

Parameters

NameTypeDescription
objAnyObject<T>The object of which to deeply retrieve its keys
options?GetDeepObjectKeysOptionsThe options with which to customize the output of this function

Returns

string[]

An array of strings holding the keys of the object

Note

By default Nested array values are flattened to arrayKey.${index}.subKey. This can be changed to arrayKey[${index}].subKey by setting options.arrayKeysIndexStyle to 'braces-with-dot'. Or it can also be changed to arrayKey[${index}]subKey by setting options.arrayKeysIndexStyle to 'braces'.

Defined in

lib/getDeepObjectKeys.ts:15


hasAtLeastOneKeyInMap

hasAtLeastOneKeyInMap<T>(map, keys): boolean

Checks whether any of the keys are in the map

Type parameters

Name
T

Parameters

NameTypeDescription
mapReadonlyMap<T, any>The map to check
keysreadonly T[]The keys to find in the map

Returns

boolean

true if at least one of the keys is in the map, false otherwise.

Defined in

lib/hasAtLeastOneKeyInMap.ts:7


inlineCodeBlock

inlineCodeBlock<C>(content): ``${C}``

Wraps text in a markdown inline codeblock

Type parameters

NameType
Cextends string

Parameters

NameTypeDescription
contentCThe expression to be wrapped in the codeblock

Returns

``${C}``

Defined in

lib/inlineCodeBlock.ts:7


isClass

isClass(input): input is Ctor

Verify if the input is a class constructor.

Parameters

NameTypeDescription
inputunknownThe function to verify

Returns

input is Ctor

Defined in

lib/isClass.ts:7


isFunction

isFunction(input): input is Function

Verify if the input is a function.

Parameters

NameTypeDescription
inputunknownThe function to verify

Returns

input is Function

Defined in

lib/isFunction.ts:6


isNullOrUndefined

isNullOrUndefined(value): value is Nullish

Checks whether or not a value is null or undefined

Parameters

NameTypeDescription
valueunknownThe value to check

Returns

value is Nullish

Defined in

lib/isNullOrUndefined.ts:7


isNullOrUndefinedOrEmpty

isNullOrUndefinedOrEmpty(value): value is "" | Nullish

Checks whether or not a value is null, undefined or '', []

Parameters

NameTypeDescription
valueunknownThe value to check

Returns

value is "" | Nullish

Defined in

lib/isNullOrUndefinedOrEmpty.ts:8


isNullOrUndefinedOrZero

isNullOrUndefinedOrZero(value): value is 0 | Nullish

Checks whether or not a value is null, undefined or 0

Parameters

NameTypeDescription
valueunknownThe value to check

Returns

value is 0 | Nullish

Defined in

lib/isNullOrUndefinedOrZero.ts:8


isNumber

isNumber(input): input is number

Verify if a number is a finite number.

Parameters

NameTypeDescription
inputunknownThe number to verify

Returns

input is number

Defined in

lib/isNumber.ts:5


isObject

isObject(input, constructorType?): input is object

Verify if the input is an object literal (or class).

Parameters

NameTypeDescription
inputunknownThe object to verify
constructorType?ObjectConstructorThe type of the constructor of the object. Use this if you want a class of your choosing to pass the check as well.

Returns

input is object

Defined in

lib/isObject.ts:8

isObject<T>(input, constructorType): input is InstanceType<T>

Type parameters

NameType
Textends Constructor<unknown>

Parameters

NameType
inputunknown
constructorTypeT

Returns

input is InstanceType<T>

Defined in

lib/isObject.ts:9


isPrimitive

isPrimitive(input): input is string | number | bigint | boolean

Check whether a value is a primitive

Parameters

NameTypeDescription
inputunknownThe input to check

Returns

input is string | number | bigint | boolean

Defined in

lib/isPrimitive.ts:7


isThenable

isThenable(input): input is Thenable

Verify if an object is a promise.

Parameters

NameTypeDescription
inputunknownThe promise to verify

Returns

input is Thenable

Defined in

lib/isThenable.ts:21


lazy

lazy<T>(cb): () => T

Lazily creates a constant or load a module and caches it internally

Type parameters

Name
T

Parameters

NameTypeDescription
cb() => TThe callback to lazily run

Returns

fn

The value returned by the callback, or the cached value if it was already initialised once.

▸ (): T

Returns

T

Defined in

lib/lazy.ts:6


makeObject

makeObject(path, value, obj?): Record<string, unknown>

Turn a dotted path into a json object.

Parameters

NameTypeDescription
pathstringThe dotted path
valueunknownThe value
objRecord<string, unknown>The object to edit

Returns

Record<string, unknown>

Defined in

lib/makeObject.ts:7


mergeDefault

mergeDefault<A, B>(base, overwrites?): DeepRequired<A & B>

Deep merges 2 objects. Properties from the second parameter are applied to the first.

Type parameters

NameType
Aextends object
Bextends Partial<A>

Parameters

NameTypeDescription
baseABase object
overwrites?BOverwrites to apply

Returns

DeepRequired<A & B>

Remark

overwrites is also mutated!

Remark

If the value of a key in overwrites is undefined then the value of that same key in base is used instead!

Remark

This is essentially { ...base, ...overwrites } but recursively

Example

const base = { a: 0, b: 1 };
const overwrites = {}; // will be { a: 0, b: 1 } after merge
mergeDefault(base, overwrites) // { a: 0, b: 1 }

Example

const base = { a: 0, b: 1 };
const overwrites = { a: 2, i: 3 };
mergeDefault(base, overwrites) // { a: 2, i: 3, b: 1 };

Example

const base = { a: 0, b: 1 };
const overwrites = { a: null };
mergeDefault(base, overwrites) // { a: null, b: 1 };

Example

const base = { a: 0, b: 1 };
const overwrites = { a: undefined };
mergeDefault(base, overwrites) // { a: 0, b: 1 };

Example

const base = { a: null };
const overwrites = { a: { b: 5 } };
mergeDefault(base, overwrites) // { a: { b: 5 } };

Defined in

lib/mergeDefault.ts:43


mergeObjects

mergeObjects<A, B>(objTarget, objSource): A & B

Merges two objects

Type parameters

NameType
Aextends object
Bextends object

Parameters

NameTypeDescription
objTargetAThe object to be merged
objSourceReadonly<B>The object to merge

Returns

A & B

Defined in

lib/mergeObjects.ts:8


noop

noop(): void

Returns

void

Defined in

lib/noop.ts:2


objectEntries

objectEntries<T>(obj): T extends ArrayLike<any> ? [`${number}`, T[number]][] : [keyof T, T[keyof T]][]

Type parameters

NameType
Textends object

Parameters

NameType
objT

Returns

T extends ArrayLike<any> ? [`${number}`, T[number]][] : [keyof T, T[keyof T]][]

Defined in

lib/objectEntries.ts:3


objectKeys

objectKeys<T>(obj): T extends ArrayLike<any> ? `${number}`[] : keyof T[]

Type parameters

NameType
Textends object

Parameters

NameType
objT

Returns

T extends ArrayLike<any> ? `${number}`[] : keyof T[]

Defined in

lib/objectKeys.ts:3


objectToTuples

objectToTuples<T>(obj, prefix?): [keyof T, T[keyof T]][]

Convert an object to a tuple

Type parameters

Name
T

Parameters

NameTypeDefault valueDescription
objAnyObject<T>undefinedThe object to convert
prefixstring''The prefix for the key

Returns

[keyof T, T[keyof T]][]

Defined in

lib/objectToTuples.ts:9


objectValues

objectValues<T>(obj): T extends ArrayLike<any> ? T[number][] : T[keyof T][]

Type parameters

NameType
Textends object

Parameters

NameType
objT

Returns

T extends ArrayLike<any> ? T[number][] : T[keyof T][]

Defined in

lib/objectValues.ts:3


omitKeysFromObject

omitKeysFromObject<Object, ObjectKeys>(source, ...keys): Omit<Object, ObjectKeys>

Clones the source object using deepClone then deletes the specified keys with Reflect.deleteProperty

Type parameters

NameTypeDescription
Objectextends objectThe object type.
ObjectKeysextends string | number | symbolThe keys of the object type.

Parameters

NameTypeDescription
sourceObjectThe input object.
...keysreadonly ObjectKeys[]The keys to omit from the object.

Returns

Omit<Object, ObjectKeys>

A new object without the specified keys.

Defined in

lib/omitKeysFromObject.ts:13


partition

partition<T>(array, predicate): T[][]

Partitions array into a tuple of two arrays, where one array contains all elements that satisfies predicate, and the other contains all elements that do not satisfy predicate.

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT[]The array to partition. This array is not mutated.
predicate(value: T, index: number) => booleanThe predicate function to determine in which partition the item should be placed. The function should return true for items that should be placed in the first partition, and false for those that should be placed in the second partition.

Returns

T[][]

A tuple of two arrays.

Defined in

lib/partition.ts:12


pickRandom

pickRandom<T>(array, amount?): T

Picks a random element from an array

Type parameters

Name
T

Parameters

NameTypeDescription
arrayreadonly T[]The array to pick a random element from
amount?1Amount of values to obtain randomly (default: 1)

Returns

T

Defined in

lib/pickRandom.ts:6

pickRandom<T>(array, amount): T[]

Type parameters

Name
T

Parameters

NameType
arrayreadonly T[]
amountnumber

Returns

T[]

Defined in

lib/pickRandom.ts:7


poll

poll<T>(cb, cbCondition, options?): Promise<Awaitable<T>>

Executes a function cb and validates the result with function cbCondition, and repeats this until cbCondition returns true or the timeout is reached.

For a synchronous variant, see pollSync.

Type parameters

Name
T

Parameters

NameTypeDescription
cb(signal: undefined | AbortSignal) => Awaitable<T>The function that should be executed.
cbCondition(value: Awaited<T>, signal: undefined | AbortSignal) => Awaitable<boolean>A function that when given the result of cb should return true if the polling should stop and should return false if the polling should continue.
optionsPollOptionsOptions to provide further modifying behaviour.

Returns

Promise<Awaitable<T>>

The result of cb as soon as cbCondition returns true, or an error if timeout is reached.

Throws

If timeout is reached.

Defined in

lib/poll.ts:41


pollSync

pollSync<T>(cb, cbCondition, options?): T

Executes a function cb and validates the result with function cbCondition, and repeats this until cbCondition returns true or the timeout is reached.

For an asynchronous variant, see poll.

Type parameters

Name
T

Parameters

NameTypeDescription
cb() => TThe function that should be executed.
cbCondition(value: T) => booleanA function that when given the result of fn should return true if the polling should stop and should return false if the polling should continue.
optionsSyncPollOptionsOptions to provide further modifying behaviour.

Returns

T

The result of cb as soon as cbCondition returns true, or an error if timeout is reached.

Throws

If timeout is reached.

Defined in

lib/pollSync.ts:29


range

range(min, max, step): number[]

Get an array of numbers with the selected range

Parameters

NameTypeDescription
minnumberThe minimum value
maxnumberThe maximum value
stepnumberThe step value

Returns

number[]

Defined in

lib/range.ts:7


regExpEsc

regExpEsc(str): string

Cleans a string from regex injection

Parameters

NameTypeDescription
strstringThe string to clean

Returns

string

Defined in

lib/regExpEsc.ts:7


retry

retry<T>(cb, retries): Promise<T>

Asynchronously calls the callback function until it either succeeds or it runs out of retries. For a synchronous variant, see retrySync.

Type parameters

Name
T

Parameters

NameTypeDescription
cb() => Awaitable<T>The function to be retried is passed in as a callback function.
retriesnumberThe number of retries is also passed in as a parameter. Minimum of 0.

Returns

Promise<T>

The result of the callback function is returned.

Defined in

lib/retry.ts:10


retrySync

retrySync<T>(cb, retries): T

Synchronously calls the callback function until it either succeeds or it runs out of retries. For an asynchronous variant, see retry.

Type parameters

Name
T

Parameters

NameTypeDescription
cb() => TThe function to be retried is passed in as a callback function.
retriesnumberThe number of retries is also passed in as a parameter. Minimum of 0.

Returns

T

The result of the callback function is returned.

Defined in

lib/retrySync.ts:8


roundNumber

roundNumber(num, scale?): number

Properly rounds up or down a number. Also supports strings using an exponent to indicate large or small numbers.

Parameters

NameTypeDefault valueDescription
numstring | numberundefinedThe number to round off
scalenumber0The amount of decimals to retain

Returns

number

Defined in

lib/roundNumber.ts:7


sleep

sleep<T>(ms, value?, options?): Promise<T>

Sleeps for the specified number of milliseconds. For a synchronous variant, see sleepSync.

Type parameters

NameType
Tundefined

Parameters

NameTypeDescription
msnumberThe number of milliseconds to sleep.
value?TA value with which the promise is fulfilled.
options?SleepOptions-

Returns

Promise<T>

See

sleepSync for a synchronous version.

Defined in

lib/sleep.ts:22


sleepSync

sleepSync<T>(ms, value?): T

Sleeps for the specified number of milliseconds synchronously. We should probably note that unlike sleep (which uses CPU tick times), sleepSync uses wall clock times, so the precision is near-absolute by comparison. That, and that synchronous means that nothing else in the thread will run for the length of the timer.

For an asynchronous variant, see sleep.

Type parameters

NameType
Tundefined

Parameters

NameTypeDescription
msnumberThe number of milliseconds to sleep.
value?TA value to return.

Returns

T

See

sleep for an asynchronous version.

Defined in

lib/sleepSync.ts:12


splitText

splitText(str, length, char?): string

Split a string by its latest space character in a range from the character 0 to the selected one.

Parameters

NameTypeDefault valueDescription
strstringundefinedThe text to split.
lengthnumberundefinedThe length of the desired string.
charstring' 'The character to split with

Returns

string

Copyright

2019 Aura Román

License

Apache-2.0

Defined in

lib/splitText.ts:9


throttle

throttle<T>(func, wait): ThrottleFn<T>

Creates a throttled function that only invokes func at most once per every wait milliseconds. The throttled function comes with a flush method to reset the last time the throttled function was invoked.

Type parameters

NameType
Textends (...args: any[]) => any

Parameters

NameTypeDescription
funcTThe function to throttle.
waitnumberThe number of milliseconds to throttle invocations to.

Returns

ThrottleFn<T>

Returns the new throttled function.

Defined in

lib/throttle.ts:13


toTitleCase

toTitleCase(str, options?): string

Converts a string to Title Case

Parameters

NameTypeDescription
strstringThe string to title case
optionsToTitleCaseOptionsThe options to use when converting the string

Returns

string

Description

This is designed to also ensure common Discord PascalCased strings are put in their TitleCase baseVariants.

You can also provide your own variants to merge with the baseVariants for your own functionality use.

Defined in

lib/toTitleCase.ts:35


tryParseJSON

tryParseJSON(value, reviver?): object | string | number | boolean | null

Try parse a stringified JSON string.

Parameters

NameTypeDescription
valuestringThe string to parse as JSON.
reviver?(this: object, key: string, value: unknown) => unknownA function that transforms the results. This function is recursively called for each member of the object.

Returns

object | string | number | boolean | null

Defined in

lib/tryParseJSON.ts:6


tryParseURL

tryParseURL(value): URL | null

Tries parse a string to a URL object

Parameters

NameTypeDescription
valuestringThe possible URL to parse

Returns

URL | null

an URL object if it was a valid URL or null if it was not.

Defined in

lib/tryParseURL.ts:6