xml-to-react
xml-to-react copied to clipboard
Allow coercion of XML node attribute values
We currently require our consumers to coerce and validate their attributes themselves within their converter callbacks.
It may be beneficial to coerce the type of each XML node attribute value for use within a converter callback.
e.g.,
<Party>
<Person name="Mary" age="20" ready="true" />
</Party>
const xmlToReact = new XMLToReact({ Person: convertPerson });
function convertPerson (attributes) {
typeof attributes.name; // 'string'
typeof attributes.age; // 'string'
typeof attributes.ready; // 'string'
return { type: 'div', props: attributes };
}
The value of each XML node attribute is a string. If we coerce each the type of each value, we would instead the following:
function convertPerson (attributes) {
typeof attributes.name; // 'string'
typeof attributes.age; // 'number'
typeof attributes.ready; // 'boolean'
return { type: 'div', props: attributes };
}
for boolean attributes, we should consider HTML's approach
related: https://github.com/CondeNast/xml-to-react/pull/16#discussion_r156202695
I was thinking we'd:
- test against
'undefined' - call
JSON.parse()
anything else? XML doesn't have implicit boolean attributes like HTML (or am i crazy?)