Skip to content

Commit 9a1f2d2

Browse files
v1.9.0
1 parent e0fd417 commit 9a1f2d2

9 files changed

+76
-19
lines changed

build/arrayToTree.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface Config {
1717
rootParentIds: {
1818
[rootParentId: string]: true;
1919
};
20+
nestedIds: boolean;
2021
}
2122
/**
2223
* Unflattens an array to a tree with runtime O(n)

build/arrayToTree.js

+7-2
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

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

build/arrayToTree.spec.js.map

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "performant-array-to-tree",
3-
"version": "1.8.2",
3+
"version": "1.9.0",
44
"description": "Converts an array of items with ids and parent ids to a nested tree in a performant `O(n)` way. Runs in browsers and node.",
55
"keywords": [
66
"array to tree",

src/arrayToTree.spec.ts

+24-11
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,37 @@ describe("arrayToTree", () => {
8585
expect(
8686
arrayToTree(
8787
[
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: "ü" },
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: "ü" },
9393
],
94-
{ id: '.key', parentId: 'my.parent', childrenField: "nodes", nestedIds: false }
94+
{
95+
id: ".key",
96+
parentId: "my.parent",
97+
childrenField: "nodes",
98+
nestedIds: false,
99+
}
95100
)
96101
).to.deep.equal([
97102
{
98-
data: { '.key': "4", 'my.parent': null, custom: "abc" },
99-
nodes: [{ data: { '.key': "31", 'my.parent': "4", custom: "12" }, nodes: [] }],
103+
data: { ".key": "4", "my.parent": null, custom: "abc" },
104+
nodes: [
105+
{ data: { ".key": "31", "my.parent": "4", custom: "12" }, nodes: [] },
106+
],
100107
},
101108
{
102-
data: { '.key': "418", 'my.parent': null, custom: "ü" },
109+
data: { ".key": "418", "my.parent": null, custom: "ü" },
103110
nodes: [
104-
{ data: { '.key': "1941", 'my.parent': "418", custom: "de" }, nodes: [] },
105-
{ data: { '.key': "1", 'my.parent': "418", custom: "ZZZz" }, nodes: [] },
111+
{
112+
data: { ".key": "1941", "my.parent": "418", custom: "de" },
113+
nodes: [],
114+
},
115+
{
116+
data: { ".key": "1", "my.parent": "418", custom: "ZZZz" },
117+
nodes: [],
118+
},
106119
],
107120
},
108121
]);

src/arrayToTree.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ export function arrayToTree(
5656
// in the lookup object and fill it with the data of the parent later
5757
// if an item has no parentId, add it as a root element to rootItems
5858
for (const item of items) {
59-
const itemId = conf.nestedIds ? getNestedProperty(item, conf.id) : item[conf.id];
60-
const parentId = conf.nestedIds ? getNestedProperty(item, conf.parentId) : item[conf.parentId];
59+
const itemId = conf.nestedIds
60+
? getNestedProperty(item, conf.id)
61+
: item[conf.id];
62+
const parentId = conf.nestedIds
63+
? getNestedProperty(item, conf.parentId)
64+
: item[conf.parentId];
6165

6266
if (conf.rootParentIds[itemId]) {
6367
throw new Error(

0 commit comments

Comments
 (0)