Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.88k stars 604 forks source link

Add custom node post-processing support #642

Open AHarman opened 2 years ago

AHarman commented 2 years ago

The purpose of this is to pass a list of processors that takes nodes, once xml2js has created them, and performs custom post-processing on them.

I was able to get some processing functionality working by writing a validator that manipulated the data but that seemed unintuitive and currently you can only have one validator function per parser config.

This isn't a breaking change and I've added a test case to cover it. The defaults set the array of processors to null so it won't affect anyone unless they explicitly add processors in.

I appreciate that the two-function way I've written might not be a bit too verbose. If you prefer I can change it to be a simple array of functions. For an example of this, here's the sample processor I put in the readme:

{
  canProcess: function(node) {
    return node?.$?.type === "integer";
  },
  process: function(node) {
    return Number.parseInt(node._);
  }
}

And here's the alternative approach:

function(node) {
  if (node?.$?.type === "integer")
    return Number.parseInt(node._);
  return node;
}

Let me know if this is preferable, or if there are any other changes you'd like to see on this.