Miscellaneous Utilities
The package includes a collection of miscellaneous utilities that can help with common use-cases in JavaScript and TypeScript projects.
Functions
deepMerge()
deepMerge(…
sources):RecursiveObject
Defined in: misc.utils.ts:69
Recursively merges properties of source objects into the target object. If a property is an object, it will be merged rather than replaced. If a property is an array, it will be replaced rather than merged.
Parameters
sources
…(string | RecursiveObject)[]
The source objects from which properties will be merged.
Returns
RecursiveObject
The merged object.
Example
const obj1 = { a: 1, b: { c: 2 } };const obj2 = { b: { d: 3 }, e: 4 };const merged = deepMerge(obj1, obj2);console.log(merged); // { a: 1, b: { c: 2, d: 3 }, e: 4 }isEmptyArray()
isEmptyArray(
array):boolean
Defined in: misc.utils.ts:47
Returns true if the given array is null, undefined or empty.
Parameters
array
unknown[]
The array to check.
Returns
boolean
Whether the given array is null, undefined or empty.
Example
isEmptyArray([]); // trueisEmptyArray(undefined); // trueisEmptyArray(null); // trueisEmptyArray([1, 2, 3]); // falseisEmptyOrWhitespace()
isEmptyOrWhitespace(
value):boolean
Defined in: misc.utils.ts:27
Checks whether the given value is null, undefined or a string that contains only whitespace characters.
Parameters
value
unknown
The value to check.
Returns
boolean
Whether the given value is null, undefined or a string that contains only whitespace characters.
Example
isEmptyOrWhitespace(''); // trueisEmptyOrWhitespace(' '); // trueisEmptyOrWhitespace(undefined); // trueisEmptyOrWhitespace(null); // trueisEmptyOrWhitespace('foo'); // falseisEmptyOrWhitespace(0); // falseisEmptyOrWhitespace({}); // falseisEmptyOrWhitespace([]); // falseisEmptyOrWhitespace(false); // falsesleep()
sleep(
ms):Promise<void>
Defined in: misc.utils.ts:8
Returns a promise that resolves after the given amount of milliseconds.
Parameters
ms
number
The amount of milliseconds.
Returns
Promise<void>
A promise that resolves after the given amount of milliseconds.
Example
await sleep(1000);stripNull()
stripNull<
T>(obj):T
Defined in: misc.utils.ts:104
Strips all null properties from the object. Does not affect nested objects or arrays.
Utilizes delete, resulting in the passed reference to be updated, so it is not necessary
to use the returned value.
Type Parameters
T
T extends object
Parameters
obj
T
The object.
Returns
T
The updated object.
Example
const obj = { a: 1, b: null, c: 3 };stripNull(obj);console.log(obj); // { a: 1, c: 3 }stripUndefined()
stripUndefined<
T>(obj):T
Defined in: misc.utils.ts:125
Strips all undefined properties from the object. Does not affect nested objects or arrays.
Utilizes delete, resulting in the passed reference to be updated, so it is not necessary
to use the returned value.
Type Parameters
T
T extends object
Parameters
obj
T
The object.
Returns
T
The updated object.
Example
const obj = { a: 1, b: undefined, c: 3 };stripUndefined(obj);console.log(obj); // { a: 1, c: 3 }