import type { Resource } from "@src/tems"; export const recusiveRemovePath = (obj: Resource, pathArray: string[]) => { if (!obj || pathArray.length === 0) return; const key = pathArray.shift(); if (key !== undefined) { if (pathArray.length === 0) { delete obj[key]; } else if (obj[key] && typeof obj[key] === "object") { recusiveRemovePath(obj[key], pathArray); } } }; export const dataBuilder = ( resource: Resource, pathToRemove: string[] = [], replacements: object = {}, removeThenReplace = false, ): Resource => { const clone = structuredClone(resource); if (!removeThenReplace) { for (const path of pathToRemove) { const splittedPath = path.split("."); recusiveRemovePath(clone, splittedPath); } } for (const [key, value] of Object.entries(replacements)) { clone[key] = value; } if (removeThenReplace) { for (const path of pathToRemove) { const splittedPath = path.split("."); recusiveRemovePath(clone, splittedPath); } } return clone; };