@osmos/arraytotree 1.0.10 → 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 +7 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -1,25 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
var __defProp = Object.defineProperty;
|
3
|
-
var __defProps = Object.defineProperties;
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
6
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
8
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
9
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
10
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
11
|
-
var __spreadValues = (a, b) => {
|
12
|
-
for (var prop in b || (b = {}))
|
13
|
-
if (__hasOwnProp.call(b, prop))
|
14
|
-
__defNormalProp(a, prop, b[prop]);
|
15
|
-
if (__getOwnPropSymbols)
|
16
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
17
|
-
if (__propIsEnum.call(b, prop))
|
18
|
-
__defNormalProp(a, prop, b[prop]);
|
19
|
-
}
|
20
|
-
return a;
|
21
|
-
};
|
22
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
23
6
|
var __export = (target, all) => {
|
24
7
|
for (var name in all)
|
25
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
@@ -47,7 +30,12 @@ function arrayToTree(nodes, option) {
|
|
47
30
|
nodes.sort((a, b) => a[sortKey] - b[sortKey]);
|
48
31
|
}
|
49
32
|
for (const node of nodes) {
|
50
|
-
|
33
|
+
if (!node[childrenKey]) {
|
34
|
+
node[childrenKey] = [];
|
35
|
+
}
|
36
|
+
}
|
37
|
+
for (const node of nodes) {
|
38
|
+
let parentId = parentKey.includes(".") ? parentKey.split(".").reduce((obj, key) => obj && obj[key], node) : node[parentKey];
|
51
39
|
if (parentId && typeof parentId === "object") {
|
52
40
|
parentId = parentId[idKey];
|
53
41
|
}
|
@@ -56,12 +44,9 @@ function arrayToTree(nodes, option) {
|
|
56
44
|
} else {
|
57
45
|
const parent = nodes.find((item) => item[idKey] === parentId);
|
58
46
|
if (parent) {
|
59
|
-
if (!parent[childrenKey]) {
|
60
|
-
parent[childrenKey] = [];
|
61
|
-
}
|
62
47
|
parent[childrenKey].push(node);
|
63
48
|
} else {
|
64
|
-
roots.push(
|
49
|
+
roots.push(node);
|
65
50
|
}
|
66
51
|
}
|
67
52
|
}
|
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 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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;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/dist/index.mjs
CHANGED
@@ -1,23 +1,3 @@
|
|
1
|
-
var __defProp = Object.defineProperty;
|
2
|
-
var __defProps = Object.defineProperties;
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
8
|
-
var __spreadValues = (a, b) => {
|
9
|
-
for (var prop in b || (b = {}))
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
12
|
-
if (__getOwnPropSymbols)
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
14
|
-
if (__propIsEnum.call(b, prop))
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
16
|
-
}
|
17
|
-
return a;
|
18
|
-
};
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
20
|
-
|
21
1
|
// src/index.ts
|
22
2
|
function arrayToTree(nodes, option) {
|
23
3
|
const { idKey, parentKey, childrenKey, sortKey } = option;
|
@@ -26,7 +6,12 @@ function arrayToTree(nodes, option) {
|
|
26
6
|
nodes.sort((a, b) => a[sortKey] - b[sortKey]);
|
27
7
|
}
|
28
8
|
for (const node of nodes) {
|
29
|
-
|
9
|
+
if (!node[childrenKey]) {
|
10
|
+
node[childrenKey] = [];
|
11
|
+
}
|
12
|
+
}
|
13
|
+
for (const node of nodes) {
|
14
|
+
let parentId = parentKey.includes(".") ? parentKey.split(".").reduce((obj, key) => obj && obj[key], node) : node[parentKey];
|
30
15
|
if (parentId && typeof parentId === "object") {
|
31
16
|
parentId = parentId[idKey];
|
32
17
|
}
|
@@ -35,12 +20,9 @@ function arrayToTree(nodes, option) {
|
|
35
20
|
} else {
|
36
21
|
const parent = nodes.find((item) => item[idKey] === parentId);
|
37
22
|
if (parent) {
|
38
|
-
if (!parent[childrenKey]) {
|
39
|
-
parent[childrenKey] = [];
|
40
|
-
}
|
41
23
|
parent[childrenKey].push(node);
|
42
24
|
} else {
|
43
|
-
roots.push(
|
25
|
+
roots.push(node);
|
44
26
|
}
|
45
27
|
}
|
46
28
|
}
|
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 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"]}
|