/** * 将列表数据转换为树形数据 (迭代方式) * 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 { findPath, listToTree, transformTree, treeToList };