Skip to content

Commit

Permalink
Merge pull request #512 from economia/master
Browse files Browse the repository at this point in the history
Allow function as default value for parsing empty tags
  • Loading branch information
Leonidas-from-XIV authored Mar 27, 2020
2 parents 198063c + fa32064 commit 1832e0b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ value})``. Possible options are:
* `normalize` (default: `false`): Trim whitespaces inside text nodes.
* `explicitRoot` (default: `true`): Set this if you want to get the root
node in the resulting object.
* `emptyTag` (default: `''`): what will the value of empty nodes be.
* `emptyTag` (default: `''`): what will the value of empty nodes be. In case
you want to use an empty object as a default value, it is better to provide a factory
function `() => ({})` instead. Without this function a plain object would
become a shared reference across all occurrences with unwanted behavior.
* `explicitArray` (default: `true`): Always put child nodes in an array if
true; otherwise an array is created only if there is more than one.
* `ignoreAttrs` (default: `false`): Ignore all XML attributes and only create
Expand Down
6 changes: 5 additions & 1 deletion lib/parser.js

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

5 changes: 4 additions & 1 deletion src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ class exports.Parser extends events
obj = obj[charkey]

if (isEmpty obj)
obj = if @options.emptyTag != '' then @options.emptyTag else emptyStr
if typeof @options.emptyTag == 'function'
obj = @options.emptyTag()
else
obj = if @options.emptyTag != '' then @options.emptyTag else emptyStr

if @options.validator?
xpath = "/" + (node["#name"] for node in stack).concat(nodeName).join("/")
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@
<tagNameProcessTest/>
<valueProcessTest>some value</valueProcessTest>
<textordertest>this is text with <b>markup</b> <em>like this</em> in the middle</textordertest>
<emptytestanother>

</emptytestanother>
</sample>
5 changes: 5 additions & 0 deletions test/parser.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ module.exports =
# determine number of items in object
equ Object.keys(r.sample.tagcasetest[0]).length, 3)

'test parse with empty objects and functions': skeleton({emptyTag: ()=> ({})}, (r)->
console.log 'Result object: ' + util.inspect r, false, 10
bool = r.sample.emptytestanother[0] is r.sample.emptytest[0]
equ bool, false)

'test parse with explicitCharkey': skeleton(explicitCharkey: true, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.chartest[0].$.desc, 'Test for CHARs'
Expand Down

0 comments on commit 1832e0b

Please sign in to comment.