@orca-fe/tools
Version:
front-end tools
59 lines (58 loc) • 3.18 kB
TypeScript
import type { RevMapOptions } from './def';
/**
* 遍历树状结构
* @param data 树状数据
* @param callback 返回值处理
* @param options 配置
*/
export declare function revMap<T extends Object, R extends Object>(data: T[], callback: (d: T, parent: T | undefined, index: number) => R | undefined, options?: RevMapOptions<T>): R[];
/**
* 遍历树状结构,并扁平化
* @param data 树状数据
* @param callback 返回值处理
* @param options 配置
*/
export declare function revFlatMap<O extends Object, T extends Object>(data: T[], callback: (d: T, parent: T | undefined, index: number, arr: T[]) => O | undefined, options?: RevMapOptions<T>): O[];
/**
* 将数组转换为树状结构
*/
export declare function list2tree<ListItem extends Object>(list: ListItem[], idField?: string, parentIdField?: string, childKey?: string): ListItem[];
export declare function getByPath<T>(data: T[], path: number[], getByIndex?: (item: T) => T[] | undefined): T | undefined;
export declare function treeFind<T>(data: T[], callback: (item: T, path: number[]) => boolean, getChildren?: (item: T) => T[] | undefined): T | undefined;
export type AbstractTreeNodeType<ChildKeyType extends string, TreeNoteType extends Record<string, unknown>> = TreeNoteType & {
[key in ChildKeyType]?: AbstractTreeNodeType<ChildKeyType, TreeNoteType>[];
};
export type TreeForOptions<ChildKeyType extends string> = {
childrenKey?: ChildKeyType;
parentPath?: number[];
parent?: Record<string, unknown>;
childFirst?: boolean;
};
/**
* 递归遍历树
* @param arr
* @param callback
* @param options
*/
export declare function treeFor<ChildKeyType extends string = 'children', T extends AbstractTreeNodeType<ChildKeyType, Record<string, unknown>> = AbstractTreeNodeType<ChildKeyType, Record<string, unknown>>>(arr: T[], callback: (item: T | null, parent: T | undefined, path: number[]) => void, options?: TreeForOptions<ChildKeyType>): void;
export type TreeMapOptions<ChildKeyType extends string> = TreeForOptions<ChildKeyType>;
/**
* 递归遍历树,并依次替换节点获得全新的树
* @param arr
* @param callback
* @param options
*/
export declare function treeMap<OutTreeNoteType extends Record<string, unknown>, ChildKeyType extends string = 'children', T extends AbstractTreeNodeType<ChildKeyType, Record<string, unknown>> = AbstractTreeNodeType<ChildKeyType, Record<string, unknown>>>(arr: T[], callback: (item: T | null, parent: AbstractTreeNodeType<ChildKeyType, OutTreeNoteType> | undefined, path: number[]) => OutTreeNoteType | null, options?: TreeMapOptions<ChildKeyType>): (AbstractTreeNodeType<ChildKeyType, OutTreeNoteType> | null)[];
export type TreeFilterOptions = {
childrenKey?: string;
};
/**
* 树过滤器,对树状结构的所有节点进行遍历(子节点优先)。
* 对于需要保留的节点,请返回 true。
* 当保留子节点时,父节点则不会被遍历,必须存在。
* @version 0.10.0
* @param arr
* @param callback
* @param options
*/
export declare function treeFilter<T extends Object>(arr: T[], callback: (item: T, path: T[]) => boolean | void, options?: TreeFilterOptions): T[];