Skip to content

Commit 9540ea4

Browse files
committed
Object builder preserves items with null values. Closes #44
1 parent 156d909 commit 9540ea4

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/XMLNode.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ module.exports = class XMLNode
4040
else if _.isFunction name
4141
lastChild = @element name.apply()
4242

43-
# expand if object but skip null values
43+
# expand if object
4444
else if _.isObject name
45-
for own key, val of name when val?
45+
for own key, val of name
4646
# evaluate if function
4747
val = val.apply() if _.isFunction val
4848

test/objectconsistency.coffee

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
vows = require 'vows'
2+
assert = require 'assert'
3+
4+
xmlbuilder = require '../src/index.coffee'
5+
6+
vows
7+
.describe('Creating XML')
8+
.addBatch
9+
'Object builder consistency':
10+
topic: () ->
11+
obj =
12+
ele: "simple element"
13+
person:
14+
name: "John"
15+
'@age': 35
16+
'?pi': 'mypi'
17+
'#comment': 'Good guy'
18+
'#cdata': 'well formed!'
19+
unescaped:
20+
'#raw': '&<>&'
21+
address:
22+
city: "Istanbul"
23+
street: "End of long and winding road"
24+
contact: [
25+
{ phone: "555-1234" }
26+
{ phone: "555-1235" }
27+
]
28+
id: () -> return 42
29+
details:
30+
'#text': 'classified'
31+
nullval: null
32+
33+
t1 = xmlbuilder.create('root', { headless: true })
34+
.ele(obj)
35+
36+
t2 = xmlbuilder.create('root', { headless: true })
37+
.ele('ele', 'simple element')
38+
.up()
39+
.ele('person')
40+
.ele('name', 'John')
41+
.up()
42+
.att('age', 35)
43+
.ins('pi', 'mypi')
44+
.com('Good guy')
45+
.dat('well formed!')
46+
.ele('unescaped')
47+
.raw('&<>&')
48+
.up()
49+
.ele('address')
50+
.ele('city', 'Istanbul')
51+
.up()
52+
.ele('street', 'End of long and winding road')
53+
.up()
54+
.up()
55+
.ele('contact')
56+
.ele('phone', '555-1234')
57+
.up()
58+
.ele('phone', '555-1235')
59+
.up()
60+
.up()
61+
.ele('id', 42)
62+
.up()
63+
.ele('details')
64+
.text('classified')
65+
.up()
66+
.ele('nullval', null)
67+
68+
return [t1, t2]
69+
70+
'resulting XML': (topic) ->
71+
assert.strictEqual topic[0].end(), topic[1].end()
72+
73+
74+
.export(module)
75+

0 commit comments

Comments
 (0)