import { each as TREE_EACH, exclude as TREE_EXCLUDE, filter as TREE_FILTER, map as TREE_MAP, toRows, toTree, type ToTreeOptions, } from '@zhengxs/js.tree'; /** * 数组去重 * * @template T * @param {T[]} arr 数组 */ export function unique(arr: T[]) { return [...new Set(arr)]; } /** * 计算数组平均值 * * @param {number[]} arr 数组 */ export function average(arr: number[]) { return arr.reduce((a, b) => a + b) / arr.length; } /** * 行结构 转 树结构 * * @export * @template P extends Obj * @template R extends Obj = P * @param {P[]} params 数据源 * @param {?ToTreeOptions} [options] 其他配置项 * @returns {*} */ export function rowsToTree

(params: P[], options?: ToTreeOptions) { return toTree(params, options); } /** * 树结构 转 行结构 * * @export * @template T extends Obj * @template U extends Obj * @param {T[]} data 数组结构数据 * @param {?string} [childrenKey] 自定义子节点属性名称 */ export function treeToRows(data: T[], childrenKey?: string): U[] { return toRows(data, childrenKey); } /** * 修改树内容 * * @export * @template T extends Obj * @template U extends Obj * @param {T[]} data 数组结构数据 * @param {(data: T, index: number, parents: T[]) => U} callback 处理回调函数,注意:如果返回的对象子级不存在将不进行递归操作 * @param {?string} [childrenKey] 自定义子节点属性名称 */ export function treeMap( data: T[], callback: (data: T, index: number, parents: T[]) => U, childrenKey?: string, ): U[] { return TREE_MAP(data, callback, childrenKey); } /** * 遍历修改所有节点 * * @export * @template T extends Obj * @param {T[]} data 数组结构数据 * @param {((data: T, index: number, parents: T[]) => boolean | void)} callback 处理回调函数,返回 `true` 将跳过子级的遍历操作 * @param {?string} [childrenKey] 自定义子节点属性名称 */ export function treeEach( data: T[], callback: (data: T, index: number, parents: T[]) => boolean, childrenKey?: string, ): T[] { return TREE_EACH(data, callback, childrenKey); } /** * 过滤树内容,获取需要的数据 * * @export * @template T extends Obj * @param {T[]} data 数组结构数据 * @param {(data: T, index: number, parents: T[]) => boolean} callback 处理回调,注意:如果返回的对象子级不存在将不进行递归操作 * @param {?(string)} [childrenKey] 自定义子节点属性名称 */ export function treeFilter( data: T[], callback: (data: T, index: number, parents: T[]) => boolean, childrenKey?: string, ): T[] { return TREE_FILTER(data, callback, childrenKey); } /** * 过滤树内容,排除不需要的数据 * * @export * @template T extends Obj * @param {T[]} data 数组结构数据 * @param {(data: T, index: number, parents: T[]) => boolean} callback 处理回调,返回 true 的数据将被过滤掉 * @param {?(string)} [childrenKey] 自定义子节点属性名称 */ export function treeExclude( data: T[], callback: (data: T, index: number, parents: T[]) => boolean, childrenKey?: string, ): T[] { return TREE_EXCLUDE(data, callback, childrenKey); }