astwerk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +348 -2
- package/dist/index.d.ts +374 -0
- package/dist/index.js +1764 -0
- package/package.json +52 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ahmet Tinastepe
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,349 @@
|
|
|
1
|
-
#
|
|
1
|
+
# astwerk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/tinas/astwerk/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/tinas/astwerk/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
A filter query language for TypeScript, inspired by GitHub search. People type a filter such as `is:open label:bug,ui amount:10..40`, or pick one from menus that write the same text. astwerk parses that text into a syntax tree for editors, binds it to your schema as a typed query for filter panels and backends, and writes every filter back in one canonical form, so a filter fits in a URL as a link anyone can open. It has no dependencies and runs wherever JavaScript runs, in the browser and on the server.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pnpm add astwerk
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quick Start
|
|
15
|
+
|
|
16
|
+
A schema names the fields a query can filter and the type of each. With it, the text of a query becomes a typed query that filters a list, and any text can be written back in its canonical form.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { bind, canonical, date, defineSchema, enumOf, number, parse, string, toPredicate } from 'astwerk'
|
|
20
|
+
|
|
21
|
+
interface Issue {
|
|
22
|
+
title: string
|
|
23
|
+
state: 'open' | 'closed'
|
|
24
|
+
labels: Array<string>
|
|
25
|
+
comments: number
|
|
26
|
+
created: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const issueSchema = defineSchema({
|
|
30
|
+
fields: {
|
|
31
|
+
is: { type: enumOf(['open', 'closed']), get: (issue: Issue) => issue.state },
|
|
32
|
+
label: { type: string(), get: issue => issue.labels },
|
|
33
|
+
comments: { type: number(), get: issue => issue.comments },
|
|
34
|
+
created: { type: date(), get: issue => issue.created },
|
|
35
|
+
},
|
|
36
|
+
text: issue => issue.title,
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const { tree } = parse('is:open label:bug,docs comments:>10 crash')
|
|
40
|
+
const { query } = bind(tree, issueSchema)
|
|
41
|
+
const busyBugs = issues.filter(toPredicate(query, issueSchema))
|
|
42
|
+
|
|
43
|
+
canonical('is:OPEN AND (label:bug OR label:docs)', issueSchema) // 'is:open label:bug,docs'
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The text is the source of truth. Keep it in the URL, and derive the tree, the query and the state of a filter panel from it whenever it changes.
|
|
47
|
+
|
|
48
|
+
## Syntax
|
|
49
|
+
|
|
50
|
+
A query is made of terms and words. A term filters one field, such as `label:bug`, and a word outside a term searches the free text.
|
|
51
|
+
|
|
52
|
+
```text
|
|
53
|
+
is:open label:bug,ui -assignee:ahmet amount:10..40 (priority:high OR no:assignee) "release notes"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
| Query | Meaning |
|
|
57
|
+
| -------------------------------------------------------- | ---------------------------------------------------------------------------- |
|
|
58
|
+
| `a b` | both `a` and `b`, and writing `AND` between them is allowed |
|
|
59
|
+
| `a OR b` | `a`, `b` or both. `AND` binds tighter, so `a b OR c` is `(a b) OR c` |
|
|
60
|
+
| `label:bug,ui` | either value of the field |
|
|
61
|
+
| `-label:bug`, `NOT label:bug` | everything without the value |
|
|
62
|
+
| `(a OR b) c` | a group |
|
|
63
|
+
| `has:label`, `no:label` | items with any label, or with none |
|
|
64
|
+
| `label:*bug*`, `title:api*`, `label:*fix` | a value that contains, starts with or ends with the text |
|
|
65
|
+
| `assignee:*` | any value, the same as `has:assignee` |
|
|
66
|
+
| `"two words"` | the quoted text as one value, with `..`, `>`, `,` and `*` read as plain text |
|
|
67
|
+
| `amount:10..40` | from 10 to 40, both included |
|
|
68
|
+
| `amount:>=10`, `amount:>10`, `amount:<40`, `amount:<=40` | a range with one bound |
|
|
69
|
+
| `amount:10..*`, `amount:*..40` | a range with an open end |
|
|
70
|
+
| `amount:<10,>100` | either range |
|
|
71
|
+
| `sprint:"Sprint 1".."Sprint 4"`, `sprint:<"Sprint 5"` | bounds with spaces, in quotes |
|
|
72
|
+
|
|
73
|
+
- Field names are written exactly as the schema names them, and a field written any other way is reported as unknown. Values match without regard to case, so `is:OPEN` finds what `is:open` finds.
|
|
74
|
+
- `has` and `no` take a field name as their value, so no field can be named `has` or `no`.
|
|
75
|
+
- `OR`, `AND` and `NOT` are operators only in capitals, so a lower-case `or` is free text.
|
|
76
|
+
- `-` and `NOT` negate one term or word. A `-` negates only at the start of a word, so `amount:-5..5` runs from -5 to 5.
|
|
77
|
+
- A value with spaces goes in quotes, such as `label:"in progress"` or `label:'in progress'`. Inside quotes, a backslash writes the quote (`\"`, `\'`) and a backslash (`\\`). A `'` inside a word, as in `don't`, is plain text.
|
|
78
|
+
- Numbers use a dot for decimals (`10.5`), because a comma separates values.
|
|
79
|
+
- Dates are ISO 8601, with an optional time (`2026-01-31`, `2026-01-31T14:30:00Z`), in UTC unless the time says otherwise. A day covers all of its hours, so `created:2026-01-31` is any time that day and `created:>2026-01-31` starts the day after.
|
|
80
|
+
- `@today`, `@today-7d` and `@today+2w` count from the current day, in days (`d`), weeks (`w`), months (`m`) or years (`y`), and a number without a unit counts days. Other `@` tokens, such as `@me`, mean what the app defines.
|
|
81
|
+
- A range splits at its first `..`, so `1.5..2` runs from 1.5 to 2.
|
|
82
|
+
|
|
83
|
+
A query with mistakes still works. An unclosed quote, a missing parenthesis or a term without a value is read as far as it goes. An unknown field or a value its field cannot take is reported as a diagnostic, stays in the text and filters nothing, so a typo never empties a list.
|
|
84
|
+
|
|
85
|
+
### Grammar
|
|
86
|
+
|
|
87
|
+
Whitespace between terms is an implicit `AND`. A missing `)` and an unclosed quote are tolerated, and `-` or `NOT` negates one word, never a group. Groups nest up to 32 deep, and a deeper parenthesis is reported and read as if it were not there.
|
|
88
|
+
|
|
89
|
+
```ebnf
|
|
90
|
+
query = orExpr ;
|
|
91
|
+
orExpr = andExpr { "OR" andExpr } ;
|
|
92
|
+
andExpr = { primary [ "AND" ] } ;
|
|
93
|
+
primary = "(" orExpr [ ")" ] | word ;
|
|
94
|
+
word = [ "-" | "NOT" ] ( term | text ) ;
|
|
95
|
+
term = key ":" valueList ;
|
|
96
|
+
valueList = value { "," value } ;
|
|
97
|
+
value = cmp | range | quoted | bare ;
|
|
98
|
+
cmp = ( ">=" | "<=" | ">" | "<" ) ( quoted | bare ) ;
|
|
99
|
+
range = [ bound ] ".." [ bound ] ;
|
|
100
|
+
bound = quoted | bare | "*" ;
|
|
101
|
+
quoted = '"' { '\"' | '\\' | any-but-double-quote } [ '"' ]
|
|
102
|
+
| "'" { "\'" | '\\' | any-but-single-quote } [ "'" ] ;
|
|
103
|
+
text = quoted | bare ;
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Canonical Form
|
|
107
|
+
|
|
108
|
+
astwerk writes every filter back in one form, so the same filter always gives the same text and the same link. Writing a canonical text again changes nothing, and the canonical text filters the same items as the text it came from.
|
|
109
|
+
|
|
110
|
+
- Spaces collapse to one, a written `AND` is dropped, and parentheses stay only where an `OR` sits inside an `AND`.
|
|
111
|
+
- A negation is written with `-`, so `NOT bug` becomes `-bug`, and `NOT NOT bug` becomes `bug`.
|
|
112
|
+
- `label:bug OR label:docs` becomes `label:bug,docs`, since a comma is an `OR` within one field.
|
|
113
|
+
- `assignee:*` and `-no:assignee` become `has:assignee`, and `-has:assignee` becomes `no:assignee`.
|
|
114
|
+
- An alias becomes the name of the field it stands for.
|
|
115
|
+
- A value is written the way its type writes it. An enum or boolean value takes the spelling of the schema, so `is:OPEN` becomes `is:open`, and a time becomes UTC.
|
|
116
|
+
- A value is quoted only when it would read differently without quotes, and always with double quotes. That covers a value with a space, a comma, a parenthesis, a quote or a `*`, a value that would read as a range, a comparison or an `@` token, and free text that is `OR`, `AND` or `NOT`, starts with `-` or contains a `:`.
|
|
117
|
+
- A range with both bounds included is written `a..b`, and a range with one bound `>=a`, `>a`, `<=b` or `<b`. Two bounds that differ in whether they are included become two terms.
|
|
118
|
+
- Ranges on one field in the same `AND` group are intersected, so `amount:>=10 amount:<=40` becomes `amount:10..40`. Ranges with nothing in common both stay, and a diagnostic reports them.
|
|
119
|
+
- Empty values and empty terms are dropped.
|
|
120
|
+
- Terms keep the order they were written in.
|
|
121
|
+
- Dynamic values such as `@today-7d` stay as written, so a link keeps meaning "the last seven days" whenever it is opened.
|
|
122
|
+
- Unknown fields and invalid values stay in the text.
|
|
123
|
+
|
|
124
|
+
## Schemas
|
|
125
|
+
|
|
126
|
+
`defineSchema` takes the fields a query can filter. Each field has a type, and may have a `label` and a `description` for suggestions, `aliases` it also answers to, and a `get` that reads its values from an item for `toPredicate`. Without `get`, a field reads the item's own property of its name.
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
const issueSchema = defineSchema({
|
|
130
|
+
fields: {
|
|
131
|
+
label: { type: string(), label: 'Label', aliases: ['labels'], get: (issue: Issue) => issue.labels },
|
|
132
|
+
milestone: { type: string(), label: 'Milestone' },
|
|
133
|
+
},
|
|
134
|
+
text: issue => issue.title,
|
|
135
|
+
})
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`get` may return one value, a list of values or nothing. A list matches when any of its values does, and an empty list or a missing value is what `no:field` finds. `text` gives the text that free words search, and without it free words filter nothing in `toPredicate`.
|
|
139
|
+
|
|
140
|
+
### Field Types
|
|
141
|
+
|
|
142
|
+
| Type | Values | Ranges | Wildcards |
|
|
143
|
+
| ---------------- | ------------------------------------------------------------------- | ------ | --------- |
|
|
144
|
+
| `string()` | any text, matched without regard to case | no | yes |
|
|
145
|
+
| `enumOf(values)` | one of the listed values, in any case, suggested as you type | no | yes |
|
|
146
|
+
| `number()` | a number with a dot for decimals | yes | no |
|
|
147
|
+
| `boolean()` | `true` or `false`, in any case | no | no |
|
|
148
|
+
| `date()` | a day or an instant, with `@today`. An item's value may be a `Date` | yes | no |
|
|
149
|
+
|
|
150
|
+
`string()`, `number()` and `date()` take `suggest`, a function from the typed prefix to suggested values that may return a promise, and `tokens`. `enumOf` takes `tokens`.
|
|
151
|
+
|
|
152
|
+
### Dynamic Values
|
|
153
|
+
|
|
154
|
+
A token is a value written with `@`, such as `@me`, that stands for something only known when the query is evaluated. A field type lists the tokens it takes, and the query keeps them as written until `toPredicate` or `resolveTokens` resolves them with a context.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
declare module 'astwerk' {
|
|
158
|
+
interface ResolveContext {
|
|
159
|
+
user?: string
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const person = string({
|
|
164
|
+
tokens: {
|
|
165
|
+
me: { description: 'The signed-in user', resolve: (_, context) => context.user ?? '' },
|
|
166
|
+
},
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
const issueSchema = defineSchema({
|
|
170
|
+
fields: {
|
|
171
|
+
assignee: { type: person, get: (issue: Issue) => issue.assignee },
|
|
172
|
+
author: { type: person, get: issue => issue.author },
|
|
173
|
+
},
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
issues.filter(toPredicate(query, issueSchema, { user: 'ahmet' }))
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
A token with `offsetUnits` takes an offset such as `@current+1` or `@today-7d`. `resolve` receives it as `{ amount, unit }`, and a written unit must be one of `offsetUnits`. The context always has `now`, the current time unless you pass another, and apps add their own members as above.
|
|
180
|
+
|
|
181
|
+
### Types of Your Own
|
|
182
|
+
|
|
183
|
+
Any object with `parse` and `format` is a field type. `parse` returns `undefined` for text that is not a value, which reports an invalid value. `compare` orders values and lets the field take comparisons and ranges, `equals` decides `field:value` when it differs from `compare`, and `wildcards: true` lets an unquoted `*` stand for any text.
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
const priority: FieldType<number> = {
|
|
187
|
+
parse: raw => (/^p\d$/i.test(raw) ? Number(raw.slice(1)) : undefined),
|
|
188
|
+
format: value => `p${value}`,
|
|
189
|
+
compare: (a, b) => a - b,
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Fields That Stand for Others
|
|
194
|
+
|
|
195
|
+
A field with `expand` is a shorthand. When a query filters, each of its conditions is replaced by the query `expand` returns, while the text keeps the field as written. A field whose `expand` returns `undefined` filters nothing, which suits an option such as `sort:`, and `valuesOf` reads its value for the whole query.
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
const sharedFields = {
|
|
199
|
+
involves: {
|
|
200
|
+
type: person,
|
|
201
|
+
expand: (condition: FieldCondition): Query => ({
|
|
202
|
+
type: 'or',
|
|
203
|
+
children: [
|
|
204
|
+
{ ...condition, field: 'author' },
|
|
205
|
+
{ ...condition, field: 'assignee' },
|
|
206
|
+
],
|
|
207
|
+
}),
|
|
208
|
+
},
|
|
209
|
+
sort: { type: enumOf(['created', 'comments']), expand: () => undefined },
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const issueSchema = defineSchema({ fields: { ...sharedFields, assignee: { type: person }, author: { type: person } } })
|
|
213
|
+
|
|
214
|
+
valuesOf(bind(parse('sort:comments involves:@me').tree, issueSchema).query, 'sort') // ['comments']
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Fields, their types and their tokens are plain values, so schemas share them by spreading the same objects.
|
|
218
|
+
|
|
219
|
+
## Parsing
|
|
220
|
+
|
|
221
|
+
`parse` reads any text into a syntax tree, and never throws. Every node has a `span`, the positions in the text it was read from, and terms keep their values as written, so the tree serves editors without a schema.
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
const { tree, diagnostics } = parse('label:"in progress')
|
|
225
|
+
// tree: { type: 'term', key: 'label', values: [{ kind: 'equal', raw: 'in progress', quoted: true, … }], … }
|
|
226
|
+
// diagnostics: [{ code: 'unclosedQuote', severity: 'warning', from: 6, to: 18 }]
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
`bind` applies a schema to the tree and reports what the schema cannot read. Show the diagnostics of both together.
|
|
230
|
+
|
|
231
|
+
| Code | Severity | When |
|
|
232
|
+
| ----------------------- | --------- | ---------------------------------------------------------------- |
|
|
233
|
+
| `unknownField` | `error` | a key, or a field named in `has:` or `no:`, is not in the schema |
|
|
234
|
+
| `invalidValue` | `error` | the field type cannot read a value |
|
|
235
|
+
| `unknownToken` | `error` | the field type has no such token, or not with that offset |
|
|
236
|
+
| `unsupportedOperator` | `error` | a comparison, range or wildcard on a type that does not take it |
|
|
237
|
+
| `negatedGroup` | `error` | `-` or `NOT` before a group |
|
|
238
|
+
| `unclosedQuote` | `warning` | a quote is not closed |
|
|
239
|
+
| `unbalancedParenthesis` | `warning` | a parenthesis is not closed, or closes nothing |
|
|
240
|
+
| `nestingTooDeep` | `warning` | groups nest more than 32 deep |
|
|
241
|
+
| `emptyRange` | `warning` | ranges on one field have nothing in common |
|
|
242
|
+
|
|
243
|
+
An `error` marks a part that filters nothing, and a `warning` text that filters but maybe not as meant. Diagnostics carry no message, so the app words them from `code`, and `from` and `to` mark the text to underline.
|
|
244
|
+
|
|
245
|
+
## Editors
|
|
246
|
+
|
|
247
|
+
`highlight` gives the spans to color, in order, with a kind for each: `key`, `value`, `dynamic` for a token, `text`, `keyword` for `OR`, `AND` and `NOT`, `negation`, `comparison`, `range`, `separator` and `paren`.
|
|
248
|
+
|
|
249
|
+
`getCompletionContext` tells what the cursor is on. On a word that may become a field name it gives a `key` context, and on a value a `value` context with the field's key, the values already in the term and any comparison written before the value. Pass the context to `getSuggestions` for the suggestions, whose `insertText` replaces the text from `from` to `to`, quoted where the value needs it.
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
const context = getCompletionContext('is:open,c', 9)
|
|
253
|
+
// { kind: 'value', key: 'is', prefix: 'c', from: 8, to: 9, taken: ['open'], atEnd: true }
|
|
254
|
+
|
|
255
|
+
if (context != undefined) {
|
|
256
|
+
const suggestions = await getSuggestions(context, issueSchema)
|
|
257
|
+
// [{ label: 'closed', insertText: 'closed' }]
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
`getSuggestions` returns a promise only when the field type's `suggest` does, so an editor that offers the latest answer should drop the answers to older prefixes. Inside quoted free text there is no context to complete.
|
|
262
|
+
|
|
263
|
+
## Queries
|
|
264
|
+
|
|
265
|
+
`bind` returns the query as a tree of these parts. Values are the field type's values, and a token stays a `TokenValue` until it is resolved, which `isTokenValue` tells apart.
|
|
266
|
+
|
|
267
|
+
| Type | Means |
|
|
268
|
+
| ----------- | --------------------------------------------------------------------- |
|
|
269
|
+
| `and`, `or` | all or any of `children` |
|
|
270
|
+
| `not` | not `child` |
|
|
271
|
+
| `in` | the field has any of `values` |
|
|
272
|
+
| `range` | the field is above `min` and below `max`, each `inclusive` or not |
|
|
273
|
+
| `text` | a free word or phrase |
|
|
274
|
+
| `empty` | the field has no value |
|
|
275
|
+
| `match` | the field has a value that fits `pattern`, where each `*` is any text |
|
|
276
|
+
| `invalid` | text that could not be bound, kept as written, filtering nothing |
|
|
277
|
+
|
|
278
|
+
`stringify` writes a query as canonical text, so a query built in code becomes a link like any other. `canonical` is `parse`, `bind` and `stringify` in one call.
|
|
279
|
+
|
|
280
|
+
### Filtering in Memory
|
|
281
|
+
|
|
282
|
+
`toPredicate` turns a query into a function for `Array.prototype.filter`. Values match without regard to case, a day covers all of its hours, and a free word matches the start of a word in the schema's `text`, so `release` finds "release notes" and `elease` does not.
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
const keep = toPredicate(query, issueSchema, { user: 'ahmet', now: new Date() })
|
|
286
|
+
const shown = issues.filter(keep)
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Backends
|
|
290
|
+
|
|
291
|
+
`fold` turns a query into a backend's query, one function per part, after leaving out the parts that could not be bound and replacing fields with `expand`. A query with nothing left folds as an empty `and`. Map field names to columns of your own and pass values as parameters, since the text of a query is user input.
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
const COLUMNS: Record<string, string> = { is: 'state', label: 'labels.name', comments: 'comment_count' }
|
|
295
|
+
|
|
296
|
+
function toSql(query: Query): { where: string; params: Array<unknown> } {
|
|
297
|
+
const params: Array<unknown> = []
|
|
298
|
+
const param = (value: unknown) => `$${params.push(value)}`
|
|
299
|
+
const where = fold<string>(resolveTokens(query, issueSchema, { user: currentUser }), issueSchema, {
|
|
300
|
+
and: parts => (parts.length == 0 ? 'TRUE' : `(${parts.join(' AND ')})`),
|
|
301
|
+
or: parts => `(${parts.join(' OR ')})`,
|
|
302
|
+
not: part => `NOT ${part}`,
|
|
303
|
+
in: ({ field, values }) => `${COLUMNS[field]} IN (${values.map(param).join(', ')})`,
|
|
304
|
+
range: ({ field, min, max }) =>
|
|
305
|
+
[
|
|
306
|
+
min && `${COLUMNS[field]} ${min.inclusive ? '>=' : '>'} ${param(min.value)}`,
|
|
307
|
+
max && `${COLUMNS[field]} ${max.inclusive ? '<=' : '<'} ${param(max.value)}`,
|
|
308
|
+
]
|
|
309
|
+
.filter(Boolean)
|
|
310
|
+
.join(' AND '),
|
|
311
|
+
text: ({ value }) => `title ILIKE ${param(`%${value}%`)}`,
|
|
312
|
+
empty: ({ field }) => `${COLUMNS[field]} IS NULL`,
|
|
313
|
+
match: ({ field, pattern }) => `${COLUMNS[field]} ILIKE ${param(pattern.replaceAll('*', '%'))}`,
|
|
314
|
+
})
|
|
315
|
+
return { where, params }
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
`resolveTokens` replaces tokens with their values for a backend that needs them. A server can also leave `@me` unresolved in the query it receives and resolve it with the signed-in user itself.
|
|
320
|
+
|
|
321
|
+
### Filter Panels
|
|
322
|
+
|
|
323
|
+
`toFlat` reads a query as the state of a filter panel, field by field: a set of values to include and exclude, with `empty` for `no:` and `has:`, or one range. Free words and parts that could not be bound are carried along. A query a panel cannot show, such as an `OR` across fields, a negated range or a wildcard, gives `undefined`, and the panel can lock until the text changes.
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
const flat = toFlat(query)
|
|
327
|
+
// { fields: { is: { kind: 'set', include: ['open'], exclude: [] } }, text: ['crash'], invalid: [] }
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
`fromFlat` builds the query back, field by field in the order of `fields`, so a field the panel adds goes after the others. `withoutField` removes every condition on one field and keeps the rest of the query as it was, which also works on a query `toFlat` cannot read.
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
if (flat != undefined) {
|
|
334
|
+
const label: FlatSet =
|
|
335
|
+
flat.fields.label?.kind == 'set' ? flat.fields.label : { kind: 'set', include: [], exclude: [] }
|
|
336
|
+
const next = { ...flat, fields: { ...flat.fields, label: { ...label, include: [...label.include, 'docs'] } } }
|
|
337
|
+
const text = stringify(fromFlat(next), issueSchema)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const cleared = stringify(withoutField(query, 'label'), issueSchema)
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## Requirements
|
|
344
|
+
|
|
345
|
+
TypeScript 5.4 or later. The package is published as ES modules.
|
|
346
|
+
|
|
347
|
+
## License
|
|
348
|
+
|
|
349
|
+
MIT
|