@osmos/arraytotree 1.0.2 → 1.0.4
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 +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +24 -30
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +24 -30
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
type
|
2
|
-
findNodeById
|
1
|
+
type TreeNodes = any[] & {
|
2
|
+
findNodeById: (id: any) => any;
|
3
3
|
};
|
4
|
-
declare function arrayToTree(
|
4
|
+
declare function arrayToTree(nodes: any[], option: {
|
5
5
|
idKey: string;
|
6
6
|
parentKey: string;
|
7
7
|
childrenKey: string;
|
8
8
|
sortKey?: string;
|
9
|
-
}):
|
9
|
+
}): TreeNodes;
|
10
10
|
|
11
11
|
export { arrayToTree };
|
package/dist/index.d.ts
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
type
|
2
|
-
findNodeById
|
1
|
+
type TreeNodes = any[] & {
|
2
|
+
findNodeById: (id: any) => any;
|
3
3
|
};
|
4
|
-
declare function arrayToTree(
|
4
|
+
declare function arrayToTree(nodes: any[], option: {
|
5
5
|
idKey: string;
|
6
6
|
parentKey: string;
|
7
7
|
childrenKey: string;
|
8
8
|
sortKey?: string;
|
9
|
-
}):
|
9
|
+
}): TreeNodes;
|
10
10
|
|
11
11
|
export { 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(
|
27
|
-
const
|
26
|
+
function arrayToTree(nodes, option) {
|
27
|
+
const { idKey, parentKey, childrenKey, sortKey } = option;
|
28
28
|
const roots = [];
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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[
|
35
|
+
parentId = parentId[idKey];
|
37
36
|
}
|
38
|
-
obj[option.childrenKey] = [];
|
39
|
-
idMap[id] = obj;
|
40
37
|
if (!parentId) {
|
41
|
-
roots.push(
|
38
|
+
roots.push(node);
|
42
39
|
} else {
|
43
|
-
const parent =
|
40
|
+
const parent = nodes.find((item) => item[idKey] === parentId);
|
44
41
|
if (parent) {
|
45
|
-
parent[
|
46
|
-
|
47
|
-
|
42
|
+
if (parent[childrenKey] === void 0) {
|
43
|
+
parent[childrenKey] = [];
|
44
|
+
}
|
45
|
+
parent[childrenKey].push(node);
|
48
46
|
}
|
49
47
|
}
|
50
48
|
}
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["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(
|
3
|
-
const
|
2
|
+
function arrayToTree(nodes, option) {
|
3
|
+
const { idKey, parentKey, childrenKey, sortKey } = option;
|
4
4
|
const roots = [];
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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[
|
11
|
+
parentId = parentId[idKey];
|
13
12
|
}
|
14
|
-
obj[option.childrenKey] = [];
|
15
|
-
idMap[id] = obj;
|
16
13
|
if (!parentId) {
|
17
|
-
roots.push(
|
14
|
+
roots.push(node);
|
18
15
|
} else {
|
19
|
-
const parent =
|
16
|
+
const parent = nodes.find((item) => item[idKey] === parentId);
|
20
17
|
if (parent) {
|
21
|
-
parent[
|
22
|
-
|
23
|
-
|
18
|
+
if (parent[childrenKey] === void 0) {
|
19
|
+
parent[childrenKey] = [];
|
20
|
+
}
|
21
|
+
parent[childrenKey].push(node);
|
24
22
|
}
|
25
23
|
}
|
26
24
|
}
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
};
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["type
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["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"]}
|