-
-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat #666: add selective ignoreAttributes by pattern or callback * chore: resolve codeclimate issues
- Loading branch information
Showing
7 changed files
with
362 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
"use strict"; | ||
|
||
const { XMLParser, XMLBuilder, XMLValidator } = require("../src/fxp"); | ||
|
||
const xmlData = ` | ||
<tag | ||
ns:attr1="a1-value" | ||
ns:attr2="a2-value" | ||
ns2:attr3="a3-value" | ||
ns2:attr4="a4-value"> | ||
value | ||
</tag>`; | ||
|
||
const jsonData = { | ||
tag: { | ||
'$ns:attr1': 'a1-value', | ||
'$ns:attr2': 'a2-value', | ||
'$ns2:attr3': 'a3-value', | ||
'$ns2:attr4': 'a4-value', | ||
tag2: { | ||
'$ns:attr1': 'a1-value', | ||
'$ns:attr2': 'a2-value', | ||
'$ns2:attr3': 'a3-value', | ||
'$ns2:attr4': 'a4-value', | ||
} | ||
} | ||
} | ||
|
||
describe("XMLParser", function () { | ||
|
||
it('must ignore parsing attributes by array of strings', () => { | ||
|
||
const options = { | ||
attributeNamePrefix: "$", | ||
ignoreAttributes: ['ns:attr1', 'ns:attr2'], | ||
parseAttributeValue: true | ||
}; | ||
const parser = new XMLParser(options); | ||
expect(parser.parse(xmlData)).toEqual({ | ||
tag: { | ||
'#text': 'value', | ||
'$ns2:attr3': 'a3-value', | ||
'$ns2:attr4': 'a4-value', | ||
}, | ||
}) | ||
|
||
expect(XMLValidator.validate(xmlData)).toBe(true); | ||
}) | ||
|
||
it('must ignore parsing attributes by array of RegExp', () => { | ||
|
||
const options = { | ||
attributeNamePrefix: "$", | ||
ignoreAttributes: [/^ns2:/], | ||
parseAttributeValue: true | ||
}; | ||
const parser = new XMLParser(options); | ||
expect(parser.parse(xmlData)).toEqual({ | ||
tag: { | ||
'#text': 'value', | ||
'$ns:attr1': 'a1-value', | ||
'$ns:attr2': 'a2-value', | ||
}, | ||
}) | ||
|
||
expect(XMLValidator.validate(xmlData)).toBe(true); | ||
}) | ||
|
||
it('must ignore parsing attributes via callback fn', () => { | ||
const xmlData = ` | ||
<tag | ||
ns:attr1="a1-value" | ||
ns:attr2="a2-value" | ||
ns2:attr3="a3-value" | ||
ns2:attr4="a4-value"> | ||
<tag2 | ||
ns:attr1="a1-value" | ||
ns:attr2="a2-value" | ||
ns2:attr3="a3-value" | ||
ns2:attr4="a4-value"> | ||
value | ||
</tag2> | ||
</tag>`; | ||
|
||
const options = { | ||
attributeNamePrefix: "$", | ||
ignoreAttributes: (aName, jPath) => aName.startsWith('ns:') || jPath === 'tag.tag2', | ||
parseAttributeValue: true | ||
}; | ||
const parser = new XMLParser(options); | ||
expect(parser.parse(xmlData)).toEqual({ | ||
tag: { | ||
'$ns2:attr3': 'a3-value', | ||
'$ns2:attr4': 'a4-value', | ||
tag2: 'value', | ||
}, | ||
}) | ||
|
||
expect(XMLValidator.validate(xmlData)).toBe(true); | ||
}) | ||
|
||
|
||
it('must ignore building attributes by array of strings', () => { | ||
|
||
const options = { | ||
attributeNamePrefix: "$", | ||
ignoreAttributes: ['ns:attr1', 'ns:attr2'], | ||
parseAttributeValue: true | ||
}; | ||
const builder = new XMLBuilder(options); | ||
expect(builder.build(jsonData)).toEqual('<tag ns2:attr3="a3-value" ns2:attr4="a4-value"><tag2 ns2:attr3="a3-value" ns2:attr4="a4-value"></tag2></tag>') | ||
|
||
expect(XMLValidator.validate(xmlData)).toBe(true); | ||
}) | ||
|
||
it('must ignore building attributes by array of RegExp', () => { | ||
|
||
const options = { | ||
attributeNamePrefix: "$", | ||
ignoreAttributes: [/^ns2:/], | ||
parseAttributeValue: true | ||
}; | ||
const builder = new XMLBuilder(options); | ||
expect(builder.build(jsonData)).toEqual('<tag ns:attr1="a1-value" ns:attr2="a2-value"><tag2 ns:attr1="a1-value" ns:attr2="a2-value"></tag2></tag>') | ||
|
||
expect(XMLValidator.validate(xmlData)).toBe(true); | ||
}) | ||
|
||
it('must ignore building attributes via callback fn', () => { | ||
|
||
const options = { | ||
attributeNamePrefix: "$", | ||
ignoreAttributes: (aName, jPath) => aName.startsWith('ns:') || jPath === 'tag.tag2', | ||
parseAttributeValue: true | ||
}; | ||
const builder = new XMLBuilder(options); | ||
expect(builder.build(jsonData)).toEqual('<tag ns2:attr3="a3-value" ns2:attr4="a4-value"><tag2></tag2></tag>') | ||
|
||
expect(XMLValidator.validate(xmlData)).toBe(true); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.