Skip to content

Commit 0ecfbe0

Browse files
Fix bug where items that came before their parents where reported as orphans, closes #18
1 parent ad29b9c commit 0ecfbe0

7 files changed

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

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

+16
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,22 @@ describe('arrayToTree', () => {
243243
'Hint: prevent orphans to result in an error by passing the following option: { throwIfOrphans: false }')
244244
})
245245

246+
it('should not throw if no orphans exist and throwIfOrphans is true, but the order is different (see #18)', () => {
247+
expect(arrayToTree([
248+
{ id: '2', parentId: 'root', foo: 'bar' },
249+
{ id: '1-1', parentId: '1', foo: 'bar' },
250+
{ id: '1', parentId: 'root', foo: 'bar' },
251+
{ id: 'root', parentId: null, bar: 'bar' },
252+
], { dataField: null, throwIfOrphans: true })).to.deep.equal([
253+
{ id: "root", parentId: null, bar: "bar", children: [
254+
{ id: "2", parentId: "root", foo: "bar", children: [] },
255+
{ id: "1", parentId: "root", foo: "bar", children: [
256+
{ id: "1-1", parentId: "1", foo: "bar", children: [] },
257+
]},
258+
]},
259+
])
260+
})
261+
246262
it('should work with empty inputs', () => {
247263
expect(arrayToTree([])).to.deep.equal([])
248264
})

src/arrayToTree.ts

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

5959
// if we track orphans, delete this item from the orphan set if it is in it
60-
if (orphanIds) { orphanIds.delete(parentId) }
60+
if (orphanIds) { orphanIds.delete(itemId) }
6161

6262
// add the current item's data to the item in the lookup table
6363
if (conf.dataField) {

0 commit comments

Comments
 (0)