string.ts 788 B

1234567891011121314151617181920212223242526272829303132
  1. import type {Token} from 'sentry/views/starfish/utils/sqlish/types';
  2. export function string(tokens: Token[]): string {
  3. let accumulator = '';
  4. function contentize(content: Token): void {
  5. if (Array.isArray(content.content)) {
  6. content.content.forEach(contentize);
  7. return;
  8. }
  9. if (typeof content.content === 'string') {
  10. if (content.type === 'Keyword') {
  11. // Break up the string on newlines
  12. accumulator += '\n';
  13. accumulator += content.content;
  14. } else if (content.type === 'Whitespace') {
  15. // Convert all whitespace to single spaces
  16. accumulator += ' ';
  17. } else {
  18. accumulator += content.content;
  19. }
  20. return;
  21. }
  22. return;
  23. }
  24. tokens.forEach(contentize);
  25. return accumulator.trim();
  26. }