Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.88k stars 602 forks source link

Order of parseNumbers and parseBooleans impacts results #539

Open bdsanfelippo opened 4 years ago

bdsanfelippo commented 4 years ago

Given a custom parser, the order of the attrValueProcessors impacts the results in a way that I found odd as a consumer of this library.

attrValueProcessors: [parseBooleans, parseNumbers]

yields different results than:

attrValueProcessors: [parseNumbers, parseBooleans]

The reason why can be found looking at the code: https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/lib/processors.js#L20

When parseNumbers is called with a boolean value, it will return NaN. When parseNumbers is called with any other string it will return that string. For example

parseNumbers("true") // returns "true"
parseNumbers(true) // returns NaN

The workaround is to make sure that parseNumbers is defined first in the array of attrValueProcessors. Coding changes could also be made. At a minimum, I recommend that the documentation here is updated:

https://github.com/Leonidas-from-XIV/node-xml2js#processing-attribute-tag-names-and-values