Skip to content

Commit 61bef56

Browse files
Change rootParentIds conf to object for better performance
1 parent f2507b9 commit 61bef56

File tree

9 files changed

+33
-30
lines changed

9 files changed

+33
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ You can provide a second argument to arrayToTree with configuration options. Rig
6464
- `childrenField`: key which will contain all child nodes of the parent node. Default: `"children"`
6565
- `dataField`: key which will contain all properties/data of the original items. Set to null if you don't want a container. Default: `"data"`
6666
- `throwIfOrphans`: option to throw an error if the array of items contains one or more items that have no parents in the array. This option has a small runtime penalty, so it's disabled by default. When enabled, the function will throw an error containing the parentIds that were not found in the items array. When disabled, the function will just ignore orphans and not add them to the tree. Default: `false`
67-
- `rootParentIds`: Array of parent ids that should be considered the top or root elements of the tree. This is useful when your tree is a subset of full tree, which means there is no item whose parent id is one of `undefined`, `null` or `''`. The array you pass in will be replace the default value. For more details, see [#23](https://github.com/philipstanislaus/performant-array-to-tree/issues/23). Default: `[null, undefined, '']`
67+
- `rootParentIds`: Object with parent ids as keys and `true` as values that should be considered the top or root elements of the tree. This is useful when your tree is a subset of full tree, which means there is no item whose parent id is one of `undefined`, `null` or `''`. The array you pass in will be replace the default value. `undefined` and `null` are always considered to be rootParentIds. For more details, see [#23](https://github.com/philipstanislaus/performant-array-to-tree/issues/23). Default: `{'': true}`
6868

6969
Example:
7070

build/arrayToTree.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export interface Config {
1414
dataField: string | null;
1515
childrenField: string;
1616
throwIfOrphans: boolean;
17-
rootParentIds: (string | undefined | null)[];
17+
rootParentIds: {
18+
[rootParentId: string]: true;
19+
};
1820
}
1921
/**
2022
* 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: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)