yaml 1.9.1 → 1.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +25 -25
  2. package/browser/dist/PlainValue-b8036b75.js +1275 -0
  3. package/browser/dist/Schema-e94716c8.js +682 -0
  4. package/browser/dist/index.js +860 -27
  5. package/browser/dist/legacy-exports.js +3 -0
  6. package/browser/dist/package.json +1 -0
  7. package/browser/dist/parse-cst.js +1904 -0
  8. package/browser/dist/resolveSeq-492ab440.js +2419 -0
  9. package/browser/dist/types.js +4 -0
  10. package/browser/dist/util.js +2 -0
  11. package/browser/dist/warnings-df54cb69.js +499 -0
  12. package/browser/index.js +1 -1
  13. package/browser/map.js +2 -4
  14. package/browser/pair.js +2 -4
  15. package/browser/parse-cst.js +1 -1
  16. package/browser/scalar.js +2 -4
  17. package/browser/schema.js +7 -10
  18. package/browser/seq.js +2 -4
  19. package/browser/types/binary.js +7 -5
  20. package/browser/types/omap.js +3 -4
  21. package/browser/types/pairs.js +3 -4
  22. package/browser/types/set.js +3 -4
  23. package/browser/types/timestamp.js +9 -5
  24. package/browser/types.js +1 -13
  25. package/browser/util.js +1 -18
  26. package/dist/Document-9b4560a1.js +757 -0
  27. package/dist/PlainValue-ec8e588e.js +876 -0
  28. package/dist/Schema-88e323a7.js +525 -0
  29. package/dist/index.js +20 -84
  30. package/dist/legacy-exports.js +16 -0
  31. package/dist/parse-cst.js +1753 -0
  32. package/dist/resolveSeq-d03cb037.js +2161 -0
  33. package/dist/test-events.js +11 -9
  34. package/dist/types.js +23 -0
  35. package/dist/util.js +19 -0
  36. package/dist/warnings-1000a372.js +416 -0
  37. package/index.d.ts +23 -9
  38. package/map.js +2 -2
  39. package/package.json +27 -42
  40. package/pair.js +2 -2
  41. package/parse-cst.d.ts +4 -0
  42. package/parse-cst.js +1 -1
  43. package/scalar.js +2 -2
  44. package/schema.js +8 -6
  45. package/seq.js +2 -2
  46. package/types/binary.js +3 -2
  47. package/types/omap.js +3 -2
  48. package/types/pairs.js +3 -2
  49. package/types/set.js +3 -2
  50. package/types/timestamp.js +6 -6
  51. package/types.d.ts +113 -98
  52. package/types.js +16 -11
  53. package/types.mjs +15 -19
  54. package/util.js +14 -14
  55. package/util.mjs +13 -20
  56. package/browser/dist/Anchors.js +0 -130
  57. package/browser/dist/Document.js +0 -824
  58. package/browser/dist/addComment.js +0 -8
  59. package/browser/dist/constants.js +0 -26
  60. package/browser/dist/cst/Alias.js +0 -46
  61. package/browser/dist/cst/BlankLine.js +0 -52
  62. package/browser/dist/cst/BlockValue.js +0 -251
  63. package/browser/dist/cst/Collection.js +0 -278
  64. package/browser/dist/cst/CollectionItem.js +0 -140
  65. package/browser/dist/cst/Comment.js +0 -44
  66. package/browser/dist/cst/Directive.js +0 -76
  67. package/browser/dist/cst/Document.js +0 -286
  68. package/browser/dist/cst/FlowCollection.js +0 -220
  69. package/browser/dist/cst/Node.js +0 -386
  70. package/browser/dist/cst/ParseContext.js +0 -246
  71. package/browser/dist/cst/PlainValue.js +0 -170
  72. package/browser/dist/cst/QuoteDouble.js +0 -245
  73. package/browser/dist/cst/QuoteSingle.js +0 -120
  74. package/browser/dist/cst/Range.js +0 -64
  75. package/browser/dist/cst/parse.js +0 -48
  76. package/browser/dist/cst/source-utils.js +0 -168
  77. package/browser/dist/errors.js +0 -127
  78. package/browser/dist/foldFlowLines.js +0 -138
  79. package/browser/dist/listTagNames.js +0 -28
  80. package/browser/dist/schema/Alias.js +0 -135
  81. package/browser/dist/schema/Collection.js +0 -260
  82. package/browser/dist/schema/Map.js +0 -161
  83. package/browser/dist/schema/Merge.js +0 -118
  84. package/browser/dist/schema/Node.js +0 -4
  85. package/browser/dist/schema/Pair.js +0 -194
  86. package/browser/dist/schema/Scalar.js +0 -42
  87. package/browser/dist/schema/Seq.js +0 -116
  88. package/browser/dist/schema/index.js +0 -368
  89. package/browser/dist/schema/parseMap.js +0 -312
  90. package/browser/dist/schema/parseSeq.js +0 -168
  91. package/browser/dist/schema/parseUtils.js +0 -106
  92. package/browser/dist/stringify.js +0 -332
  93. package/browser/dist/tags/core.js +0 -141
  94. package/browser/dist/tags/failsafe/index.js +0 -4
  95. package/browser/dist/tags/failsafe/map.js +0 -53
  96. package/browser/dist/tags/failsafe/seq.js +0 -39
  97. package/browser/dist/tags/failsafe/string.js +0 -28
  98. package/browser/dist/tags/index.js +0 -36
  99. package/browser/dist/tags/json.js +0 -77
  100. package/browser/dist/tags/options.js +0 -26
  101. package/browser/dist/tags/yaml-1.1/binary.js +0 -87
  102. package/browser/dist/tags/yaml-1.1/index.js +0 -189
  103. package/browser/dist/tags/yaml-1.1/omap.js +0 -137
  104. package/browser/dist/tags/yaml-1.1/pairs.js +0 -78
  105. package/browser/dist/tags/yaml-1.1/set.js +0 -118
  106. package/browser/dist/tags/yaml-1.1/timestamp.js +0 -97
  107. package/browser/dist/test-events.js +0 -163
  108. package/browser/dist/toJSON.js +0 -19
  109. package/browser/dist/warnings.js +0 -39
  110. package/dist/Anchors.js +0 -120
  111. package/dist/Document.js +0 -714
  112. package/dist/addComment.js +0 -14
  113. package/dist/constants.js +0 -31
  114. package/dist/cst/Alias.js +0 -33
  115. package/dist/cst/BlankLine.js +0 -40
  116. package/dist/cst/BlockValue.js +0 -243
  117. package/dist/cst/Collection.js +0 -266
  118. package/dist/cst/CollectionItem.js +0 -135
  119. package/dist/cst/Comment.js +0 -33
  120. package/dist/cst/Directive.js +0 -59
  121. package/dist/cst/Document.js +0 -276
  122. package/dist/cst/FlowCollection.js +0 -213
  123. package/dist/cst/Node.js +0 -360
  124. package/dist/cst/ParseContext.js +0 -253
  125. package/dist/cst/PlainValue.js +0 -156
  126. package/dist/cst/QuoteDouble.js +0 -232
  127. package/dist/cst/QuoteSingle.js +0 -108
  128. package/dist/cst/Range.js +0 -60
  129. package/dist/cst/parse.js +0 -50
  130. package/dist/cst/source-utils.js +0 -175
  131. package/dist/errors.js +0 -97
  132. package/dist/foldFlowLines.js +0 -143
  133. package/dist/listTagNames.js +0 -33
  134. package/dist/schema/Alias.js +0 -105
  135. package/dist/schema/Collection.js +0 -184
  136. package/dist/schema/Map.js +0 -98
  137. package/dist/schema/Merge.js +0 -75
  138. package/dist/schema/Node.js +0 -7
  139. package/dist/schema/Pair.js +0 -174
  140. package/dist/schema/Scalar.js +0 -26
  141. package/dist/schema/Seq.js +0 -73
  142. package/dist/schema/index.js +0 -310
  143. package/dist/schema/parseMap.js +0 -334
  144. package/dist/schema/parseSeq.js +0 -175
  145. package/dist/schema/parseUtils.js +0 -101
  146. package/dist/stringify.js +0 -352
  147. package/dist/tags/core.js +0 -121
  148. package/dist/tags/failsafe/index.js +0 -12
  149. package/dist/tags/failsafe/map.js +0 -32
  150. package/dist/tags/failsafe/seq.js +0 -29
  151. package/dist/tags/failsafe/string.js +0 -37
  152. package/dist/tags/index.js +0 -53
  153. package/dist/tags/json.js +0 -64
  154. package/dist/tags/options.js +0 -36
  155. package/dist/tags/yaml-1.1/binary.js +0 -94
  156. package/dist/tags/yaml-1.1/index.js +0 -162
  157. package/dist/tags/yaml-1.1/omap.js +0 -100
  158. package/dist/tags/yaml-1.1/pairs.js +0 -75
  159. package/dist/tags/yaml-1.1/set.js +0 -82
  160. package/dist/tags/yaml-1.1/timestamp.js +0 -90
  161. package/dist/toJSON.js +0 -21
  162. package/dist/warnings.js +0 -48
