@osmos/arraytotree 1.0.3 → 1.0.5

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.d.mts CHANGED
@@ -1,11 +1,11 @@
1
- type result = any[] & {
2
- findNodeById?: (id: any, nodes: any[]) => any;
1
+ type TreeNodes = any[] & {
2
+ findNodeById: (id: any) => any;
3
3
  };
4
- declare function arrayToTree(array: any[], option: {
4
+ declare function arrayToTree(nodes: any[], option: {
5
5
  idKey: string;
6
6
  parentKey: string;
7
7
  childrenKey: string;
8
8
  sortKey?: string;
9
- }): result;
9
+ }): TreeNodes;
10
10
 
11
- export { arrayToTree };
11
+ export { type TreeNodes, arrayToTree };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- type result = any[] & {
2
- findNodeById?: (id: any, nodes: any[]) => any;
1
+ type TreeNodes = any[] & {
2
+ findNodeById: (id: any) => any;
3
3
  };
4
- declare function arrayToTree(array: any[], option: {
4
+ declare function arrayToTree(nodes: any[], option: {
5
5
  idKey: string;
6
6
  parentKey: string;
7
7
  childrenKey: string;
8
8
  sortKey?: string;
9
- }): result;
9
+ }): TreeNodes;
10
10
 
11
- export { arrayToTree };
11
+ export { type TreeNodes, arrayToTree };
package/dist/index.js CHANGED
@@ -23,44 +23,39 @@ __export(src_exports, {
23
23
  arrayToTree: () => arrayToTree
24
24
  });
25
25
  module.exports = __toCommonJS(src_exports);
26
- function arrayToTree(array, option) {
27
- const idMap = {};
26
+ function arrayToTree(nodes, option) {
27
+ const { idKey, parentKey, childrenKey, sortKey } = option;
28
28
  const roots = [];
29
- for (const obj of array) {
30
- const id = obj[option.idKey];
31
- let parentId = obj;
32
- for (const key of option.parentKey.split(".")) {
33
- parentId = parentId[key];
34
- }
29
+ if (sortKey) {
30
+ nodes.sort((a, b) => a[sortKey] - b[sortKey]);
31
+ }
32
+ for (const node of nodes) {
33
+ let parentId = node[parentKey];
35
34
  if (parentId && typeof parentId === "object") {
36
- parentId = parentId[option.idKey];
35
+ parentId = parentId[idKey];
37
36
  }
38
- obj[option.childrenKey] = [];
39
- idMap[id] = obj;
40
37
  if (!parentId) {
41
- roots.push(obj);
38
+ roots.push(node);
42
39
  } else {
43
- const parent = idMap[parentId];
40
+ const parent = nodes.find((item) => item[idKey] === parentId);
44
41
  if (parent) {
45
- parent[option.childrenKey].push(obj);
46
- } else {
47
- roots.push(obj);
42
+ if (parent[childrenKey] === void 0) {
43
+ parent[childrenKey] = [];
44
+ }
45
+ parent[childrenKey].push(node);
48
46
  }
49
47
  }
50
48
  }
51
- if (option.sortKey) {
52
- for (const obj of array) {
53
- obj[option.childrenKey].sort((a, b) => a[option.sortKey] - b[option.sortKey]);
54
- }
55
- }
56
- function findNodeById(id, nodes) {
57
- for (const node of nodes) {
58
- if (node[option.idKey] === id) {
59
- return node;
60
- }
61
- const foundNode = findNodeById(id, node[option.childrenKey]);
62
- if (foundNode) {
63
- return foundNode;
49
+ function findNodeById(id, nodes2) {
50
+ if (nodes2) {
51
+ for (const node of nodes2) {
52
+ if (node[idKey] === id) {
53
+ return node;
54
+ }
55
+ const foundNode = findNodeById(id, node[childrenKey]);
56
+ if (foundNode) {
57
+ return foundNode;
58
+ }
64
59
  }
65
60
  }
66
61
  return null;
@@ -68,7 +63,6 @@ function arrayToTree(array, option) {
68
63
  roots.findNodeById = (id) => findNodeById(id, roots);
69
64
  return roots;
70
65
  }
71
- console.log(11);
72
66
  // Annotate the CommonJS export names for ESM import in node:
73
67
  0 && (module.exports = {
74
68
  arrayToTree
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["type result = any[] & { findNodeById?: (id: any, nodes: any[]) => any }\n\nexport function arrayToTree(\n array: any[],\n option: {\n idKey: string\n parentKey: string\n childrenKey: string\n sortKey?: string\n },\n): result {\n const idMap: { [key: string]: any } = {}\n const roots: result = []\n\n for (const obj of array) {\n const id = obj[option.idKey]\n\n let parentId = obj\n for (const key of option.parentKey.split('.')) {\n parentId = parentId[key]\n }\n\n if (parentId && typeof parentId === 'object') {\n parentId = parentId[option.idKey]\n }\n\n obj[option.childrenKey] = []\n\n idMap[id] = obj\n\n if (!parentId) {\n roots.push(obj)\n } else {\n const parent = idMap[parentId]\n if (parent) {\n parent[option.childrenKey].push(obj)\n } else {\n roots.push(obj)\n }\n }\n }\n\n // Sort children based on the sortKey\n if (option.sortKey) {\n for (const obj of array) {\n obj[option.childrenKey].sort((a: any, b: any) => a[option.sortKey!] - b[option.sortKey!])\n }\n }\n\n // Method to find a node in the tree with the idKey\n function findNodeById(id: any, nodes: any[]): any {\n for (const node of nodes) {\n if (node[option.idKey] === id) {\n return node\n }\n const foundNode = findNodeById(id, node[option.childrenKey])\n if (foundNode) {\n return foundNode\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\nconsole.log(11)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,SAAS,YACZ,OACA,QAMM;AACN,QAAM,QAAgC,CAAC;AACvC,QAAM,QAAgB,CAAC;AAEvB,aAAW,OAAO,OAAO;AACrB,UAAM,KAAK,IAAI,OAAO,KAAK;AAE3B,QAAI,WAAW;AACf,eAAW,OAAO,OAAO,UAAU,MAAM,GAAG,GAAG;AAC3C,iBAAW,SAAS,GAAG;AAAA,IAC3B;AAEA,QAAI,YAAY,OAAO,aAAa,UAAU;AAC1C,iBAAW,SAAS,OAAO,KAAK;AAAA,IACpC;AAEA,QAAI,OAAO,WAAW,IAAI,CAAC;AAE3B,UAAM,EAAE,IAAI;AAEZ,QAAI,CAAC,UAAU;AACX,YAAM,KAAK,GAAG;AAAA,IAClB,OAAO;AACH,YAAM,SAAS,MAAM,QAAQ;AAC7B,UAAI,QAAQ;AACR,eAAO,OAAO,WAAW,EAAE,KAAK,GAAG;AAAA,MACvC,OAAO;AACH,cAAM,KAAK,GAAG;AAAA,MAClB;AAAA,IACJ;AAAA,EACJ;AAGA,MAAI,OAAO,SAAS;AAChB,eAAW,OAAO,OAAO;AACrB,UAAI,OAAO,WAAW,EAAE,KAAK,CAAC,GAAQ,MAAW,EAAE,OAAO,OAAQ,IAAI,EAAE,OAAO,OAAQ,CAAC;AAAA,IAC5F;AAAA,EACJ;AAGA,WAAS,aAAa,IAAS,OAAmB;AAC9C,eAAW,QAAQ,OAAO;AACtB,UAAI,KAAK,OAAO,KAAK,MAAM,IAAI;AAC3B,eAAO;AAAA,MACX;AACA,YAAM,YAAY,aAAa,IAAI,KAAK,OAAO,WAAW,CAAC;AAC3D,UAAI,WAAW;AACX,eAAO;AAAA,MACX;AAAA,IACJ;AACA,WAAO;AAAA,EACX;AAGA,QAAM,eAAe,CAAC,OAAY,aAAa,IAAI,KAAK;AAExD,SAAO;AACX;AAGA,QAAQ,IAAI,EAAE;","names":[]}
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) {\n roots.push(node)\n } else {\n const parent = nodes.find((item) => item[idKey] === parentId)\n if (parent) {\n if (parent[childrenKey] === undefined) {\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,CAAC,UAAU;AACX,YAAM,KAAK,IAAI;AAAA,IACnB,OAAO;AACH,YAAM,SAAS,MAAM,KAAK,CAAC,SAAS,KAAK,KAAK,MAAM,QAAQ;AAC5D,UAAI,QAAQ;AACR,YAAI,OAAO,WAAW,MAAM,QAAW;AACnC,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"]}
package/dist/index.mjs CHANGED
@@ -1,42 +1,37 @@
1
1
  // src/index.ts
2
- function arrayToTree(array, option) {
3
- const idMap = {};
2
+ function arrayToTree(nodes, option) {
3
+ const { idKey, parentKey, childrenKey, sortKey } = option;
4
4
  const roots = [];
5
- for (const obj of array) {
6
- const id = obj[option.idKey];
7
- let parentId = obj;
8
- for (const key of option.parentKey.split(".")) {
9
- parentId = parentId[key];
10
- }
5
+ if (sortKey) {
6
+ nodes.sort((a, b) => a[sortKey] - b[sortKey]);
7
+ }
8
+ for (const node of nodes) {
9
+ let parentId = node[parentKey];
11
10
  if (parentId && typeof parentId === "object") {
12
- parentId = parentId[option.idKey];
11
+ parentId = parentId[idKey];
13
12
  }
14
- obj[option.childrenKey] = [];
15
- idMap[id] = obj;
16
13
  if (!parentId) {
17
- roots.push(obj);
14
+ roots.push(node);
18
15
  } else {
19
- const parent = idMap[parentId];
16
+ const parent = nodes.find((item) => item[idKey] === parentId);
20
17
  if (parent) {
21
- parent[option.childrenKey].push(obj);
22
- } else {
23
- roots.push(obj);
18
+ if (parent[childrenKey] === void 0) {
19
+ parent[childrenKey] = [];
20
+ }
21
+ parent[childrenKey].push(node);
24
22
  }
25
23
  }
26
24
  }
27
- if (option.sortKey) {
28
- for (const obj of array) {
29
- obj[option.childrenKey].sort((a, b) => a[option.sortKey] - b[option.sortKey]);
30
- }
31
- }
32
- function findNodeById(id, nodes) {
33
- for (const node of nodes) {
34
- if (node[option.idKey] === id) {
35
- return node;
36
- }
37
- const foundNode = findNodeById(id, node[option.childrenKey]);
38
- if (foundNode) {
39
- return foundNode;
25
+ function findNodeById(id, nodes2) {
26
+ if (nodes2) {
27
+ for (const node of nodes2) {
28
+ if (node[idKey] === id) {
29
+ return node;
30
+ }
31
+ const foundNode = findNodeById(id, node[childrenKey]);
32
+ if (foundNode) {
33
+ return foundNode;
34
+ }
40
35
  }
41
36
  }
42
37
  return null;
@@ -44,7 +39,6 @@ function arrayToTree(array, option) {
44
39
  roots.findNodeById = (id) => findNodeById(id, roots);
45
40
  return roots;
46
41
  }
47
- console.log(11);
48
42
  export {
49
43
  arrayToTree
50
44
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["type result = any[] & { findNodeById?: (id: any, nodes: any[]) => any }\n\nexport function arrayToTree(\n array: any[],\n option: {\n idKey: string\n parentKey: string\n childrenKey: string\n sortKey?: string\n },\n): result {\n const idMap: { [key: string]: any } = {}\n const roots: result = []\n\n for (const obj of array) {\n const id = obj[option.idKey]\n\n let parentId = obj\n for (const key of option.parentKey.split('.')) {\n parentId = parentId[key]\n }\n\n if (parentId && typeof parentId === 'object') {\n parentId = parentId[option.idKey]\n }\n\n obj[option.childrenKey] = []\n\n idMap[id] = obj\n\n if (!parentId) {\n roots.push(obj)\n } else {\n const parent = idMap[parentId]\n if (parent) {\n parent[option.childrenKey].push(obj)\n } else {\n roots.push(obj)\n }\n }\n }\n\n // Sort children based on the sortKey\n if (option.sortKey) {\n for (const obj of array) {\n obj[option.childrenKey].sort((a: any, b: any) => a[option.sortKey!] - b[option.sortKey!])\n }\n }\n\n // Method to find a node in the tree with the idKey\n function findNodeById(id: any, nodes: any[]): any {\n for (const node of nodes) {\n if (node[option.idKey] === id) {\n return node\n }\n const foundNode = findNodeById(id, node[option.childrenKey])\n if (foundNode) {\n return foundNode\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\nconsole.log(11)\n"],"mappings":";AAEO,SAAS,YACZ,OACA,QAMM;AACN,QAAM,QAAgC,CAAC;AACvC,QAAM,QAAgB,CAAC;AAEvB,aAAW,OAAO,OAAO;AACrB,UAAM,KAAK,IAAI,OAAO,KAAK;AAE3B,QAAI,WAAW;AACf,eAAW,OAAO,OAAO,UAAU,MAAM,GAAG,GAAG;AAC3C,iBAAW,SAAS,GAAG;AAAA,IAC3B;AAEA,QAAI,YAAY,OAAO,aAAa,UAAU;AAC1C,iBAAW,SAAS,OAAO,KAAK;AAAA,IACpC;AAEA,QAAI,OAAO,WAAW,IAAI,CAAC;AAE3B,UAAM,EAAE,IAAI;AAEZ,QAAI,CAAC,UAAU;AACX,YAAM,KAAK,GAAG;AAAA,IAClB,OAAO;AACH,YAAM,SAAS,MAAM,QAAQ;AAC7B,UAAI,QAAQ;AACR,eAAO,OAAO,WAAW,EAAE,KAAK,GAAG;AAAA,MACvC,OAAO;AACH,cAAM,KAAK,GAAG;AAAA,MAClB;AAAA,IACJ;AAAA,EACJ;AAGA,MAAI,OAAO,SAAS;AAChB,eAAW,OAAO,OAAO;AACrB,UAAI,OAAO,WAAW,EAAE,KAAK,CAAC,GAAQ,MAAW,EAAE,OAAO,OAAQ,IAAI,EAAE,OAAO,OAAQ,CAAC;AAAA,IAC5F;AAAA,EACJ;AAGA,WAAS,aAAa,IAAS,OAAmB;AAC9C,eAAW,QAAQ,OAAO;AACtB,UAAI,KAAK,OAAO,KAAK,MAAM,IAAI;AAC3B,eAAO;AAAA,MACX;AACA,YAAM,YAAY,aAAa,IAAI,KAAK,OAAO,WAAW,CAAC;AAC3D,UAAI,WAAW;AACX,eAAO;AAAA,MACX;AAAA,IACJ;AACA,WAAO;AAAA,EACX;AAGA,QAAM,eAAe,CAAC,OAAY,aAAa,IAAI,KAAK;AAExD,SAAO;AACX;AAGA,QAAQ,IAAI,EAAE;","names":[]}
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) {\n roots.push(node)\n } else {\n const parent = nodes.find((item) => item[idKey] === parentId)\n if (parent) {\n if (parent[childrenKey] === undefined) {\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,CAAC,UAAU;AACX,YAAM,KAAK,IAAI;AAAA,IACnB,OAAO;AACH,YAAM,SAAS,MAAM,KAAK,CAAC,SAAS,KAAK,KAAK,MAAM,QAAQ;AAC5D,UAAI,QAAQ;AACR,YAAI,OAAO,WAAW,MAAM,QAAW;AACnC,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osmos/arraytotree",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "transform flat array to tree structure",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",