Skip to content

Commit

Permalink
Implemented emptyTag option. If set, empty tags will not be {} but th…
Browse files Browse the repository at this point in the history
…e value of the option.

Closes GH Leonidas-from-XIV#20
  • Loading branch information
Leonidas-from-XIV committed Aug 14, 2011
1 parent 0313fd0 commit 5f54d69
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/xml2js.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/xml2js.coffee
@@ -1,6 +1,10 @@
sax = require 'sax'
events = require 'events'

# Underscore has a nice function for this, but we try to go without dependencies
isEmpty = (thing) ->
return typeof thing == 'object' && thing? && Object.keys(thing).length == 0

class exports.Parser extends events.EventEmitter
constructor: (opts) ->

Expand Down Expand Up @@ -65,6 +69,10 @@ class exports.Parser extends events.EventEmitter
if Object.keys(obj).length == 1 and "#" of obj and not @EXPLICIT_CHARKEY
obj = obj["#"]

if options.emptyTag != undefined && isEmpty obj
obj = options.emptyTag

# check whether we closed all the open tags
if stack.length > 0
if nodeName not of s
s[nodeName] = obj
Expand Down
3 changes: 3 additions & 0 deletions test/xml2js.test.coffee
Expand Up @@ -71,3 +71,6 @@ module.exports =

'test default empty tag result': skeleton(undefined, (r) ->
assert.deepEqual r['emptytest'], {})

'test empty tag result specified null': skeleton({emptyTag: null}, (r) ->
assert.equal r['emptytest'], null)

0 comments on commit 5f54d69

Please sign in to comment.