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

Bug: arrays of arrays get flattened #1

Open
giacecco opened this issue Nov 11, 2013 · 4 comments
Open

Bug: arrays of arrays get flattened #1

giacecco opened this issue Nov 11, 2013 · 4 comments

Comments

@giacecco
Copy link

See the example below.

var jpath = require("node-jpath"),
    test = { data: [ [ 1, 2 ], [ 3, 4 ]] }; 
console.log(jpath.filter(test, "data"));

I would expect that to return:

[ [1, 2], [3, 4] ]

Instead, it returns:

[ 1, 2, 3, 4 ]

Is that intentional? Thanks.

Giacecco

@stsvilik
Copy link
Owner

Yes it is intentional. It would be a lot more difficult to handle returned values if they where broken down by their parent containers. Flattened output ensures consistent handling regardless of where the match was found.
Your example is pretty simple, may not even require filtering, but when it gets several levels deep thats where flattening comes in handy.

@giacecco
Copy link
Author

I understand your point, although it still is somehow 'conceptually wrong' in my opinion.

Let me try to change your mind :-D

My example was simple just not to require you to study the more complicated source I am using jpath in (http://dico.im/180640w). I am using jpath to 'subset' the responses I get from calling a commercial API. As such, I have no control on the design of the API whatsoever. Unfortunately the API designers decided to return its results in that array of arrays format for one specific API call.

By unsolicitedly flattening the array, jpath 'breaks' the format of the original API, hence its results do not correspond any longer to the documentation, which in turn creates unexpected results in the client application.

This is a problem for me, but also for any other future user of jpath who will find herself in my same situation.

What do you reckon? If your main motivation in writing jpath is just your own use, of course I can't argue with that. But if your objective is to offer a universal - and incredibly useful by the way - XPath-like filter, then you can't arbitrarily change the format of the input object because it is 'simpler'.

In any case, great work. I know that I may fork your project and make it work the way I believe is right, but I have not been a developer for many years and it would take days to me. I really hope I can convince you :-)

Giacecco

@stsvilik
Copy link
Owner

Ok, I'll take a look at it again :) This code is such a mind-hog to consume yourself in.

@giacecco
Copy link
Author

Thanks, and I can imagine how tricky the source must be. Let me know if I can help somewhere it is more suitable to a newbie.

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

2 participants