@snailuu/base
Version:
604 lines (583 loc) • 21.1 kB
text/typescript
declare const EMPTY: symbol;
declare function warning(...args: any[]): void;
/** 函数类型 */
type TFunc<T extends unknown[], R = any> = (...args: T) => R;
/** 任一函数 */
type TAnyFunc = TFunc<any[]>;
/** 获取函数的参数类型 */
type TArgsType<T> = T extends (...args: infer A) => any ? A : any[];
/** 获取数组第一个元素的类型 */
type THeadType<T extends any[]> = T extends [infer H] ? H : T extends [infer H, ...any[]] ? H : never;
/** 获取数组最后一个元素的类型 */
type TLastType<T extends any[]> = T extends [...any[], infer L] ? L : T extends [infer L] ? L : never;
/** 获取数组剩余元素的类型 */
type TTailTypes<A extends any[]> = A extends [any] ? [] : A extends [any, ...infer T] ? T : [];
/** 获取函数形参类型 */
type TGetArgs<F> = F extends (...args: infer A) => any ? A : [];
/** 获取函数返回值类型 */
type TGetReturnType<F> = F extends (...args: any[]) => infer R ? R : F;
/** 对象类型 */
type TObject<T, K extends TObjKeyType = TObjKeyType> = Record<K, T>;
/** js 支持的所有类型 */
type TAllType = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'function' | 'asyncfunction' | 'generatorfunction' | 'promise' | 'symbol' | 'set' | 'map' | 'weakset' | 'weakmap' | 'date' | 'rgexp' | 'error' | 'null' | 'undefined' | 'bigint' | 'file' | 'urlsearchparams' | 'formdata' | 'arraybuffer' | 'dateview' | 'int8array' | 'uint8array' | 'uint8clampedarray' | 'int16array' | 'uint16array' | 'int32array' | 'uint32array' | 'float32array' | 'float64array' | 'bigint64array' | 'biguint64array' | 'blob';
/** 指定类型或指定类型的数组 */
type TMany<T> = T | T[];
/** 对象 key 支持的类型 */
type TObjKeyType = string | number | symbol;
/** 获取 Promise 返回值的类型 */
type TPromiseValue<T> = Awaited<T>;
/** 深度获取对象属性的类型 */
type TDeepGetPropType<K extends string[], T extends TObject<any>, I = T[K[0]]> = I extends undefined ? undefined : K extends [any] ? I : I extends TObject<any> ? TDeepGetPropType<TTailTypes<K>, I> : never;
/**
* 执行一次的函数, 后续调用返回上一次执行结果
* @param func 运行函数
* @returns 函数执行结果
*/
declare function onceFunc<T extends TAnyFunc>(func: T): T;
/**
* 缓存函数执行结果
*/
declare class MemoizeMap {
#private;
set(key: any, value: any): void;
get(key: any): any;
has(key: any): boolean;
}
/**
* 缓存函数执行结果
* @param func 运行函数
* @param resolver 缓存键值解析函数, 默认为第一个参数
* @returns 函数调用结果
*/
declare function memoize<F extends TAnyFunc>(func: F, resolver?: (...args: TGetArgs<F>) => any): {
(...args: TGetArgs<F>): TGetReturnType<F>;
cache: MemoizeMap;
};
/**
* 缓存函数执行结果
* @param cacheLoad 缓存加载函数
* @returns 缓存函数
*/
declare const cacheByReturn: <T extends () => any, R = ReturnType<T>>(cacheLoad: T) => (...args: TGetArgs<R>) => TGetReturnType<R>;
/**
* 运行函数如果报错执行自定义异常处理函数, 立即执行,调用时就会运行传入的函数
* @param runner 运行函数
* @param catcher 异常处理函数
* @returns 函数执行结果
*/
declare function tryCall<F extends TAnyFunc>(runner: F, catcher?: (e: any) => void): ReturnType<F>;
/**
* 运行函数如果报错执行自定义异常处理函数,返回一个新函数,可以接收参数
* @param runner 运行函数
* @param catcher 异常处理函数
* @returns 函数执行结果
*/
declare function tryCallFunc<F extends TAnyFunc>(runner: F, catcher?: (e: any) => void): TFunc<Parameters<F>, ReturnType<F>>;
/**
* 运行函数如果报错返回错误对象而不是抛出错误
* @param func 运行函数
* @param args 运行函数参数
* @returns 函数执行结果或错误对象
*/
declare function completion<T, A extends any[]>(func: (...args: A) => T, ...args: A): T | Error;
declare const sleep: (ms: number) => Promise<unknown>;
declare function sleepSync(ms: number): void;
/**
* 防抖函数
* @param func 函数
* @param time 延迟时间
* @param immediately 是否立即执行
* @returns 防抖函数
*/
declare function debounce<F extends TAnyFunc>(func: F, time?: number, immediately?: boolean): (...args: TArgsType<F>) => void;
/**
* 节流函数
* @param func 函数
* @param time 延迟时间
* @param immediately 是否立即执行
* @returns 节流函数
*/
declare function throttle<F extends TAnyFunc>(func: F, time?: number, immediately?: boolean): (...args: TArgsType<F>) => void;
/**
* 将任务分批执行
* @param task 任务
* @returns 任务调度器
*/
declare function chunkTask<F extends TAnyFunc>(task: F): (datas: Parameters<F>[] | number) => Promise<ReturnType<F>[]>;
/**
* 将异步函数执行转换为同步函数执行
* @param fn 异步函数
* @returns 同步执行的异步函数
*/
declare function toSyncFunc<F extends (...args: any[]) => Promise<any>>(fn: F): (...args: TGetArgs<F>) => TGetReturnType<F>;
/** 空函数 */
declare const noop: (...args: any[]) => any;
/** 获取当前时间 */
declare const getNow: () => number;
/**
* 安全的获取全局对象
*/
declare const getSafeGlobal: (...args: unknown[] | []) => any;
interface CookieOptions {
duration?: number;
expires?: string | Date;
domain?: string;
maxAge?: number;
path?: string;
}
/**
* 生成 cookie 字符串
*/
declare function generateCookieInfo(options?: CookieOptions): string;
/**
* 获取 promise 的 resolve 和 reject 函数和 promise 本身
*/
declare function withResolvers<T>(func?: (resolve: (value: T) => void, reject: (reason?: any) => void) => any): {
resolve: (value: T) => void;
reject: (reason?: any) => void;
promise: Promise<T>;
};
/**
* 获取 AliApp 版本和名称
*/
declare const getAliAppEnv: () => {
appName: string;
appVersion: string;
};
/**
* 获取设备信息
*/
declare const getDeviceInfo: () => {
appName: string;
appVersion: string;
screenWidth: number;
screenHeight: number;
devicePixelRatio: number;
platform: string;
userAgent: string;
};
/** 生成随机字符串 */
declare function getRandomString(len?: number): string;
/** 生成 blob 链接 */
declare function createLinkByString(resource: BlobPart): string;
type GCArgs = string | (undefined | null | string | Record<string, boolean>)[] | Record<string, boolean> | undefined | null;
/** 生成 classname 字符串 */
declare function generateClassName(...args: GCArgs[]): string;
/**
* 生成 classname 字符串
*
* @alias generateClassName
*/
declare const gc: typeof generateClassName;
/**
* 获取操作系统类型
*/
declare const getOsType: () => "android" | "ios" | "openHarmony" | "mac" | "windows" | "linux" | "aix" | "freebsd" | "haiku" | "openbsd" | "sunos" | "cygwin" | "netbsd" | "other";
/**
* 获取真实类型
*/
declare function getType(value: any): TAllType;
/**
* 获取 userAgent
*/
declare const getUserAgent: () => string;
/** 判断是否为浏览器环境 */
declare const isWeb: () => boolean;
/** 判断是否为 iframe 环境 */
declare function isInIframe(): boolean;
/** 判断是否为 node 环境 */
declare const isNode: () => boolean;
/** 小程序环境 */
declare const isMiniApp: () => boolean;
/** 阿里小程序 */
declare const isAliMiniApp: () => boolean;
/** 字节小程序 */
declare const isByteDanceMicroApp: () => boolean;
/** 微信小程序 */
declare const isWeChatMiniProgram: () => boolean;
/** weex 环境 */
declare const isWeex: () => boolean;
/** ios */
declare const isIOS: () => boolean;
/** 安卓 */
declare const isAndroid: () => boolean;
/** 鸿蒙 */
declare const isOpenHarmony: () => boolean;
/** chrome */
declare const isChrome: () => boolean;
/** firefox */
declare const isFirefox: () => boolean;
/** safari */
declare const isSafari: () => boolean;
/** 新 edge */
declare const isNewEdge: () => boolean;
/** 旧 edge */
declare const isOldEdge: () => boolean;
/** edge */
declare const isEdge: () => boolean;
/** kraken 环境 */
declare const isKraken: () => boolean;
/** 快应用 */
declare const isQuickApp: () => boolean;
/** 淘宝 */
declare const isTBWeb: () => boolean;
/** 淘特 */
declare const isLTWeb: () => boolean;
/** 点淘 */
declare const isTbLive: () => boolean;
/** 所有淘宝 web 环境 */
declare const isTbWebEnv: () => boolean;
/** 微信 */
declare const isWechatWeb: () => boolean;
/** 支付宝 */
declare const isAliPayWeb: () => boolean;
/** 钉钉 */
declare const isWebInDingding: () => boolean;
/** 淘宝买菜团长端 */
declare const isTuan: () => boolean;
/** 零售通 */
declare const isLST: () => boolean;
/** 零销宝 */
declare const isLXB: () => boolean;
/** 阿里 app */
declare const isAliAppWeb: () => boolean;
/** 钉钉小程序 */
declare const isDingdingMiniapp: () => boolean;
/** 淘系小程序 */
declare const isTaobaoMiniapp: () => boolean;
/** 支付宝 | 菜鸟小程序 */
declare const isAlipayMiniapp: () => boolean;
/** 阿里小程序 */
declare const isTBMiniapp: () => boolean;
/** 淘特小程序 */
declare const isLTMiniapp: () => boolean;
/** 淘菜菜小程序 */
declare const isMMCMiniapp: () => boolean;
/** 犀鸟小程序 */
declare const isXiNiaoapp: () => boolean;
/** 菜鸟小程序 */
declare const isCaiNiaoApp: () => boolean;
/** 支付宝小程序 */
declare const isAlipayApp: () => boolean;
/** 百度小程序 */
declare const isBaiduSmartProgram: () => boolean;
/** 快手小程序 */
declare const isKuaiShouMiniProgram: () => boolean;
/** 小程序 */
declare const isAliMiniappPlatform: () => boolean;
/** 淘宝 */
declare const isTBNode: () => boolean;
/** 淘特 */
declare const isLTNode: () => boolean;
/** 微信 */
declare const isWechatNode: () => boolean;
/** 淘宝 */
declare const isTB: () => boolean;
/** 淘特 */
declare const isLT: () => boolean;
/** 支付宝 */
declare const isAliPay: () => boolean;
/** 天猫 */
declare const isTmall: () => boolean;
/** 阿里app */
declare const isAliApp: () => boolean;
/** 微信端 */
declare const isWechat: () => boolean;
/** 菜鸟商业版本App,内嵌团长端小程序 */
declare const isCaiNiaoBusiness: () => boolean;
/** 菜鸟商业版本App */
declare const isCaiNiao: () => boolean;
/** 阿里系app */
declare const isAliUa: () => boolean;
/** 盒马 */
declare const isHmApp: () => boolean;
/** 优酷 */
declare const isYouKu: () => boolean;
/** 支付宝 webview */
declare const isAlipayMiniWeb: () => boolean;
/** 淘特的 webview */
declare const isLTMiniWeb: () => boolean;
/** 【历史兼容】淘宝的 webview */
declare const isLBMiniWeb: () => boolean;
/** 淘宝的 webview */
declare const isTBMiniWeb: () => boolean;
/** 钉钉 webview */
declare const isDingTalk: () => boolean;
/** 团长小程序 webview 嵌套的 h5 */
declare const isTuanWebview: () => boolean;
/** 微信小程序 webview */
declare const isWechatMiniWeb: () => boolean;
/** 微信 h5 */
declare const isWechatH5: () => boolean;
/** 小程序 webview */
declare const isWebInMiniApp: () => boolean;
/** 阿里小程序 webview */
declare const isAliWebInMiniApp: () => boolean;
/** 阿里应用小程序 */
declare const isAliAppMiniApp: () => boolean;
/**
* mini 系列 780
*
* iPhone X / iPhone XS / iphone 11 pro 812
*
* iphone 11, 11 pro max 896
*
* iphone 12, 13, 14 844
*
* iphone 12, 13, 14 plus 926
*
* iphone 14 pro 852
*
* iphone 14 pro max 932
*
* 如果要判断是否为「刘海屏」,建议使用 `isIOSNotchScreen`
*/
declare const isIPhoneX: () => boolean;
/**
* 是否 iPhone XS Max (2688 x 1242)
*/
declare const isIPhoneXSMax: () => boolean;
/**
* 是否 iPhone XR (1792 x 828)
*/
declare const isIPhoneXR: () => boolean;
/**
* 是否 iPhone14 pro max
*/
declare const isIPhone14PM: () => boolean;
/**
* 是否为 iOS 的「刘海屏」,用于针对顶部状态栏做特殊处理
*
* 目前 iOS 所有的「刘海屏」的适配方案是一样的
*/
declare const isIOSNotchScreen: () => boolean;
/**
* 判断一个值是否为类数组
* @param data 待判断数据类型
*
* tips:
* 1. 有索引属性
* 2. 有length属性且为非负整数
* 3. 对象
*/
declare function isArrayLike(data: any): boolean;
declare function isArray(data: any): data is Array<any>;
declare function isArray<T>(value: T[]): value is T[];
/**
* 判断是否为 null 或 undefined
*/
declare function isNull(value: any): value is null;
/**
* 判断是否为 NaN
*/
declare function isNaN(value: any): value is typeof Number.NaN;
/**
* 判断是否为数字
*/
declare function isNumber(value: any): value is number;
/**
* 判断是否为 http 链接
*/
declare function isHttpUrlString(value: any): boolean;
/**
* 判断是否为 https 链接
*/
declare function isHttpsUrlString(value: any): boolean;
/**
* 判断是否为 blob 链接
*/
declare function isBlobUrlString(value: any): boolean;
/**
* 判断是否为 data 链接
*/
declare function isDataUrlString(value: any): boolean;
/**
* 判断是否为 url
*/
declare function isUrl(value: any): boolean;
/**
* 判断是否为 true 或字符串 true
*/
declare function isTrue(value: any): value is true;
/**
* 判断是否为 false 或字符串 false
*/
declare function isFalse(value: any): value is false;
/**
* 判断是否为空值 (通常用于 io 数据的判断)
*
* @warning 对于对象来说, 调用的是 Object.keys 获取长度, 所以只判断可枚举属性
*/
declare function isEmpty(value: any): boolean;
/**
* 判断是否为字符串
*/
declare function isString(value: any): value is string;
/**
* 判断是否为 undefined 或字符串 undefined
*/
declare function isUndef(value: any): value is undefined;
/**
* 检查浏览器是否支持某个 CSS 特性
* @param feature
*/
declare function caniuseCSSFeature(feature: string): boolean;
declare function caniuse(feature: keyof typeof window): boolean;
declare function isAsyncFunc(value: any): value is (...args: any[]) => Promise<any>;
declare function isConstructor(obj: any): boolean;
declare function isPromise(value: any): value is Promise<any>;
declare function isPromise<T>(value: Promise<T>): value is Promise<T>;
declare const isFile: (...args: [] | [value: any]) => boolean;
declare const isBlob: (...args: [] | [value: any]) => boolean;
type GetArray<T> = T extends any[] ? T : T[];
/**
* 获取数组,如果不是数组,则会用数组包裹,类数组会使用 Array.from 转换
*/
declare function getArray<T>(value?: T): GetArray<T>;
/**
* 获取数组切片
* @param array 原数组
* @param size 每个子数组的长度
* @param skip 跳过开始指定个数的元素
*/
declare function getArraySlice<T>(array: T[], size?: number, skip?: number): T[][];
/**
* 异步过滤数组
* @param arr 原数组
* @param predicate 过滤函数
*/
declare function asyncFilter<T>(arr: T[], predicate: (item: T, index: number) => Promise<boolean> | boolean): Promise<T[]>;
/**
* 深拷贝
*/
declare function deepClone<T extends TObject<any>>(obj: T, hash?: WeakMap<WeakKey, any>): T;
/**
* 合并对象, 会修改 target 对象
*
* tips: 如不希望修改 target 对象, 请使用 cloneMerge
*/
declare function merge(target: any, ...sources: any[]): any;
/**
* 合并对象, 返回新对象,不影响原数据
*/
declare function cloneMerge(target: any, ...sources: any[]): any;
/**
* 将可迭代对象转换为对象
*/
declare const fromEntries: (entries: Iterable<readonly [PropertyKey, any]>) => any;
/**
* stream 转 string
*/
declare function streamToString(stream: ReadableStream): Promise<string>;
/**
* stream 转 arrayBuffer
*/
declare function streamToArrayBuffer(stream: ReadableStream): Promise<ArrayBuffer>;
/**
* arrayBuffer 转 base64
*/
declare function arrayBufferToBase64String(arrayBuffer: ArrayBuffer): string;
/**
* arrayBuffer 转 base64
*
* 使用 arrayBufferToBase64String 代替, arrayBufferToBase64String 已兼容 chunk 方式
*
* TODO: 后续大版本迭代会移除该方法
* @deprecated
*/
declare const arrayBufferToChunkBase64String: typeof arrayBufferToBase64String;
/**
* base64 转 Uint8Array
*/
declare function base64StringToUint8Array(base64String: string): Uint8Array<ArrayBuffer>;
/**
* base64 转 blob
*
* 使用 base64StringToBlob 代替, base64StringToBlob 已兼容 chunk 方式
*
* TODO: 后续大版本迭代会移除该方法
* @deprecated
*/
declare function chunkBase64StringToBlob(base64String: string): Blob;
/**
* base64 转 blob
*/
declare function base64StringToBlob(base64String: string): Blob;
/**
* base64 转 arrayBuffer
*
* 使用 base64StringToArrayBuffer 代替, base64StringToArrayBuffer 已兼容 chunk 方式
*
* TODO: 后续大版本迭代会移除该方法
* @deprecated
*/
declare const chunkBase64StringToArrayBuffer: typeof base64StringToArrayBuffer;
/**
* base64 转 arrayBuffer
*/
declare function base64StringToArrayBuffer(base64String: string): Promise<ArrayBuffer>;
/**
* string 转 readonly stream
*/
declare function stringToStream(source: string): ReadableStream<any>;
/**
* string 转 Uint8Array
*/
declare function stringToBinary(source: string): Uint8Array<ArrayBufferLike>;
/**
* arrayBuffer 转 string
*/
declare function binaryToString(binary: AllowSharedBufferSource): string;
/**
* stream 转 base64
*/
declare function streamToBase64String(stream: ReadableStream): Promise<string>;
/**
* stream 转 base64
*
* 使用 streamToBase64String 代替
*
* TODO: 后续大版本迭代会移除该方法
* @deprecated
*/
declare const streamToChunkBase64String: typeof streamToBase64String;
/**
* base64 转 stream
*
* 使用 base64StringToStream 代替, base64StringToStream 已兼容 chunk 方式
*
* TODO: 后续大版本迭代会移除该方法
* @deprecated
*/
declare const chunkBase64StringToStream: typeof base64StringToStream;
/**
* base64 转 stream
*/
declare function base64StringToStream(source: string): ReadableStream<Uint8Array<ArrayBufferLike>>;
/**
* arrayBuffer 转 stream
*/
declare function arrayBufferToStream(source: AllowSharedBufferSource): ReadableStream<any>;
/**
* blob 转 base64
*
* 使用 blobToBase64String 代替
*
* TODO: 后续大版本迭代会移除该方法
* @deprecated
*/
declare const blobToChunkBase64String: typeof blobToBase64String;
/**
* blob 转 base64
*/
declare function blobToBase64String(blob: Blob): Promise<string>;
interface ParseUrlOptions {
hashQueryToSearchParams?: boolean;
}
/**
* 解析 url
*/
declare function parseUrl(path?: string, options?: ParseUrlOptions): URL;
declare function parseSearch(search: string): URLSearchParams;
declare function parseSearchObject(search: string | URLSearchParams): any;
export { type CookieOptions, EMPTY, type TAllType, type TAnyFunc, type TArgsType, type TDeepGetPropType, type TFunc, type TGetArgs, type TGetReturnType, type THeadType, type TLastType, type TMany, type TObjKeyType, type TObject, type TPromiseValue, type TTailTypes, arrayBufferToBase64String, arrayBufferToChunkBase64String, arrayBufferToStream, asyncFilter, base64StringToArrayBuffer, base64StringToBlob, base64StringToStream, base64StringToUint8Array, binaryToString, blobToBase64String, blobToChunkBase64String, cacheByReturn, caniuse, caniuseCSSFeature, chunkBase64StringToArrayBuffer, chunkBase64StringToBlob, chunkBase64StringToStream, chunkTask, cloneMerge, completion, createLinkByString, debounce, deepClone, fromEntries, gc, generateClassName, generateCookieInfo, getAliAppEnv, getArray, getArraySlice, getDeviceInfo, getNow, getOsType, getRandomString, getSafeGlobal, getType, getUserAgent, isAliApp, isAliAppMiniApp, isAliAppWeb, isAliMiniApp, isAliMiniappPlatform, isAliPay, isAliPayWeb, isAliUa, isAliWebInMiniApp, isAlipayApp, isAlipayMiniWeb, isAlipayMiniapp, isAndroid, isArray, isArrayLike, isAsyncFunc, isBaiduSmartProgram, isBlob, isBlobUrlString, isByteDanceMicroApp, isCaiNiao, isCaiNiaoApp, isCaiNiaoBusiness, isChrome, isConstructor, isDataUrlString, isDingTalk, isDingdingMiniapp, isEdge, isEmpty, isFalse, isFile, isFirefox, isHmApp, isHttpUrlString, isHttpsUrlString, isIOS, isIOSNotchScreen, isIPhone14PM, isIPhoneX, isIPhoneXR, isIPhoneXSMax, isInIframe, isKraken, isKuaiShouMiniProgram, isLBMiniWeb, isLST, isLT, isLTMiniWeb, isLTMiniapp, isLTNode, isLTWeb, isLXB, isMMCMiniapp, isMiniApp, isNaN, isNewEdge, isNode, isNull, isNumber, isOldEdge, isOpenHarmony, isPromise, isQuickApp, isSafari, isString, isTB, isTBMiniWeb, isTBMiniapp, isTBNode, isTBWeb, isTaobaoMiniapp, isTbLive, isTbWebEnv, isTmall, isTrue, isTuan, isTuanWebview, isUndef, isUrl, isWeChatMiniProgram, isWeb, isWebInDingding, isWebInMiniApp, isWechat, isWechatH5, isWechatMiniWeb, isWechatNode, isWechatWeb, isWeex, isXiNiaoapp, isYouKu, memoize, merge, noop, onceFunc, parseSearch, parseSearchObject, parseUrl, sleep, sleepSync, streamToArrayBuffer, streamToBase64String, streamToChunkBase64String, streamToString, stringToBinary, stringToStream, throttle, toSyncFunc, tryCall, tryCallFunc, warning, withResolvers };