@gulibs/react-autoroutes-client
Version:
Client-side utilities for React auto routes
129 lines • 3.73 kB
TypeScript
import { Localized, LocalizedOrNode } from './types';
/**
* 将对象转换为格式化的JSON字符串
* 支持Map类型的自动转换
*/
export declare function toJSON(obj: any): string;
/**
* 将Map对象转换为数组
*/
export declare function mapToArray<K, V>(map: Map<K, V>): V[];
/**
* 检查对象是否为记录类型
*/
export declare const isRecord: (obj: any) => obj is Record<string, any>;
/**
* 规范化路由路径
* 移除多余的斜杠,确保路径格式正确
*/
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: any): boolean;
/**
* 检查值是否为函数
*/
export declare function isFunction(value: any): value is Function;
/**
* 检查值是否为Promise
*/
export declare function isPromise(value: any): value is Promise<any>;
/**
* 安全的类型转换
*/
export declare function safeParseInt(value: any, 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;
/**
* 深度合并对象
*/
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, any>): string;
/**
* 更新URL查询参数
*/
export declare function updateQuery(url: string, params: Record<string, any>): 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>;
//# sourceMappingURL=utils.d.ts.map