Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom node post-processing support #642

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AHarman
Copy link

@AHarman AHarman commented Apr 17, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant