Files
2026-08-11 13:47:48 +08:00

116 lines
5.6 KiB
TypeScript

/**
* 把任意值按目标类型做类型转换(json/array/number/integer/float/bool/string 互转)
* @param {*} data 待转换的值
* @param {string} type 目标类型
* @returns {*} 转换后的值,没有匹配的转换规则时原样返回
*/
declare function toPrimitive(data: any, type: string): any;
/**
* 判断值的基础类型,返回 'null'/'array'/'json'/'bool'/'string'/'integer'/'float' 之一
* @param {*} value 待判断的值
* @returns {string} 类型名
*/
declare function getPrimitiveType(value: any): string;
/**
* 按 registerProps 的字段配置(code/field/router/query/string)解析出实际的 props 数据;
* 配置里出现 type: 'code' 时走异步表达式求值,否则走同步解析
* @param {Object} [registerProps] props 的类型/来源配置,支持嵌套
* @param {Object} [data] 表达式求值、字段取值时使用的上下文数据
* @param {Object} [fns] 表达式求值时可调用的额外函数
* @param {*} [defaultValue] 字段没有取到值时的兜底默认值
* @returns {Object|Promise<Object>} 解析后的 props 数据;含 code 类型字段时返回 Promise
*/
declare function getWidgetPropsData(registerProps?: any, data?: any, fns?: any, defaultValue?: any): any | Promise<any>;
/**
* 递归遍历每个成员做条件判断,成员是 object 或 array 时继续深入遍历
* @param {*} items 待遍历的值(对象/数组/其他)
* @param {(value: *, key: string|number) => boolean} [func] 判断函数,返回 true 即命中
* @returns {boolean} 只要有一处命中就返回 true
*/
declare function deepSome(items: any, func?: (value: any, key: string | number) => boolean): boolean;
/**
* 递归替换对象(含嵌套对象/数组)里的 key,不在 keyMap 里的 key 保持不变
* @param {*} obj 待处理的对象/数组/其他值
* @param {Object<string, string>} keyMap 旧 key 到新 key 的映射
* @returns {*} 替换 key 后的新对象;非对象/数组的值原样返回
*/
declare function replaceKeys(obj: any, keyMap: {
[x: string]: string;
}): any;
/**
* 生成一个 UUID v4 格式的字符串(非加密安全,仅用于前端本地标识)
* @returns {string} UUID 字符串
*/
declare function uuid(): string;
/**
* 从 @pkg 风格的包路径里取出包名(路径第 4 段),如 @pkg/owner/category/name/... -> name
* @param {string} pkg 包路径字符串
* @returns {string} 包名,解析不出时返回空字符串
*/
declare function getPkgName(pkg: string): string;
/**
* 从 @pkg 风格的包路径里取出分类(路径第 3 段),如 @pkg/owner/category/... -> category
* @param {string} pkg 包路径字符串
* @returns {string} 分类名,解析不出时返回空字符串
*/
declare function getPkgCategory(pkg: string): string;
/**
* 从 @pkg 风格的包路径里取出所有者(路径第 2 段),如 @pkg/owner/... -> owner
* @param {string} pkg 包路径字符串
* @returns {string} 所有者名,解析不出时返回空字符串
*/
declare function getPkgOwner(pkg: string): string;
/**
* 生成 JSON 的 MurmurHash3
* @param {Object} obj - JSON 对象
* @param {number} bits - 输出位数 32 或 128,默认 32
* @param {"x86"|"x64"|"auto"} algo - 算法版本,默认 auto
* @returns {string} hash 字符串
*/
declare function hashJSON(obj: any, bits?: number, algo?: "x86" | "x64" | "auto"): string;
/**
* 创建一个 id 生成器;返回的函数把任意一组参数(对象/函数按引用、原始值按值)映射成
* 形如 "1::2::3" 的可读唯一 key,相同的参数组合总是得到相同的 id
* @returns {(...args: *[]) => string} getId 函数
*/
declare function makeUniqueIdGenerator(): (...args: any[]) => string;
/**
* 判断 URL 是否包含域名(包括域名、localhost 和 IP 地址)
* @param {string} url - 待检测的 URL 字符串
* @returns {boolean} 如果包含域名/主机名返回 true,否则返回 false
*
* @example
* hasUrlPrefix('https://www.example.com/path') // true
* hasUrlPrefix('http://example.com') // true
* hasUrlPrefix('//example.com/path') // true
* hasUrlPrefix('example.com/path') // true
* hasUrlPrefix('www.example.com') // true
* hasUrlPrefix('http://localhost:3000') // true
* hasUrlPrefix('http://127.0.0.1:8080') // true
* hasUrlPrefix('http://192.168.1.1') // true
* hasUrlPrefix('/path/to/resource') // false
* hasUrlPrefix('./relative/path') // false
* hasUrlPrefix('../relative/path') // false
*/
declare function hasUrlPrefix(url: string): boolean;
/**
* 为 URL 添加默认前缀(如果没有域名前缀)
* @param {string} url - 待处理的 URL 字符串
* @param {string} defaultPrefix - 默认前缀,默认为当前域名
* @returns {string} 处理后的 URL
*
* @example
* getUrlWithPrefix('/path/to/resource') // 'http://current-domain.com/path/to/resource'
* getUrlWithPrefix('/api/data', 'https://api.example.com') // 'https://api.example.com/api/data'
* getUrlWithPrefix('https://example.com/path') // 'https://example.com/path' (不变)
* getUrlWithPrefix('example.com/path') // 'example.com/path' (不变,已有域名)
*/
declare function getUrlWithPrefix(url: string, defaultPrefix?: string): string;
/**
* 生成 UUID v4 字符串,优先用 crypto.randomUUID/getRandomValues,都不支持时降级为 Math.random
* @returns {string} UUID 字符串
*/
declare function generateUUID(): string;
export { deepSome, generateUUID, getPkgCategory, getPkgName, getPkgOwner, getPrimitiveType, getUrlWithPrefix, getWidgetPropsData, hasUrlPrefix, hashJSON, makeUniqueIdGenerator, replaceKeys, toPrimitive, uuid };