UNPKG

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