Skip to content

Commit e8bc785

Browse files
Support empty string parentIds as root objects
1 parent c4b6c4e commit e8bc785

7 files changed

+50
-5
lines changed

build/arrayToTree.js

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

+22
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.

src/arrayToTree.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,29 @@ describe('arrayToTree', () => {
193193
])
194194
})
195195

196+
it('should treat objects with empty string as parentId as root objects', () => {
197+
expect(arrayToTree([
198+
{ id: '4', parentId: '', custom: 'abc' },
199+
{ id: '31', parentId: '4', custom: '12' },
200+
{ id: '1941', parentId: '418', custom: 'de' },
201+
{ id: '1', parentId: '418', custom: 'ZZZz' },
202+
{ id: '418', parentId: '', custom: 'ü' },
203+
{ id: '1313', parentId: '13', custom: 'Not existing' },
204+
])).to.deep.equal([
205+
{
206+
data: { id: '4', parentId: '', custom: 'abc' }, children: [
207+
{ data: { id: '31', parentId: '4', custom: '12' }, children: [] },
208+
],
209+
},
210+
{
211+
data: { id: '418', parentId: '', custom: 'ü' }, children: [
212+
{ data: { id: '1941', parentId: '418', custom: 'de' }, children: [] },
213+
{ data: { id: '1', parentId: '418', custom: 'ZZZz' }, children: [] },
214+
],
215+
},
216+
])
217+
})
218+
196219
it('should work with empty inputs', () => {
197220
expect(arrayToTree([])).to.deep.equal([])
198221
})

src/arrayToTree.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function arrayToTree (items: Item[], config: Partial<Config> = {}): TreeI
5959

6060
const TreeItem = lookup[itemId]
6161

62-
if (parentId === null || parentId === undefined) {
62+
if (parentId === null || parentId === undefined || parentId === '') {
6363
// is a root item
6464
rootItems.push(TreeItem)
6565
} else {

0 commit comments

Comments
 (0)