xml-to-react icon indicating copy to clipboard operation
xml-to-react copied to clipboard

Allow coercion of XML node attribute values

Open taveras opened this issue 7 years ago • 1 comments

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

taveras avatar Dec 14 '17 16:12 taveras

I was thinking we'd:

  1. test against 'undefined'
  2. call JSON.parse()

anything else? XML doesn't have implicit boolean attributes like HTML (or am i crazy?)

pgoldrbx avatar Dec 18 '17 16:12 pgoldrbx