Skip to content

Commit ab6f44a

Browse files
Merge pull request #7 from philipstanislaus/dataOption
Add option to maintain structure of your data. (#6)
2 parents 81df55e + bc38bcb commit ab6f44a

File tree

9 files changed

+234
-27
lines changed

9 files changed

+234
-27
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ Which results in the following array:
5959

6060
You can provide a second argument to arrayToTree with configuration options. Right now, you can set the following:
6161

62-
- `id`: key of the id field of the item
63-
- `parentId`: key of the parent's id field of the item
62+
- `id`: key of the id field of the item. Default: `"id"`
63+
- `parentId`: key of the parent's id field of the item. Default: `"parentId"`
64+
- `dataField`: key which will contain all properties/data of the origina items. Set to null if you don't want a container. Default: `"data"`
6465

6566
Example:
6667

@@ -88,6 +89,32 @@ Which produces:
8889
]
8990
```
9091

92+
Example with no data field:
93+
94+
```js
95+
const tree = arrayToTree([
96+
{ id: '4', parentId: null, custom: 'abc' },
97+
{ id: '31', parentId: '4', custom: '12' },
98+
{ id: '1941', parentId: '418', custom: 'de' },
99+
{ id: '1', parentId: '418', custom: 'ZZZz' },
100+
{ id: '418', parentId: null, custom: 'ü'},
101+
], { dataField: null })
102+
```
103+
104+
Which produces:
105+
106+
```js
107+
[
108+
{ id: '4', parentId: null, custom: 'abc', children: [
109+
{ id: '31', parentId: '4', custom: '12', children: [] },
110+
] },
111+
{ id: '418', parentId: null, custom: 'ü', children: [
112+
{ id: '1941', parentId: '418', custom: 'de', children: [] },
113+
{ id: '1', parentId: '418', custom: 'ZZZz', children: [] },
114+
] },
115+
]
116+
```
117+
91118
## TypeScript
92119

93120
This project includes types, just import the module as usual:

build/arrayToTree.d.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
export interface Item {
2-
id: string;
3-
parentId: string | null;
2+
id?: string;
3+
parentId?: string | null;
44
[key: string]: any;
55
}
66
export interface TreeItem {
7-
data: Item | null;
7+
id?: string;
8+
parentId?: string | null;
9+
[key: string]: Item | any;
810
children: TreeItem[];
911
}
1012
export interface Config {
1113
id: string;
1214
parentId: string;
15+
dataField: string | null;
1316
}
1417
/**
1518
* Unflattens an array to a tree with runtime O(n)
1619
*/
17-
export declare function arrayToTree(items: Item[], config?: Config): TreeItem[];
20+
export declare function arrayToTree(items: Item[], config?: Partial<Config>): TreeItem[];

build/arrayToTree.js

Lines changed: 28 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.spec.js

Lines changed: 66 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)