UNPKG

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