{"version":3,"sources":["../src/tree/toPidTree.ts"],"names":["toPidTree","treeObj","options","opts","DefaultTreeOptions","childrenKey","idKey","includeLevel","includePath","mapper","nodes","forEachTreeByDfs","node","parent","level","path","index","newNode","omit"],"mappings":";;;;AAuCO,SAASA,CAMdC,CAAAA,CAAAA,CAA8BC,CAAqH,CAAA,CAEjJ,IAAMC,CAAO,CAAA,MAAA,CAAO,MAAO,CAAA,EAAIC,CAAAA,CAAAA,CAAoBF,CAAW,EAAA,EAAE,CAC1D,CAAA,CAAE,WAAAG,CAAAA,CAAAA,CAAY,KAAAC,CAAAA,CAAAA,CAAM,YAAAC,CAAAA,CAAAA,CAAa,KAAM,WAAAC,CAAAA,CAAAA,CAAY,KAAM,CAAA,MAAA,CAAAC,GAAO,CAAA,CAAIN,CAEtEO,CAAAA,CAAAA,CAA2D,EAC/D,CAAA,OAAAC,GAAgCV,CAAAA,CAAAA,CAAQ,CAAC,CAAC,IAAAW,CAAAA,CAAAA,CAAK,OAAAC,CAAO,CAAA,KAAA,CAAAC,CAAM,CAAA,IAAA,CAAAC,CAAK,CAAA,KAAA,CAAAC,CAAK,CAAA,GAAI,CACtE,GAAG,OAAOP,GAAS,EAAA,UAAA,CAAW,CAC1B,IAAIQ,CAAU,CAAA,MAAA,CAAO,OAAO,CACxB,CAACX,CAAK,EAAGM,EAAKN,CAAK,CAAA,CACnB,GAAI,CAAA,IACR,EAAEG,GAAO,CAAA,CAAC,IAAAG,CAAAA,CAAAA,CAAK,MAAAC,CAAAA,CAAAA,CAAO,KAAAC,CAAAA,CAAAA,CAAM,MAAAE,CAAM,CAAA,IAAA,CAAAD,CAAI,CAAC,CAAC,CAAA,CACxCE,CAAQ,CAAA,GAAA,CAAKJ,EAASA,CAASP,GAAAA,CAAK,CAAK,CAAA,IAAA,CACtCC,CAAcU,GAAAA,CAAAA,CAAQ,KAAQH,CAAAA,CAAAA,CAAAA,CAC9BN,IAAaS,CAAQ,CAAA,IAAA,CAAOF,CAC/BL,CAAAA,CAAAA,CAAAA,CAAM,IAAKO,CAAAA,CAAO,CAClBP,CAAAA,CAAAA,CAAM,OACV,CAAA,KAAK,CACD,IAAIO,CAAU,CAAA,MAAA,CAAO,MAAO,CAAA,GAAGC,GAAKN,CAAAA,CAAAA,CAAKP,CAAW,CAAC,CACrDY,CAAAA,CAAAA,CAAQ,GAAKJ,CAAAA,CAAAA,CAASA,IAASP,CAAK,CAAA,CAAK,IACtCC,CAAAA,CAAAA,GAAcU,CAAQ,CAAA,KAAA,CAAQH,CAC9BN,CAAAA,CAAAA,CAAAA,GAAaS,EAAQ,IAAOF,CAAAA,CAAAA,CAAAA,CAC/BL,CAAM,CAAA,IAAA,CAAKO,CAAO,EACtB,CACJ,CAAEd,CAAAA,CAAI,EACCO,CACX","file":"chunk-S3PQSDW6.mjs","sourcesContent":["/**\n * \n * 将{id:any,children:[{},{},...,{}]}形式的树转换为[{id,pid,...},{id,pid,...},...,{id,pid,...}]的树结构\n * \n * \n */\n\n\n\n\nimport { DefaultTreeOptions } from \"./consts\";\nimport { TreeNode, TreeNodeBase, TreeNodeId, TreeNodeOptions } from \"./types\";\nimport { omit } from \"../object/omit\"\nimport { forEachTreeByDfs } from \"./forEachTreeByDfs\";\n\n export interface ToPidTreeOptions<\n    FromNode extends TreeNodeBase = TreeNode,\n    ToNode extends TreeNodeBase = FromNode,\n    IdKey extends string = 'id',\n    ChildrenKey extends string = 'children',\n    Path=string\n> extends TreeNodeOptions{\n     includeLevel?: boolean\n     includePath?: boolean\n     mapper?:({node,level,parent,path,index}:{node:FromNode,level:number,parent?:FromNode | null ,path:Path[],index:number}) => Omit<ToNode,ChildrenKey | IdKey>\n }\n  \nexport type PidTreeNode< \n    Node extends Record<string,any> = {[key: string]: any},\n    IdKey extends string = 'id',\n    Path = string\n> = Node      \n    & TreeNodeId<IdKey,Node[IdKey]>  \n    & {\n        pid:Node[IdKey] | null\n        level?:number\n        path?:Path[]\n    } \n\nexport function toPidTree<\n    FromNode extends TreeNodeBase = TreeNode,\n    ToNode extends TreeNodeBase = FromNode,\n    IdKey extends string = 'id',\n    ChildrenKey extends string = 'children',\n    Path = string\n>(treeObj:FromNode | FromNode[],options?:ToPidTreeOptions<FromNode,ToNode,IdKey,ChildrenKey,Path>):PidTreeNode<Omit<ToNode,ChildrenKey>,IdKey,Path>[]{\n    \n    const opts = Object.assign({}, DefaultTreeOptions ,options || {}) as Required<ToPidTreeOptions<FromNode,ToNode,IdKey,ChildrenKey,Path>>     \n    const { childrenKey,idKey,includeLevel=true, includePath=false,mapper } = opts\n\n    let nodes:PidTreeNode<Omit<ToNode,ChildrenKey>,IdKey,Path>[] = []\n    forEachTreeByDfs<FromNode,Path>(treeObj,({node,parent,level,path,index})=>{\n        if(typeof(mapper)=='function'){\n            let newNode = Object.assign({\n                [idKey]: node[idKey],\n                pid:null\n            },mapper({node,parent,level,index,path})) as PidTreeNode<ToNode,IdKey,Path>     \n            newNode.pid =parent ? parent?.[idKey]  : null            \n            if(includeLevel) newNode.level = level\n            if(includePath) newNode.path = path \n            nodes.push(newNode)\n            nodes.push()\n        }else{\n            let newNode = Object.assign({},omit(node,childrenKey)) as PidTreeNode<ToNode,IdKey,Path>\n            newNode.pid =parent ? parent?.[idKey]  : null\n            if(includeLevel) newNode.level = level\n            if(includePath) newNode.path = path\n            nodes.push(newNode)\n        }                \n    },opts)\n    return nodes\n}"]}