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,139 +0,0 @@
1
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
- import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
3
- import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
4
- import _createClass from "@babel/runtime/helpers/createClass";
5
- import _inherits from "@babel/runtime/helpers/inherits";
6
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
7
- import { Type } from '../constants';
8
- import { YAMLReferenceError } from '../errors';
9
- import _toJSON from '../toJSON';
10
- import Collection from './Collection';
11
- import Node from './Node';
12
- import Pair from './Pair';
13
-
14
- var getAliasCount = function getAliasCount(node, anchors) {
15
- if (node instanceof Alias) {
16
- var anchor = anchors.find(function (a) {
17
- return a.node === node.source;
18
- });
19
- return anchor.count * anchor.aliasCount;
20
- } else if (node instanceof Collection) {
21
- var count = 0;
22
- var _iteratorNormalCompletion = true;
23
- var _didIteratorError = false;
24
- var _iteratorError = undefined;
25
-
26
- try {
27
- for (var _iterator = node.items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
28
- var item = _step.value;
29
- var c = getAliasCount(item, anchors);
30
- if (c > count) count = c;
31
- }
32
- } catch (err) {
33
- _didIteratorError = true;
34
- _iteratorError = err;
35
- } finally {
36
- try {
37
- if (!_iteratorNormalCompletion && _iterator.return != null) {
38
- _iterator.return();
39
- }
40
- } finally {
41
- if (_didIteratorError) {
42
- throw _iteratorError;
43
- }
44
- }
45
- }
46
-
47
- return count;
48
- } else if (node instanceof Pair) {
49
- var kc = getAliasCount(node.key, anchors);
50
- var vc = getAliasCount(node.value, anchors);
51
- return Math.max(kc, vc);
52
- }
53
-
54
- return 1;
55
- };
56
-
57
- var Alias = /*#__PURE__*/function (_Node) {
58
- _inherits(Alias, _Node);
59
-
60
- _createClass(Alias, null, [{
61
- key: "stringify",
62
- value: function stringify(_ref, _ref2) {
63
- var range = _ref.range,
64
- source = _ref.source;
65
- var anchors = _ref2.anchors,
66
- doc = _ref2.doc,
67
- implicitKey = _ref2.implicitKey,
68
- inStringifyKey = _ref2.inStringifyKey;
69
- var anchor = Object.keys(anchors).find(function (a) {
70
- return anchors[a] === source;
71
- });
72
- if (!anchor && inStringifyKey) anchor = doc.anchors.getName(source) || doc.anchors.newName();
73
- if (anchor) return "*".concat(anchor).concat(implicitKey ? ' ' : '');
74
- var msg = doc.anchors.getName(source) ? 'Alias node must be after source node' : 'Source node not found for alias node';
75
- throw new Error("".concat(msg, " [").concat(range, "]"));
76
- }
77
- }]);
78
-
79
- function Alias(source) {
80
- var _this;
81
-
82
- _classCallCheck(this, Alias);
83
-
84
- _this = _possibleConstructorReturn(this, _getPrototypeOf(Alias).call(this));
85
- _this.source = source;
86
- _this.type = Type.ALIAS;
87
- return _this;
88
- }
89
-
90
- _createClass(Alias, [{
91
- key: "toJSON",
92
- value: function toJSON(arg, ctx) {
93
- var _this2 = this;
94
-
95
- if (!ctx) return _toJSON(this.source, arg, ctx);
96
- var anchors = ctx.anchors,
97
- maxAliasCount = ctx.maxAliasCount;
98
- var anchor = anchors.find(function (a) {
99
- return a.node === _this2.source;
100
- });
101
- /* istanbul ignore if */
102
-
103
- if (!anchor || anchor.res === undefined) {
104
- var msg = 'This should not happen: Alias anchor was not resolved?';
105
- if (this.cstNode) throw new YAMLReferenceError(this.cstNode, msg);else throw new ReferenceError(msg);
106
- }
107
-
108
- if (maxAliasCount >= 0) {
109
- anchor.count += 1;
110
- if (anchor.aliasCount === 0) anchor.aliasCount = getAliasCount(this.source, anchors);
111
-
112
- if (anchor.count * anchor.aliasCount > maxAliasCount) {
113
- var _msg = 'Excessive alias count indicates a resource exhaustion attack';
114
- if (this.cstNode) throw new YAMLReferenceError(this.cstNode, _msg);else throw new ReferenceError(_msg);
115
- }
116
- }
117
-
118
- return anchor.res;
119
- } // Only called when stringifying an alias mapping key while constructing
120
- // Object output.
121
-
122
- }, {
123
- key: "toString",
124
- value: function toString(ctx) {
125
- return Alias.stringify(this, ctx);
126
- }
127
- }, {
128
- key: "tag",
129
- set: function set(t) {
130
- throw new Error('Alias nodes cannot have tags');
131
- }
132
- }]);
133
-
134
- return Alias;
135
- }(Node);
136
-
137
- _defineProperty(Alias, "default", true);
138
-
139
- export { Alias as default };
@@ -1,267 +0,0 @@
1
- import _toArray from "@babel/runtime/helpers/toArray";
2
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
- import _createClass from "@babel/runtime/helpers/createClass";
4
- import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
5
- import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
6
- import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
7
- import _inherits from "@babel/runtime/helpers/inherits";
8
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
9
- import _typeof from "@babel/runtime/helpers/typeof";
10
- import addComment from '../addComment';
11
- import Node from './Node';
12
- import Pair from './Pair';
13
- import Scalar from './Scalar';
14
-
15
- function collectionFromPath(schema, path, value) {
16
- var v = value;
17
-
18
- for (var i = path.length - 1; i >= 0; --i) {
19
- var k = path[i];
20
- var o = Number.isInteger(k) && k >= 0 ? [] : {};
21
- o[k] = v;
22
- v = o;
23
- }
24
-
25
- return schema.createNode(v, false);
26
- } // null, undefined, or an empty non-string iterable (e.g. [])
27
-
28
-
29
- export var isEmptyPath = function isEmptyPath(path) {
30
- return path == null || _typeof(path) === 'object' && path[Symbol.iterator]().next().done;
31
- };
32
-
33
- var Collection = /*#__PURE__*/function (_Node) {
34
- _inherits(Collection, _Node);
35
-
36
- function Collection(schema) {
37
- var _this;
38
-
39
- _classCallCheck(this, Collection);
40
-
41
- _this = _possibleConstructorReturn(this, _getPrototypeOf(Collection).call(this));
42
-
43
- _defineProperty(_assertThisInitialized(_this), "items", []);
44
-
45
- _this.schema = schema;
46
- return _this;
47
- }
48
-
49
- _createClass(Collection, [{
50
- key: "addIn",
51
- value: function addIn(path, value) {
52
- if (isEmptyPath(path)) this.add(value);else {
53
- var _path = _toArray(path),
54
- key = _path[0],
55
- rest = _path.slice(1);
56
-
57
- var node = this.get(key, true);
58
- if (node instanceof Collection) node.addIn(rest, value);else if (node === undefined && this.schema) this.set(key, collectionFromPath(this.schema, rest, value));else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest));
59
- }
60
- }
61
- }, {
62
- key: "deleteIn",
63
- value: function deleteIn(_ref) {
64
- var _ref2 = _toArray(_ref),
65
- key = _ref2[0],
66
- rest = _ref2.slice(1);
67
-
68
- if (rest.length === 0) return this.delete(key);
69
- var node = this.get(key, true);
70
- if (node instanceof Collection) return node.deleteIn(rest);else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest));
71
- }
72
- }, {
73
- key: "getIn",
74
- value: function getIn(_ref3, keepScalar) {
75
- var _ref4 = _toArray(_ref3),
76
- key = _ref4[0],
77
- rest = _ref4.slice(1);
78
-
79
- var node = this.get(key, true);
80
- if (rest.length === 0) return !keepScalar && node instanceof Scalar ? node.value : node;else return node instanceof Collection ? node.getIn(rest, keepScalar) : undefined;
81
- }
82
- }, {
83
- key: "hasAllNullValues",
84
- value: function hasAllNullValues() {
85
- return this.items.every(function (node) {
86
- if (!(node instanceof Pair)) return false;
87
- var n = node.value;
88
- return n == null || n instanceof Scalar && n.value == null && !n.commentBefore && !n.comment && !n.tag;
89
- });
90
- }
91
- }, {
92
- key: "hasIn",
93
- value: function hasIn(_ref5) {
94
- var _ref6 = _toArray(_ref5),
95
- key = _ref6[0],
96
- rest = _ref6.slice(1);
97
-
98
- if (rest.length === 0) return this.has(key);
99
- var node = this.get(key, true);
100
- return node instanceof Collection ? node.hasIn(rest) : false;
101
- }
102
- }, {
103
- key: "setIn",
104
- value: function setIn(_ref7, value) {
105
- var _ref8 = _toArray(_ref7),
106
- key = _ref8[0],
107
- rest = _ref8.slice(1);
108
-
109
- if (rest.length === 0) {
110
- this.set(key, value);
111
- } else {
112
- var node = this.get(key, true);
113
- if (node instanceof Collection) node.setIn(rest, value);else if (node === undefined && this.schema) this.set(key, collectionFromPath(this.schema, rest, value));else throw new Error("Expected YAML collection at ".concat(key, ". Remaining path: ").concat(rest));
114
- }
115
- } // overridden in implementations
116
-
117
- /* istanbul ignore next */
118
-
119
- }, {
120
- key: "toJSON",
121
- value: function toJSON() {
122
- return null;
123
- }
124
- }, {
125
- key: "toString",
126
- value: function toString(ctx, _ref9, onComment, onChompKeep) {
127
- var _this2 = this;
128
-
129
- var blockItem = _ref9.blockItem,
130
- flowChars = _ref9.flowChars,
131
- isMap = _ref9.isMap,
132
- itemIndent = _ref9.itemIndent;
133
- var _ctx = ctx,
134
- doc = _ctx.doc,
135
- indent = _ctx.indent;
136
- var inFlow = this.type && this.type.substr(0, 4) === 'FLOW' || ctx.inFlow;
137
- if (inFlow) itemIndent += ' ';
138
- var allNullValues = isMap && this.hasAllNullValues();
139
- ctx = Object.assign({}, ctx, {
140
- allNullValues: allNullValues,
141
- indent: itemIndent,
142
- inFlow: inFlow,
143
- type: null
144
- });
145
- var chompKeep = false;
146
- var hasItemWithNewLine = false;
147
- var nodes = this.items.reduce(function (nodes, item, i) {
148
- var comment;
149
-
150
- if (item) {
151
- if (!chompKeep && item.spaceBefore) nodes.push({
152
- type: 'comment',
153
- str: ''
154
- });
155
- if (item.commentBefore) item.commentBefore.match(/^.*$/gm).forEach(function (line) {
156
- nodes.push({
157
- type: 'comment',
158
- str: "#".concat(line)
159
- });
160
- });
161
- if (item.comment) comment = item.comment;
162
- if (inFlow && (!chompKeep && item.spaceBefore || item.commentBefore || item.comment || item.key && (item.key.commentBefore || item.key.comment) || item.value && (item.value.commentBefore || item.value.comment))) hasItemWithNewLine = true;
163
- }
164
-
165
- chompKeep = false;
166
- var str = doc.schema.stringify(item, ctx, function () {
167
- return comment = null;
168
- }, function () {
169
- return chompKeep = true;
170
- });
171
- if (inFlow && !hasItemWithNewLine && str.includes('\n')) hasItemWithNewLine = true;
172
- if (inFlow && i < _this2.items.length - 1) str += ',';
173
- str = addComment(str, itemIndent, comment);
174
- if (chompKeep && (comment || inFlow)) chompKeep = false;
175
- nodes.push({
176
- type: 'item',
177
- str: str
178
- });
179
- return nodes;
180
- }, []);
181
- var str;
182
-
183
- if (nodes.length === 0) {
184
- str = flowChars.start + flowChars.end;
185
- } else if (inFlow) {
186
- var start = flowChars.start,
187
- end = flowChars.end;
188
- var strings = nodes.map(function (n) {
189
- return n.str;
190
- });
191
-
192
- if (hasItemWithNewLine || strings.reduce(function (sum, str) {
193
- return sum + str.length + 2;
194
- }, 2) > Collection.maxFlowStringSingleLineLength) {
195
- str = start;
196
- var _iteratorNormalCompletion = true;
197
- var _didIteratorError = false;
198
- var _iteratorError = undefined;
199
-
200
- try {
201
- for (var _iterator = strings[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
202
- var s = _step.value;
203
- str += s ? "\n ".concat(indent).concat(s) : '\n';
204
- }
205
- } catch (err) {
206
- _didIteratorError = true;
207
- _iteratorError = err;
208
- } finally {
209
- try {
210
- if (!_iteratorNormalCompletion && _iterator.return != null) {
211
- _iterator.return();
212
- }
213
- } finally {
214
- if (_didIteratorError) {
215
- throw _iteratorError;
216
- }
217
- }
218
- }
219
-
220
- str += "\n".concat(indent).concat(end);
221
- } else {
222
- str = "".concat(start, " ").concat(strings.join(' '), " ").concat(end);
223
- }
224
- } else {
225
- var _strings = nodes.map(blockItem);
226
-
227
- str = _strings.shift();
228
- var _iteratorNormalCompletion2 = true;
229
- var _didIteratorError2 = false;
230
- var _iteratorError2 = undefined;
231
-
232
- try {
233
- for (var _iterator2 = _strings[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
234
- var _s = _step2.value;
235
- str += _s ? "\n".concat(indent).concat(_s) : '\n';
236
- }
237
- } catch (err) {
238
- _didIteratorError2 = true;
239
- _iteratorError2 = err;
240
- } finally {
241
- try {
242
- if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
243
- _iterator2.return();
244
- }
245
- } finally {
246
- if (_didIteratorError2) {
247
- throw _iteratorError2;
248
- }
249
- }
250
- }
251
- }
252
-
253
- if (this.comment) {
254
- str += '\n' + this.comment.replace(/^/gm, "".concat(indent, "#"));
255
- if (onComment) onComment();
256
- } else if (chompKeep && onChompKeep) onChompKeep();
257
-
258
- return str;
259
- }
260
- }]);
261
-
262
- return Collection;
263
- }(Node);
264
-
265
- _defineProperty(Collection, "maxFlowStringSingleLineLength", 60);
266
-
267
- export { Collection as default };
@@ -1,178 +0,0 @@
1
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
- import _createClass from "@babel/runtime/helpers/createClass";
3
- import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
4
- import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
5
- import _get from "@babel/runtime/helpers/get";
6
- import _inherits from "@babel/runtime/helpers/inherits";
7
- import Collection from './Collection';
8
- import Pair from './Pair';
9
- import Scalar from './Scalar';
10
- export function findPair(items, key) {
11
- var k = key instanceof Scalar ? key.value : key;
12
- var _iteratorNormalCompletion = true;
13
- var _didIteratorError = false;
14
- var _iteratorError = undefined;
15
-
16
- try {
17
- for (var _iterator = items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
18
- var it = _step.value;
19
-
20
- if (it instanceof Pair) {
21
- if (it.key === key || it.key === k) return it;
22
- if (it.key && it.key.value === k) return it;
23
- }
24
- }
25
- } catch (err) {
26
- _didIteratorError = true;
27
- _iteratorError = err;
28
- } finally {
29
- try {
30
- if (!_iteratorNormalCompletion && _iterator.return != null) {
31
- _iterator.return();
32
- }
33
- } finally {
34
- if (_didIteratorError) {
35
- throw _iteratorError;
36
- }
37
- }
38
- }
39
-
40
- return undefined;
41
- }
42
-
43
- var YAMLMap = /*#__PURE__*/function (_Collection) {
44
- _inherits(YAMLMap, _Collection);
45
-
46
- function YAMLMap() {
47
- _classCallCheck(this, YAMLMap);
48
-
49
- return _possibleConstructorReturn(this, _getPrototypeOf(YAMLMap).apply(this, arguments));
50
- }
51
-
52
- _createClass(YAMLMap, [{
53
- key: "add",
54
- value: function add(pair, overwrite) {
55
- if (!pair) pair = new Pair(pair);else if (!(pair instanceof Pair)) pair = new Pair(pair.key || pair, pair.value);
56
- var prev = findPair(this.items, pair.key);
57
- var sortEntries = this.schema && this.schema.sortMapEntries;
58
-
59
- if (prev) {
60
- if (overwrite) prev.value = pair.value;else throw new Error("Key ".concat(pair.key, " already set"));
61
- } else if (sortEntries) {
62
- var i = this.items.findIndex(function (item) {
63
- return sortEntries(pair, item) < 0;
64
- });
65
- if (i === -1) this.items.push(pair);else this.items.splice(i, 0, pair);
66
- } else {
67
- this.items.push(pair);
68
- }
69
- }
70
- }, {
71
- key: "delete",
72
- value: function _delete(key) {
73
- var it = findPair(this.items, key);
74
- if (!it) return false;
75
- var del = this.items.splice(this.items.indexOf(it), 1);
76
- return del.length > 0;
77
- }
78
- }, {
79
- key: "get",
80
- value: function get(key, keepScalar) {
81
- var it = findPair(this.items, key);
82
- var node = it && it.value;
83
- return !keepScalar && node instanceof Scalar ? node.value : node;
84
- }
85
- }, {
86
- key: "has",
87
- value: function has(key) {
88
- return !!findPair(this.items, key);
89
- }
90
- }, {
91
- key: "set",
92
- value: function set(key, value) {
93
- this.add(new Pair(key, value), true);
94
- }
95
- /**
96
- * @param {*} arg ignored
97
- * @param {*} ctx Conversion context, originally set in Document#toJSON()
98
- * @param {Class} Type If set, forces the returned collection type
99
- * @returns {*} Instance of Type, Map, or Object
100
- */
101
-
102
- }, {
103
- key: "toJSON",
104
- value: function toJSON(_, ctx, Type) {
105
- var map = Type ? new Type() : ctx && ctx.mapAsMap ? new Map() : {};
106
- if (ctx && ctx.onCreate) ctx.onCreate(map);
107
- var _iteratorNormalCompletion2 = true;
108
- var _didIteratorError2 = false;
109
- var _iteratorError2 = undefined;
110
-
111
- try {
112
- for (var _iterator2 = this.items[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
113
- var item = _step2.value;
114
- item.addToJSMap(ctx, map);
115
- }
116
- } catch (err) {
117
- _didIteratorError2 = true;
118
- _iteratorError2 = err;
119
- } finally {
120
- try {
121
- if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
122
- _iterator2.return();
123
- }
124
- } finally {
125
- if (_didIteratorError2) {
126
- throw _iteratorError2;
127
- }
128
- }
129
- }
130
-
131
- return map;
132
- }
133
- }, {
134
- key: "toString",
135
- value: function toString(ctx, onComment, onChompKeep) {
136
- if (!ctx) return JSON.stringify(this);
137
- var _iteratorNormalCompletion3 = true;
138
- var _didIteratorError3 = false;
139
- var _iteratorError3 = undefined;
140
-
141
- try {
142
- for (var _iterator3 = this.items[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
143
- var item = _step3.value;
144
- if (!(item instanceof Pair)) throw new Error("Map items must all be pairs; found ".concat(JSON.stringify(item), " instead"));
145
- }
146
- } catch (err) {
147
- _didIteratorError3 = true;
148
- _iteratorError3 = err;
149
- } finally {
150
- try {
151
- if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
152
- _iterator3.return();
153
- }
154
- } finally {
155
- if (_didIteratorError3) {
156
- throw _iteratorError3;
157
- }
158
- }
159
- }
160
-
161
- return _get(_getPrototypeOf(YAMLMap.prototype), "toString", this).call(this, ctx, {
162
- blockItem: function blockItem(n) {
163
- return n.str;
164
- },
165
- flowChars: {
166
- start: '{',
167
- end: '}'
168
- },
169
- isMap: true,
170
- itemIndent: ctx.indent || ''
171
- }, onComment, onChompKeep);
172
- }
173
- }]);
174
-
175
- return YAMLMap;
176
- }(Collection);
177
-
178
- export { YAMLMap as default };