Skip to content

Commit 26729d5

Browse files
committed
fix: simplify child array initialization in _array2tree function
1 parent f7b84dd commit 26729d5

File tree

2 files changed

+2
-7
lines changed

2 files changed

+2
-7
lines changed

src/helper/convert.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ function _array2tree(data, options = {}) {
8181

8282
const items = [];
8383
data.forEach(d => {
84-
if (typeof d[c.child_name] === 'undefined') {
85-
d[c.child_name] = [];
86-
} else {
87-
throw new Error('child name "' + c.child_name + '" is reserved for child data, please use another name');
88-
}
84+
d[c.child_name] = [];
8985
items[d[c.data_index]] = d;
9086
if (typeof d[c.parent_index] === 'undefined' || typeof d[c.data_index] === 'undefined') {
9187
throw new Error('data must have "' + c.parent_index + '" and "' + c.data_index + '"');

tests/convert.tests.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ describe('convert test case', function () {
5050
// throw error when data is not array
5151
expect(() => _array2tree(null)).to.throw('data must be an array');
5252
expect(() => _array2tree([{ id: 1 }])).to.throw('data must have "parent_id" and "id"');
53-
expect(() => _array2tree([{ id: 1, parent_id: 0, child: [] }])).to.throw('child name "child" is reserved for child data, please use another name');
5453
});
5554

5655
it('tree2array', function () {
@@ -70,7 +69,7 @@ describe('convert test case', function () {
7069
{
7170
'id': 4,
7271
'parent_id': 2,
73-
'child': []
72+
'child': ''
7473
}
7574
]
7675
}

0 commit comments

Comments
 (0)