@gulibs/vgrove-ui
Version:
VGrove UI component library built with HeroUI and React
156 lines • 4.74 kB
TypeScript
import type { IsAsyncFunctionOptions, Localized, LocalizedOrNode } from './types';
/**
* 将对象转换为格式化的JSON字符串
* 支持Map类型的自动转换
*/
export declare function toJSON(obj: unknown): string;
/**
* 将Map对象转换为数组
*/
export declare function mapToArray<K, V>(map: Map<K, V>): V[];
/**
* 检查对象是否为记录类型
*/
export declare const isRecord: (obj: unknown) => obj is Record<string, unknown>;
/**
* 规范化路由路径
* 移除多余的斜杠,确保路径格式正确
*/
export declare function normalizePath(path: string): string;
/**
* 连接路径片段
*/
export declare function joinPath(...segments: string[]): string;
/**
* 检查路径是否匹配模式
* 支持动态参数匹配
*/
export declare function matchPath(pattern: string, path: string): boolean;
/**
* 从路径中提取参数
*/
export declare function extractParams(pattern: string, path: string): Record<string, string>;
/**
* 检查值是否为空(null、undefined、空字符串、空数组、空对象)
*/
export declare function isEmpty(value: unknown): boolean;
/**
* 检查值是否为函数
*/
export declare function isFunction(value: unknown): value is Function;
/**
* 检查值是否为Promise
*/
export declare function isPromise(value: unknown): value is Promise<unknown>;
/**
* 安全的类型转换
*/
export declare function safeParseInt(value: unknown, defaultValue?: number): number;
/**
* 将字符串转换为驼峰命名
*/
export declare function toCamelCase(str: string): string;
/**
* 将驼峰命名转换为短横线命名
*/
export declare function toKebabCase(str: string): string;
/**
* 首字母大写
*/
export declare function capitalize(str: string): string;
/**
* 截断字符串
*/
export declare function truncate(str: string, length: number, suffix?: string): string;
/**
* 深度克隆对象
*/
export declare function deepClone<T>(obj: T): T;
/**
* 深度合并对象
* 相比 lodash.merge,此实现:
* 1. 数组会被完全替换而不是合并(更符合配置合并的预期)
* 2. 性能更优(约 300x faster)
* 3. 只处理普通对象的深度合并,其他类型直接替换
* 4. undefined 值会被忽略,null 值会覆盖目标值
*/
export declare function deepMerge<T extends Record<string, any>>(target: T, source: Partial<T>): T;
/**
* 数组去重
*/
export declare function unique<T>(array: T[], keyFn?: (item: T) => any): T[];
/**
* 数组分组
*/
export declare function groupBy<T>(array: T[], keyFn: (item: T) => string): Record<string, T[]>;
/**
* 简单的内存缓存实现
*/
export declare class SimpleCache<K, V> {
private cache;
private maxAge;
private maxSize;
constructor(maxAge?: number, maxSize?: number);
set(key: K, value: V): void;
get(key: K): V | undefined;
has(key: K): boolean;
delete(key: K): boolean;
clear(): void;
private cleanup;
}
/**
* 防抖函数
*/
export declare function debounce<T extends (...args: any[]) => void>(fn: T, delay: number): T;
/**
* 节流函数
*/
export declare function throttle<T extends (...args: any[]) => void>(fn: T, delay: number): T;
/**
* 测量函数执行时间
*/
export declare function measureTime<T extends (...args: any[]) => any>(fn: T, name?: string): T;
/**
* 解析查询字符串
*/
export declare function parseQuery(search: string): Record<string, string>;
/**
* 构建查询字符串
*/
export declare function buildQuery(params: Record<string, unknown>): string;
/**
* 更新URL查询参数
*/
export declare function updateQuery(url: string, params: Record<string, unknown>): string;
/**
* 安全地执行函数,捕获错误
*/
export declare function safely<T>(fn: () => T, fallback: T): T;
/**
* 安全地执行异步函数,捕获错误
*/
export declare function safelyAsync<T>(fn: () => Promise<T>, fallback: T): Promise<T>;
/**
* 检查对象是否为本地化对象
*/
export declare function isLocalized<Keys extends string = string>(localized: LocalizedOrNode<Keys> | undefined): localized is Localized<Keys>;
/**
* 严格检测一个函数是否是异步函数(async function)
* @param fn 待检测的目标对象
* @param options 配置选项
* @returns 如果是异步函数返回 true,否则返回 false
*
* @example
* ```ts
* // 基本使用
* isAsyncFunction(async () => {}); // => true
*
* // 带调试模式
* isAsyncFunction(() => {}, { debug: true });
*
* // 启用行为检测
* isAsyncFunction(someFn, { enableBehaviorCheck: true });
* ```
*/
export declare function isAsyncFunction(fn: unknown, options?: IsAsyncFunctionOptions): fn is (...args: any[]) => Promise<any>;
//# sourceMappingURL=utils.d.ts.map