blockString.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(lines);
  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. while (lines.length > 0 && isBlank(lines[0])) {
  26. lines.shift();
  27. }
  28. while (lines.length > 0 && isBlank(lines[lines.length - 1])) {
  29. lines.pop();
  30. } // Return a string of the lines joined with U+000A.
  31. return lines.join('\n');
  32. }
  33. /**
  34. * @internal
  35. */
  36. function getBlockStringIndentation(lines) {
  37. var commonIndent = null;
  38. for (var i = 1; i < lines.length; i++) {
  39. var line = lines[i];
  40. var indent = leadingWhitespace(line);
  41. if (indent === line.length) {
  42. continue; // skip empty lines
  43. }
  44. if (commonIndent === null || indent < commonIndent) {
  45. commonIndent = indent;
  46. if (commonIndent === 0) {
  47. break;
  48. }
  49. }
  50. }
  51. return commonIndent === null ? 0 : commonIndent;
  52. }
  53. function leadingWhitespace(str) {
  54. var i = 0;
  55. while (i < str.length && (str[i] === ' ' || str[i] === '\t')) {
  56. i++;
  57. }
  58. return i;
  59. }
  60. function isBlank(str) {
  61. return leadingWhitespace(str) === str.length;
  62. }
  63. /**
  64. * Print a block string in the indented block form by adding a leading and
  65. * trailing blank line. However, if a block string starts with whitespace and is
  66. * a single-line, adding a leading blank line would strip that whitespace.
  67. *
  68. * @internal
  69. */
  70. function printBlockString(value) {
  71. var indentation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  72. var preferMultipleLines = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  73. var isSingleLine = value.indexOf('\n') === -1;
  74. var hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
  75. var hasTrailingQuote = value[value.length - 1] === '"';
  76. var hasTrailingSlash = value[value.length - 1] === '\\';
  77. var printAsMultipleLines = !isSingleLine || hasTrailingQuote || hasTrailingSlash || preferMultipleLines;
  78. var result = ''; // Format a multi-line block quote to account for leading space.
  79. if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
  80. result += '\n' + indentation;
  81. }
  82. result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
  83. if (printAsMultipleLines) {
  84. result += '\n';
  85. }
  86. return '"""' + result.replace(/"""/g, '\\"""') + '"""';
  87. }