@gitborlando/utils
Version:
JavaScript/TypeScript 实用工具集合
101 lines (95 loc) • 5.12 kB
TypeScript
type NoopFunc = typeof noopFunc;
declare function noopFunc(): void;
type AnyFunc = typeof anyFunc;
declare function anyFunc(...args: any[]): any;
type AnyObject = Record<string, any>;
declare const AnyObject: AnyObject;
type ValueOf<T extends Record<string, any>> = T[keyof T];
type AllKeys<T extends Record<string, any>> = T extends Record<string, any> ? T extends any[] ? never : keyof T | {
[K in keyof T]: AllKeys<T[K]>;
}[keyof T] : never;
type ClientXY = {
clientX: number;
clientY: number;
};
declare function firstOne<T extends any = any>(input: T[] | Set<T>): T | undefined;
declare function lastOne<T extends any = any>(input: T[] | Set<T>): T;
declare function flushFuncs<T extends NoopFunc>(input: T[] | Set<T>): void;
declare function stableIndex<T extends any = any>(arr: T[], index?: number): number;
declare function loopFor<T>(arr: T[], callback: (cur: T, next: T, prev: T, index: number) => 'continue' | 'break' | void): void;
declare function reverseFor<T>(items: T[], callback: (item: T, index: number) => any): void;
declare function reverse<T extends any>(arr: T[]): T[];
declare function range(count: number): number[];
declare class Cache<K, V> {
cache: Map<K, V>;
private compareCache;
get(key: K): V;
set(key: K, value: V): V;
delete(key: K): void;
getSet(key: K, fn: () => V, compare?: unknown[]): V;
clear(): void;
forEach(callback: (key: K, value: V, map: Map<K, V>) => void): void;
keys(): MapIterator<K>;
values(): MapIterator<V>;
entries(): MapIterator<[K, V]>;
fromObject(obj: Record<string | number | symbol, V>): void;
toObject(): Record<string | number | symbol, V>;
}
declare function createCache<K, V>(): Cache<K, V>;
declare class ObjCache<V> {
cache: Record<string, V>;
private compareCache;
get(key: string): V;
set(key: string, value: V): V;
delete(key: string): void;
clear(): void;
keys(): string[];
values(): V[];
entries(): [string, V][];
getSet(key: string, fn: () => V, compare?: unknown[]): V;
}
declare function createObjCache<V>(): ObjCache<V>;
declare const This: any;
declare function Delete<T>(object: Record<string, T>, key: string): void;
declare function Delete<T>(target: T[], find: string | ((value: T) => void)): void;
declare function iife<T extends any = any>(callback: () => T): T;
declare function matchCase<T extends string, R extends any>(Case: T, obj: Record<T, R>): R;
declare function matchCase<T extends string, R extends any>(Case: T, Default: R, obj: Record<T, R>): R;
declare function Log<T>(someThing: T, label?: string): T;
declare function macroMatch(_input: TemplateStringsArray): (left: any) => any;
declare function clone<T extends any>(object: T): T;
declare function jsonFy(obj: any): string;
declare function jsonParse<T = any>(obj: any, fallback?: T): T | undefined;
declare function memorize<T extends any[], R extends any>(func: (...args: T) => R): (...args: T) => R;
declare function debounce<T extends any[], R extends any>(wait: number, func: (...args: T) => R): (...args: T) => void;
declare function objKeys<K extends string>(obj: Partial<Record<K, any>>): K[];
declare class Raf {
private ids;
request(callback: (next: NoopFunc) => void): this;
cancelAll(): this;
}
declare function objectKey(obj: AnyObject): string | undefined;
declare function suffixOf(input?: string, lowerCase?: boolean): string;
declare function createFuncAOP<T extends (...args: any[]) => any>(before?: (...args: any[]) => void, after?: (...args: any[]) => void): (func: T) => (...args: Parameters<T>) => any;
declare function SetTimeout(func: () => any, time?: number): void;
declare function optionalSet<T>(target: T | undefined | null, key: keyof T, value: T[keyof T]): void;
declare function miniId(size?: number, alphabet?: string): string;
declare function ensure<T>(maybeUndefinedOrNull: T, fallback: Exclude<T, undefined | null>): Exclude<T, null | undefined> | NonNullable<T>;
declare class Is {
static number(value: any): value is number;
static string(value: any): value is string;
static boolean(value: any): value is boolean;
static array<T>(value: any): value is T[];
static object(value: any): value is object;
static function(value: any): value is Function;
static null(value: any): value is null;
static undefined(value: any): value is undefined;
static symbol(value: any): value is symbol;
static nullable(value: any): value is null | undefined;
static notNullable<T>(value: any): value is T;
static falsy(value: any): boolean;
static notFalsy(value: any): boolean;
static empty(value: any): boolean;
static notEmpty(value: any): boolean;
}
export { type AllKeys, type AnyFunc, AnyObject, type ClientXY, Delete, Is, Log, type NoopFunc, Raf, SetTimeout, This, type ValueOf, anyFunc, clone, createCache, createFuncAOP, createObjCache, debounce, ensure, firstOne, flushFuncs, iife, jsonFy, jsonParse, lastOne, loopFor, macroMatch, matchCase, memorize, miniId, noopFunc, objKeys, objectKey, optionalSet, range, reverse, reverseFor, stableIndex, suffixOf };