@vulppi/toolbelt
Version:
A collection of tools and utilities.
226 lines (222 loc) • 6.9 kB
TypeScript
// Generated by dts-bundle-generator v9.5.1
/**
* The clone function is a utility function that creates a deep copy of an object.
* It uses structuredClone if available, otherwise falls back to JSON serialization.
*
* @param obj - The object to clone.
* @returns A deep copy of the input object.
*/
export declare function clone<E extends any>(obj: E): E;
/**
* The omitNullables function is a utility function that creates a deep copy of an object
* and omits any properties that are null or undefined.
*
* @param obj - Object to omit null or undefined values from
* @returns A new object with null or undefined values omitted
*
* @example
* const original = { a: 1, b: null, c: undefined, d: { e: 2, f: null } };
* const result = omitNullables(original);
* console.log(result); // { a: 1, d: { e: 2 } }
*/
export declare function omitNullables<R extends object>(obj: R): R;
/**
* The omitShallowProps function is a utility function that creates a shallow copy of an object
* and omits the specified keys from the copy.
*
* @param obj - The object to clone and omit properties from.
* @param keys - The keys to omit from the cloned object.
* @returns A new object with the specified keys omitted.
*
* @example
* const original = { a: 1, b: 2, c: 3 };
* const result = omitShallowProps(original, 'b', 'c');
* console.log(result); // { a: 1 }
*/
export declare function omitShallowProps<P extends object, K extends keyof P>(obj: P, ...keys: K[]): Omit<P, K>;
/**
* Parse string bytes to number
* 1kb = 1024 bytes
* 1mb = 1048576 bytes
* 1gb = 1073741824 bytes
* 1tb = 1099511627776 bytes
*
* @param bytes
* @returns
*/
export declare function parseStringBytesToNumber(bytes: string | number): number;
/**
* Parses a string to its corresponding primitive value (number, boolean, null, undefined, NaN, Infinity).
* Returns the original string if it doesn't match any known primitive value representations.
*/
export declare function parseStringToAutoDetectPrimitiveValue(val?: string | null): string | number | boolean | null | undefined;
/**
* The promiseDelay function is a utility function that returns a Promise
* that resolves after a specified number of milliseconds.
*/
export declare function promiseDelay(ms: number): Promise<never>;
/**
* The tryCatch function is a utility function that allows you to run a function
* and catch any errors that occur without a block try/catch statement.
*
* @param cb - Function to run
* @returns [result, error]
*
* @example
* const [result, error] = tryCatch(() => {
* return JSON.parse(jsonText);
* });
* if (error) {
* console.error('Error fetching data:', error);
* } else {
* console.log('Data:', result);
* }
*
*/
export declare function tryCatch<F extends (...args: any[]) => any>(cb: F): [
ReturnType<F>,
null
] | [
null,
Error
];
/**
* The tryCatchAsync function is a utility function that allows you to run an async function
* and catch any errors that occur without a block try/catch statement.
*
* @param cb - Function to run
* @returns [result, error]
*
* @example
* const [result, error] = await tryCatch(async () => {
* const data = await fetchData();
* return data;
* });
* if (error) {
* console.error('Error fetching data:', error);
* } else {
* console.log('Data:', result);
* }
*
*/
export declare function tryCatchAsync<F extends (...args: any[]) => Promise<any>>(cb: F): Promise<[
ReturnType<F>,
null
] | [
null,
Error
]>;
/**
* The tryCatchCallback function is a utility function that allows you to run a callback function
* and catch any errors that occur without a block try/catch statement.
*
* @param run - Function to run
* @param cbErr - Callback function to handle errors
*
* @returns The result of the run function or null if an error occurs.
*
* @example
* tryCatchCallback(() => {
* throw new Error('Test error');
* }, (err) => {
* console.error('Caught error:', err);
* });
*/
export declare function tryCatchCallback<R extends Function>(run: R, cbErr: any): any;
/**
* The tryCatchPromise function is a utility function that allows you
* catch any errors that occur without a block try/catch statement or then/catch methods.
*
* @param promise - Promise to await
* @returns [result, error]
*
* @example
* const [result, error] = await tryCatchPromise(fetchData());
* if (error) {
* console.error('Error fetching data:', error);
* } else {
* console.log('Data:', result);
* }
*
*/
export declare function tryCatchPromise<T>(promise: Promise<T>): Promise<[
T,
null
] | [
null,
Error
]>;
/**
* Generic type for html elements
*/
export declare type NodeElement = Element | HTMLElement | SVGElement;
export declare type Null = null | undefined;
export declare type Nullable<T = any> = Null | T;
export declare type Nullish<T = any> = Nullable<T>;
export declare type Num = `${SNum}` | `${SNum1}${SNum}`;
/**
* Make all properties in T optional (deep)
* @example
* type T0 = {
* a: {
* b: {
* c: number
* }
* }
* }
* type T1 = PartialDeep<T0> // { a?: { b?: { c?: number } } }
*/
export declare type PartialDeep<T> = {
[P in keyof T]?: T[P] extends object ? PartialDeep<T[P]> : T[P];
};
/**
* Make all properties in T readonly (deep)
*/
export declare type ReadonlyDeep<T> = {
readonly [P in keyof T]: T[P] extends object ? ReadonlyDeep<T[P]> : T[P];
};
/**
* Make all properties in T required (deep)
* @example
* type T0 = {
* a?: {
* b?: {
* c?: number
* }
* }
* }
* type T1 = RequiredDeep<T0> // { a: { b: { c: number } } }
*/
export declare type RequiredDeep<T> = {
[P in keyof T]-?: T[P] extends object ? RequiredDeep<T[P]> : T[P];
};
/**
* Accepts generic strings and known string keys and injects values
*/
export declare type StringInjection<V extends string, A extends ArgsObject> = V extends `${infer L}$${infer K1 extends Num}${infer R}` ? A[K1] extends string ? StringInjection<`${L}"${A[K1]}"${R}`, Omit<A, K1>> : StringInjection<`${L}${A[K1]}${R}`, Omit<A, K1>> : V;
/**
* Accepts generic strings and known string keys
*/
export declare type Unstring<K extends string> = K | Omit<string, K>;
/**
* Union of values of an array
*/
export declare type ValuesOf<T extends readonly any[]> = T[number];
type ArgsObject = {
readonly [K in Num]?: string | number;
};
/**
* The Disposable type defines an interface for objects that require cleanup
* when they are no longer in use. It ensures that a cleanup method,
* typically called `dispose`, is implemented, allowing for proper resource
* management and preventing memory leaks.
*/
type Disposable$1 = {
[Symbol.dispose]: () => void | Promise<void>;
};
type SNum = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
type SNum1 = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
export {
Disposable$1 as Disposable,
};
export {};