Skip to content

Support for chaining multiple 'or' / 'and' operators #16

@Hughp135

Description

@Hughp135

I was surprised to find out that you cannot chain multiple and / or conditions after each other?

For example, this results in a syntax error:

// Input:
[
  {"name": "Chris", "age": 23, "city": "New York"},
  {"name": "Emily", "age": 19, "city": "Atlanta"},
  {"name": "Joe", "age": 32, "city": "New York"},
  {"name": "Kevin", "age": 19, "city": "Atlanta"},
  {"name": "Michelle", "age": 27, "city": "Los Angeles"},
  {"name": "Robert", "age": 45, "city": "Manhattan"},
  {"name": "Sarah", "age": 31, "city": "New York"}
]

// Failing queries:
filter((.city == "New York") and (.age > 30) and (.name == "Sarah"))
filter((.city == "New York") or (.age > 30) or (.name == "Sarah"))


// Result:
SyntaxError: Character ',' expected (pos: 45)

The only way I found around this was to nest the conditions resulting in ugly queries:

filter(
  or (
    (.city == "New York") or (.age > 30),
    (.name == "Sarah")
  )
)

Or for a more verbose example:

filter(
  or (
    (.city == "New York"),
    or (
      (.name == "Sarah"),
     or (
        (.name == "Emily"),
        or (
          (.name == "Kevin"),
          or (
            (.name == "Michelle")
          )
        )
      )
    )
  )
)

Surely this isn't how people are expected to use the library? So I think if the or and and functions could support multiple parameters, it would be a lot simpler.

Suggested Changes:

// Should be valid:
(.name == "Sarah") or (.name == "Emily") or (.name == "Kevin")

// Should also be valid:
or (
  (.name == "Sarah"),
  (.name == "Emily"),
  (.name == "Kevin")
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions