@heybrostudio/utils
Version:
Opinionated collection of commonly used JavaScript/TypeScript utilities by @heybrostudio
98 lines (90 loc) • 2.73 kB
TypeScript
/**
* Flatten type
*/
type Flatten<T> = {
[K in keyof T]: T[K] extends object ? Flatten<T[K]> : T[K];
} & {};
/**
* Type `T` or a promise that resolves to type `T`
*/
type MayBePromise<T> = T | Promise<T>;
/**
* Check if it is of type `any`
*
* @see
* The source code for this type was inspired by vueuse's `IsAny` type util
* https://github.com/vueuse/vueuse/blob/1558cd2b5b019abc1feda6d702caa1053a182903/packages/shared/utils/types.ts#L155C4-L155C60
*/
type IsAny<T> = 0 extends 1 & T ? true : false;
/**
* Function types that specify parameter and return value types.
* The default return value type is `void`.
*/
type Func<T, R = void> = IsAny<T> extends true ? (param: any) => R : [T] extends [void] ? () => R : (param: T) => R;
/**
* Promised `setTimeout`
*
* @param ms milliseconds
* @param callback callback method
* @returns Promise
*/
declare function sleep(ms: number, callback?: Func<void, MayBePromise<void>>): Promise<void>;
/**
* Print green text
*
* @param text Text you want to print
* @returns Output green text
*/
declare function green(text: string): string;
/**
* Print cyan text
*
* @param text Text you want to print
* @returns Output cyan text
*/
declare function cyan(text: string): string;
/**
* Print red text
* @param text Text you want to print
* @returns Output red text
*/
declare function red(text: string): string;
/**
* Print gray text
* @param text Text you want to print
* @returns Output gray text
*/
declare function gray(text: string): string;
/**
* Camel to underscore
* @param key The string to be converted
* @returns The string linked by underscores
*/
declare function camelToUnderscore(key: string): string;
/**
* Generate a discount code
*
* @param length Specify the number of characters, default 8, max 18
* @returns A string of the specified number of characters consisting of uppercase letters and numbers.
*/
declare function generateDiscount(length?: number): string;
/**
* Remove tracking parameters from url
*
* @param url The url with tracking parameters
* @returns The url after removing the tracking parameter
*/
declare function removeTracking(url: string): string;
/**
* Get the package path of the specified dependency
* @param packageName Package name
* @returns Package path
*/
declare function getRootByPackageName(packageName: string): string;
/**
* Download of xlsx documents
* @param value BlobPart value
* @param filename File name
*/
declare function downloadXlsx(value: BlobPart, filename: string): void;
export { type Flatten, type Func, type IsAny, type MayBePromise, camelToUnderscore, cyan, downloadXlsx, generateDiscount, getRootByPackageName, gray, green, red, removeTracking, sleep };