Skip to content

Commit 80a80fe

Browse files
Merge pull request #16 from philipstanislaus/SpawnAtis-master
Add childrenField option
2 parents bc0a30a + db40982 commit 80a80fe

9 files changed

+37
-33
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ You can provide a second argument to arrayToTree with configuration options. Rig
6161

6262
- `id`: key of the id field of the item. Default: `"id"`
6363
- `parentId`: key of the parent's id field of the item. Default: `"parentId"`
64+
- `childrenField`: key which will contain all child nodes of the parent node. Default: `"children"`
6465
- `dataField`: key which will contain all properties/data of the original items. Set to null if you don't want a container. Default: `"data"`
6566

6667
Example:
@@ -72,19 +73,19 @@ const tree = arrayToTree([
7273
{ num: '1941', ref: '418', custom: 'de' },
7374
{ num: '1', ref: '418', custom: 'ZZZz' },
7475
{ num: '418', ref: null, custom: 'ü'},
75-
], { id: 'num', parentId: 'ref' })
76+
], { id: 'num', parentId: 'ref', childrenField: 'nodes' })
7677
```
7778

7879
Which produces:
7980

8081
```js
8182
[
82-
{ data: { num: '4', ref: null, custom: 'abc' }, children: [
83-
{ data: { num: '31', ref: '4', custom: '12' }, children: [] },
83+
{ data: { num: '4', ref: null, custom: 'abc' }, nodes: [
84+
{ data: { num: '31', ref: '4', custom: '12' }, nodes: [] },
8485
] },
85-
{ data: { num: '418', ref: null, custom: 'ü'}, children: [
86-
{ data: { num: '1941', ref: '418', custom: 'de' }, children: [] },
87-
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, children: [] },
86+
{ data: { num: '418', ref: null, custom: 'ü'}, nodes: [
87+
{ data: { num: '1941', ref: '418', custom: 'de' }, nodes: [] },
88+
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, nodes: [] },
8889
] },
8990
]
9091
```

build/arrayToTree.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ export interface Item {
66
export interface TreeItem {
77
id?: string | number;
88
parentId?: string | number | null;
9-
[key: string]: Item | any;
10-
children: TreeItem[];
9+
[key: string]: Item | TreeItem[] | any;
1110
}
1211
export interface Config {
1312
id: string;
1413
parentId: string;
1514
dataField: string | null;
15+
childrenField: string;
1616
}
1717
/**
1818
* Unflattens an array to a tree with runtime O(n)

build/arrayToTree.js

Lines changed: 6 additions & 4 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: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.spec.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.

src/arrayToTree.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ describe('arrayToTree', () => {
5555
{ num: '1', ref: '418', custom: 'ZZZz' },
5656
{ num: '418', ref: null, custom: 'ü' },
5757
]),
58-
{ id: 'num', parentId: 'ref' },
58+
{ id: 'num', parentId: 'ref', childrenField: 'nodes' },
5959
)).to.deep.equal([
6060
{
61-
data: { num: '4', ref: null, custom: 'abc' }, children: [
62-
{ data: { num: '31', ref: '4', custom: '12' }, children: [] },
61+
data: { num: '4', ref: null, custom: 'abc' }, nodes: [
62+
{ data: { num: '31', ref: '4', custom: '12' }, nodes: [] },
6363
],
6464
},
6565
{
66-
data: { num: '418', ref: null, custom: 'ü' }, children: [
67-
{ data: { num: '1941', ref: '418', custom: 'de' }, children: [] },
68-
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, children: [] },
66+
data: { num: '418', ref: null, custom: 'ü' }, nodes: [
67+
{ data: { num: '1941', ref: '418', custom: 'de' }, nodes: [] },
68+
{ data: { num: '1', ref: '418', custom: 'ZZZz' }, nodes: [] },
6969
],
7070
},
7171
])

src/arrayToTree.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ export interface Item {
77
export interface TreeItem {
88
id?: string | number,
99
parentId?: string | number | null,
10-
[key: string]: Item | any,
11-
children: TreeItem[]
10+
[key: string]: Item | TreeItem[] | any,
1211
}
1312

1413
export interface Config {
1514
id: string,
1615
parentId: string,
1716
dataField: string | null,
17+
childrenField: string
1818
}
1919

2020
const defaultConfig: Config = {
2121
id: 'id',
2222
parentId: 'parentId',
2323
dataField: 'data',
24+
childrenField: 'children',
2425
}
2526

2627
/**
@@ -46,14 +47,14 @@ export function arrayToTree (items: Item[], config: Partial<Config> = {}): TreeI
4647
// look whether item already exists in the lookup table
4748
if (!Object.prototype.hasOwnProperty.call(lookup, itemId)) {
4849
// item is not yet there, so add a preliminary item (its data will be added later)
49-
lookup[itemId] = { children: [] }
50+
lookup[itemId] = { [conf.childrenField]: [] }
5051
}
5152

5253
// add the current item's data to the item in the lookup table
5354
if (conf.dataField) {
5455
lookup[itemId][conf.dataField] = item
5556
} else {
56-
lookup[itemId] = { ...item, children: lookup[itemId].children }
57+
lookup[itemId] = { ...item, [conf.childrenField]: lookup[itemId][conf.childrenField] }
5758
}
5859

5960
const TreeItem = lookup[itemId]
@@ -67,11 +68,11 @@ export function arrayToTree (items: Item[], config: Partial<Config> = {}): TreeI
6768
// look whether the parent already exists in the lookup table
6869
if (!Object.prototype.hasOwnProperty.call(lookup, parentId)) {
6970
// parent is not yet there, so add a preliminary parent (its data will be added later)
70-
lookup[parentId] = { children: [] }
71+
lookup[parentId] = { [conf.childrenField]: [] }
7172
}
7273

7374
// add the current item to the parent
74-
lookup[parentId].children.push(TreeItem)
75+
lookup[parentId][conf.childrenField].push(TreeItem)
7576
}
7677
}
7778

0 commit comments

Comments
 (0)