utils-lib-js
Version:
JavaScript工具函数,封装的一些常用的js函数
74 lines (73 loc) • 3.7 kB
TypeScript
export type IKey = string | symbol | number;
export interface IObject<T> {
[key: IKey]: T | IObject<any>;
}
export interface IPromise extends IObject<any> {
promise: Promise<void>;
resolve: (res: any) => unknown;
reject: (err: any) => unknown;
}
export type IInstance<T> = {
_instance: Function;
} & T;
export type IDemoteArray<T> = Array<IDemoteArray<T> | T>;
export type IRandomNum = (min: number, max: number, bool?: boolean) => number;
export type IUrlSplit = (url: string) => IObject<any>;
export type IUrlJoin = (url: string, query: object) => string;
export type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
export type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
export type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U;
export type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>;
export type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U;
export type IEnumInversion = (target: IObject<string>) => IObject<string>;
export type ICloneDeep = (target?: any) => any;
export type ICreateObjectVariable = (type: string, source?: any) => any;
export type ICreateObject = <T, U extends T>(source: T) => U;
export type IInherit = <T extends Function>(source: T, target?: Function) => Function;
export type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
export type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void;
export type IStringToJson = (target: string) => IObject<any> | null;
export type IJsonToString = (target: IObject<any>) => string;
export type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
export type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
export type IDefer = (timer?: number) => IPromise;
export type ICatchAwait<T extends Promise<any>> = (defer: T) => T;
export type IRequestFrame = (callback: (start: number) => void, delay: number) => () => void;
export type IArrayRandom<T extends any[]> = (arr: T) => T;
export type IArrayUniq<T extends any[]> = (arr: T) => T;
export type IArrayDemote<T extends IDemoteArray<any>> = (arr: T, result?: T) => T;
export type IArrayLoop = (list: unknown[], current?: number) => {
item: unknown;
current: number;
};
type TagAttributes = Partial<Record<string, string | boolean>>;
interface IElementParams<T> {
ele: T | string;
style?: Partial<CSSStyleDeclaration>;
attr?: TagAttributes;
parent?: T;
}
export type ICreateElement<T = HTMLElement> = (params: IElementParams<T>) => T;
export type IAddHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
export type IStopBubble = (e: Event) => void;
export type IStopDefault = (e: Event) => void;
export type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
export type IDispatchEvent = <T extends Document>(ele: T, data: any) => void;
export type ILogOneLine = (str: string, overwrite?: boolean, warp?: boolean) => void;
export type ILogLoopParams = {
loopList?: string[];
index?: number;
isStop?: boolean;
timer?: number;
};
export type ILogLoop = (opts?: ILogLoopParams) => ILogLoopParams | void;
export type IAnimateFrame = {
id: number | null;
duration: number;
isActive: boolean;
fn(timer: number): void;
start(duration: number): void;
stop(id?: number): void;
animate(timer: number): void;
};
export {};