blockString.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(rawString);
  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. var startLine = 0;
  19. while (startLine < lines.length && isBlank(lines[startLine])) {
  20. ++startLine;
  21. }
  22. var endLine = lines.length;
  23. while (endLine > startLine && isBlank(lines[endLine - 1])) {
  24. --endLine;
  25. } // Return a string of the lines joined with U+000A.
  26. return lines.slice(startLine, endLine).join('\n');
  27. }
  28. function isBlank(str) {
  29. for (var i = 0; i < str.length; ++i) {
  30. if (str[i] !== ' ' && str[i] !== '\t') {
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. /**
  37. * @internal
  38. */
  39. export function getBlockStringIndentation(value) {
  40. var _commonIndent;
  41. var isFirstLine = true;
  42. var isEmptyLine = true;
  43. var indent = 0;
  44. var commonIndent = null;
  45. for (var i = 0; i < value.length; ++i) {
  46. switch (value.charCodeAt(i)) {
  47. case 13:
  48. // \r
  49. if (value.charCodeAt(i + 1) === 10) {
  50. ++i; // skip \r\n as one symbol
  51. }
  52. // falls through
  53. case 10:
  54. // \n
  55. isFirstLine = false;
  56. isEmptyLine = true;
  57. indent = 0;
  58. break;
  59. case 9: // \t
  60. case 32:
  61. // <space>
  62. ++indent;
  63. break;
  64. default:
  65. if (isEmptyLine && !isFirstLine && (commonIndent === null || indent < commonIndent)) {
  66. commonIndent = indent;
  67. }
  68. isEmptyLine = false;
  69. }
  70. }
  71. return (_commonIndent = commonIndent) !== null && _commonIndent !== void 0 ? _commonIndent : 0;
  72. }
  73. /**
  74. * Print a block string in the indented block form by adding a leading and
  75. * trailing blank line. However, if a block string starts with whitespace and is
  76. * a single-line, adding a leading blank line would strip that whitespace.
  77. *
  78. * @internal
  79. */
  80. export function printBlockString(value) {
  81. var indentation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  82. var preferMultipleLines = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  83. var isSingleLine = value.indexOf('\n') === -1;
  84. var hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
  85. var hasTrailingQuote = value[value.length - 1] === '"';
  86. var hasTrailingSlash = value[value.length - 1] === '\\';
  87. var printAsMultipleLines = !isSingleLine || hasTrailingQuote || hasTrailingSlash || preferMultipleLines;
  88. var result = ''; // Format a multi-line block quote to account for leading space.
  89. if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
  90. result += '\n' + indentation;
  91. }
  92. result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
  93. if (printAsMultipleLines) {
  94. result += '\n';
  95. }
  96. return '"""' + result.replace(/"""/g, '\\"""') + '"""';
  97. }