getStatsGroups.ts 733 B

123456789101112131415161718192021222324252627
  1. const byRE = /\s+by\s+/im;
  2. /**
  3. * groups look like this: (@a.foo)( as )(bar),
  4. * group 1 is the field, group 2 is " as " and group 3 is the alias
  5. * this regex will not advance past any non-identifier or whitespace characters, e.g. |
  6. */
  7. const groupsRE = /([\w$@().]+)(?:(\s+as\s+)([\w$]+))?\s*,?\s*/iy;
  8. export function getStatsGroups(query: string): string[] {
  9. let groups = [];
  10. // find " by "
  11. let b;
  12. if ((b = query.match(byRE))) {
  13. // continue incremental scanning from there for groups & aliases
  14. groupsRE.lastIndex = b.index! + b[0].length;
  15. let g;
  16. while ((g = groupsRE.exec(query))) {
  17. groups.push(g[2] ? g[3] : g[1]);
  18. groupsRE.lastIndex = g.index + g[0].length;
  19. }
  20. }
  21. return groups;
  22. }