Skip to content

Commit

Permalink
fix: better handle undefined children
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed May 24, 2021
1 parent 024d68e commit 4bac3c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ function h (t, props, ...children) {

while (children.length) {
const child = children.shift()
if (!child) continue
child.pop ? children.push(...child) : c.push(child)
if (child) {
child.pop ? children.push(...child) : c.push(child)
}
}

if (t.call) return t({ ...props, children: c })
Expand Down
21 changes: 21 additions & 0 deletions test/hyposcript.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ module.exports = (test, assert) => {
assert(html === `<div><span></span><span></span></div>`)
})

test('handles falsy coercion', () => {
const val = undefined
const html = <div>{val && <h1>{val}</h1>}</div>
assert(!/h1/.test(html))
})

test('can return null', () => {
const Comp = () => null
const html = (
Expand All @@ -129,6 +135,21 @@ module.exports = (test, assert) => {
assert(html === `<div><h1>foo</h1><h1>bar</h1></div>`)
})

test('h - array children with falsy values', async () => {
const value = undefined
const html = h('div', null, [
h('h1', {}, 'foo'),
value && h('h2', {}, value),
false && h('h3', {}, 'baz')
])
assert(html === `<div><h1>foo</h1></div>`)
})

test('h - undefined children', async () => {
const html = h('div', {}, [h('h1', {}, 'hello'), undefined])
assert(html === `<div><h1>hello</h1></div>`)
})

test('h - children as props', async () => {
const html = h('div', {
children: [h('h1', {}, 'foo'), h('h1', {}, 'bar')]
Expand Down

0 comments on commit 4bac3c1

Please sign in to comment.