blockString.js 2.9 KB

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