dfilatov / jspath

DSL that enables you to navigate and find data within your JSON documents
MIT License
553 stars 40 forks source link

Difference between multiple predicates and the logical AND operator #48

Closed antstei closed 8 years ago

antstei commented 8 years ago

Consider the following example JSON:

var doc = {
    "books" : [
            {
            "id"     : 1,
            "title"  : "Clean Code",
            "author" : { "name" : "Robert C. Martin" },
            "price"  : 7
        },
        {
            "id"     : 2,
            "title"  : "Maintainable JavaScript",
            "author" : { "name" : "Nicholas C. Zakas" },
            "price"  : 12
        },
        {
            "id"     : 3,
            "title"  : "Agile Software Development",
            "author" : { "name" : "Robert C. Martin" },
            "price"  : 15
        }
    ]
};

I wonder now if there is a difference between the usage of multiple predicates,

JSPath.apply('.books{.price < 15}{.price > 5}.title', doc);
//yields ["Clean Code", "Maintainable JavaScript"]

or the logical AND operator (&&),

JSPath.apply('.books{.price < 15 && .price > 5}.title', doc);
//yields ["Clean Code", "Maintainable JavaScript"]

to find all books whose price is between 5 and 15?

dfilatov commented 8 years ago

There's no difference. The result will be the same.

antstei commented 8 years ago

Ok thanks, I see :smile: On the other hand why should multiple predicates still be used when there is an equivalent logical operator which leads to the same result? :confused:

dfilatov commented 8 years ago

There might be more complicated cases which can be resolved only using multiple predicates, for instance: .a{.b}[0]{.c}

antstei commented 8 years ago

Thanks I see :smile: