Skip to content

Commit a53ab8c

Browse files
Add option to throw error if orphans exist
1 parent 2b2216c commit a53ab8c

9 files changed

+95
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ You can provide a second argument to arrayToTree with configuration options. Rig
6363
- `parentId`: key of the parent's id field of the item. Default: `"parentId"`
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"`
66+
- `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`
6667

6768
Example:
6869

build/arrayToTree.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Config {
1313
parentId: string;
1414
dataField: string | null;
1515
childrenField: string;
16+
throwIfOrphans: boolean;
1617
}
1718
/**
1819
* Unflattens an array to a tree with runtime O(n)

build/arrayToTree.js

+19-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/arrayToTree.spec.js

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)