@osmos/arraytotree 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -43,6 +43,8 @@ function arrayToTree(nodes, option) {
43
43
  parent[childrenKey] = [];
44
44
  }
45
45
  parent[childrenKey].push(node);
46
+ } else {
47
+ roots.push(node);
46
48
  }
47
49
  }
48
50
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\n\nexport function arrayToTree(\n nodes: any[],\n option: {\n idKey: string\n parentKey: string\n childrenKey: string\n sortKey?: string\n },\n): TreeNodes {\n const {idKey, parentKey, childrenKey, sortKey} = option\n const roots: any = []\n\n if (sortKey) {\n nodes.sort((a: any, b: any) => a[sortKey] - b[sortKey])\n }\n\n\n for (const node of nodes) {\n\n let parentId = node[parentKey];\n\n if (parentId && typeof parentId === 'object') {\n parentId = parentId[idKey]\n }\n\n if (parentId === null || parentId === undefined) {\n roots.push(node)\n } else {\n const parent = nodes.find((item) => item[idKey] === parentId)\n if (parent) {\n if (!parent[childrenKey]) {\n parent[childrenKey] = []\n }\n parent[childrenKey].push(node)\n }\n }\n }\n\n function findNodeById(id: any, nodes?: any[]): any {\n if (nodes) {\n for (const node of nodes) {\n if (node[idKey] === id) {\n return node\n }\n const foundNode = findNodeById(id, node[childrenKey])\n if (foundNode) {\n return foundNode\n }\n }\n }\n return null\n }\n\n // Add the findNodeById method to the result\n roots.findNodeById = (id: any) => findNodeById(id, roots)\n\n return roots\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,SAAS,YACZ,OACA,QAMS;AACT,QAAM,EAAC,OAAO,WAAW,aAAa,QAAO,IAAI;AACjD,QAAM,QAAa,CAAC;AAEpB,MAAI,SAAS;AACT,UAAM,KAAK,CAAC,GAAQ,MAAW,EAAE,OAAO,IAAI,EAAE,OAAO,CAAC;AAAA,EAC1D;AAGA,aAAW,QAAQ,OAAO;AAEtB,QAAI,WAAW,KAAK,SAAS;AAE7B,QAAI,YAAY,OAAO,aAAa,UAAU;AAC1C,iBAAW,SAAS,KAAK;AAAA,IAC7B;AAEA,QAAI,aAAa,QAAQ,aAAa,QAAW;AAC7C,YAAM,KAAK,IAAI;AAAA,IACnB,OAAO;AACH,YAAM,SAAS,MAAM,KAAK,CAAC,SAAS,KAAK,KAAK,MAAM,QAAQ;AAC5D,UAAI,QAAQ;AACR,YAAI,CAAC,OAAO,WAAW,GAAG;AACtB,iBAAO,WAAW,IAAI,CAAC;AAAA,QAC3B;AACA,eAAO,WAAW,EAAE,KAAK,IAAI;AAAA,MACjC;AAAA,IACJ;AAAA,EACJ;AAEA,WAAS,aAAa,IAASA,QAAoB;AAC/C,QAAIA,QAAO;AACP,iBAAW,QAAQA,QAAO;AACtB,YAAI,KAAK,KAAK,MAAM,IAAI;AACpB,iBAAO;AAAA,QACX;AACA,cAAM,YAAY,aAAa,IAAI,KAAK,WAAW,CAAC;AACpD,YAAI,WAAW;AACX,iBAAO;AAAA,QACX;AAAA,MACJ;AAAA,IACJ;AACA,WAAO;AAAA,EACX;AAGA,QAAM,eAAe,CAAC,OAAY,aAAa,IAAI,KAAK;AAExD,SAAO;AACX;","names":["nodes"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\n\nexport function arrayToTree(\n nodes: any[],\n option: {\n idKey: string\n parentKey: string\n childrenKey: string\n sortKey?: string\n },\n): TreeNodes {\n const {idKey, parentKey, childrenKey, sortKey} = option\n const roots: any = []\n\n if (sortKey) {\n nodes.sort((a: any, b: any) => a[sortKey] - b[sortKey])\n }\n\n\n for (const node of nodes) {\n\n let parentId = node[parentKey];\n\n if (parentId && typeof parentId === 'object') {\n parentId = parentId[idKey]\n }\n\n if (parentId === null || parentId === undefined) {\n roots.push(node)\n } else {\n const parent = nodes.find((item) => item[idKey] === parentId)\n if (parent) {\n if (!parent[childrenKey]) {\n parent[childrenKey] = []\n }\n parent[childrenKey].push(node)\n }else{\n roots.push(node)\n }\n }\n }\n\n function findNodeById(id: any, nodes?: any[]): any {\n if (nodes) {\n for (const node of nodes) {\n if (node[idKey] === id) {\n return node\n }\n const foundNode = findNodeById(id, node[childrenKey])\n if (foundNode) {\n return foundNode\n }\n }\n }\n return null\n }\n\n // Add the findNodeById method to the result\n roots.findNodeById = (id: any) => findNodeById(id, roots)\n\n return roots\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,SAAS,YACZ,OACA,QAMS;AACT,QAAM,EAAC,OAAO,WAAW,aAAa,QAAO,IAAI;AACjD,QAAM,QAAa,CAAC;AAEpB,MAAI,SAAS;AACT,UAAM,KAAK,CAAC,GAAQ,MAAW,EAAE,OAAO,IAAI,EAAE,OAAO,CAAC;AAAA,EAC1D;AAGA,aAAW,QAAQ,OAAO;AAEtB,QAAI,WAAW,KAAK,SAAS;AAE7B,QAAI,YAAY,OAAO,aAAa,UAAU;AAC1C,iBAAW,SAAS,KAAK;AAAA,IAC7B;AAEA,QAAI,aAAa,QAAQ,aAAa,QAAW;AAC7C,YAAM,KAAK,IAAI;AAAA,IACnB,OAAO;AACH,YAAM,SAAS,MAAM,KAAK,CAAC,SAAS,KAAK,KAAK,MAAM,QAAQ;AAC5D,UAAI,QAAQ;AACR,YAAI,CAAC,OAAO,WAAW,GAAG;AACtB,iBAAO,WAAW,IAAI,CAAC;AAAA,QAC3B;AACA,eAAO,WAAW,EAAE,KAAK,IAAI;AAAA,MACjC,OAAK;AACD,cAAM,KAAK,IAAI;AAAA,MACnB;AAAA,IACJ;AAAA,EACJ;AAEA,WAAS,aAAa,IAASA,QAAoB;AAC/C,QAAIA,QAAO;AACP,iBAAW,QAAQA,QAAO;AACtB,YAAI,KAAK,KAAK,MAAM,IAAI;AACpB,iBAAO;AAAA,QACX;AACA,cAAM,YAAY,aAAa,IAAI,KAAK,WAAW,CAAC;AACpD,YAAI,WAAW;AACX,iBAAO;AAAA,QACX;AAAA,MACJ;AAAA,IACJ;AACA,WAAO;AAAA,EACX;AAGA,QAAM,eAAe,CAAC,OAAY,aAAa,IAAI,KAAK;AAExD,SAAO;AACX;","names":["nodes"]}
package/dist/index.mjs CHANGED
@@ -19,6 +19,8 @@ function arrayToTree(nodes, option) {
19
19
  parent[childrenKey] = [];
20
20
  }
21
21
  parent[childrenKey].push(node);
22
+ } else {
23
+ roots.push(node);
22
24
  }
