125 lines
3.3 KiB
TypeScript
125 lines
3.3 KiB
TypeScript
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<T>(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<R, P>} [options] 其他配置项
|
|
* @returns {*}
|
|
*/
|
|
export function rowsToTree<P extends Obj, R extends Obj = P>(params: P[], options?: ToTreeOptions<R, P>) {
|
|
return toTree<R, P>(params, options);
|
|
}
|
|
|
|
/**
|
|
* 树结构 转 行结构
|
|
*
|
|
* @export
|
|
* @template T extends Obj
|
|
* @template U extends Obj
|
|
* @param {T[]} data 数组结构数据
|
|
* @param {?string} [childrenKey] 自定义子节点属性名称
|
|
*/
|
|
export function treeToRows<T extends Obj, U extends Obj>(data: T[], childrenKey?: string): U[] {
|
|
return toRows<T, U>(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<T extends Obj, U extends Obj>(
|
|
data: T[],
|
|
callback: (data: T, index: number, parents: T[]) => U,
|
|
childrenKey?: string,
|
|
): U[] {
|
|
return TREE_MAP<T, U>(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<T extends Obj>(
|
|
data: T[],
|
|
callback: (data: T, index: number, parents: T[]) => boolean,
|
|
childrenKey?: string,
|
|
): T[] {
|
|
return TREE_EACH<T>(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<T extends Obj>(
|
|
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<T extends Obj>(
|
|
data: T[],
|
|
callback: (data: T, index: number, parents: T[]) => boolean,
|
|
childrenKey?: string,
|
|
): T[] {
|
|
return TREE_EXCLUDE(data, callback, childrenKey);
|
|
}
|