Query

Utility functions for managing search and filter contexts. It can be used for:

  • User typed text containing simple text terms, which might be quoted. For instance, name or 'search term'.
  • User typed text containing faceted filter terms. For instance, name:value.
  • Combined text terms, faceted filters, and boolean logic. The boolean logic available includes AND, OR, NOT, and parentheses for grouping.

NOTE: This component comes from the grommet-addons repository. You will need to add a dependency to that repository in order to use it.

Constructor

string
Typically, this is the user typed text string.

Methods

error ({object})
An exception object indicating syntactical issues with parsing.
set ({string})
Replace the current text string.
toString ()
Gets the current text string.
tree ()
Returns a boolean logic tree object. This permits syntax highlighting and programmatic processing. Connecting nodes are of the form { op: and|or, left: , right: }. Leaf nodes are of the form { text: , name:, value: , not: true|false }

Examples

Boolean Logic

let query = new Query('(a OR b) AND (n:v OR n:w) "big deal" NOT 01:23:45:67:89:ab 2016-01-31T16:45:46Z');
query.toString();
>> (a OR b) AND (n:v OR n:w) "big deal" NOT 01:23:45:67:89:ab 2016-01-31T16:45:46Z
query.error();
>> undefined
query.tree();
>> {
  "left": {
    "left": {
      "text": "a"
    },
    "op": "OR",
    "right": {
      "text": "b"
    }
  },
  "op": "AND",
  "right": {
    "left": {
      "text": "n:v OR n:w",
      "name": "n",
      "value": "v OR n"
    },
    "right": {
      "left": {
        "text": "big deal"
      },
      "right": {
        "text": "01:23:45:67:89:ab 2016-01-31T16:45:46Z",
        "name": "01",
        "value": "23",
        "not": true
      },
      "op": "AND"
    },
    "op": "AND"
  }
}

Syntax error

let errorQuery = new Query('a b "');
errorQuery.toString();
>> a b "
errorQuery.error();
>> Syntax error at character 4: "