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

options.arrayMode support #135

Closed
wants to merge 5 commits into from
Closed

options.arrayMode support #135

wants to merge 5 commits into from

Conversation

iduchy
Copy link

@iduchy iduchy commented Jan 13, 2019

the arrayMode option enebles all child elements to be in an array. It improves queries in NoSQL

the arrayMode option enebles all child elements to be in an array. It improves queries in NoSQL
@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.148% when pulling 8f3342e on iduchy:master into 1c4ea5c on NaturalIntelligence:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.148% when pulling 8f3342e on iduchy:master into 1c4ea5c on NaturalIntelligence:master.

@amitguptagwl amitguptagwl added help wanted Feature-Request New features suggested by users and removed help wanted Feature-Request New features suggested by users labels Jan 14, 2019
@amitguptagwl
Copy link
Member

Thanks for your PR. But this feature requires more discussion. Eg what would be the expected output for different conditions and tests to ensure that we achieve all targeted features. This feature was previously implemented and removed due to lack of clarity. Please search for closed issues.

@iduchy
Copy link
Author

iduchy commented Jan 14, 2019

I needed a feature to make all child elements be within an array. The default behavior adds a child as an object if its parent element has only one child:

parentTagName:{
     childTagname: {...}, //single element
     childTagname2: [{...},{...},{...},...], //multiple elements
     ...
}

This requires checking all the time a type of all children to make queries:

for (const k in parentElement){
    if ( [<textKey>, <attrKey>].includes(d) ) continue;
    if ( ! Array.isArray(parentElement[k]) parentElement[k] = [parentElement[k]] //<--------- !!!!
    ...
}

The difference matters a lot in NoSQL queries for data research (performace and not all db's ql support type checking).

I had not founded any implemented feature for the arrayMode option and i've bound the fetaure to the option. Expected behavior:

parentTagName:{
     childTagname: [{...}], //single element
     childTagname2: [{...},{...},{...},...], //multiple elements
     ...
}

@amitguptagwl
Copy link
Member

What would be the expected output for this?

<root>
    <item id="a">
        <sub>1</sub>
        <sub>2</sub>
    </item>
    <item id="b">
        <sub>3</sub>
    </item>
</root>

Can you please test your changes against sample.xml? And please add some unit tests to validate your expectations.

@amitguptagwl
Copy link
Member

  1. Can you plz rename your test file and append _spec in last? (just like other test files)
  2. Can you plz add the tests using jasmine format? Here is the sample
describe("XMLParser", function() {
    it("should parse attributes with valid names", function() {
        const xmlData = `<some>valid XML data</some>`;
        const expected = {
            "expected": "js object"
        };

        let result = parser.parse(xmlData, {
            attributeNamePrefix: "",
            ignoreAttributes:    false,
            parseAttributeValue: true,
            arrayMode: true
        });

        //console.log(JSON.stringify(result,null,4));
        expect(result).toEqual(expected);

        result = validator.validate(xmlData);
        expect(result).toBe(true);
   });
});

With the test file you've created what I can understand is;

input

<root foo="bar">
    <item id="a">
        <sub>1</sub>
        <sub>2</sub>
    </item>
    <item id="b">
        <sub>3</sub>
    </item>
    <item2 id="b">
        <sub>3</sub>
    </item2>
</root>

output

{
    root:[{ 
        "@_foo": "bar",
        item:[ 
            { '@_id': 'a', sub: [ 1, 2 ] }, 
            { '@_id': 'b', sub: [ 3 ] } 
        ] ,
        item2: [{
            '@_id': 'b', sub: [ 3 ] 
        }]
    }] 
}
  • root element would not be bound in an array.
  • an array will have single object which will be having properties for attributs, text Data, cdata, and other tags
  • if there is a leaf node, it will be having an array of values
  • no detail about self closing tag?

Can you please generate the output against sample.xml mentioned in last comment? Because it has most of the scenarios.

@iduchy
Copy link
Author

iduchy commented Jan 16, 2019

ok, it is more complicated than i expexted. I'm going to close the pull request and open an issue soon

@iduchy iduchy closed this Jan 16, 2019
@amitguptagwl
Copy link
Member

As I previously suggested, there are closed issues on the same topic. You can take their reference too to understand more scenarios. :)

@ralyodio
Copy link

ralyodio commented Apr 3, 2019

Is there any progress? I too would like a "forceArray: true" for child elements.

@amitguptagwl
Copy link
Member

@chovy we need to finalize the expected result for each scenario. As that is not final yet, there is no progress.

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

Successfully merging this pull request may close these issues.

4 participants