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

304 properties as array fix #516

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ value})``. Possible options are:
* `mergeAttrs` (default: `false`): Merge attributes and child elements as
properties of the parent, instead of keying attributes off a child
attribute object. This option is ignored if `ignoreAttrs` is `true`.
* `mergeAttrsArray` (default: `true`): Ignored if `mergeAttrs` is `false`.
While merging attributes as properties of the parent (`mergeAttrs` is `true`),
when `mergeAttrsArray` is `false`, the value for an attribute is the raw value
rather than an array.
* `validator` (default `null`): You can specify a callable that validates
the resulting structure somehow, however you want. See unit tests
for an example.
Expand Down
2 changes: 2 additions & 0 deletions lib/defaults.js

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

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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"David Wood <[email protected]> (http://codesleuth.co.uk/)",
"Nicolas Maquet (https://github.com/nmaquet)",
"Lovell Fuller (http://lovell.info/)",
"d3adc0d3 (https://github.com/d3adc0d3)"
"d3adc0d3 (https://github.com/d3adc0d3)",
"IRCraziestTaxi (https://github.com/IRCraziestTaxi)"
],
"main": "./lib/xml2js",
"files": [
Expand Down
8 changes: 8 additions & 0 deletions src/defaults.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ exports.defaults = {
# merge attributes and child elements onto parent object. this may
# cause collisions.
mergeAttrs: false
# when mergeAttrs and explicitArray are true
# and mergeAttrsArray is false (default true),
# do not parse attributes into an array.
mergeAttrsArray: true
explicitRoot: false
validator: null
xmlns : false
Expand Down Expand Up @@ -45,6 +49,10 @@ exports.defaults = {
explicitArray: true
ignoreAttrs: false
mergeAttrs: false
# when mergeAttrs and explicitArray are true
# and mergeAttrsArray is false (default true),
# do not parse attributes into an array.
mergeAttrsArray: true
explicitRoot: true
validator: null
xmlns : false
Expand Down
5 changes: 4 additions & 1 deletion src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ class exports.Parser extends events.EventEmitter
newValue = if @options.attrValueProcessors then processItem(@options.attrValueProcessors, node.attributes[key], key) else node.attributes[key]
processedKey = if @options.attrNameProcessors then processItem(@options.attrNameProcessors, key) else key
if @options.mergeAttrs
@assignOrPush obj, processedKey, newValue
if @options.mergeAttrsArray
@assignOrPush obj, processedKey, newValue
else
obj[processedKey] = newValue
else
obj[attrkey][processedKey] = newValue

Expand Down
18 changes: 18 additions & 0 deletions test/parser.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ module.exports =
equ r.sample.listtest[0].single[0], 'Single'
equ r.sample.listtest[0].attr[0], 'Attribute')

'test parse with mergeAttrs and not mergeAttrsArray': skeleton(mergeAttrs: true, mergeAttrsArray: false, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.chartest[0].desc, 'Test for CHARs'
equ r.sample.chartest[0]._, 'Character data here!'
equ r.sample.cdatatest[0].desc, 'Test for CDATA'
equ r.sample.cdatatest[0].misc, 'true'
equ r.sample.cdatatest[0]._, 'CDATA here!'
equ r.sample.nochartest[0].desc, 'No data'
equ r.sample.nochartest[0].misc, 'false'
equ r.sample.listtest[0].item[0].subitem[0], 'Foo(1)'
equ r.sample.listtest[0].item[0].subitem[1], 'Foo(2)'
equ r.sample.listtest[0].item[0].subitem[2], 'Foo(3)'
equ r.sample.listtest[0].item[0].subitem[3], 'Foo(4)'
equ r.sample.listtest[0].item[1], 'Qux.'
equ r.sample.listtest[0].item[2], 'Quux.'
equ r.sample.listtest[0].single[0], 'Single'
equ r.sample.listtest[0].attr, 'Attribute')

'test parse with mergeAttrs and not explicitArray': skeleton(mergeAttrs: true, explicitArray: false, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.chartest.desc, 'Test for CHARs'
Expand Down