blockString.mjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Produces the value of a block string from its parsed raw value, similar to
  3. * CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc.
  4. *
  5. * This implements the GraphQL spec's BlockStringValue() static algorithm.
  6. */
  7. export function dedentBlockStringValue(rawString) {
  8. // Expand a block string's raw value into independent lines.
  9. var lines = rawString.split(/\r\n|[\n\r]/g); // Remove common indentation from all lines but first.
  10. var commonIndent = getBlockStringIndentation(lines);
  11. if (commonIndent !== 0) {
  12. for (var i = 1; i < lines.length; i++) {
  13. lines[i] = lines[i].slice(commonIndent);
  14. }
  15. } // Remove leading and trailing blank lines.
  16. while (lines.length > 0 && isBlank(lines[0])) {
  17. lines.shift();
  18. }
  19. while (lines.length > 0 && isBlank(lines[lines.length - 1])) {
  20. lines.pop();
  21. } // Return a string of the lines joined with U+000A.
  22. return lines.join('\n');
  23. } // @internal
  24. export function getBlockStringIndentation(lines) {
  25. var commonIndent = null;
  26. for (var i = 1; i < lines.length; i++) {
  27. var line = lines[i];
  28. var indent = leadingWhitespace(line);
  29. if (indent === line.length) {
  30. continue; // skip empty lines
  31. }
  32. if (commonIndent === null || indent < commonIndent) {
  33. commonIndent = indent;
  34. if (commonIndent === 0) {
  35. break;
  36. }
  37. }
  38. }
  39. return commonIndent === null ? 0 : commonIndent;
  40. }
  41. function leadingWhitespace(str) {
  42. var i = 0;
  43. while (i < str.length && (str[i] === ' ' || str[i] === '\t')) {
  44. i++;
  45. }
  46. return i;
  47. }
  48. function isBlank(str) {
  49. return leadingWhitespace(str) === str.length;
  50. }
  51. /**
  52. * Print a block string in the indented block form by adding a leading and
  53. * trailing blank line. However, if a block string starts with whitespace and is
  54. * a single-line, adding a leading blank line would strip that whitespace.
  55. */
  56. export function printBlockString(value) {
  57. var indentation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  58. var preferMultipleLines = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  59. var isSingleLine = value.indexOf('\n') === -1;
  60. var hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
  61. var hasTrailingQuote = value[value.length - 1] === '"';
  62. var printAsMultipleLines = !isSingleLine || hasTrailingQuote || preferMultipleLines;
  63. var result = ''; // Format a multi-line block quote to account for leading space.
  64. if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
  65. result += '\n' + indentation;
  66. }
  67. result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
  68. if (printAsMultipleLines) {
  69. result += '\n';
  70. }
  71. return '"""' + result.replace(/"""/g, '\\"""') + '"""';
  72. }