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

are we able to manipulate none compact JS like compact JS? #173

Open
lengerrong opened this issue Aug 12, 2021 · 1 comment
Open

are we able to manipulate none compact JS like compact JS? #173

lengerrong opened this issue Aug 12, 2021 · 1 comment

Comments

@lengerrong
Copy link

given below xml:

<?xml version="1.0" encoding="utf-8"?>
<note importance="high" logged="true">
    <title>Happy</title>
    <todo>Work</todo>
    <todo>Play</todo>
</note>

if we parse with {compact: false} option, we will get none compact JS object:

{
  declaration: { attributes: { version: '1.0', encoding: 'utf-8' } },
  elements: [
    {
      type: 'element',
      name: 'note',
      attributes: [Object],
      elements: [Array]
    }
  ]
}

For this JS object, are we able to manipulate it as a compact JS?
js['note']['title']

Maybe use Proxy & Reflect in xml2js if {compact:false} applied?

@lengerrong
Copy link
Author

new Proxy(result1, {
        get: (target, propKey, receiver) => {
                let type = Reflect.get(target, 'type', receiver);
                if (type === 'element' && propKey !== 'attributes' || !type && propKey !== 'declaration' && propKey !== 'elements') {
                        return Reflect.get(target, 'elements', receiver).filter(e => e.name === propKey);
                } else {
                        return Reflect.get(target, propKey, receiver);
                }
        }
});

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

No branches or pull requests

1 participant