Skip to content

Commit db40982

Browse files
Rename nodesField config key to childrenField
1 parent d493ebb commit db40982

9 files changed

+19
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ 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"`
65-
- `nodesField`: key which will contain all child nodes of the parent node . Default: `"children"`
6666

6767
Example:
6868

@@ -73,7 +73,7 @@ const tree = arrayToTree([
7373
{ num: '1941', ref: '418', custom: 'de' },
7474
{ num: '1', ref: '418', custom: 'ZZZz' },
7575
{ num: '418', ref: null, custom: 'ü'},
76-
], { id: 'num', parentId: 'ref', nodesField: 'nodes' })
76+
], { id: 'num', parentId: 'ref', childrenField: 'nodes' })
7777
```
7878

7979
Which produces:

build/arrayToTree.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface Config {
1212
id: string;
1313
parentId: string;
1414
dataField: string | null;
15-
nodesField: string;
15+
childrenField: string;
1616
}
1717
/**
1818
* Unflattens an array to a tree with runtime O(n)

build/arrayToTree.js

Lines changed: 5 additions & 5 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: 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.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('arrayToTree', () => {
5555
{ num: '1', ref: '418', custom: 'ZZZz' },
5656
{ num: '418', ref: null, custom: 'ü' },
5757
]),
58-
{ id: 'num', parentId: 'ref', nodesField: 'nodes' },
58+
{ id: 'num', parentId: 'ref', childrenField: 'nodes' },
5959
)).to.deep.equal([
6060
{
6161
data: { num: '4', ref: null, custom: 'abc' }, nodes: [

src/arrayToTree.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export interface Config {
1414
id: string,
1515
parentId: string,
1616
dataField: string | null,
17-
nodesField: string
17+
childrenField: string
1818
}
1919

2020
const defaultConfig: Config = {
2121
id: 'id',
2222
parentId: 'parentId',
2323
dataField: 'data',
24-
nodesField: 'children',
24+
childrenField: 'children',
2525
}
2626

2727
/**
@@ -47,14 +47,14 @@ export function arrayToTree (items: Item[], config: Partial<Config> = {}): TreeI
4747
// look whether item already exists in the lookup table
4848
if (!Object.prototype.hasOwnProperty.call(lookup, itemId)) {
4949
// item is not yet there, so add a preliminary item (its data will be added later)
50-
lookup[itemId] = { [conf.nodesField]: [] }
50+
lookup[itemId] = { [conf.childrenField]: [] }
5151
}
5252

5353
// add the current item's data to the item in the lookup table
5454
if (conf.dataField) {
5555
lookup[itemId][conf.dataField] = item
5656
} else {
57-
lookup[itemId] = { ...item, [conf.nodesField]: lookup[itemId][conf.nodesField] }
57+
lookup[itemId] = { ...item, [conf.childrenField]: lookup[itemId][conf.childrenField] }
5858
}
5959

6060
const TreeItem = lookup[itemId]
@@ -68,11 +68,11 @@ export function arrayToTree (items: Item[], config: Partial<Config> = {}): TreeI
6868
// look whether the parent already exists in the lookup table
6969
if (!Object.prototype.hasOwnProperty.call(lookup, parentId)) {
7070
// parent is not yet there, so add a preliminary parent (its data will be added later)
71-
lookup[parentId] = { [conf.nodesField]: [] }
71+
lookup[parentId] = { [conf.childrenField]: [] }
7272
}
7373

7474
// add the current item to the parent
75-
lookup[parentId][conf.nodesField].push(TreeItem)
75+
lookup[parentId][conf.childrenField].push(TreeItem)
7676
}
7777
}
7878

0 commit comments

Comments
 (0)