yaml 1.8.3 → 1.10.0

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