@osmos/arraytotree 1.0.11 → 1.0.12

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
@@ -26,7 +26,6 @@ module.exports = __toCommonJS(src_exports);
26
26
  function arrayToTree(nodes, option) {
27
27
  const { idKey, parentKey, childrenKey, sortKey } = option;
28
28
  const roots = [];
29
- const nodeMap = {};
30
29
  if (sortKey) {
31
30
  nodes.sort((a, b) => a[sortKey] - b[sortKey]);
32
31
  }
@@ -34,17 +33,16 @@ function arrayToTree(nodes, option) {
34
33
  if (!node[childrenKey]) {
35
34
  node[childrenKey] = [];
36
35
  }
37
- nodeMap[node[idKey]] = node;
38
36
  }
39
37
  for (const node of nodes) {
40
- let parentId = node[parentKey];
38
+ let parentId = parentKey.includes(".") ? parentKey.split(".").reduce((obj, key) => obj && obj[key], node) : node[parentKey];
41
39
  if (parentId && typeof parentId === "object") {
42
40
  parentId = parentId[idKey];
43
41
  }
44
42
  if (parentId === null || parentId === void 0) {
45
43
  roots.push(node);
46
44
  } else {
47
- const parent = nodeMap[parentId];
45
+ const parent = nodes.find((item) => item[idKey] === parentId);
48
46
  if (parent) {
49
47
  parent[childrenKey].push(node);
50
48
  } else {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\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 // Create node map first\n const nodeMap: {[key: string]: any} = {}\n\n if (sortKey) {\n nodes.sort((a: any, b: any) => a[sortKey] - b[sortKey])\n }\n\n // Initialize all nodes with children arrays\n for (const node of nodes) {\n if (!node[childrenKey]) {\n node[childrenKey] = []\n }\n nodeMap[node[idKey]] = node\n }\n\n // Establish parent-child relationships\n for (const node of nodes) {\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 = nodeMap[parentId]\n if (parent) {\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}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACO,SAAS,YACZ,OACA,QAMS;AACT,QAAM,EAAC,OAAO,WAAW,aAAa,QAAO,IAAI;AACjD,QAAM,QAAa,CAAC;AAGpB,QAAM,UAAgC,CAAC;AAEvC,MAAI,SAAS;AACT,UAAM,KAAK,CAAC,GAAQ,MAAW,EAAE,OAAO,IAAI,EAAE,OAAO,CAAC;AAAA,EAC1D;AAGA,aAAW,QAAQ,OAAO;AACtB,QAAI,CAAC,KAAK,WAAW,GAAG;AACpB,WAAK,WAAW,IAAI,CAAC;AAAA,IACzB;AACA,YAAQ,KAAK,KAAK,CAAC,IAAI;AAAA,EAC3B;AAGA,aAAW,QAAQ,OAAO;AACtB,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,QAAQ,QAAQ;AAC/B,UAAI,QAAQ;AACR,eAAO,WAAW,EAAE,KAAK,IAAI;AAAA,MACjC,OAAO;AACH,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"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\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 // Initialize all nodes with children arrays\n for (const node of nodes) {\n if (!node[childrenKey]) {\n node[childrenKey] = []\n }\n }\n\n // Establish parent-child relationships\n for (const node of nodes) {\n // Handle nested path access for parentKey\n let parentId = parentKey.includes('.')\n ? parentKey.split('.').reduce((obj, key) => obj && obj[key], node)\n : 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 parent[childrenKey].push(node)\n } else {\n // Add to roots if parent doesn't exist\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}"],"mappings":";;;;;;;;;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"]}
package/dist/index.mjs CHANGED
@@ -2,7 +2,6 @@
2
2
  function arrayToTree(nodes, option) {
3
3
  const { idKey, parentKey, childrenKey, sortKey } = option;
4
4
  const roots = [];
5
- const nodeMap = {};
6
5
  if (sortKey) {
7
6
  nodes.sort((a, b) => a[sortKey] - b[sortKey]);
8
7
  }
@@ -10,17 +9,16 @@ function arrayToTree(nodes, option) {
10
9
  if (!node[childrenKey]) {
11
10
  node[childrenKey] = [];
12
11
  }
13
- nodeMap[node[idKey]] = node;
14
12
  }
15
13
  for (const node of nodes) {
16
- let parentId = node[parentKey];
14
+ let parentId = parentKey.includes(".") ? parentKey.split(".").reduce((obj, key) => obj && obj[key], node) : node[parentKey];
17
15
  if (parentId && typeof parentId === "object") {
18
16
  parentId = parentId[idKey];
19
17
  }
20
18
  if (parentId === null || parentId === void 0) {
21
19
  roots.push(node);
22
20
  } else {
23
- const parent = nodeMap[parentId];
21
+ const parent = nodes.find((item) => item[idKey] === parentId);
24
22
  if (parent) {
25
23
  parent[childrenKey].push(node);
26
24
  } else {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\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 // Create node map first\n const nodeMap: {[key: string]: any} = {}\n\n if (sortKey) {\n nodes.sort((a: any, b: any) => a[sortKey] - b[sortKey])\n }\n\n // Initialize all nodes with children arrays\n for (const node of nodes) {\n if (!node[childrenKey]) {\n node[childrenKey] = []\n }\n nodeMap[node[idKey]] = node\n }\n\n // Establish parent-child relationships\n for (const node of nodes) {\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 = nodeMap[parentId]\n if (parent) {\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}"],"mappings":";AACO,SAAS,YACZ,OACA,QAMS;AACT,QAAM,EAAC,OAAO,WAAW,aAAa,QAAO,IAAI;AACjD,QAAM,QAAa,CAAC;AAGpB,QAAM,UAAgC,CAAC;AAEvC,MAAI,SAAS;AACT,UAAM,KAAK,CAAC,GAAQ,MAAW,EAAE,OAAO,IAAI,EAAE,OAAO,CAAC;AAAA,EAC1D;AAGA,aAAW,QAAQ,OAAO;AACtB,QAAI,CAAC,KAAK,WAAW,GAAG;AACpB,WAAK,WAAW,IAAI,CAAC;AAAA,IACzB;AACA,YAAQ,KAAK,KAAK,CAAC,IAAI;AAAA,EAC3B;AAGA,aAAW,QAAQ,OAAO;AACtB,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,QAAQ,QAAQ;AAC/B,UAAI,QAAQ;AACR,eAAO,WAAW,EAAE,KAAK,IAAI;AAAA,MACjC,OAAO;AACH,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"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\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 // Initialize all nodes with children arrays\n for (const node of nodes) {\n if (!node[childrenKey]) {\n node[childrenKey] = []\n }\n }\n\n // Establish parent-child relationships\n for (const node of nodes) {\n // Handle nested path access for parentKey\n let parentId = parentKey.includes('.')\n ? parentKey.split('.').reduce((obj, key) => obj && obj[key], node)\n : 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 parent[childrenKey].push(node)\n } else {\n // Add to roots if parent doesn't exist\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}"],"mappings":";AACO,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;AACtB,QAAI,CAAC,KAAK,WAAW,GAAG;AACpB,WAAK,WAAW,IAAI,CAAC;AAAA,IACzB;AAAA,EACJ;AAGA,aAAW,QAAQ,OAAO;AAEtB,QAAI,WAAW,UAAU,SAAS,GAAG,IAC/B,UAAU,MAAM,GAAG,EAAE,OAAO,CAAC,KAAK,QAAQ,OAAO,IAAI,GAAG,GAAG,IAAI,IAC/D,KAAK,SAAS;AAEpB,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,eAAO,WAAW,EAAE,KAAK,IAAI;AAAA,MACjC,OAAO;AAEH,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.11",
3
+ "version": "1.0.12",
4
4
  "description": "transform flat array to tree structure",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",