Commit d84e5ed1 authored by Robert Knight's avatar Robert Knight

Unify runtime and typecheck-time field lists

parent 160feeb8
/** const filterFields = [
* Names of supported fields that can be specified via `{filename}:{term}`
* in {@link parseFilterQuery}.
*/
export type FilterField =
| 'cfi'
| 'quote'
| 'page'
| 'since'
| 'tag'
| 'text'
| 'uri'
| 'user';
const filterFields: FilterField[] = [
'cfi', 'cfi',
'quote', 'quote',
'page', 'page',
...@@ -21,22 +7,21 @@ const filterFields: FilterField[] = [ ...@@ -21,22 +7,21 @@ const filterFields: FilterField[] = [
'text', 'text',
'uri', 'uri',
'user', 'user',
]; ] as const;
/**
* Names of supported fields that can be specified via `{filename}:{term}`
* in {@link parseFilterQuery}.
*/
export type FilterField = (typeof filterFields)[number];
const searchFields = ['group', 'quote', 'tag', 'text', 'uri', 'user'] as const;
/** /**
* Names of fields that can be used in `{field}:{term}` queries with * Names of fields that can be used in `{field}:{term}` queries with
* {@link parseHypothesisSearchQuery}. * {@link parseHypothesisSearchQuery}.
*/ */
export type SearchField = 'group' | 'quote' | 'tag' | 'text' | 'uri' | 'user'; export type SearchField = (typeof searchFields)[number];
const searchFields: SearchField[] = [
'group',
'quote',
'tag',
'text',
'uri',
'user',
];
/** /**
* Splits a search term into filter and data. * Splits a search term into filter and data.
...@@ -49,7 +34,7 @@ const searchFields: SearchField[] = [ ...@@ -49,7 +34,7 @@ const searchFields: SearchField[] = [
*/ */
function splitTerm( function splitTerm(
term: string, term: string,
fieldNames: string[], fieldNames: readonly string[],
): [null | string, string] { ): [null | string, string] {
const filter = term.slice(0, term.indexOf(':')); const filter = term.slice(0, term.indexOf(':'));
if (!filter) { if (!filter) {
...@@ -91,7 +76,7 @@ function removeSurroundingQuotes(text: string) { ...@@ -91,7 +76,7 @@ function removeSurroundingQuotes(text: string) {
* within quotes are split on whitespace. Terms inside single or double quotes * within quotes are split on whitespace. Terms inside single or double quotes
* are returned as whole tokens, with the surrounding quotes removed. * are returned as whole tokens, with the surrounding quotes removed.
*/ */
function tokenize(query: string, fieldNames: string[]): string[] { function tokenize(query: string, fieldNames: readonly string[]): string[] {
const tokenMatches = query.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g); const tokenMatches = query.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g);
if (!tokenMatches) { if (!tokenMatches) {
return []; return [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment