2018-11-30 08:49:54 -06:00
|
|
|
const selectorRegexp = /(?:^|\s){[^{]*}/g;
|
2019-03-28 09:58:19 -05:00
|
|
|
const caseInsensitive = '(?i)'; // Golang mode modifier for Loki, doesn't work in JavaScript
|
2018-11-28 03:46:35 -06:00
|
|
|
export function parseQuery(input: string) {
|
2018-12-09 11:44:59 -06:00
|
|
|
input = input || '';
|
2018-11-28 03:46:35 -06:00
|
|
|
const match = input.match(selectorRegexp);
|
|
|
|
let query = '';
|
|
|
|
let regexp = input;
|
|
|
|
|
|
|
|
if (match) {
|
2018-11-30 08:49:54 -06:00
|
|
|
query = match[0].trim();
|
2018-11-28 03:46:35 -06:00
|
|
|
regexp = input.replace(selectorRegexp, '').trim();
|
|
|
|
}
|
|
|
|
|
2019-03-28 09:58:19 -05:00
|
|
|
if (regexp) {
|
|
|
|
regexp = caseInsensitive + regexp;
|
|
|
|
}
|
2018-11-28 03:46:35 -06:00
|
|
|
return { query, regexp };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatQuery(selector: string, search: string): string {
|
|
|
|
return `${selector || ''} ${search || ''}`.trim();
|
|
|
|
}
|