23
25
  }
24
26
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\n\nexport function arrayToTree(\n nodes: any[],\n option: {\n idKey: string\n parentKey: string\n childrenKey: string\n sortKey?: string\n },\n): TreeNodes {\n const {idKey, parentKey, childrenKey, sortKey} = option\n const roots: any = []\n\n if (sortKey) {\n nodes.sort((a: any, b: any) => a[sortKey] - b[sortKey])\n }\n\n\n for (const node of nodes) {\n\n let parentId = node[parentKey];\n\n if (parentId && typeof parentId === 'object') {\n parentId = parentId[idKey]\n }\n\n if (parentId === null || parentId === undefined) {\n roots.push(node)\n } else {\n const parent = nodes.find((item) => item[idKey] === parentId)\n if (parent) {\n if (!parent[childrenKey]) {\n parent[childrenKey] = []\n }\n parent[childrenKey].push(node)\n }\n }\n }\n\n function findNodeById(id: any, nodes?: any[]): any {\n if (nodes) {\n for (const node of nodes) {\n if (node[idKey] === id) {\n return node\n }\n const foundNode = findNodeById(id, node[childrenKey])\n if (foundNode) {\n return foundNode\n }\n }\n }\n return null\n }\n\n // Add the findNodeById method to the result\n roots.findNodeById = (id: any) => findNodeById(id, roots)\n\n return roots\n}\n\n"],"mappings":";AAEO,SAAS,YACZ,OACA,QAMS;AACT,QAAM,EAAC,OAAO,WAAW,aAAa,QAAO,IAAI;AACjD,QAAM,QAAa,CAAC;AAEpB,MAAI,SAAS;AACT,UAAM,KAAK,CAAC,GAAQ,MAAW,EAAE,OAAO,IAAI,EAAE,OAAO,CAAC;AAAA,EAC1D;AAGA,aAAW,QAAQ,OAAO;AAEtB,QAAI,WAAW,KAAK,SAAS;AAE7B,QAAI,YAAY,OAAO,aAAa,UAAU;AAC1C,iBAAW,SAAS,KAAK;AAAA,IAC7B;AAEA,QAAI,aAAa,QAAQ,aAAa,QAAW;AAC7C,YAAM,KAAK,IAAI;AAAA,IACnB,OAAO;AACH,YAAM,SAAS,MAAM,KAAK,CAAC,SAAS,KAAK,KAAK,MAAM,QAAQ;AAC5D,UAAI,QAAQ;AACR,YAAI,CAAC,OAAO,WAAW,GAAG;AACtB,iBAAO,WAAW,IAAI,CAAC;AAAA,QAC3B;AACA,eAAO,WAAW,EAAE,KAAK,IAAI;AAAA,MACjC;AAAA,IACJ;AAAA,EACJ;AAEA,WAAS,aAAa,IAASA,QAAoB;AAC/C,QAAIA,QAAO;AACP,iBAAW,QAAQA,QAAO;AACtB,YAAI,KAAK,KAAK,MAAM,IAAI;AACpB,iBAAO;AAAA,QACX;AACA,cAAM,YAAY,aAAa,IAAI,KAAK,WAAW,CAAC;AACpD,YAAI,WAAW;AACX,iBAAO;AAAA,QACX;AAAA,MACJ;AAAA,IACJ;AACA,WAAO;AAAA,EACX;AAGA,QAAM,eAAe,CAAC,OAAY,aAAa,IAAI,KAAK;AAExD,SAAO;AACX;","names":["nodes"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\n\nexport function arrayToTree(\n nodes: any[],\n option: {\n idKey: string\n parentKey: string\n childrenKey: string\n sortKey?: string\n },\n): TreeNodes {\n const {idKey, parentKey, childrenKey, sortKey} = option\n const roots: any = []\n\n if (sortKey) {\n nodes.sort((a: any, b: any) => a[sortKey] - b[sortKey])\n }\n\n\n for (const node of nodes) {\n\n let parentId = node[parentKey];\n\n if (parentId && typeof parentId === 'object') {\n parentId = parentId[idKey]\n }\n\n if (parentId === null || parentId === undefined) {\n roots.push(node)\n } else {\n const parent = nodes.find((item) => item[idKey] === parentId)\n if (parent) {\n if (!parent[childrenKey]) {\n parent[childrenKey] = []\n }\n parent[childrenKey].push(node)\n }else{\n roots.push(node)\n }\n }\n }\n\n function findNodeById(id: any, nodes?: any[]): any {\n if (nodes) {\n for (const node of nodes) {\n if (node[idKey] === id) {\n return node\n }\n const foundNode = findNodeById(id, node[childrenKey])\n if (foundNode) {\n return foundNode\n }\n }\n }\n return null\n }\n\n // Add the findNodeById method to the result\n roots.findNodeById = (id: any) => findNodeById(id, roots)\n\n return roots\n}\n\n"],"mappings":";AAEO,SAAS,YACZ,OACA,QAMS;AACT,QAAM,EAAC,OAAO,WAAW,aAAa,QAAO,IAAI;AACjD,QAAM,QAAa,CAAC;AAEpB,MAAI,SAAS;AACT,UAAM,KAAK,CAAC,GAAQ,MAAW,EAAE,OAAO,IAAI,EAAE,OAAO,CAAC;AAAA,EAC1D;AAGA,aAAW,QAAQ,OAAO;AAEtB,QAAI,WAAW,KAAK,SAAS;AAE7B,QAAI,YAAY,OAAO,aAAa,UAAU;AAC1C,iBAAW,SAAS,KAAK;AAAA,IAC7B;AAEA,QAAI,aAAa,QAAQ,aAAa,QAAW;AAC7C,YAAM,KAAK,IAAI;AAAA,IACnB,OAAO;AACH,YAAM,SAAS,MAAM,KAAK,CAAC,SAAS,KAAK,KAAK,MAAM,QAAQ;AAC5D,UAAI,QAAQ;AACR,YAAI,CAAC,OAAO,WAAW,GAAG;AACtB,iBAAO,WAAW,IAAI,CAAC;AAAA,QAC3B;AACA,eAAO,WAAW,EAAE,KAAK,IAAI;AAAA,MACjC,OAAK;AACD,cAAM,KAAK,IAAI;AAAA,MACnB;AAAA,IACJ;AAAA,EACJ;AAEA,WAAS,aAAa,IAASA,QAAoB;AAC/C,QAAIA,QAAO;AACP,iBAAW,QAAQA,QAAO;AACtB,YAAI,KAAK,KAAK,MAAM,IAAI;AACpB,iBAAO;AAAA,QACX;AACA,cAAM,YAAY,aAAa,IAAI,KAAK,WAAW,CAAC;AACpD,YAAI,WAAW;AACX,iBAAO;AAAA,QACX;AAAA,MACJ;AAAA,IACJ;AACA,WAAO;AAAA,EACX;AAGA,QAAM,eAAe,CAAC,OAAY,aAAa,IAAI,KAAK;AAExD,SAAO;AACX;","names":["nodes"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osmos/arraytotree",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "transform flat array to tree structure",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",