staff_data/packages/common/src/utils.ts

107 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-09-03 20:19:33 +08:00
import { Staff } from "@prisma/client";
import { DataNode } from "./type";
export function findNodeByKey(
nodes: DataNode[],
targetKey: string
): DataNode | null {
let result: DataNode | null = null;
for (const node of nodes) {
if (node.key === targetKey) {
return node;
}
if (node.children && node.children.length > 0) {
result = findNodeByKey(node.children, targetKey);
if (result) {
return result;
}
}
}
return result;
}
export function findStaffById(
nodes: DataNode[],
staffId: string
): Staff | null {
for (const node of nodes) {
// 在当前节点的staffs数组中查找
const foundStaff = node?.data?.staffs.find(
(staff: Staff) => staff.id === staffId
);
if (foundStaff) {
return foundStaff;
}
// 如果当前节点的staffs数组中没有找到则递归在子节点中查找
if (node.children) {
const foundInChildren = findStaffById(node.children, staffId);
if (foundInChildren) {
return foundInChildren;
}
}
}
// 如果在所有节点及其子节点中都没有找到返回null
return null;
}
interface MappingConfig {
titleField?: string;
keyField?: string;
valueField?: string;
hasChildrenField?: string; // Optional, in case the structure has nested items
childrenField?: string;
}
export function mapToDataNodes(
inputArray: any[],
config: MappingConfig = {}
): DataNode[] {
const {
titleField = "title",
keyField = "key",
valueField = "value",
hasChildrenField = "hasChildren",
childrenField = "children"
} = config;
return inputArray.map((item) => {
const hasChildren = item[hasChildrenField] || false;
const children = item[childrenField]
return {
title: item[titleField] || "",
key: item[keyField] || "",
value: item[valueField] || null,
data: item,
children: children
? mapToDataNodes(children, { titleField, keyField, valueField, hasChildrenField, childrenField })
: undefined,
hasChildren
};
});
}
/**
*
*
*
*
*
* @template T -
* @param {T[]} array1 -
* @param {T[]} array2 -
* @returns {T[]}
*
* @example
* const array1 = [1, 2, 3, 4];
* const array2 = [3, 4, 5, 6];
* const result = mergeAndDeduplicate(array1, array2);
* console.log(result); // 输出: [1, 2, 3, 4, 5, 6]
*/
export function mergeAndDeduplicate<T = string>(array1: T[], array2: T[]): T[] {
const set = new Set([...array1, ...array2]);
return Array.from(set);
}