Module: @sapphire/utilities
Classes
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
Type parameters
Name |
---|
T |
Type declaration
• (...args
)
A generic abstract constructor without parameters
Parameters
Name | Type |
---|---|
...args | any [] |
Defined in
projects/utilities/packages/utilities/src/lib/types.ts:64
AbstractCtor
Ƭ AbstractCtor<A
, R
>: (...args
: A
) => R
Type parameters
Name | Type |
---|---|
A | extends Arr = readonly any [] |
R | any |
Type declaration
• (...args
)
A generic abstract constructor with parameters
Parameters
Name | Type |
---|---|
...args | A |
Defined in
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/lib/types.ts:106
ArgumentTypes
Ƭ ArgumentTypes<F
>: F
extends (...args
: infer A) => any
? A
: never
Type parameters
Name | Type |
---|---|
F | extends (...args : any []) => unknown |
Defined in
projects/utilities/packages/utilities/src/lib/types.ts:38
Arr
Ƭ Private
Arr: readonly any
[]
A readonly array of any values.
Defined in
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/lib/types.ts:79
Builtin
Ƭ Builtin: Primitive
| Function
| Date
| Error
| RegExp
Defined in
projects/utilities/packages/utilities/src/lib/types.ts:4
Constructor
Ƭ Constructor<T
>: (...args
: any
[]) => T
Type parameters
Name |
---|
T |
Type declaration
• (...args
)
A generic constructor without parameters
Parameters
Name | Type |
---|---|
...args | any [] |
Defined in
projects/utilities/packages/utilities/src/lib/types.ts:59
Ctor
Ƭ Ctor<A
, R
>: (...args
: A
) => R
Type parameters
Name | Type |
---|---|
A | extends Arr = readonly any [] |
R | any |
Type declaration
• (...args
)
A generic constructor with parameters
Parameters
Name | Type |
---|---|
...args | A |
Defined in
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/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 T]-?: DeepRequired<T[K]> } : NonNullable
<T
>
Type parameters
Name |
---|
T |
Defined in
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/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
Name | Type |
---|---|
T | unknown |
Defined in
projects/utilities/packages/utilities/src/lib/types.ts:90
Nullish
Ƭ Nullish: null
| undefined
Type union for the full 2 billion dollar mistake in the JavaScript ecosystem
Defined in
projects/utilities/packages/utilities/src/lib/types.ts:84
PartialRequired
Ƭ PartialRequired<T
, K
>: Partial
<Omit
<T
, K
>> & Required
<Pick
<T
, K
>>
Type parameters
Name | Type |
---|---|
T | T |
K | extends keyof T |
Defined in
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/lib/types.ts:127
Primitive
Ƭ Primitive: string
| number
| boolean
| bigint
| symbol
| undefined
| null
Defined in
projects/utilities/packages/utilities/src/lib/types.ts:1
RequiredExcept
Ƭ RequiredExcept<T
, K
>: Partial
<Pick
<T
, K
>> & Required
<Omit
<T
, K
>>
Type parameters
Name | Type |
---|---|
T | T |
K | extends keyof T |
Defined in
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/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
projects/utilities/packages/utilities/src/lib/types.ts:172
ThrottleFn
Ƭ ThrottleFn<T
>: T
& { flush
: () => void
}
Type parameters
Name | Type |
---|---|
T | extends (...args : any []) => any |
Defined in
projects/utilities/packages/utilities/src/lib/throttle.ts:1
Functions
arrayStrictEquals
▸ arrayStrictEquals<T
>(arr1
, arr2
): boolean
Compare if both arrays are strictly equal
Type parameters
Name | Type |
---|---|
T | extends readonly unknown [] |
Parameters
Name | Type | Description |
---|---|---|
arr1 | T | The array to compare to |
arr2 | T | The array to compare with |
Returns
boolean
Defined in
projects/utilities/packages/utilities/src/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
Name | Type | Description |
---|---|---|
value | unknown | The value to cast to another type |
Returns
T
The value but as type T
Defined in
projects/utilities/packages/utilities/src/lib/cast.ts:10
chunk
▸ chunk<T
>(array
, chunkSize
): T
[][]
Splits up an array into chunks
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
array | readonly T [] | The array to chunk up |
chunkSize | number | The size of each individual chunk |
Returns
T
[][]
Defined in
projects/utilities/packages/utilities/src/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
Name | Type |
---|---|
T | extends Ctor |
Parameters
Name | Type | Description |
---|---|---|
value | Ctor | The constructor to be checked against. |
base | T | The base constructor. |
Returns
value is T
Defined in
projects/utilities/packages/utilities/src/lib/classExtends.ts:8
codeBlock
▸ codeBlock<C
>(content
): ````
${C}
````
Wraps the content inside a codeblock with no language
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
Type parameters
Name | Type |
---|---|
C | extends string |
Parameters
Name | Type |
---|---|
content | C |
Returns
```` ${C} ````
Defined in
projects/utilities/packages/utilities/src/lib/codeBlock.ts:14
▸ codeBlock<L
, C
>(language
, content
): ````${L}
${C}
````
Wraps the content inside a codeblock with the specified language
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
Type parameters
Name | Type |
---|---|
L | extends string |
C | extends string |
Parameters
Name | Type |
---|---|
language | L |
content | C |
Returns
````${L} ${C} ````
Defined in
projects/utilities/packages/utilities/src/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.
Copyright
2019 Aura Román
License
Apache-2.0
Parameters
Name | Type | Description |
---|---|---|
str | string | The text to split. |
length | number | The length of the desired string. |
Returns
string
Defined in
projects/utilities/packages/utilities/src/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
Name | Type |
---|---|
FnArgumentsType | extends any [] |
FnReturnType | FnReturnType |
Parameters
Name | Type | Description |
---|---|---|
func | (...args : FnArgumentsType ) => FnReturnType | The function to debounce. |
options | DebounceSettings | The options object. |
Returns
DebouncedFunc
<FnArgumentsType
, FnReturnType
>
Returns the new debounced function.
Defined in
projects/utilities/packages/utilities/src/lib/debounce/debounce.ts:68
deepClone
▸ deepClone<T
>(source
): T
Deep clone an object
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
source | T | The object to clone |
Returns
T
Defined in
projects/utilities/packages/utilities/src/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
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']
Type parameters
Name |
---|
TValue |
Parameters
Name | Type | Description |
---|---|---|
value | Nullish | TValue | The 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.
Defined in
projects/utilities/packages/utilities/src/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
Example
// TypeScript Type: (string | undefined | null)[]
const someArray = ['one', 'two', undefined, null, ''];
// TypeScript Type: string[]
const filteredArray = someArray.filter(filterNullAndUndefinedAndEmpty);
// Result: ['one', 'two']
Type parameters
Name |
---|
TValue |
Parameters
Name | Type | Description |
---|---|---|
value | "" | Nullish | TValue | The 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.
Defined in
projects/utilities/packages/utilities/src/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
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]
Type parameters
Name |
---|
TValue |
Parameters
Name | Type | Description |
---|---|---|
value | 0 | Nullish | TValue | The 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.
Defined in
projects/utilities/packages/utilities/src/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.
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'
.
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
obj | AnyObject <T > | The object of which to deeply retrieve its keys |
options? | GetDeepObjectKeysOptions | The options with which to customize the output of this function |
Returns
string
[]
An array of strings holding the keys of the object
Defined in
projects/utilities/packages/utilities/src/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
Name | Type | Description |
---|---|---|
map | ReadonlyMap <T , any > | The map to check |
keys | readonly 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
projects/utilities/packages/utilities/src/lib/hasAtLeastOneKeyInMap.ts:7
inlineCodeBlock
▸ inlineCodeBlock<C
>(content
): ``${C}``
Wraps text in a markdown inline codeblock
Type parameters
Name | Type |
---|---|
C | extends string |
Parameters
Name | Type | Description |
---|---|---|
content | C | The expression to be wrapped in the codeblock |
Returns
``${C}``
Defined in
projects/utilities/packages/utilities/src/lib/inlineCodeBlock.ts:7
isClass
▸ isClass(input
): input is Ctor
Verify if the input is a class constructor.
Parameters
Name | Type | Description |
---|---|---|
input | unknown | The function to verify |
Returns
input is Ctor
Defined in
projects/utilities/packages/utilities/src/lib/isClass.ts:7
isFunction
▸ isFunction(input
): input is Function
Verify if the input is a function.
Parameters
Name | Type | Description |
---|---|---|
input | unknown | The function to verify |
Returns
input is Function
Defined in
projects/utilities/packages/utilities/src/lib/isFunction.ts:6
isNullOrUndefined
▸ isNullOrUndefined(value
): value is Nullish
Checks whether or not a value is null
or undefined
Parameters
Name | Type | Description |
---|---|---|
value | unknown | The value to check |
Returns
value is Nullish
Defined in
projects/utilities/packages/utilities/src/lib/isNullOrUndefined.ts:7
isNullOrUndefinedOrEmpty
▸ isNullOrUndefinedOrEmpty(value
): value is "" | Nullish
Checks whether or not a value is null
, undefined
or ''
, []
Parameters
Name | Type | Description |
---|---|---|
value | unknown | The value to check |
Returns
value is "" | Nullish
Defined in
projects/utilities/packages/utilities/src/lib/isNullOrUndefinedOrEmpty.ts:8
isNullOrUndefinedOrZero
▸ isNullOrUndefinedOrZero(value
): value is 0 | Nullish
Checks whether or not a value is null
, undefined
or 0
Parameters
Name | Type | Description |
---|---|---|
value | unknown | The value to check |
Returns
value is 0 | Nullish
Defined in
projects/utilities/packages/utilities/src/lib/isNullOrUndefinedOrZero.ts:8
isNumber
▸ isNumber(input
): input is number
Verify if a number is a finite number.
Parameters
Name | Type | Description |
---|---|---|
input | unknown | The number to verify |
Returns
input is number
Defined in
projects/utilities/packages/utilities/src/lib/isNumber.ts:5
isObject
▸ isObject(input
, constructorType?
): input is object
Verify if the input is an object literal (or class).
Parameters
Name | Type | Description |
---|---|---|
input | unknown | The object to verify |
constructorType? | ObjectConstructor | The 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
projects/utilities/packages/utilities/src/lib/isObject.ts:8
▸ isObject<T
>(input
, constructorType
): input is InstanceType<T>
Type parameters
Name | Type |
---|---|
T | extends Constructor <unknown > |
Parameters
Name | Type |
---|---|
input | unknown |
constructorType | T |
Returns
input is InstanceType<T>
Defined in
projects/utilities/packages/utilities/src/lib/isObject.ts:9
isPrimitive
▸ isPrimitive(input
): input is string | number | bigint | boolean
Check whether a value is a primitive
Parameters
Name | Type | Description |
---|---|---|
input | unknown | The input to check |
Returns
input is string | number | bigint | boolean
Defined in
projects/utilities/packages/utilities/src/lib/isPrimitive.ts:7
isThenable
▸ isThenable(input
): input is Thenable
Verify if an object is a promise.
Parameters
Name | Type | Description |
---|---|---|
input | unknown | The promise to verify |
Returns
input is Thenable
Defined in
projects/utilities/packages/utilities/src/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
Name | Type | Description |
---|---|---|
cb | () => T | The 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
projects/utilities/packages/utilities/src/lib/lazy.ts:6
makeObject
▸ makeObject(path
, value
, obj?
): Record
<string
, unknown
>
Turn a dotted path into a json object.
Parameters
Name | Type | Description |
---|---|---|
path | string | The dotted path |
value | unknown | The value |
obj | Record <string , unknown > | The object to edit |
Returns
Record
<string
, unknown
>
Defined in
projects/utilities/packages/utilities/src/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.
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 } };
Type parameters
Name | Type |
---|---|
A | extends object |
B | extends Partial <A > |
Parameters
Name | Type | Description |
---|---|---|
base | A | Base object |
overwrites? | B | Overwrites to apply |
Returns
DeepRequired
<A
& B
>
Defined in
projects/utilities/packages/utilities/src/lib/mergeDefault.ts:43
mergeObjects
▸ mergeObjects<A
, B
>(objTarget
, objSource
): A
& B
Merges two objects
Type parameters
Name | Type |
---|---|
A | extends object |
B | extends object |
Parameters
Name | Type | Description |
---|---|---|
objTarget | A | The object to be merged |
objSource | Readonly <B > | The object to merge |
Returns
A
& B
Defined in
projects/utilities/packages/utilities/src/lib/mergeObjects.ts:8
noop
▸ noop(): void
Returns
void
Defined in
projects/utilities/packages/utilities/src/lib/noop.ts:2
objectEntries
▸ objectEntries<T
>(obj
): T
extends ArrayLike
<any
> ? [`${number}`, T
[number
]][] : [keyof T
, T
[keyof T
]][]
Type parameters
Name | Type |
---|---|
T | extends object |
Parameters
Name | Type |
---|---|
obj | T |
Returns
T
extends ArrayLike
<any
> ? [`${number}`, T
[number
]][] : [keyof T
, T
[keyof T
]][]
Defined in
projects/utilities/packages/utilities/src/lib/objectEntries.ts:3
objectKeys
▸ objectKeys<T
>(obj
): T
extends ArrayLike
<any
> ? `${number}`[] : keyof T
[]
Type parameters
Name | Type |
---|---|
T | extends object |
Parameters
Name | Type |
---|---|
obj | T |
Returns
T
extends ArrayLike
<any
> ? `${number}`[] : keyof T
[]
Defined in
projects/utilities/packages/utilities/src/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
Name | Type | Default value | Description |
---|---|---|---|
obj | AnyObject <T > | undefined | The object to convert |
prefix | string | '' | The prefix for the key |
Returns
[keyof T
, T
[keyof T
]][]
Defined in
projects/utilities/packages/utilities/src/lib/objectToTuples.ts:9
objectValues
▸ objectValues<T
>(obj
): T
extends ArrayLike
<any
> ? T
[number
][] : T
[keyof T
][]
Type parameters
Name | Type |
---|---|
T | extends object |
Parameters
Name | Type |
---|---|
obj | T |
Returns
T
extends ArrayLike
<any
> ? T
[number
][] : T
[keyof T
][]
Defined in
projects/utilities/packages/utilities/src/lib/objectValues.ts:3
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
Name | Type | Description |
---|---|---|
array | T [] | The array to partition. This array is not mutated. |
predicate | (value : T , index : number ) => boolean | The 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
projects/utilities/packages/utilities/src/lib/partition.ts:12
pickRandom
▸ pickRandom<T
>(array
, amount?
): T
Picks a random element from an array
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
array | readonly T [] | The array to pick a random element from |
amount? | 1 | Amount of values to obtain randomly (default: 1) |
Returns
T
Defined in
projects/utilities/packages/utilities/src/lib/pickRandom.ts:6
▸ pickRandom<T
>(array
, amount
): T
[]
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
array | readonly T [] |
amount | number |
Returns
T
[]
Defined in
projects/utilities/packages/utilities/src/lib/pickRandom.ts:7
range
▸ range(min
, max
, step
): number
[]
Get an array of numbers with the selected range
Parameters
Name | Type | Description |
---|---|---|
min | number | The minimum value |
max | number | The maximum value |
step | number | The step value |
Returns
number
[]
Defined in
projects/utilities/packages/utilities/src/lib/range.ts:7
regExpEsc
▸ regExpEsc(str
): string
Cleans a string from regex injection
Parameters
Name | Type | Description |
---|---|---|
str | string | The string to clean |
Returns
string
Defined in
projects/utilities/packages/utilities/src/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
Name | Type | Description |
---|---|---|
cb | () => Awaitable <T > | The function to be retried is passed in as a callback function. |
retries | number | The 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
projects/utilities/packages/utilities/src/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
Name | Type | Description |
---|---|---|
cb | () => T | The function to be retried is passed in as a callback function. |
retries | number | The 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
projects/utilities/packages/utilities/src/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
Name | Type | Default value | Description |
---|---|---|---|
num | string | number | undefined | The number to round off |
scale | number | 0 | The amount of decimals to retain |
Returns
number
Defined in
projects/utilities/packages/utilities/src/lib/roundNumber.ts:7
sleep
▸ sleep<T
>(ms
, value?
, options?
): Promise
<T
>
Sleeps for the specified number of milliseconds.
See
sleepSync for a synchronous version.
Type parameters
Name | Type |
---|---|
T | undefined |
Parameters
Name | Type | Description |
---|---|---|
ms | number | The number of milliseconds to sleep. |
value? | T | A value with which the promise is fulfilled. |
options? | SleepOptions | - |
Returns
Promise
<T
>
Defined in
projects/utilities/packages/utilities/src/lib/sleep.ts:35
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.
See
sleep for an asynchronous version.
Type parameters
Name | Type |
---|---|
T | undefined |
Parameters
Name | Type | Description |
---|---|---|
ms | number | The number of milliseconds to sleep. |
value? | T | A value to return. |
Returns
T
Defined in
projects/utilities/packages/utilities/src/lib/sleep.ts:64
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.
Copyright
2019 Aura Román
License
Apache-2.0
Parameters
Name | Type | Default value | Description |
---|---|---|---|
str | string | undefined | The text to split. |
length | number | undefined | The length of the desired string. |
char | string | ' ' | The character to split with |
Returns
string
Defined in
projects/utilities/packages/utilities/src/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
Name | Type |
---|---|
T | extends (...args : any []) => any |
Parameters
Name | Type | Description |
---|---|---|
func | T | The function to throttle. |
wait | number | The number of milliseconds to throttle invocations to. |
Returns
ThrottleFn
<T
>
Returns the new throttled function.
Defined in
projects/utilities/packages/utilities/src/lib/throttle.ts:13
toTitleCase
▸ toTitleCase(str
, options?
): string
Converts a string to Title Case
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.
Parameters
Name | Type | Description |
---|---|---|
str | string | The string to title case |
options | ToTitleCaseOptions | The options to use when converting the string |
Returns
string
Defined in
projects/utilities/packages/utilities/src/lib/toTitleCase.ts:35
tryParseJSON
▸ tryParseJSON(value
): object
| string
| number
Try parse a stringified JSON string.
Parameters
Name | Type | Description |
---|---|---|
value | string | The value to parse |
Returns
object
| string
| number
Defined in
projects/utilities/packages/utilities/src/lib/tryParseJSON.ts:5
tryParseURL
▸ tryParseURL(value
): URL
| null
Tries parse a string to a URL object
Parameters
Name | Type | Description |
---|---|---|
value | string | The possible URL to parse |
Returns
URL
| null
an URL object if it was a valid URL or null
if it was not.
Defined in
projects/utilities/packages/utilities/src/lib/tryParseURL.ts:6