UNPKG

@gitborlando/utils

Version:

JavaScript/TypeScript 实用工具集合

168 lines (157 loc) 7.29 kB
import { IXY, IRect } from '@gitborlando/geo'; 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[]; type DragData = { current: IXY; start: IXY; shift: IXY; delta?: IXY; marquee: IRect; }; declare class DragUtil { canMove: boolean; started: boolean; private current; private start; private shift; private marquee; private downHandler?; private startHandler?; private moveHandler?; private endHandler?; private isInfinity; needInfinity: () => this; onDown: (callback?: (data: DragData) => void) => this; onStart: (callback?: (data: DragData) => void) => this; onMove: (callback: (data: DragData) => void) => this; onDestroy: (callback?: (data: DragData) => void) => this; onSlide: (callback: (data: DragData) => void) => this; private destroy; private calculateMarquee; private setDataToDefault; } declare const isLeftMouse: (e: MouseEvent) => boolean; declare const isRightMouse: (e: MouseEvent) => boolean; type EventListenOptions = { capture?: boolean; once?: boolean; target?: any; }; type WindowEventKeys = keyof WindowEventMap; type ListenFunc<K extends WindowEventKeys> = (this: Window, ev: WindowEventMap[K]) => any; declare function listen<K extends WindowEventKeys>(...args: [K, ListenFunc<K>]): () => void; declare function listen<K extends WindowEventKeys>(...args: [K, EventListenOptions, ListenFunc<K>]): () => void; declare function stopPropagation(callback?: (e?: any) => any): (e: any) => void; declare function preventDefault(callback?: (e?: any) => any): (e: any) => void; declare class StorageUtil { #private; set: <T>(key: string, value: T) => void; get: <T>(key: string) => T | undefined; } type WheelData = { e: WheelEvent; direction: 1 | -1; }; declare class WheelUtil { private beforeWheelHandler?; private duringWheelHandler?; private afterWheelHandler?; private wheelTimeOut?; private curFrameTriggered; onBeforeWheel: (handler: (data: WheelData) => void) => void; onDuringWheel: (handler: (data: WheelData) => void) => void; onAfterWheel: (handler: (data: WheelData) => void) => void; onWheel: (e: WheelEvent) => void; } 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 | undefined; declare function jsonParse(obj: any): any; 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 nanoid(size?: number): string; 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, type DragData, DragUtil, type EventListenOptions, Is, type ListenFunc, Log, type NoopFunc, Raf, SetTimeout, StorageUtil, This, type ValueOf, type WheelData, WheelUtil, type WindowEventKeys, anyFunc, clone, createCache, createFuncAOP, createObjCache, debounce, firstOne, flushFuncs, iife, isLeftMouse, isRightMouse, jsonFy, jsonParse, lastOne, listen, loopFor, macroMatch, matchCase, memorize, nanoid, noopFunc, objKeys, objectKey, optionalSet, preventDefault, range, reverse, reverseFor, stableIndex, stopPropagation, suffixOf };