-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.ts
37 lines (36 loc) · 919 Bytes
/
parse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export interface ParseResult {
/** Current search term */
currentQuery?: LabeledQuery;
/** Completed searches */
completedQueries: LabeledQuery[];
/** Interspersed text fragments */
texts: string[];
}
export interface LabeledQuery {
query: string;
label?: string;
}
export function parse(query: string) {
const result: ParseResult = {
completedQueries: [],
texts: [],
};
const parts = query.split("+");
const len = parts.length;
if (len === 1) { // includes no '+' chars
result.currentQuery = { query };
} else {
for (let i = 0; i < len; i++) {
const part = parts[i];
if (i % 2 === 0) result.texts.push(part);
else {
const [query, label] = part.split("|");
result.completedQueries.push({ query, ...{ label } });
}
}
if (len % 2 === 0) {
result.currentQuery = result.completedQueries.pop()!;
}
}
return result;
}