@osmos/arraytotree 1.0.9 → 1.0.11
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 +8 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -26,9 +26,16 @@ 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 = {};
|
29
30
|
if (sortKey) {
|
30
31
|
nodes.sort((a, b) => a[sortKey] - b[sortKey]);
|
31
32
|
}
|
33
|
+
for (const node of nodes) {
|
34
|
+
if (!node[childrenKey]) {
|
35
|
+
node[childrenKey] = [];
|
36
|
+
}
|
37
|
+
nodeMap[node[idKey]] = node;
|
38
|
+
}
|
32
39
|
for (const node of nodes) {
|
33
40
|
let parentId = node[parentKey];
|
34
41
|
if (parentId && typeof parentId === "object") {
|
@@ -37,11 +44,8 @@ function arrayToTree(nodes, option) {
|
|
37
44
|
if (parentId === null || parentId === void 0) {
|
38
45
|
roots.push(node);
|
39
46
|
} else {
|
40
|
-
const parent =
|
47
|
+
const parent = nodeMap[parentId];
|
41
48
|
if (parent) {
|
42
|
-
if (!parent[childrenKey]) {
|
43
|
-
parent[childrenKey] = [];
|
44
|
-
}
|
45
49
|
parent[childrenKey].push(node);
|
46
50
|
} else {
|
47
51
|
roots.push(node);
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\
|
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"]}
|
package/dist/index.mjs
CHANGED
@@ -2,9 +2,16 @@
|
|
2
2
|
function arrayToTree(nodes, option) {
|
3
3
|
const { idKey, parentKey, childrenKey, sortKey } = option;
|
4
4
|
const roots = [];
|
5
|
+
const nodeMap = {};
|
5
6
|
if (sortKey) {
|
6
7
|
nodes.sort((a, b) => a[sortKey] - b[sortKey]);
|
7
8
|
}
|
9
|
+
for (const node of nodes) {
|
10
|
+
if (!node[childrenKey]) {
|
11
|
+
node[childrenKey] = [];
|
12
|
+
}
|
13
|
+
nodeMap[node[idKey]] = node;
|
14
|
+
}
|
8
15
|
for (const node of nodes) {
|
9
16
|
let parentId = node[parentKey];
|
10
17
|
if (parentId && typeof parentId === "object") {
|
@@ -13,11 +20,8 @@ function arrayToTree(nodes, option) {
|
|
13
20
|
if (parentId === null || parentId === void 0) {
|
14
21
|
roots.push(node);
|
15
22
|
} else {
|
16
|
-
const parent =
|
23
|
+
const parent = nodeMap[parentId];
|
17
24
|
if (parent) {
|
18
|
-
if (!parent[childrenKey]) {
|
19
|
-
parent[childrenKey] = [];
|
20
|
-
}
|
21
25
|
parent[childrenKey].push(node);
|
22
26
|
} else {
|
23
27
|
roots.push(node);
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type TreeNodes = any[] & { findNodeById: (id: any) => any }\
|
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"]}
|