Skip to content

Commit c886154

Browse files
Merge pull request #15 from philipstanislaus/support-integers
Add support for integer keys
2 parents 9c4247f + 00ded66 commit c886154

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

build/arrayToTree.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export interface Item {
2-
id?: string;
3-
parentId?: string | null;
2+
id?: string | number;
3+
parentId?: string | number | null;
44
[key: string]: any;
55
}
66
export interface TreeItem {
7-
id?: string;
8-
parentId?: string | null;
7+
id?: string | number;
8+
parentId?: string | number | null;
99
[key: string]: Item | any;
1010
children: TreeItem[];
1111
}

build/arrayToTree.spec.js

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

+22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ describe('arrayToTree', () => {
2424
])
2525
})
2626

27+
it('should work with integer keys', () => {
28+
expect(arrayToTree([
29+
{ id: 4, parentId: null, custom: 'abc' },
30+
{ id: 31, parentId: 4, custom: '12' },
31+
{ id: 1941, parentId: 418, custom: 'de' },
32+
{ id: 1, parentId: 418, custom: 'ZZZz' },
33+
{ id: 418, parentId: null, custom: 'ü' },
34+
])).to.deep.equal([
35+
{
36+
data: { id: 4, parentId: null, custom: 'abc' }, children: [
37+
{ data: { id: 31, parentId: 4, custom: '12' }, children: [] },
38+
],
39+
},
40+
{
41+
data: { id: 418, parentId: null, custom: 'ü' }, children: [
42+
{ data: { id: 1941, parentId: 418, custom: 'de' }, children: [] },
43+
{ data: { id: 1, parentId: 418, custom: 'ZZZz' }, children: [] },
44+
],
45+
},
46+
])
47+
})
48+
2749
it('should work with nested objects and custom keys', () => {
2850
expect(arrayToTree(
2951
([

src/arrayToTree.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
export interface Item {
2-
id?: string
3-
parentId?: string | null,
2+
id?: string | number
3+
parentId?: string | number | null,
44
[key: string]: any,
55
}
66

77
export interface TreeItem {
8-
id?: string,
9-
parentId?: string | null,
8+
id?: string | number,
9+
parentId?: string | number | null,
1010
[key: string]: Item | any,
1111
children: TreeItem[]
1212
}

0 commit comments

Comments
 (0)