blockString.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.dedentBlockStringValue = dedentBlockStringValue;
  6. exports.getBlockStringIndentation = getBlockStringIndentation;
  7. exports.printBlockString = printBlockString;
  8. /**
  9. * Produces the value of a block string from its parsed raw value, similar to
  10. * CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc.
  11. *
  12. * This implements the GraphQL spec's BlockStringValue() static algorithm.
  13. *
  14. * @internal
  15. */
  16. function dedentBlockStringValue(rawString) {
  17. // Expand a block string's raw value into independent lines.
  18. var lines = rawString.split(/\r\n|[\n\r]/g); // Remove common indentation from all lines but first.
  19. var commonIndent = getBlockStringIndentation(rawString);
  20. if (commonIndent !== 0) {
  21. for (var i = 1; i < lines.length; i++) {
  22. lines[i] = lines[i].slice(commonIndent);
  23. }
  24. } // Remove leading and trailing blank lines.
  25. var startLine = 0;
  26. while (startLine < lines.length && isBlank(lines[startLine])) {
  27. ++startLine;
  28. }
  29. var endLine = lines.length;
  30. while (endLine > startLine && isBlank(lines[endLine - 1])) {
  31. --endLine;
  32. } // Return a string of the lines joined with U+000A.
  33. return lines.slice(startLine, endLine).join('\n');
  34. }
  35. function isBlank(str) {
  36. for (var i = 0; i < str.length; ++i) {
  37. if (str[i] !== ' ' && str[i] !== '\t') {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. /**
  44. * @internal
  45. */
  46. function getBlockStringIndentation(value) {
  47. var _commonIndent;
  48. var isFirstLine = true;
  49. var isEmptyLine = true;
  50. var indent = 0;
  51. var commonIndent = null;
  52. for (var i = 0; i < value.length; ++i) {
  53. switch (value.charCodeAt(i)) {
  54. case 13:
  55. // \r
  56. if (value.charCodeAt(i + 1) === 10) {
  57. ++i; // skip \r\n as one symbol
  58. }
  59. // falls through
  60. case 10:
  61. // \n
  62. isFirstLine = false;
  63. isEmptyLine = true;
  64. indent = 0;
  65. break;
  66. case 9: // \t
  67. case 32:
  68. // <space>
  69. ++indent;
  70. break;
  71. default:
  72. if (isEmptyLine && !isFirstLine && (commonIndent === null || indent < commonIndent)) {
  73. commonIndent = indent;
  74. }
  75. isEmptyLine = false;
  76. }
  77. }
  78. return (_commonIndent = commonIndent) !== null && _commonIndent !== void 0 ? _commonIndent : 0;
  79. }
  80. /**
  81. * Print a block string in the indented block form by adding a leading and
  82. * trailing blank line. However, if a block string starts with whitespace and is
  83. * a single-line, adding a leading blank line would strip that whitespace.
  84. *
  85. * @internal
  86. */
  87. function printBlockString(value) {
  88. var indentation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  89. var preferMultipleLines = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  90. var isSingleLine = value.indexOf('\n') === -1;
  91. var hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
  92. var hasTrailingQuote = value[value.length - 1] === '"';
  93. var hasTrailingSlash = value[value.length - 1] === '\\';
  94. var printAsMultipleLines = !isSingleLine || hasTrailingQuote || hasTrailingSlash || preferMultipleLines;
  95. var result = ''; // Format a multi-line block quote to account for leading space.
  96. if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
  97. result += '\n' + indentation;
  98. }
  99. result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
  100. if (printAsMultipleLines) {
  101. result += '\n';
  102. }
  103. return '"""' + result.replace(/"""/g, '\\"""') + '"""';
  104. }