diff options
author | Eric Bailey <git@esb.lol> | 2024-09-25 15:54:43 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-25 15:54:43 -0500 |
commit | b1ca2503de55c41431aac38db4d164da7d506d4f (patch) | |
tree | bc9ee42651a4c639d910e6a31572d47fb45c5c93 /src/screens/Search/utils.ts | |
parent | 6bc001a30e4376e706fd1c10469065e0e78e1bf0 (diff) | |
download | voidsky-b1ca2503de55c41431aac38db4d164da7d506d4f.tar.zst |
Add language filtering UI to search (#5459)
* Use new TextField for search bar * Add lang dropdown * Dialog * Revert "Dialog" This reverts commit 257573cd9c2a70d29df4ef5bdd503eea4ae411fe. * Extract util, test, cleanup * Fix formatting * Pass through other params * Fix sticky header * Fix stale data, hide/show * Improve query parsing * Replace memo * Couple tweaks * Revert cancel change * Remove unused placeholder
Diffstat (limited to 'src/screens/Search/utils.ts')
-rw-r--r-- | src/screens/Search/utils.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/screens/Search/utils.ts b/src/screens/Search/utils.ts new file mode 100644 index 000000000..dcf92c092 --- /dev/null +++ b/src/screens/Search/utils.ts @@ -0,0 +1,43 @@ +export type Params = Record<string, string> + +export function parseSearchQuery(rawQuery: string) { + let base = rawQuery + const rawLiterals = rawQuery.match(/[^:\w\d]".+?"/gi) || [] + + // remove literals from base + for (const literal of rawLiterals) { + base = base.replace(literal.trim(), '') + } + + // find remaining params in base + const rawParams = base.match(/[a-z]+:[a-z-\.@\d:"]+/gi) || [] + + for (const param of rawParams) { + base = base.replace(param, '') + } + + base = base.trim() + + const params = rawParams.reduce((params, param) => { + const [name, ...value] = param.split(/:/) + params[name] = value.join(':').replace(/"/g, '') // dates can contain additional colons + return params + }, {} as Params) + const literals = rawLiterals.map(l => String(l).trim()) + + return { + query: [base, literals.join(' ')].filter(Boolean).join(' '), + params, + } +} + +export function makeSearchQuery(query: string, params: Params) { + return [ + query, + Object.entries(params) + .map(([name, value]) => `${name}:${value}`) + .join(' '), + ] + .filter(Boolean) + .join(' ') +} |