blockString.mjs 2.8 KB

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