declare function isType(value: any): string; declare function isType(value: any, type: string): boolean; declare const isString: (value: any) => value is string; declare const isNumber: (value: any) => value is number; declare const isBoolean: (value: any) => value is boolean; declare const isArray: (value: any) => value is any[]; declare const isFunction: (value: any) => boolean; declare const isUndefined: (value: any) => boolean; declare const isNull: (value: any) => boolean; declare const isSymbol: (value: any) => value is symbol; declare const isBigInt: (value: any) => value is bigint; declare const isObject: (value: any) => boolean; declare const isPlainObject: (value: any) => boolean; declare const isNil: (value: any) => boolean; declare const isEmpty: (value: any) => boolean; declare const isInvalid: (value: any) => boolean; /** * 在原生 JavaScript 中,input 事件监听器是处理用户输入的常见方式。然而,由于中文输入法(如拼音输入法)会涉及候选词的选择,因此在中文输入时,input 事件可能在用户选择候选词之前触发,导致输入处理不完整。为了解决这个问题,可以利用合成事件(通过 compositionstart、compositionupdate 和 compositionend 事件)与 input 事件的结合来确保完整的输入内容被正确处理。 * * 利用合成事件追踪输入法的组合状态: * 1. compositionstart: 当用户开始使用输入法输入时触发 * 2. compositionupdate: 当输入法的组合文字更新时触发 * 3. compositionend: 当用户完成输入法输入(选择候选词)时触发 * 4. 使用 composing 标志来追踪当前是否正在进行输入法输入 */ /** * handle input event with composition events * @param inputEl input element * @param callback callback */ declare function addInputListener(inputEl: string | HTMLInputElement, callback: (value: string, event: Event) => void): () => void; type Direction = 'top' | 'right' | 'bottom' | 'left'; type Callback = (this: HTMLElement, direction: Direction, e: MouseEvent) => void; /** * 监听鼠标在元素上移入和移出的方向 * @param {HTMLElement} element 监听元素 * @param {Callback} onEnter 鼠标进入回调 * @param {Callback} onLeave 鼠标离开回调 * * 这个函数提供了以下功能: * 可以检测鼠标从元素的四个边缘(上、右、下、左)进入和离开的情况 * 支持自定义回调函数处理进入和离开事件 * 返回清理函数以便移除事件监听器 */ declare function detectMouseDirection(element: HTMLElement, onEnter: Callback, onLeave: Callback): () => void; interface TagElementMap { img: HTMLImageElement; script: HTMLScriptElement; link: HTMLLinkElement; } interface IOptions { /** * 标签类型 * @type 'img' | 'script' | 'link' */ tag: T; /** * 标签属性 */ attributes?: Record; /** * 是否异步加载 * @default false * @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-async */ async?: boolean; /** * 是否延迟加载 * @default false * @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-defer */ defer?: boolean; /** * 加载超时时间 * @default 0 */ timeout?: number; /** * 加载成功回调 */ onload?: (el: TagElementMap[T], clean: () => void, event?: Event) => void; /** * 加载失败回调 */ onerror?: (el: TagElementMap[T], err?: Error) => void; /** * 是否检查资源是否存在 * @default true */ checkExist?: boolean; /** * 插入到指定元素中 */ appendTo?: HTMLElement; } /** * 加载资源文件 * 1. 支持 img, link, script 文件标签 * 2. 检测已存在情况 * @param {string} url 资源Url * @param {IOptions} options 配置项 * @returns */ declare function loadResource(url: string, options?: T | IOptions): { element: TagElementMap[T]; clean: () => void; } | undefined; type RgbTuple = [number, number, number]; type RgbaTuple = [number, number, number, number]; type HslTuple = [number, number, number]; type RgbResultMap = { string: string; array: RgbTuple | RgbaTuple; object: { r: number; g: number; b: number; a?: number; }; }; /** * 判断是否是十六进制颜色值 * @param color 颜色值 * @returns boolean */ declare function isHexColor(color: string): boolean; /** * 判断是否为 RGB 颜色 * @param color 颜色值 * @returns boolean */ declare function isRgbColor(color: string): boolean; /** * 验证HSL颜色值 */ declare function isHslColor(color: string): boolean; /** * 判断颜色是否为暗色 * @param color - 颜色值 * @returns */ declare function isDarkColor(color: string): boolean; /** * 判断颜色是否为浅色 * @param color 颜色值 */ declare function isLightColor(color: string): boolean; /** * 将十六进制颜色值转换为RGB值 * @param color 十六进制颜色值 * @param type 返回值的类型,可以是 'string', 'array', 或 'object' * @returns */ declare function hexToRgb(color: string, type?: K): RgbResultMap[K]; /** * 将 RGB 颜色值转换为十六进制颜色码 * @param rgb - 颜色值 * @returns 十六进制颜色码,如 "#ff0000" */ declare function rgbToHex(rgb: string): string; declare function rgbToHex(rgb: RgbTuple): string; declare function rgbToHex(r: number, g: number, b: number): string; /** * 颜色转为 RGB * @param color - 颜色值 * @returns [R, G, B, A] */ declare function colorRgb(color: string, type?: K): RgbResultMap[K]; /** * 设置颜色透明度 * @param color 颜色值 * @param opacity 透明度 * @returns */ declare function setColorOpacity(color: string, opacity: number): string; /** * 调整颜色亮度 * @param color 颜色值 * @param amount 调整量 (-1 到 1,负值变暗,正值变亮) */ declare function setColorBrightness(color: string, amount: number): string; /** * 调整颜色暗度 * @param color 颜色值 * @param amount 调整量, 取值范围为 0 到 1 */ declare const darkenColor: (color: string, amount: number) => string; /** * 调整颜色亮度 * @param color 颜色值 * @param amount 调整量, 取值范围为 0 到 1 */ declare const lightenColor: (color: string, amount: number) => string; /** * 混合两种颜色 * @param color1 第一个颜色 * @param color2 第二个颜色 * @param weight 混合权重(0-1),默认0.5 */ declare function mixColors(color1: string, color2: string, weight?: number): string; /** * 获取互补色 */ declare function colorComplementary(color: string): string; /** * 计算颜色的相对亮度值 * @param r - 红色分量(0-255) * @param g - 绿色分量(0-255) * @param b - 蓝色分量(0-255) * @returns 相对亮度值 */ declare function colorLuminance(color: string): number; /** * 计算两种颜色之间的对比度 (相对亮度计算) * * 返回对比度比率,是一个大于 1 的浮点数。比如 1 表示无对比度,21 表示最大对比度。 * 根据 WCAG 2.0 标准,对比度应至少为 4.5:1 才能确保文本的可读性。 * 对比度值越大,表示两个颜色之间的差异越明显,可读性越好。 * * @param color1 - 第一个颜色 * @param color2 - 第二个颜色 * @returns 对比度值 */ declare function colorContrast(color1: string, color2: string): number; /** * 随机8位Id字符 * Generates a random 8-character ID using base-36 encoding. * @returns A random ID string. */ declare const randomId: () => string; /** * 生成 uuid * @returns */ declare function uuid(): string; /** * 通过 blob生成 uuid * const uuid = () => URL.createObjectURL(new Blob()).split('/').pop() */ /** * 生成随机整数 * @param min 最小值 * @param max 最大值 * @returns */ declare function randomInt(min: number, max: number): number; interface RandomIntListOptions { min: number; max: number; count: number; unique?: boolean; sort?: 'asc' | 'desc'; } /** * 生成随机整数列表 * @param min 最小值 * @param max 最大值 * @param count 数量 * @param unique 是否唯一 * @param sort 排序 * @returns */ declare function randomIntList(options?: RandomIntListOptions): number[]; /** * 生成随机浮点数 * @param min 最小值 * @param max 最大值 * @returns */ declare function randomFloat(min: number, max: number): number; /** * 生成随机布尔值 * @returns */ declare function randomBool(): boolean; /** * 生成随机字符串 * @param length 长度 * @param characters 字符集 * @returns */ declare function randomChar(length: number, characters?: string): string; /** * 生成随机元素, 支持设置权重 * @param items 元素列表 * @param weights 权重列表 * @returns */ declare function randomItem(items: T[], weights?: number[]): T; /** * 随机打乱数组 * @param array 数组 * @returns */ declare function shuffleArray(array: any[]): any[]; /** * 生成随机颜色 * @returns */ declare function randomColor(): string; /** * 生成随机日期 * @param start 开始日期 * @param end 结束日期 * @returns */ declare function randomDate(end: Date | string | number): Date; declare function randomDate(start: Date | string | number, end: Date | string | number): Date; /** * 防抖函数 * 实现原理: 每次触发事件时都取消之前的延时调用方法 * 应用场景: 按钮提交、搜索框输入、窗口大小改变事件 * 实现方式: 利用setTimeout返回的id清除延时调用 * * @example * const inputFunc = debounce((e) => { * console.log('input'); * }, 1000); * input.addEventListener('input', inputFunc); * */ /** * 防抖函数 * @param func 要执行的函数 * @param wait 等待时间 * @param immediate 是否立即执行 (true: leading模式,false: trailing模式) * @returns 防抖函数 */ declare function debounce any>(func: T, wait: number, immediate?: boolean): T & { cancel: () => void; }; /** * 防抖函数 * // 1. 非立即执行版 (trailing模式, 滞后执行) function debounce(func, wait) { let timer = null; return (...args) => { clearTimeout(timer); timer = setTimeout(() => { func.apply(this, args); }, wait); } } // 2. 支持立即执行版 function debounce(func, wait, immediate) { let timer = null; return function(...args) { clearTimeout(timer); timer = setTimeout(() => { timer = null; // 2.滞后执行, trailing模式 if (!immediate) func.apply(this, args); }, wait); if (immediate && !timer) { // 1.立即执行, leading模式 func.apply(this, args); } } } */ /** * 节流函数 * 实现原理: 利用时间戳和定时器 * 应用场景: 拖拽、缩放、动画等 * 实现方式: 通过闭包保存一个标记来判断是否需要执行函数 * * @example * const resizeFunc = throttle((e) => { * console.log('resize'); * }, 1000); * window.addEventListener('resize', resizeFunc); * */ /** * 节流函数 * @param func 要执行的函数 * @param wait 等待时间 * @param options 选项 * @param options.leading 是否立即执行 (true: leading模式,false: trailing模式) * @param options.trailing 是否在最后一次调用后执行一次 (true: trailing模式,false: 不执行) * @returns 节流函数 */ declare function throttle any>(func: T, wait: number, options?: { leading?: boolean; trailing?: boolean; }): T & { cancel: () => void; }; /** * 节流函数 * // 1.普通节流 function throttle(func, wait) { let timer = null; return (...args) => { if (timer) return; timer = setTimeout(() => { func.apply(this, args); timer = null; }, wait); } } // 2.立即执行一次 function throttle(func, wait) { let timer = null; let last = 0; // 上一次执行的时间 return (...args) => { clearTimeout(timer); let now = Date.now(); // 当前时间 // 距离上次执行时间大于等于wait时,执行函数 if (now - last >= wait) { func.apply(this, args); last = now; // 更新上一次执行时间 } else { timer = setTimeout(() => { func.apply(this, args); last = Date.now(); }, wait); } } } */ interface Assign { (target: T, source: U): T & U; (target: T, source1: U, source2: V): T & U & V; (target: T, source1: U, source2: V, source3: W): T & U & V & W; } declare const assign: Assign; /** * 递归方式深度克隆 (轻量级) * 1. 实现原始类型和对象的深度克隆,支持Date、RegExp类型,以及循环引用的处理。 * 2. 需要支持 Map、Set、Symbol等特殊类型的克隆,请使用 `cloneDeep` 函数。 * 3. 对于大量数据对象的克隆,建议使用 `cloneLoop` 函数。 * * @param obj 要克隆的对象 * @param hash 用于存储已经克隆过的对象,避免循环引用问题 * @returns */ declare function clone(obj: T, hash?: WeakMap): T; /** * 深度克隆注意事项: * 1. 支持深度克隆数据 * 2. 支持特殊类型数据,如 Map、Set、Date、RegExp、ArrayBuffer、TypedArray、DataView、Symbol 等 * 3. 对象属性描述符 * 4. 循环引用处理 * 5. 栈溢出处理 * 6. 性能优化 */ /** * 克隆 Symbol * 1. Symbol 本质上是唯一的,不能真正地"克隆" * 2. 对于全局 Symbol(使用 Symbol.for 创建的),直接使用 Symbol.for 重新获取 * 3. 对于普通 Symbol,创建一个新的 Symbol 实例 * 4. 在克隆对象时,确保同时处理普通属性和 Symbol 属性及保持属性描述符 * @param sym 待克隆的 Symbol * @returns * * @warning `Object(Symbol.prototype.valueOf.call(testSymbol))` 是创建一个 Symbol 对象包装器,而不是一个新的 Symbol, 其值与原 Symbol 相同,但类型不同。 */ /** * 使用递归方式实现深度克隆,支持各种常见数据类型. * @param target 需要克隆的值 * @param hash 用于存储已经克隆过的对象,避免循环引用问题 * @returns * * @warning 对于数据量比较大的对象,可能会出现递归栈溢出。建议其他方案,比如 `cloneLoop` 或 `cloneJSON`。 */ declare function cloneDeep(target: T, hash?: WeakMap): T; /** * 使用迭代方式实现深度克隆 * 1. 暂不支持Symbol、Map、Set等特殊类型 * * @param value 需要克隆的值 * @returns */ declare function cloneLoop(value: T): T; /** 循环是算法的核心,它模拟了递归的过程: 1. 每次从栈中取出一个任务 2. 遍历该对象的所有属性 3. 对每个属性值进行判断和处理: 3.1 如果是基本类型,直接赋值 3.2 如果是已经克隆过的对象(在 map 中存在),直接使用已克隆的值 3.3 如果是新的对象类型: 3.3.1 创建新的空对象或数组 3.3.2 将映射关系存入 WeakMap 3.3.3 将新的克隆任务推入栈中 这种迭代方式的优势: 1. 避免了递归调用导致的调用栈溢出 2. 内存使用更可控,因为任务栈的大小是线性的 3. 在处理非常深的对象结构时更安全 具体示例来过程: const obj = { a: { b: { c: 1 } }, d: [1, 2, {e: 3}] }; // 克隆过程中的栈变化: // 初始栈:[{source: obj, target: {}, keys: ['a', 'd']}] // stack length: 1, [root] // 处理 d 属性时: // 栈:[{source: obj.d, target: [], keys: ['0', '1', '2']}] // stack length: 2, ['a', 'd'] // 处理 e 属性时: // 栈:[{source: {e: 3}, target: {}, keys: ['e']}] // stack length: 2, ['a', 'e'] // 处理 a 属性时: // 栈:[{source: obj.a, target: {}, keys: ['b']}] // stack length: 1, ['a'] // 处理 b 属性时: // 栈:[{source: obj.a.b, target: {}, keys: ['c']}] // stack length: 1, ['b] */ /** * 使用 JSON 方法实现深度克隆 * @param obj 要克隆的对象 * @returns */ declare function cloneJSON(obj: T): T; /** * 将列表数据转换为树形数据 (迭代方式) * 1. 首先创建一个 map 对象,用于通过 id 快速查找节点。 * 2. 遍历列表,将每个节点添加到 map 中,并初始化 children 数组。 * 3. 再次遍历列表,将每个节点添加到其父节点的 children 数组中。 * 4. 最后返回根节点数组。 * * 复杂度分析: * 1. 时间复杂度:O(n),其中 n 是列表的长度。 * 2. 空间复杂度:O(n),其中 n 是列表的长度。 */ type ListItem = { [key: string]: any; }; type Options$3 = { /** * 节点标识字段 * @default "id" */ idKey?: string; /** * 父节点标识字段 * @default "parentId" */ parentKey?: string; /** * 子节点字段 * @default "children" */ childrenKey?: K; /** * 转换节点函数, 默认copy当前节点 * @default (item) => ({ ...item }); * @param item 当前节点 * @returns */ transform?: (item: T) => R; /** * 判断是否为根节点 * @default (item) => !item.parentId * @param item 当前节点 * @returns */ isRoot?: (item: T) => boolean; }; type TreeNode = R & { [P in K]: TreeNode[]; }; /** * Converts a flat list of items into a hierarchical tree structure. * @param list List data * @param options Options * @returns tree data */ declare function listToTree(list: T[], options?: Options$3): TreeNode[]; type Options$2 = { childrenKey?: string; removeChildren?: boolean; transform?: (node: T) => R; }; /** * 将树形结构转换为列表 * @param tree 树形结构 * @param options 配置项 * @param options.childrenKey 子节点键名,默认为 'children' * @param options.removeChildren 是否移除子节点,默认为 true * @param options.transform 转换函数,默认为 (node) => ({ ...node }) * @returns 列表 */ declare function treeToList, R extends Record = T>(tree: T[] | T, options?: Options$2): R[]; interface Options$1 { /** * 结果过滤器 * @param item 当前节点数据 * @returns */ filter?: (item: any) => boolean; /** * 结果子节点属性名 * @default 'children' */ childKey?: keyof T; /** * 数据子节点属性名 * @default 'children' */ childProp?: keyof T; } /** * 转换树形结构数据 * @param {Array} tree 树形结构数据 * @param {Object} transformer 数据转换函数 * @param {Object} options 转换选项 * @param {String} options.filter filter 结果过滤器 * @param {String} options.childKey childKey 结果`子节点`属性名 * @param {String} options.childProp childProp 数据`子节点`属性名 * @returns {Array} 转换后的树形结构数据 */ declare function transformTree>(tree: T[], transformer: (node: T) => any, options?: Options$1): any[]; interface Options { /** * 目标属性 * @default 'value' */ key: K; /** * 子节点属性 * @default 'children' */ children?: keyof T; /** * 节点数据转换器 * @param node 节点数据 * @returns 转换后的数据 */ transformer?: (node: T) => any; } type ValidatorFunc = (node: T) => boolean; /** * 递归查找路径 * @param {array} tree 树形结构数据 * @param {string|number|function} value 查找目标 * @param {object} options 配置项 * @param {String} options.key 目标属性 * @param {String} options.children 子节点属性 * @param {String} options.transformer 节点数据转换器 * @returns {array} 路径 */ declare function findPath, K extends keyof T>(tree: T[], value: T[K] | ValidatorFunc, options: Options | K): any[]; export { addInputListener, assign, clone, cloneDeep, cloneJSON, cloneLoop, colorComplementary, colorContrast, colorLuminance, colorRgb as colorRGB, colorRgb, darkenColor, debounce, detectMouseDirection, findPath, hexToRgb, isArray, isBigInt, isBoolean, isDarkColor, isEmpty, isFunction, isHexColor, isHslColor, isInvalid, isLightColor, isNil, isNull, isNumber, isObject, isPlainObject, isRgbColor, isString, isSymbol, isType, isUndefined, lightenColor, listToTree, loadResource, mixColors, randomBool, randomChar, randomColor, randomDate, randomFloat, randomId, randomInt, randomIntList, randomItem, rgbToHex, setColorBrightness, setColorOpacity, shuffleArray, throttle, transformTree, treeToList, uuid }; export type { HslTuple, RgbResultMap, RgbTuple, RgbaTuple };