@@ -1,175 +0,0 @@
1
- "use strict";
2
-
3
- exports.parseSeq = parseSeq;
4
-
5
- var _constants = require("../constants");
6
-
7
- var _errors = require("../errors");
8
-
9
- var _Pair = require("./Pair");
10
-
11
- var _parseUtils = require("./parseUtils");
12
-
13
- var _Seq = require("./Seq");
14
-
15
- var _Collection = require("./Collection");
16
-
17
- function parseSeq(doc, cst) {
18
- if (cst.type !== _constants.Type.SEQ && cst.type !== _constants.Type.FLOW_SEQ) {
19
- const msg = `A ${cst.type} node cannot be resolved as a sequence`;
20
- doc.errors.push(new _errors.YAMLSyntaxError(cst, msg));
21
- return null;
22
- }
23
-
24
- const {
25
- comments,
26
- items
27
- } = cst.type === _constants.Type.FLOW_SEQ ? resolveFlowSeqItems(doc, cst) : resolveBlockSeqItems(doc, cst);
28
- const seq = new _Seq.YAMLSeq();
29
- seq.items = items;
30
- (0, _parseUtils.resolveComments)(seq, comments);
31
-
32
- if (!doc.options.mapAsMap && items.some(it => it instanceof _Pair.Pair && it.key instanceof _Collection.Collection)) {
33
- const warn = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.';
34
- doc.warnings.push(new _errors.YAMLWarning(cst, warn));
35
- }
36
-
37
- cst.resolved = seq;
38
- return seq;
39
- }
40
-
41
- function resolveBlockSeqItems(doc, cst) {
42
- const comments = [];
43
- const items = [];
44
-
45
- for (let i = 0; i < cst.items.length; ++i) {
46
- const item = cst.items[i];
47
-
48
- switch (item.type) {
49
- case _constants.Type.BLANK_LINE:
50
- comments.push({
51
- before: items.length
52
- });
53
- break;
54
-
55
- case _constants.Type.COMMENT:
56
- comments.push({
57
- comment: item.comment,
58
- before: items.length
59
- });
60
- break;
61
-
62
- case _constants.Type.SEQ_ITEM:
63
- if (item.error) doc.errors.push(item.error);
64
- items.push(doc.resolveNode(item.node));
65
-
66
- if (item.hasProps) {
67
- const msg = 'Sequence items cannot have tags or anchors before the - indicator';
68
- doc.errors.push(new _errors.YAMLSemanticError(item, msg));
69
- }
70
-
71
- break;
72
-
73
- default:
74
- if (item.error) doc.errors.push(item.error);
75
- doc.errors.push(new _errors.YAMLSyntaxError(item, `Unexpected ${item.type} node in sequence`));
76
- }
77
- }
78
-
79
- return {
80
- comments,
81
- items
82
- };
83
- }
84
-
85
- function resolveFlowSeqItems(doc, cst) {
86
- const comments = [];
87
- const items = [];
88
- let explicitKey = false;
89
- let key = undefined;
90
- let keyStart = null;
91
- let next = '[';
92
-
93
- for (let i = 0; i < cst.items.length; ++i) {
94
- const item = cst.items[i];
95
-
96
- if (typeof item.char === 'string') {
97
- const {
98
- char,
99
- offset
100
- } = item;
101
-
102
- if (char !== ':' && (explicitKey || key !== undefined)) {
103
- if (explicitKey && key === undefined) key = next ? items.pop() : null;
104
- items.push(new _Pair.Pair(key));
105
- explicitKey = false;
106
- key = undefined;
107
- keyStart = null;
108
- }
109
-
110
- if (char === next) {
111
- next = null;
112
- } else if (!next && char === '?') {
113
- explicitKey = true;
114
- } else if (next !== '[' && char === ':' && key === undefined) {
115
- if (next === ',') {
116
- key = items.pop();
117
-
118
- if (key instanceof _Pair.Pair) {
119
- const msg = 'Chaining flow sequence pairs is invalid';
120
- const err = new _errors.YAMLSemanticError(cst, msg);
121
- err.offset = offset;
122
- doc.errors.push(err);
123
- }
124
-
125
- if (!explicitKey) (0, _parseUtils.checkKeyLength)(doc.errors, cst, i, key, keyStart);
126
- } else {
127
- key = null;
128
- }
129
-
130
- keyStart = null;
131
- explicitKey = false; // TODO: add error for non-explicit multiline plain key
132
-
133
- next = null;
134
- } else if (next === '[' || char !== ']' || i < cst.items.length - 1) {
135
- const msg = `Flow sequence contains an unexpected ${char}`;
136
- const err = new _errors.YAMLSyntaxError(cst, msg);
137
- err.offset = offset;
138
- doc.errors.push(err);
139
- }
140
- } else if (item.type === _constants.Type.BLANK_LINE) {
141
- comments.push({
142
- before: items.length
143
- });
144
- } else if (item.type === _constants.Type.COMMENT) {
145
- comments.push({
146
- comment: item.comment,
147
- before: items.length
148
- });
149
- } else {
150
- if (next) {
151
- const msg = `Expected a ${next} in flow sequence`;
152
- doc.errors.push(new _errors.YAMLSemanticError(item, msg));
153
- }
154
-
155
- const value = doc.resolveNode(item);
156
-
157
- if (key === undefined) {
158
- items.push(value);
159
- } else {
160
- items.push(new _Pair.Pair(key, value));
161
- key = undefined;
162
- }
163
-
164
- keyStart = item.range.start;
165
- next = ',';
166
- }
167
- }
168
-
169
- (0, _parseUtils.checkFlowCollectionEnd)(doc.errors, cst);
170
- if (key !== undefined) items.push(new _Pair.Pair(key));
171
- return {
172
- comments,
173
- items
174
- };
175
- }
@@ -1,101 +0,0 @@
1
- "use strict";
2
-
3
- exports.checkFlowCollectionEnd = checkFlowCollectionEnd;
4
- exports.checkKeyLength = checkKeyLength;
5
- exports.resolveComments = resolveComments;
6
-
7
- var _errors = require("../errors");
8
-
9
- var _constants = require("../constants");
10
-
11
- function checkFlowCollectionEnd(errors, cst) {
12
- let char, name;
13
-
14
- switch (cst.type) {
15
- case _constants.Type.FLOW_MAP:
16
- char = '}';
17
- name = 'flow map';
18
- break;
19
-
20
- case _constants.Type.FLOW_SEQ:
21
- char = ']';
22
- name = 'flow sequence';
23
- break;
24
-
25
- default:
26
- errors.push(new _errors.YAMLSemanticError(cst, 'Not a flow collection!?'));
27
- return;
28
- }
29
-
30
- let lastItem;
31
-
32
- for (let i = cst.items.length - 1; i >= 0; --i) {
33
- const item = cst.items[i];
34
-
35
- if (!item || item.type !== _constants.Type.COMMENT) {
36
- lastItem = item;
37
- break;
38
- }
39
- }
40
-
41
- if (lastItem && lastItem.char !== char) {
42
- const msg = `Expected ${name} to end with ${char}`;
43
- let err;
44
-
45
- if (typeof lastItem.offset === 'number') {
46
- err = new _errors.YAMLSemanticError(cst, msg);
47
- err.offset = lastItem.offset + 1;
48
- } else {
49
- err = new _errors.YAMLSemanticError(lastItem, msg);
50
- if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start;
51
- }
52
-
53
- errors.push(err);
54
- }
55
- }
56
-
57
- function checkKeyLength(errors, node, itemIdx, key, keyStart) {
58
- if (!key || typeof keyStart !== 'number') return;
59
- const item = node.items[itemIdx];
60
- let keyEnd = item && item.range && item.range.start;
61
-
62
- if (!keyEnd) {
63
- for (let i = itemIdx - 1; i >= 0; --i) {
64
- const it = node.items[i];
65
-
66
- if (it && it.range) {
67
- keyEnd = it.range.end + 2 * (itemIdx - i);
68
- break;
69
- }
70
- }
71
- }
72
-
73
- if (keyEnd > keyStart + 1024) {
74
- const k = String(key).substr(0, 8) + '...' + String(key).substr(-8);
75
- errors.push(new _errors.YAMLSemanticError(node, `The "${k}" key is too long`));
76
- }
77
- }
78
-
79
- function resolveComments(collection, comments) {
80
- for (const {
81
- afterKey,
82
- before,
83
- comment
84
- } of comments) {
85
- let item = collection.items[before];
86
-
87
- if (!item) {
88
- if (comment !== undefined) {
89
- if (collection.comment) collection.comment += '\n' + comment;else collection.comment = comment;
90
- }
91
- } else {
92
- if (afterKey && item.value) item = item.value;
93
-
94
- if (comment === undefined) {
95
- if (afterKey || !item.commentBefore) item.spaceBefore = true;
96
- } else {
97
- if (item.commentBefore) item.commentBefore += '\n' + comment;else item.commentBefore = comment;
98
- }
99
- }
100
- }
101
- }
package/dist/stringify.js DELETED
@@ -1,352 +0,0 @@
1
- "use strict";
2
-
3
- exports.stringifyNumber = stringifyNumber;
4
- exports.stringifyString = stringifyString;
5
-
6
- var _addComment = require("./addComment");
7
-
8
- var _constants = require("./constants");
9
-
10
- var _foldFlowLines = require("./foldFlowLines");
11
-
12
- var _options = require("./tags/options");
13
-
14
- const getFoldOptions = ({
15
- indentAtStart
16
- }) => indentAtStart ? Object.assign({
17
- indentAtStart
18
- }, _options.strOptions.fold) : _options.strOptions.fold;
19
-
20
- function stringifyNumber({
21
- format,
22
- minFractionDigits,
23
- tag,
24
- value
25
- }) {
26
- if (typeof value === 'bigint') return String(value);
27
- if (!isFinite(value)) return isNaN(value) ? '.nan' : value < 0 ? '-.inf' : '.inf';
28
- let n = JSON.stringify(value);
29
-
30
- if (!format && minFractionDigits && (!tag || tag === 'tag:yaml.org,2002:float') && /^\d/.test(n)) {
31
- let i = n.indexOf('.');
32
-
33
- if (i < 0) {
34
- i = n.length;
35
- n += '.';
36
- }
37
-
38
- let d = minFractionDigits - (n.length - i - 1);
39
-
40
- while (d-- > 0) n += '0';
41
- }
42
-
43
- return n;
44
- }
45
-
46
- function lineLengthOverLimit(str, limit) {
47
- const strLen = str.length;
48
- if (strLen <= limit) return false;
49
-
50
- for (let i = 0, start = 0; i < strLen; ++i) {
51
- if (str[i] === '\n') {
52
- if (i - start > limit) return true;
53
- start = i + 1;
54
- if (strLen - start <= limit) return false;
55
- }
56
- }
57
-
58
- return true;
59
- }
60
-
61
- function doubleQuotedString(value, ctx) {
62
- const {
63
- implicitKey,
64
- indent
65
- } = ctx;
66
- const {
67
- jsonEncoding,
68
- minMultiLineLength
69
- } = _options.strOptions.doubleQuoted;
70
- const json = JSON.stringify(value);
71
- if (jsonEncoding) return json;
72
- let str = '';
73
- let start = 0;
74
-
75
- for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
76
- if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
77
- // space before newline needs to be escaped to not be folded
78
- str += json.slice(start, i) + '\\ ';
79
- i += 1;
80
- start = i;
81
- ch = '\\';
82
- }
83
-
84
- if (ch === '\\') switch (json[i + 1]) {
85
- case 'u':
86
- {
87
- str += json.slice(start, i);
88
- const code = json.substr(i + 2, 4);
89
-
90
- switch (code) {
91
- case '0000':
92
- str += '\\0';
93
- break;
94
-
95
- case '0007':
96
- str += '\\a';
97
- break;
98
-
99
- case '000b':
100
- str += '\\v';
101
- break;
102
-
103
- case '001b':
104
- str += '\\e';
105
- break;
106
-
107
- case '0085':
108
- str += '\\N';
109
- break;
110
-
111
- case '00a0':
112
- str += '\\_';
113
- break;
114
-
115
- case '2028':
116
- str += '\\L';
117
- break;
118
-
119
- case '2029':
120
- str += '\\P';
121
- break;
122
-
123
- default:
124
- if (code.substr(0, 2) === '00') str += '\\x' + code.substr(2);else str += json.substr(i, 6);
125
- }
126
-
127
- i += 5;
128
- start = i + 1;
129
- }
130
- break;
131
-
132
- case 'n':
133
- if (implicitKey || json[i + 2] === '"' || json.length < minMultiLineLength) {
134
- i += 1;
135
- } else {
136
- // folding will eat first newline
137
- str += json.slice(start, i) + '\n\n';
138
-
139
- while (json[i + 2] === '\\' && json[i + 3] === 'n' && json[i + 4] !== '"') {
140
- str += '\n';
141
- i += 2;
142
- }
143
-
144
- str += indent; // space after newline needs to be escaped to not be folded
145
-
146
- if (json[i + 2] === ' ') str += '\\';
147
- i += 1;
148
- start = i + 1;
149
- }
150
-
151
- break;
152
-
153
- default:
154
- i += 1;
155
- }
156
- }
157
-
158
- str = start ? str + json.slice(start) : json;
159
- return implicitKey ? str : (0, _foldFlowLines.foldFlowLines)(str, indent, _foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx));
160
- }
161
-
162
- function singleQuotedString(value, ctx) {
163
- const {
164
- indent,
165
- implicitKey
166
- } = ctx;
167
-
168
- if (implicitKey) {
169
- if (/\n/.test(value)) return doubleQuotedString(value, ctx);
170
- } else {
171
- // single quoted string can't have leading or trailing whitespace around newline
172
- if (/[ \t]\n|\n[ \t]/.test(value)) return doubleQuotedString(value, ctx);
173
- }
174
-
175
- const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
176
- return implicitKey ? res : (0, _foldFlowLines.foldFlowLines)(res, indent, _foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
177
- }
178
-
179
- function blockString({
180
- comment,
181
- type,
182
- value
183
- }, ctx, onComment, onChompKeep) {
184
- // 1. Block can't end in whitespace unless the last line is non-empty.
185
- // 2. Strings consisting of only whitespace are best rendered explicitly.
186
- if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
187
- return doubleQuotedString(value, ctx);
188
- }
189
-
190
- const indent = ctx.indent || (ctx.forceBlockIndent ? ' ' : '');
191
- const indentSize = indent ? '2' : '1'; // root is at -1
192
-
193
- const literal = type === _constants.Type.BLOCK_FOLDED ? false : type === _constants.Type.BLOCK_LITERAL ? true : !lineLengthOverLimit(value, _options.strOptions.fold.lineWidth - indent.length);
194
- let header = literal ? '|' : '>';
195
- if (!value) return header + '\n';
196
- let wsStart = '';
197
- let wsEnd = '';
198
- value = value.replace(/[\n\t ]*$/, ws => {
199
- const n = ws.indexOf('\n');
200
-
201
- if (n === -1) {
202
- header += '-'; // strip
203
- } else if (value === ws || n !== ws.length - 1) {
204
- header += '+'; // keep
205
-
206
- if (onChompKeep) onChompKeep();
207
- }
208
-
209
- wsEnd = ws.replace(/\n$/, '');
210
- return '';
211
- }).replace(/^[\n ]*/, ws => {
212
- if (ws.indexOf(' ') !== -1) header += indentSize;
213
- const m = ws.match(/ +$/);
214
-
215
- if (m) {
216
- wsStart = ws.slice(0, -m[0].length);
217
- return m[0];
218
- } else {
219
- wsStart = ws;
220
- return '';
221
- }
222
- });
223
- if (wsEnd) wsEnd = wsEnd.replace(/\n+(?!\n|$)/g, `$&${indent}`);
224
- if (wsStart) wsStart = wsStart.replace(/\n+/g, `$&${indent}`);
225
-
226
- if (comment) {
227
- header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ');
228
- if (onComment) onComment();
229
- }
230
-
231
- if (!value) return `${header}${indentSize}\n${indent}${wsEnd}`;
232
-
233
- if (literal) {
234
- value = value.replace(/\n+/g, `$&${indent}`);
235
- return `${header}\n${indent}${wsStart}${value}${wsEnd}`;
236
- }
237
-
238
- value = value.replace(/\n+/g, '\n$&').replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
239
- // ^ ind.line ^ empty ^ capture next empty lines only at end of indent
240
- .replace(/\n+/g, `$&${indent}`);
241
- const body = (0, _foldFlowLines.foldFlowLines)(`${wsStart}${value}${wsEnd}`, indent, _foldFlowLines.FOLD_BLOCK, _options.strOptions.fold);
242
- return `${header}\n${indent}${body}`;
243
- }
244
-
245
- function plainString(item, ctx, onComment, onChompKeep) {
246
- const {
247
- comment,
248
- type,
249
- value
250
- } = item;
251
- const {
252
- actualString,
253
- implicitKey,
254
- indent,
255
- inFlow,
256
- tags
257
- } = ctx;
258
-
259
- if (implicitKey && /[\n[\]{},]/.test(value) || inFlow && /[[\]{},]/.test(value)) {
260
- return doubleQuotedString(value, ctx);
261
- }
262
-
263
- if (!value || /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
264
- // not allowed:
265
- // - empty string, '-' or '?'
266
- // - start with an indicator character (except [?:-]) or /[?-] /
267
- // - '\n ', ': ' or ' \n' anywhere
268
- // - '#' not preceded by a non-space char
269
- // - end with ' ' or ':'
270
- return implicitKey || inFlow || value.indexOf('\n') === -1 ? value.indexOf('"') !== -1 && value.indexOf("'") === -1 ? singleQuotedString(value, ctx) : doubleQuotedString(value, ctx) : blockString(item, ctx, onComment, onChompKeep);
271
- }
272
-
273
- if (!implicitKey && !inFlow && type !== _constants.Type.PLAIN && value.indexOf('\n') !== -1) {
274
- // Where allowed & type not set explicitly, prefer block style for multiline strings
275
- return blockString(item, ctx, onComment, onChompKeep);
276
- }
277
-
278
- const str = value.replace(/\n+/g, `$&\n${indent}`); // Verify that output will be parsed as a string, as e.g. plain numbers and
279
- // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
280
- // and others in v1.1.
281
-
282
- if (actualString && typeof tags.resolveScalar(str).value !== 'string') {
283
- return doubleQuotedString(value, ctx);
284
- }
285
-
286
- const body = implicitKey ? str : (0, _foldFlowLines.foldFlowLines)(str, indent, _foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
287
-
288
- if (comment && !inFlow && (body.indexOf('\n') !== -1 || comment.indexOf('\n') !== -1)) {
289
- if (onComment) onComment();
290
- return (0, _addComment.addCommentBefore)(body, indent, comment);
291
- }
292
-
293
- return body;
294
- }
295
-
296
- function stringifyString(item, ctx, onComment, onChompKeep) {
297
- const {
298
- defaultType
299
- } = _options.strOptions;
300
- const {
301
- implicitKey,
302
- inFlow
303
- } = ctx;
304
- let {
305
- type,
306
- value
307
- } = item;
308
-
309
- if (typeof value !== 'string') {
310
- value = String(value);
311
- item = Object.assign({}, item, {
312
- value
313
- });
314
- }
315
-
316
- const _stringify = _type => {
317
- switch (_type) {
318
- case _constants.Type.BLOCK_FOLDED:
319
- case _constants.Type.BLOCK_LITERAL:
320
- return blockString(item, ctx, onComment, onChompKeep);
321
-
322
- case _constants.Type.QUOTE_DOUBLE:
323
- return doubleQuotedString(value, ctx);
324
-
325
- case _constants.Type.QUOTE_SINGLE:
326
- return singleQuotedString(value, ctx);
327
-
328
- case _constants.Type.PLAIN:
329
- return plainString(item, ctx, onComment, onChompKeep);
330
-
331
- default:
332
- return null;
333
- }
334
- };
335
-
336
- if (type !== _constants.Type.QUOTE_DOUBLE && /[\x00-\x08\x0b-\x1f\x7f-\x9f]/.test(value)) {
337
- // force double quotes on control characters
338
- type = _constants.Type.QUOTE_DOUBLE;
339
- } else if ((implicitKey || inFlow) && (type === _constants.Type.BLOCK_FOLDED || type === _constants.Type.BLOCK_LITERAL)) {
340
- // should not happen; blocks are not valid inside flow containers
341
- type = _constants.Type.QUOTE_DOUBLE;
342
- }
343
-
344
- let res = _stringify(type);
345
-
346
- if (res === null) {
347
- res = _stringify(defaultType);
348
- if (res === null) throw new Error(`Unsupported default string type ${defaultType}`);
349
- }
350
-
351
- return res;
352
- }