Skip to content

Commit e0fd417

Browse files
feat: add option to disable nested ids
1 parent 594f4d1 commit e0fd417

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ You can provide a second argument to arrayToTree with configuration options. Rig
6666

6767
- `id`: key of the id field of the item. Also works with nested properties (e. g. `"nested.parentId"`). Default: `"id"`.
6868
- `parentId`: key of the parent's id field of the item. Also works with nested properties (e. g. `"nested.parentId"`). Default: `"parentId"`.
69+
- `nestedIds`: option to enable/disable nested ids. Default: `true`.
6970
- `childrenField`: key which will contain all child nodes of the parent node. Default: `"children"`
7071
- `dataField`: key which will contain all properties/data of the original items. Set to null if you don't want a container. Default: `"data"`
7172
- `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`
72-
- `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}`
73+
- `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}`
7374

7475
Example:
7576

src/arrayToTree.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ describe("arrayToTree", () => {
8181
]);
8282
});
8383

84+
it("should work with nested objects and a custom key with dots if nested properties are disabled", () => {
85+
expect(
86+
arrayToTree(
87+
[
88+
{ '.key': "4", 'my.parent': null, custom: "abc" },
89+
{ '.key': "31", 'my.parent': "4", custom: "12" },
90+
{ '.key': "1941", 'my.parent': "418", custom: "de" },
91+
{ '.key': "1", 'my.parent': "418", custom: "ZZZz" },
92+
{ '.key': "418", 'my.parent': null, custom: "ü" },
93+
],
94+
{ id: '.key', parentId: 'my.parent', childrenField: "nodes", nestedIds: false }
95+
)
96+
).to.deep.equal([
97+
{
98+
data: { '.key': "4", 'my.parent': null, custom: "abc" },
99+
nodes: [{ data: { '.key': "31", 'my.parent': "4", custom: "12" }, nodes: [] }],
100+
},
101+
{
102+
data: { '.key': "418", 'my.parent': null, custom: "ü" },
103+
nodes: [
104+
{ data: { '.key': "1941", 'my.parent': "418", custom: "de" }, nodes: [] },
105+
{ data: { '.key': "1", 'my.parent': "418", custom: "ZZZz" }, nodes: [] },
106+
],
107+
},
108+
]);
109+
});
110+
84111
it("should ignore objects if parentId does not exist", () => {
85112
expect(
86113
arrayToTree([

src/arrayToTree.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface Config {
1717
childrenField: string;
1818
throwIfOrphans: boolean;
1919
rootParentIds: { [rootParentId: string]: true }; // use an object here for fast lookups
20+
nestedIds: boolean;
2021
}
2122

2223
const defaultConfig: Config = {
@@ -26,6 +27,7 @@ const defaultConfig: Config = {
2627
childrenField: "children",
2728
throwIfOrphans: false,
2829
rootParentIds: { "": true },
30+
nestedIds: true,
2931
};
3032

3133
/**
@@ -54,8 +56,8 @@ export function arrayToTree(
5456
// in the lookup object and fill it with the data of the parent later
5557
// if an item has no parentId, add it as a root element to rootItems
5658
for (const item of items) {
57-
const itemId = getNestedProperty(item, conf.id);
58-
const parentId = getNestedProperty(item, conf.parentId);
59+
const itemId = conf.nestedIds ? getNestedProperty(item, conf.id) : item[conf.id];
60+
const parentId = conf.nestedIds ? getNestedProperty(item, conf.parentId) : item[conf.parentId];
5961

6062
if (conf.rootParentIds[itemId]) {
6163
throw new Error(

0 commit comments

Comments
 (0)