Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow function as default value for parsing empty tags #512

Merged
merged 3 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,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.EventEmitter
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 @@ -72,6 +72,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