Skip to content

Commit

Permalink
IsArray option isn't parsing tags with 0 as value correctly #490
Browse files Browse the repository at this point in the history
  • Loading branch information
omggga committed Mar 22, 2023
1 parent 292fb78 commit 762b593
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
31 changes: 31 additions & 0 deletions spec/arrayMode_spec.js
Expand Up @@ -175,6 +175,37 @@ describe("XMLParser with arrayMode enabled", function () {
expect(result).toEqual(expected);
});

it("should parse leaf tags with zero or false value correctly in arrayMode", function () {
const xmlDataExample = `
<report>
<value>0</value>
<isNew>false</isNew>
<isReport>true</isReport>
</report>`

const expected = {
"report":
[
{
"value": 0,
"isNew": false,
"isReport": true
}
]
};

const options = {
ignoreAttributes: false,
isArray: (tagName, jpath, isLeafNode, isAttribute) => {
if(!isLeafNode) return true;
}
}
const parser = new XMLParser(options);
const result = parser.parse(xmlDataExample);
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});

it("should parse only attributes as Array if set", function () {

const expected = {
Expand Down
14 changes: 13 additions & 1 deletion src/xmlparser/node2json.js
Expand Up @@ -94,8 +94,20 @@ function assignAttributes(obj, attrMap, jpath, options){
}

function isLeafTag(obj, options){
const { textNodeName } = options;
const propCount = Object.keys(obj).length;
if( propCount === 0 || (propCount === 1 && obj[options.textNodeName]) ) return true;

if (propCount === 0) {
return true;
}

if (
propCount === 1 &&
(obj[textNodeName] || typeof obj[textNodeName] === "boolean" || obj[textNodeName] === 0)
) {
return true;
}

return false;
}
exports.prettify = prettify;

0 comments on commit 762b593

Please sign in to comment.