export declare function deepFirstSearch({ tree, callback, childrenKey, _parentNode, }: { /** * 树形结构数据 */ tree: Record; /** * 遍历节点时将被调用的函数。在函数内部可以修改节点信息。 * @param node 当前节点 * @param parentNode 父节点 */ callback: (node: TNode, parentNode?: TNode) => void; /** * 可指定children键名。默认键名为children */ childrenKey?: keyof TNode; /** * 递归时用来传递父节点 * @ignore */ readonly _parentNode?: TNode; }): Record; /** * 深度优先遍历树节点 * * ```ts * import { Tree } from 'sunny-js' * const _tree = [ * { * name: 'parentA', * children: [ * { * name: 'child', * }, * ], * }, * ] * // 深度优先遍历树 * Tree.deepFirstSearch({ * tree: _tree, * callback(node, parentNode) { * console.log(node) * }, * }) * ``` */ export declare function deepFirstSearch({ tree, callback, childrenKey, _parentNode, }: { /** * 树形结构数据 */ tree: TNode[]; /** * 遍历节点时将被调用的函数。在函数内部可以修改节点信息。 * @param node 当前节点 * @param parentNode 父节点 */ callback: (node: TNode, parentNode?: TNode) => void; /** * 可指定children键名。默认键名为children */ childrenKey?: keyof TNode; /** * 递归时用来传递父节点 * @ignore */ readonly _parentNode?: TNode; }): TNode[]; /** * 创建访问链 * * 将根据树的父子关系,用指定字段值创建链式字符串。 * 在没有唯一id的节点数据中,可以用于创建唯一索引id * * 举个例子 * ```ts * import { Tree } from 'sunny-js' * const _tree = [ * { * name: 'parentA', * children: [ * { * name: 'child', * }, * ], * }, * { * name: 'parentB', * children: [ * { * name: 'child', * }, * ], * }, * ] * Tree.deepFirstSearch({ * tree: _tree, * callback(node, parentNode) { * Tree.addChainingKey({ * node, * parentNode, * readKey: 'name', * }) * }, * }) * console.log(_tree) * ``` * 在遍历树节点后,如果打印被修改后的树,会如下所示 * ```json * [ * { * name: 'parentA', * chainingKey: 'parentA', * children: [ * { * name: 'child', * chainingKey: 'parentA/child', * } * ] * }, * { * name: 'parentB', * chainingKey: 'parentB', * children: [ * { * name: 'child', * chainingKey: 'parentB/child', * } * ] * } * ] * ``` * * @see 一般搭配{@link deepFirstSearch}一起使用 */ export declare function addChainingKey({ node, parentNode, readKey, chainingKey, separator, }: { /** * 当前节点引用 */ node: TNode; /** * 父节点引用 */ parentNode?: TNode; /** * 读取字段值的键名,用来创建chainingKey */ readKey: keyof TNode; /** * 自定义chainingKey名称。默认为`chainingKey` */ chainingKey?: string; /** * 自定义chainingKey的分隔符。默认为正斜杠`/` */ separator?: string; }): string; /** * 为节点添加与父节点关联关系 * * ```ts * import { Tree } from 'sunny-js' * const _tree = [ * { * name: 'parentA', * children: [ * { * name: 'child', * }, * ], * }, * ] * // 深度优先遍历树,并添加父节点引用 * Tree.deepFirstSearch({ * tree: _tree, * callback(node, parentNode) { * Tree.addParent({ * node, * parentNode, * }) * }, * }) * console.log(_tree) * ``` * * @see 一般搭配{@link deepFirstSearch}一起使用 */ export declare function addParent, TParentKey extends PropertyKey = 'parent', TReadKey extends keyof TNode = undefined>({ node, parentNode, parentKey, readKey, }: { /** * 当前节点引用 */ node: TNode; /** * 父节点引用 */ parentNode?: TNode; /** * 定义父节点引用的字段名称。默认为parent */ parentKey?: TParentKey; /** * 如果提供字段名则获取父节点字段值作为parentKey的字段值。 * 如果不提供则默认为引用 */ readKey?: TReadKey; }): TNode & { [K in TParentKey]: TReadKey extends undefined ? TNode : TNode[TReadKey]; }; /** * 为树创建索引 * * ```ts * import { Tree } from 'sunny-js' * interface CustomTree { * index: number * children?: CustomTree[] * } * const _customTree: CustomTree[] = [ * { * index: 0, * children: [ * { * index: 1, * children: [{ index: 2 }, { index: 3 }], * }, * ], * }, * { index: 4 }, * { * index: 5, * children: [ * { * index: 6, * children: [{ index: 7 }], * }, * { index: 8 }, * ], * }, * { index: 9 }, * ] * console.log( * '通过树节点index属性值创建索引对象', * Tree.buildIndex(_customTree, 'index') * ) * console.log( * '通过树节点index属性值组成chainingKey创建索引对象', * Tree.buildIndex(_customTree, 'index', { indexByChainingKey: true }) * ) * ``` * * @param tree 树型结构数据 * @param readKey 用来创建索引的属性名 * @param childrenKey * @param indexByChainingKey * @see {@link addChainingKey} */ export declare function buildIndex(tree: TNode[], readKey: keyof TNode, { childrenKey, indexByChainingKey, }?: { /** * 可指定children键名。默认键名为children */ childrenKey?: keyof TNode; /** * 借用{@link addChainingKey}来创建索引 */ indexByChainingKey?: boolean | { /** * 自定义chainingKey名称。默认为`chainingKey` */ chainingKey?: string; /** * 自定义chainingKey的分隔符。默认为正斜杠`/` */ separator?: string; }; }): Record;