Skip to content

Commit

Permalink
xml: ignore false children
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Nov 29, 2019
1 parent 109053d commit 9137af4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/xml/lib/x.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
const Element = require('./Element')

function append(el, child) {
if (child === false || child === null || child === undefined) return
if (child instanceof Element) {
el.append(child)
} else if (Array.isArray(child)) {
child.forEach(c => append(el, c))
} else if (child !== null && child !== undefined) {
} else {
el.append(String(child))
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/xml/test/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const test = require('ava')
const Element = require('../lib/Element')

test('ignore __self and __source properties', t => {
// TODO probably better to ignore in serialization instead
test('ignore __self and __source attributes', t => {
const el = new Element('foo', {
__source: 'source',
__self: 'self',
Expand Down
22 changes: 22 additions & 0 deletions packages/xml/test/x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const test = require('ava')
const x = require('../lib/x')

test('ignore false children', t => {
const el = x('foo', {}, false)

t.is(el.children.length, 0)
})

test('ignore null children', t => {
const el = x('foo', {}, null)

t.is(el.children.length, 0)
})

test('ignore undefined children', t => {
const el = x('foo', {}, undefined)

t.is(el.children.length, 0)
})

0 comments on commit 9137af4

Please sign in to comment.