@osmos/arraytotree 1.0.0

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/README.md ADDED
File without changes
@@ -0,0 +1,11 @@
1
+ type result = any[] & {
2
+ findNodeById?: (id: any, nodes: any[]) => any;
3
+ };
4
+ declare function arrayToTree(array: any[], option: {
5
+ idKey: string;
6
+ parentKey: string;
7
+ childrenKey: string;
8
+ sortKey?: string;
9
+ }): result;
10
+
11
+ export { arrayToTree };
@@ -0,0 +1,11 @@
1
+ type result = any[] & {
2
+ findNodeById?: (id: any, nodes: any[]) => any;
3
+ };
4
+ declare function arrayToTree(array: any[], option: {
5
+ idKey: string;
6
+ parentKey: string;
7
+ childrenKey: string;
8
+ sortKey?: string;
9
+ }): result;
10
+
11
+ export { arrayToTree };
package/dist/index.js ADDED
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ arrayToTree: () => arrayToTree
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+ function arrayToTree(array, option) {
27
+ const idMap = {};
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
+ }
35
+ if (parentId && typeof parentId === "object") {
36
+ parentId = parentId[option.idKey];
37
+ }
38
+ obj[option.childrenKey] = [];
39
+ idMap[id] = obj;
40
+ if (!parentId) {
41
+ roots.push(obj);
42
+ } else {
43
+ const parent = idMap[parentId];
44
+ if (parent) {
45
+ parent[option.childrenKey].push(obj);
46
+ } else {
47
+ roots.push(obj);
48
+ }
49
+ }
50
+ }
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;
64
+ }
65
+ }
66
+ return null;
67
+ }
68
+ roots.findNodeById = (id) => findNodeById(id, roots);
69
+ return roots;
70
+ }
71
+ console.log(11);
72
+ // Annotate the CommonJS export names for ESM import in node:
73
+ 0 && (module.exports = {
74
+ arrayToTree
75
+ });
76
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,51 @@
1
+ // src/index.ts
2
+ function arrayToTree(array, option) {
3
+ const idMap = {};
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
+ }
11
+ if (parentId && typeof parentId === "object") {
12
+ parentId = parentId[option.idKey];
13
+ }
14
+ obj[option.childrenKey] = [];
15
+ idMap[id] = obj;
16
+ if (!parentId) {
17
+ roots.push(obj);
18
+ } else {
19
+ const parent = idMap[parentId];
20
+ if (parent) {
21
+ parent[option.childrenKey].push(obj);
22
+ } else {
23
+ roots.push(obj);
24
+ }
25
+ }
26
+ }
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;
40
+ }
41
+ }
42
+ return null;
43
+ }
44
+ roots.findNodeById = (id) => findNodeById(id, roots);
45
+ return roots;
46
+ }
47
+ console.log(11);
48
+ export {
49
+ arrayToTree
50
+ };
51
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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":[]}
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@osmos/arraytotree",
3
+ "version": "1.0.0",
4
+ "description": "transform flat array to tree structure",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsup",
13
+ "test": "jest"
14
+ },
15
+ "keywords": [
16
+ "array",
17
+ "tree"
18
+ ],
19
+ "author": "osmos",
20
+ "license": "ISC",
21
+ "devDependencies": {
22
+ "@types/jest": "^29.5.12",
23
+ "jest": "^29.7.0",
24
+ "ts-jest": "^29.1.2",
25
+ "ts-node": "^10.9.2",
26
+ "tsup": "^8.0.2",
27
+ "typescript": "^5.4.5"
28
+ }
29
+ }