UNPKG

@diyaner/ding

Version:

dingiyan常用ts/js工具

114 lines (113 loc) 4.16 kB
type TListID = string | number; type Obj = { [key: string]: any; }; export declare class ListToTree<T extends Obj> { constructor( /** origin list */ originlist: T[]); /** 静态方法,实例化一个listToTree,并返回实例 */ static init<T extends object>(originList: T[]): ListToTree<T>; /** transaction option */ private option; list: T[]; /** transacted tree */ tree: TreeNode<T>[]; /** * in passing transacted to obj when to tree. * * the options.id is key. */ obj: { [key: TListID]: TreeNode<T>; }; setOption(opt: IListToTreeOption<T>): void; getOption(): IListToTreeOption<T>; /** * the main method. do turn list to tree. */ toTree(option?: IListToTreeOption<T>): TreeNode<T>[]; /** * get one node's level. * * top level is 0 * */ getLevel(id: TListID): number; /** get node's parents node id. */ getParentIds(id: TListID): TListID[]; /** get all parents node object. */ getParentNodes(id: TListID): TreeNode<T>[]; getNode(id: TListID): TreeNode<T>; /** 获取一个节点的所有后代子节点id列表 */ getAllChildrens(id: TListID): string[]; /** remove one or multiple node by id */ removeNode(id: TListID | TListID[]): TreeNode<T>[]; pushNode(obj: T): TreeNode<T>[]; /** 检查源数据列表 */ private checkTheOriginList; /** 检查toTree的选项参数 */ private checkParams; } export default ListToTree; /** the list to tree transaction option. */ export interface IListToTreeOption<T extends object> { /** the item unique id field. the filed's value must number or stringNumber */ id: string; /** the parent id field name. must require. and all of list item must give it. the filed's value must number or stringNumber */ parent: string; /** 排序函数,在每层级元素遍历前,可先排序 */ sort?: (arr: T[]) => T[]; /** 根节点的上级id值 用于确定根数组 */ rootValue: TListID; /** 源对象映射到label的字段名 */ mapToLabel?: string; /** 源对象映射到value的字段名,如果不提供,默认以主键id字段映射 */ mapToValue?: string; /** 是否允许空的子元素 默认允许,设置为false则没有子元素的节点将没有children属性 */ allowEmptyChildren?: boolean; /** 父id没有时,是否将之改为根值(处理游离节点无法加入以rootValue为根的树里面) */ absentParentResetToRootValue?: boolean; /** 循环遍历原始list时,可执行此回调函数,进行自定义处理 */ itemHandler?: (obj: T, parentTreeNode?: TreeNode<T>) => void; /** 节点toJson方法的自定义逻辑 */ treeNodeToJson?: (obj: ReturnType<TreeNode<T>["toJSON"]>, treeNode: TreeNode<T>) => any; } /** 树节点类 */ declare class TreeNode<originObj extends Obj> { constructor(obj: any, option: IListToTreeOption<originObj>); private option; /** origin object 原始对象 */ origin: originObj; /** 节点层级 */ level: number; /** 父节点id的数组 从父级一直到顶层 */ parentIds: TListID[]; /** 子节点 */ children?: TreeNode<originObj>[]; /** 父级节点实例 */ parentNode?: TreeNode<originObj>; /** node label 从源对象中映射 */ get label(): any; /** node id 从源对象中映射 */ get id(): TListID; /** node value 从源对象映射,使用自定义map映射或id的映射 */ get value(): TListID; /** node parent id 从源对象中映射 的父节点id */ get parent(): TListID; /** 获取所有后代节点的id列表 */ getAllChildrensId(): TListID[]; /** 获取所有后代节点对象 */ getAllChildrensNode(): TreeNode<originObj>[]; /** 用于json序列化,去除循环引用,获取get属性 */ toJSON(): { id: TListID; value: TListID; parent: TListID; label: any; level: number; parentIds: TListID[]; origin: originObj; children: TreeNode<originObj>[]; }; } export { TreeNode };