Bug: arrays of arrays get flattened
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
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.
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
Ok, I'll take a look at it again :) This code is such a mind-hog to consume yourself in.
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.