UNPKG

gzjs-utils

Version:

smart js utils

91 lines (90 loc) 3.04 kB
export type TreeNodeEF = { __level__: number; __isRoot__: boolean; } & Record<any, any>; /** * @param curr 当前项 * @param parent 父项 * @param idx 当前项目所在列表中的索引 * @param arr 当前项所在的列表对象 */ export type EachTreeConsumer<T> = { /** * * @param curr 当前节点 * @param parent 父节点 * @param idx 当前节点所在列表的索引 * @param arr 当前节点所在的列表 */ (curr: T, parent?: T | null, idx?: number, arr?: T[]): any; }; /** * 筛选器 * @param c 当前节点 * @param p 父节点 * @param idx 当前节点在列表中的索引 * @param arr 当前节点所在的列表 */ export type TreeFilter<T> = { /** * * @param c 当前节点 * @param p 父节点 * @param idx 当前节点在列表中的索引 * @param arr 当前节点所在的列表 * */ (c: T, p: T | null, idx: number, arr?: T[]): boolean; }; declare const _TreeUtils_: { /** * 构建树型结构 * @param arr 数据列表 * @param idField id 唯一标识节点名称.默认=id * @param parentField 父级节点名称.默认=parentId * @param childrenField 子节点名称 .默认=children */ buildTree<T, C extends string, R = T & { __level__: number; __isRoot__: boolean; } & Record<any, any> & { [K in C]: R[]; }>(arr: T[], idField?: string, parentField?: string, childrenField?: C | "children"): R[]; /** * 遍历树节点 * @param arr * @param callback * @param childrenField 默认=children */ eachTree<T, R = T>(arr: T[], callback: EachTreeConsumer<T>, childrenField?: string | "children"): R[]; /** * 查找 树节点 只返回一项 * @param treeList * @param filter * @param childrenField 默认=children */ findTreeItem<T>(treeList: T[], filter: TreeFilter<T>, childrenField?: string | "children"): T | null; /** * 输转 数组 * @param arr * @param childrenField 默认=children * @param removeChildren 是否删除 children 节点 */ treeToArray<T>(arr: T[] | T, childrenField?: string, removeChildren?: boolean): T[]; /** * 查找 一项 * @param arr 数据源 * @param filter 筛选器 * @param childrenField 默认=children * * @return The first element in the array that satisfies the provided testing function. Otherwise, undefined is returned. */ find<T>(arr: T[], filter: TreeFilter<T>, childrenField?: string | "children"): T | null; /** * 查找数 * @param arr 数列表 * @param filter 过滤器 * @param childrenField 默认=children * @return A shallow copy of the given array containing just the elements that pass the test. If no elements pass the test, an empty array is returned. */ filter<T>(arr: T[], filter: TreeFilter<T>, childrenField?: string | "children"): T[]; }; export default _TreeUtils_; export type TreeUtils = typeof _TreeUtils_;