Skip to content

Commit b481a31

Browse files
committed
refactor: simplify toChildArray guards
1 parent 56a649f commit b481a31

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/diff/children.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,14 @@ function insert(parentVNode, oldDom, parentDom, isMounting) {
427427
*/
428428
export function toChildArray(children, out) {
429429
out = out || [];
430-
if (children == NULL || typeof children == 'boolean') {
431-
} else if (isArray(children)) {
432-
children.some(child => {
433-
toChildArray(child, out);
434-
});
435-
} else {
436-
out.push(children);
430+
if (children != NULL && typeof children != 'boolean') {
431+
if (isArray(children)) {
432+
children.some(child => {
433+
toChildArray(child, out);
434+
});
435+
} else {
436+
out.push(children);
437+
}
437438
}
438439
return out;
439440
}

test/browser/toChildArray.test.jsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,36 @@ describe('toChildArray', () => {
6060
expect(scratch.innerHTML).to.equal('<div></div>');
6161
});
6262

63+
it('filters nullish and boolean values', () => {
64+
expect(toChildArray(null)).to.deep.equal([]);
65+
expect(toChildArray(undefined)).to.deep.equal([]);
66+
expect(toChildArray(false)).to.deep.equal([]);
67+
expect(toChildArray(true)).to.deep.equal([]);
68+
});
69+
70+
it('preserves zero and empty string values', () => {
71+
expect(toChildArray(0)).to.deep.equal([0]);
72+
expect(toChildArray('')).to.deep.equal(['']);
73+
});
74+
75+
it('flattens sparse nested arrays', () => {
76+
const nested = [0, ''];
77+
nested.length = 3;
78+
const sparse = [null];
79+
sparse[2] = [undefined, false, nested];
80+
sparse[4] = true;
81+
sparse[6] = 'last';
82+
83+
expect(toChildArray(sparse)).to.deep.equal([0, '', 'last']);
84+
});
85+
86+
it('flattens Array subclasses', () => {
87+
class ChildrenArray extends Array {}
88+
const children = new ChildrenArray('first', [null, 'second']);
89+
90+
expect(toChildArray(children)).to.deep.equal(['first', 'second']);
91+
});
92+
6393
it('should skip a function child', () => {
6494
const child = num => num.toFixed(2);
6595
render(<Foo>{child}</Foo>, scratch);

0 commit comments

Comments
 (0)