Skip to content

Commit e5e9ddd

Browse files
committed
Add note about transformQuery for mapping custom combinators/operators
1 parent d240910 commit e5e9ddd

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

docs/api/export.mdx

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,49 @@ const query: RuleGroupType = {
5050
};
5151
```
5252

53+
:::tip
54+
55+
_<abbr title="Too long; didn't read">TL;DR</abbr>: For best results, use the [default combinators and operators](./misc#defaults) or map custom combinators/operators to the defaults with [`transformQuery`](./misc#transformquery)._
56+
57+
While `formatQuery` technically accepts query objects of type `RuleGroupTypeAny` (i.e. `RuleGroupType` or `RuleGroupTypeIC`), it is not guaranteed to process a query correctly unless the query also conforms to the type `DefaultRuleGroupTypeAny` (i.e. `DefaultRuleGroupType` or `DefaultRuleGroupTypeIC`).
58+
59+
In practice, this means that all `combinator` and `operator` properties in the query must match the `name` of an element in [`defaultCombinators` or `defaultOperators`](./misc#defaults), respectively. If you implement custom combinator/operator names, you can use the [`transformQuery` function](./misc#transformquery) to map your query properties to the defaults.
60+
61+
For example, assume your implementation replaces the default "between" operator (`{ name: "between", label: "between" }`) with `{ name: "b/w", label: "b/w" }`. Any rules using this operator would have `operator: "b/w"` instead of `operator: "between"`. So if a query looked like this...
62+
63+
```json
64+
{
65+
"combinator": "and",
66+
"rules": [
67+
{
68+
"field": "someNumber",
69+
"operator": "b/w",
70+
"value": "12,14"
71+
}
72+
]
73+
}
74+
```
75+
76+
...you could run it through `transformQuery` with the `operatorMap` option:
77+
78+
```ts
79+
const newQuery = transformQuery(query, { operatorMap: { 'b/w': 'between' } });
80+
// {
81+
// "combinator": "and",
82+
// "rules": [
83+
// {
84+
// "field": "someNumber",
85+
// "operator": "between",
86+
// "value": "12,14"
87+
// }
88+
// ]
89+
// }
90+
```
91+
92+
The `newQuery` object would be ready for processing by `formatQuery`, including its special handling of the "between" operator.
93+
94+
:::
95+
5396
## Basic usage
5497

5598
### JSON
@@ -260,17 +303,17 @@ To avoid information loss, this option is more strict about what qualifies as "n
260303
The following expressions all evaluate to `true`:
261304

262305
```ts
263-
parseFloat('000111abcdef') === 111;
306+
parseFloat('000111abcdef') === 111; // everything from the 'a' on is ignored by `parseFloat`
264307

265308
formatQuery(
266309
{ rules: [{ field: 'f', operator: '=', value: '000111abcdef' }] },
267310
{ format: 'sql', parseNumbers: true }
268-
) === "(f = '000111abcdef')";
311+
) === "(f = '000111abcdef')"; // `value` contains non-numeric characters, so remains as-is
269312

270313
formatQuery(
271314
{ rules: [{ field: 'f', operator: '=', value: ' 000111 ' }] },
272315
{ format: 'sql', parseNumbers: true }
273-
) === '(f = 111)';
316+
) === '(f = 111)'; // after trimming whitespace, `value` is wholly numeric
274317
```
275318

276319
:::

0 commit comments

Comments
 (0)