query_utils.ts 610 B

12345678910111213141516171819202122
  1. const selectorRegexp = /(?:^|\s){[^{]*}/g;
  2. const caseInsensitive = '(?i)'; // Golang mode modifier for Loki, doesn't work in JavaScript
  3. export function parseQuery(input: string) {
  4. input = input || '';
  5. const match = input.match(selectorRegexp);
  6. let query = '';
  7. let regexp = input;
  8. if (match) {
  9. query = match[0].trim();
  10. regexp = input.replace(selectorRegexp, '').trim();
  11. }
  12. if (regexp) {
  13. regexp = caseInsensitive + regexp;
  14. }
  15. return { query, regexp };
  16. }
  17. export function formatQuery(selector: string, search: string): string {
  18. return `${selector || ''} ${search || ''}`.trim();
  19. }