Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.87k stars 601 forks source link

Feature/builder attribute name pattern #596

Closed bigbri64 closed 3 years ago

bigbri64 commented 3 years ago

Added property attrpattern to BuilderOptions

attrpattern can be a regular expression or string that represents an Object key name pattern that indicates an XML attribute.

This feature enables support for attribute names as merged properties:

as JSON

{
  "ElementWithId" : {
    "_id": "id",
    "prop2": "prop2"
  }
}

as XML

<ElementWithId id="id">
  <prop2>prop2</prop2>
</ElementWithId>

Example BuilderOptions

const XML_BUILDER_OPTIONS: BuilderOptions = {
  attrpattern: /^_(.+)$/g,
  attrkey: '_',
  charkey: '#'
};

Example ParserOptions

const XML_PARSER_OPTIONS: ParserOptions = {
  attrpattern: /^_(.+)$/g,
  attrkey: '_',
  charkey: '#',
  explicitArray: false,
  mergeAttrs: true,
  attrNameProcessors: [
    (name: string) => {
      const attrpattern = /^_(.+)$/g;
      const attrkey = '_';
      return name.match(attrpattern) ? name : `${attrkey}${name}`;
    }
  ],
};
coveralls commented 3 years ago

Coverage Status

Coverage remained the same at 97.74% when pulling 2b2528aca0df34b4a27dd78586161e9e4a9bd689 on bigbri64:feature/builder-attribute-name-pattern into 1832e0b6b2de30a5e326d1cf21708cd32305a538 on Leonidas-from-XIV:master.

coveralls commented 3 years ago

Coverage Status

Coverage remained the same at 97.74% when pulling 959ffa5faec6276eff6d51981ae723102709b45e on bigbri64:feature/builder-attribute-name-pattern into 1832e0b6b2de30a5e326d1cf21708cd32305a538 on Leonidas-from-XIV:master.

franciscohanna92 commented 3 years ago

Why was this PR not merge? I think it's similar to what I'm proposing in #606

Leonidas-from-XIV commented 3 years ago

Because I don't have the time to review and maintain this code at the moment. I am not particularly fond of the current code base and don't want to invest additional effort when rewriting it would be more sensible.

bigbri64 commented 2 years ago

Because I don't have the time to review and maintain this code at the moment. I am not particularly fond of the current code base and don't want to invest additional effort when rewriting it would be more sensible.

Hi Marek! Thank you for writing this utility. I have used it to build a data type mapping library that supports the ACORD reference architecture (insurance related data model). What did you have in mind for re-architecture? I am curious to know if it something I might be able to help with?

Thanks!