michaelkourlas / node-js2xmlparser

Popular Node.js module for parsing JavaScript objects into XML
https://www.npmjs.org/package/js2xmlparser
Apache License 2.0
222 stars 52 forks source link

typeHandlers not working for fields in `@` #97

Closed soulchild closed 2 years ago

soulchild commented 2 years ago

Is the following supposed to still leave bla="undefined" in the resulting XML when an attribute is undefined within an @ object? I was expecting the typeHandler to remove the attribute altogether, like in the second example.

const { parse, Absent } = require('js2xmlparser');

const opts = {
  format: {
    pretty: true,
    doubleQuotes: true,
  },
  typeHandlers: {
    '[object Undefined]': () => Absent.instance,
  },
};

console.log(
  parse(
    'Foo',
    {
      foo: {
        '@': {
          bla: undefined,
        },
      },
    },
    opts,
  ),
);

// <?xml version="1.0"?>
// <Foo>
//    <foo bla="undefined"/>
// </Foo>

console.log(
  parse(
    'Foo',
    {
      foo: {
        bla: undefined,
      },
    },
    opts,
  ),
);

// <?xml version="1.0"?>
// <Foo>
//    <foo/>
// </Foo>
michaelkourlas commented 2 years ago

This is what I would expect as well. I will fix this in the next major release.

soulchild commented 2 years ago

Works perfectly! Thanks @michaelkourlas!