yaml 2.0.0-4 → 2.0.0-8

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 (220) hide show
  1. package/README.md +5 -5
  2. package/browser/dist/compose/compose-collection.js +12 -9
  3. package/browser/dist/compose/compose-doc.js +23 -6
  4. package/browser/dist/compose/compose-node.js +27 -17
  5. package/browser/dist/compose/compose-scalar.js +21 -23
  6. package/browser/dist/compose/composer.js +108 -78
  7. package/browser/dist/compose/resolve-block-map.js +41 -29
  8. package/browser/dist/compose/resolve-block-scalar.js +14 -17
  9. package/browser/dist/compose/resolve-block-seq.js +21 -17
  10. package/browser/dist/compose/resolve-end.js +7 -8
  11. package/browser/dist/compose/resolve-flow-collection.js +158 -199
  12. package/browser/dist/compose/resolve-flow-scalar.js +62 -26
  13. package/browser/dist/compose/resolve-props.js +72 -37
  14. package/browser/dist/compose/util-contains-newline.js +15 -12
  15. package/browser/dist/compose/util-map-includes.js +17 -0
  16. package/browser/dist/doc/Document.js +74 -99
  17. package/browser/dist/doc/anchors.js +72 -0
  18. package/browser/dist/doc/createNode.js +35 -19
  19. package/browser/dist/doc/directives.js +5 -0
  20. package/browser/dist/errors.js +16 -12
  21. package/browser/dist/index.js +3 -3
  22. package/browser/dist/nodes/Alias.js +51 -28
  23. package/browser/dist/nodes/Collection.js +23 -92
  24. package/browser/dist/nodes/Node.js +9 -1
  25. package/browser/dist/nodes/Pair.js +16 -231
  26. package/browser/dist/nodes/YAMLMap.js +8 -4
  27. package/browser/dist/nodes/YAMLSeq.js +7 -4
  28. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  29. package/browser/dist/nodes/toJS.js +12 -10
  30. package/browser/dist/options.js +1 -2
  31. package/browser/dist/parse/cst-scalar.js +219 -0
  32. package/browser/dist/parse/cst-stringify.js +61 -0
  33. package/browser/dist/parse/cst-visit.js +97 -0
  34. package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
  35. package/browser/dist/parse/lexer.js +149 -125
  36. package/browser/dist/parse/parser.js +272 -196
  37. package/browser/dist/public-api.js +11 -14
  38. package/browser/dist/schema/Schema.js +28 -0
  39. package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
  40. package/browser/dist/schema/common/null.js +13 -0
  41. package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  42. package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
  43. package/browser/dist/schema/core/bool.js +19 -0
  44. package/browser/dist/schema/core/float.js +40 -0
  45. package/browser/dist/schema/core/int.js +38 -0
  46. package/browser/dist/schema/core/schema.js +23 -0
  47. package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
  48. package/browser/dist/schema/tags.js +79 -0
  49. package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  50. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  51. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  52. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  53. package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  54. package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  55. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  56. package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  57. package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  58. package/browser/dist/stringify/stringify.js +8 -6
  59. package/browser/dist/stringify/stringifyCollection.js +122 -0
  60. package/browser/dist/stringify/stringifyComment.js +14 -0
  61. package/browser/dist/stringify/stringifyDocument.js +64 -0
  62. package/browser/dist/stringify/stringifyPair.js +111 -0
  63. package/browser/dist/stringify/stringifyString.js +53 -52
  64. package/browser/dist/visit.js +20 -1
  65. package/dist/compose/compose-collection.d.ts +4 -4
  66. package/dist/compose/compose-collection.js +12 -9
  67. package/dist/compose/compose-doc.d.ts +4 -3
  68. package/dist/compose/compose-doc.js +23 -6
  69. package/dist/compose/compose-node.d.ts +15 -7
  70. package/dist/compose/compose-node.js +27 -17
  71. package/dist/compose/compose-scalar.d.ts +4 -3
  72. package/dist/compose/compose-scalar.js +20 -22
  73. package/dist/compose/composer.d.ts +28 -23
  74. package/dist/compose/composer.js +109 -79
  75. package/dist/compose/resolve-block-map.d.ts +5 -4
  76. package/dist/compose/resolve-block-map.js +41 -29
  77. package/dist/compose/resolve-block-scalar.d.ts +5 -3
  78. package/dist/compose/resolve-block-scalar.js +14 -17
  79. package/dist/compose/resolve-block-seq.d.ts +4 -4
  80. package/dist/compose/resolve-block-seq.js +21 -17
  81. package/dist/compose/resolve-end.d.ts +3 -2
  82. package/dist/compose/resolve-end.js +7 -8
  83. package/dist/compose/resolve-flow-collection.d.ts +4 -5
  84. package/dist/compose/resolve-flow-collection.js +157 -198
  85. package/dist/compose/resolve-flow-scalar.d.ts +5 -3
  86. package/dist/compose/resolve-flow-scalar.js +62 -26
  87. package/dist/compose/resolve-props.d.ts +16 -10
  88. package/dist/compose/resolve-props.js +72 -37
  89. package/dist/compose/util-contains-newline.d.ts +1 -1
  90. package/dist/compose/util-contains-newline.js +15 -12
  91. package/dist/compose/util-empty-scalar-position.d.ts +1 -1
  92. package/dist/compose/util-map-includes.d.ts +4 -0
  93. package/dist/compose/util-map-includes.js +19 -0
  94. package/dist/doc/Document.d.ts +35 -15
  95. package/dist/doc/Document.js +72 -97
  96. package/dist/doc/anchors.d.ts +24 -0
  97. package/dist/doc/anchors.js +77 -0
  98. package/dist/doc/createNode.d.ts +10 -11
  99. package/dist/doc/createNode.js +34 -18
  100. package/dist/doc/directives.d.ts +1 -0
  101. package/dist/doc/directives.js +5 -0
  102. package/dist/errors.d.ts +11 -8
  103. package/dist/errors.js +16 -12
  104. package/dist/index.d.ts +5 -3
  105. package/dist/index.js +3 -3
  106. package/dist/nodes/Alias.d.ts +19 -9
  107. package/dist/nodes/Alias.js +51 -28
  108. package/dist/nodes/Collection.d.ts +9 -17
  109. package/dist/nodes/Collection.js +22 -91
  110. package/dist/nodes/Node.d.ts +10 -4
  111. package/dist/nodes/Node.js +9 -0
  112. package/dist/nodes/Pair.d.ts +10 -13
  113. package/dist/nodes/Pair.js +15 -230
  114. package/dist/nodes/Scalar.d.ts +6 -3
  115. package/dist/nodes/YAMLMap.d.ts +3 -3
  116. package/dist/nodes/YAMLMap.js +8 -4
  117. package/dist/nodes/YAMLSeq.d.ts +3 -3
  118. package/dist/nodes/YAMLSeq.js +7 -4
  119. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  120. package/dist/nodes/addPairToJSMap.js +105 -0
  121. package/dist/nodes/toJS.d.ts +4 -5
  122. package/dist/nodes/toJS.js +12 -10
  123. package/dist/options.d.ts +41 -25
  124. package/dist/options.js +1 -2
  125. package/dist/parse/cst-scalar.d.ts +58 -0
  126. package/dist/parse/cst-scalar.js +223 -0
  127. package/dist/parse/cst-stringify.d.ts +8 -0
  128. package/dist/parse/cst-stringify.js +63 -0
  129. package/dist/parse/cst-visit.d.ts +39 -0
  130. package/dist/parse/cst-visit.js +99 -0
  131. package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
  132. package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
  133. package/dist/parse/lexer.d.ts +7 -14
  134. package/dist/parse/lexer.js +150 -126
  135. package/dist/parse/parser.d.ts +31 -24
  136. package/dist/parse/parser.js +274 -198
  137. package/dist/public-api.d.ts +7 -7
  138. package/dist/public-api.js +11 -14
  139. package/dist/{doc → schema}/Schema.d.ts +6 -3
  140. package/dist/schema/Schema.js +30 -0
  141. package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
  142. package/dist/{tags/failsafe → schema/common}/map.js +0 -0
  143. package/dist/schema/common/null.d.ts +4 -0
  144. package/dist/schema/common/null.js +15 -0
  145. package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
  146. package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  147. package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
  148. package/dist/{tags/failsafe → schema/common}/string.js +0 -0
  149. package/dist/schema/core/bool.d.ts +4 -0
  150. package/dist/schema/core/bool.js +21 -0
  151. package/dist/schema/core/float.d.ts +4 -0
  152. package/dist/schema/core/float.js +44 -0
  153. package/dist/schema/core/int.d.ts +4 -0
  154. package/dist/schema/core/int.js +42 -0
  155. package/dist/schema/core/schema.d.ts +1 -0
  156. package/dist/schema/core/schema.js +25 -0
  157. package/dist/schema/json/schema.d.ts +2 -0
  158. package/dist/{tags/json.js → schema/json/schema.js} +5 -6
  159. package/dist/schema/tags.d.ts +41 -0
  160. package/dist/schema/tags.js +82 -0
  161. package/dist/{tags → schema}/types.d.ts +3 -7
  162. package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
  163. package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  164. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  165. package/dist/schema/yaml-1.1/bool.js +29 -0
  166. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  167. package/dist/schema/yaml-1.1/float.js +47 -0
  168. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  169. package/dist/schema/yaml-1.1/int.js +76 -0
  170. package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
  171. package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  172. package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
  173. package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  174. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  175. package/dist/schema/yaml-1.1/schema.js +39 -0
  176. package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
  177. package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  178. package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
  179. package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  180. package/dist/stringify/stringify.d.ts +1 -2
  181. package/dist/stringify/stringify.js +8 -6
  182. package/dist/stringify/stringifyCollection.d.ts +21 -0
  183. package/dist/stringify/stringifyCollection.js +124 -0
  184. package/dist/stringify/stringifyComment.d.ts +2 -0
  185. package/dist/stringify/stringifyComment.js +17 -0
  186. package/dist/stringify/stringifyDocument.d.ts +3 -0
  187. package/dist/stringify/stringifyDocument.js +66 -0
  188. package/dist/stringify/stringifyPair.d.ts +3 -0
  189. package/dist/stringify/stringifyPair.js +113 -0
  190. package/dist/stringify/stringifyString.js +53 -52
  191. package/dist/test-events.d.ts +1 -2
  192. package/dist/test-events.js +25 -12
  193. package/dist/util.d.ts +1 -1
  194. package/dist/visit.d.ts +10 -1
  195. package/dist/visit.js +20 -1
  196. package/package.json +9 -8
  197. package/browser/dist/doc/Anchors.js +0 -102
  198. package/browser/dist/doc/Schema.js +0 -27
  199. package/browser/dist/doc/getSchemaTags.js +0 -30
  200. package/browser/dist/stringify/addComment.js +0 -17
  201. package/browser/dist/tags/core.js +0 -109
  202. package/browser/dist/tags/failsafe/index.js +0 -7
  203. package/browser/dist/tags/index.js +0 -34
  204. package/browser/dist/tags/yaml-1.1/index.js +0 -152
  205. package/dist/doc/Anchors.d.ts +0 -36
  206. package/dist/doc/Anchors.js +0 -104
  207. package/dist/doc/Schema.js +0 -29
  208. package/dist/doc/getSchemaTags.d.ts +0 -3
  209. package/dist/doc/getSchemaTags.js +0 -32
  210. package/dist/stringify/addComment.d.ts +0 -2
  211. package/dist/stringify/addComment.js +0 -20
  212. package/dist/tags/core.d.ts +0 -14
  213. package/dist/tags/core.js +0 -119
  214. package/dist/tags/failsafe/index.d.ts +0 -1
  215. package/dist/tags/failsafe/index.js +0 -9
  216. package/dist/tags/index.d.ts +0 -31
  217. package/dist/tags/index.js +0 -37
  218. package/dist/tags/json.d.ts +0 -2
  219. package/dist/tags/yaml-1.1/index.d.ts +0 -2
  220. package/dist/tags/yaml-1.1/index.js +0 -154
@@ -21,13 +21,11 @@ function parseOptions(options) {
21
21
  * EmptyStream and contain additional stream information. In
22
22
  * TypeScript, you should use `'empty' in docs` as a type guard for it.
23
23
  */
24
- function parseAllDocuments(source, options) {
24
+ function parseAllDocuments(source, options = {}) {
25
25
  const { lineCounter, prettyErrors } = parseOptions(options);
26
- const docs = [];
27
- const composer = new Composer(doc => docs.push(doc), options);
28
- const parser = new Parser(composer.next, lineCounter === null || lineCounter === void 0 ? void 0 : lineCounter.addNewLine);
29
- parser.parse(source);
30
- composer.end();
26
+ const parser = new Parser(lineCounter === null || lineCounter === void 0 ? void 0 : lineCounter.addNewLine);
27
+ const composer = new Composer(options);
28
+ const docs = Array.from(composer.compose(parser.parse(source)));
31
29
  if (prettyErrors && lineCounter)
32
30
  for (const doc of docs) {
33
31
  doc.errors.forEach(prettifyError(source, lineCounter));
@@ -38,21 +36,20 @@ function parseAllDocuments(source, options) {
38
36
  return Object.assign([], { empty: true }, composer.streamInfo());
39
37
  }
40
38
  /** Parse an input string into a single YAML.Document */
41
- function parseDocument(source, options) {
39
+ function parseDocument(source, options = {}) {
42
40
  const { lineCounter, prettyErrors } = parseOptions(options);
41
+ const parser = new Parser(lineCounter === null || lineCounter === void 0 ? void 0 : lineCounter.addNewLine);
42
+ const composer = new Composer(options);
43
43
  // `doc` is always set by compose.end(true) at the very latest
44
44
  let doc = null;
45
- const composer = new Composer(_doc => {
45
+ for (const _doc of composer.compose(parser.parse(source), true, source.length)) {
46
46
  if (!doc)
47
47
  doc = _doc;
48
48
  else if (doc.options.logLevel !== 'silent') {
49
- const errMsg = 'Source contains multiple documents; please use YAML.parseAllDocuments()';
50
- doc.errors.push(new YAMLParseError(_doc.range[0], errMsg));
49
+ doc.errors.push(new YAMLParseError(_doc.range.slice(0, 2), 'MULTIPLE_DOCS', 'Source contains multiple documents; please use YAML.parseAllDocuments()'));
50
+ break;
51
51
  }
52
- }, options);
53
- const parser = new Parser(composer.next, lineCounter === null || lineCounter === void 0 ? void 0 : lineCounter.addNewLine);
54
- parser.parse(source);
55
- composer.end(true, source.length);
52
+ }
56
53
  if (prettyErrors && lineCounter) {
57
54
  doc.errors.forEach(prettifyError(source, lineCounter));
58
55
  doc.warnings.forEach(prettifyError(source, lineCounter));
@@ -0,0 +1,28 @@
1
+ import { MAP, SCALAR, SEQ } from '../nodes/Node.js';
2
+ import { map } from './common/map.js';
3
+ import { seq } from './common/seq.js';
4
+ import { string } from './common/string.js';
5
+ import { coreKnownTags, getTags } from './tags.js';
6
+
7
+ const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
8
+ class Schema {
9
+ constructor({ customTags, merge, resolveKnownTags, schema, sortMapEntries }) {
10
+ this.merge = !!merge;
11
+ this.name = schema || 'core';
12
+ this.knownTags = resolveKnownTags ? coreKnownTags : {};
13
+ this.tags = getTags(customTags, this.name);
14
+ Object.defineProperty(this, MAP, { value: map });
15
+ Object.defineProperty(this, SCALAR, { value: string });
16
+ Object.defineProperty(this, SEQ, { value: seq });
17
+ // Used by createMap()
18
+ this.sortMapEntries =
19
+ sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
20
+ }
21
+ clone() {
22
+ const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
23
+ copy.tags = this.tags.slice();
24
+ return copy;
25
+ }
26
+ }
27
+
28
+ export { Schema };
@@ -0,0 +1,13 @@
1
+ import { Scalar } from '../../nodes/Scalar.js';
2
+
3
+ const nullTag = {
4
+ identify: value => value == null,
5
+ createNode: () => new Scalar(null),
6
+ default: true,
7
+ tag: 'tag:yaml.org,2002:null',
8
+ test: /^(?:~|[Nn]ull|NULL)?$/,
9
+ resolve: () => new Scalar(null),
10
+ stringify: ({ source }, ctx) => source && nullTag.test.test(source) ? source : ctx.options.nullStr
11
+ };
12
+
13
+ export { nullTag };
@@ -1,6 +1,6 @@
1
+ import { createNode } from '../../doc/createNode.js';
1
2
  import { isSeq } from '../../nodes/Node.js';
2
3
  import { YAMLSeq } from '../../nodes/YAMLSeq.js';
3
- import { createNode } from '../../doc/createNode.js';
4
4
 
5
5
  function createSeq(schema, obj, ctx) {
6
6
  const { replacer } = ctx;
@@ -0,0 +1,19 @@
1
+ import { Scalar } from '../../nodes/Scalar.js';
2
+
3
+ const boolTag = {
4
+ identify: value => typeof value === 'boolean',
5
+ default: true,
6
+ tag: 'tag:yaml.org,2002:bool',
7
+ test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
8
+ resolve: str => new Scalar(str[0] === 't' || str[0] === 'T'),
9
+ stringify({ source, value }, ctx) {
10
+ if (source && boolTag.test.test(source)) {
11
+ const sv = source[0] === 't' || source[0] === 'T';
12
+ if (value === sv)
13
+ return source;
14
+ }
15
+ return value ? ctx.options.trueStr : ctx.options.falseStr;
16
+ }
17
+ };
18
+
19
+ export { boolTag };
@@ -0,0 +1,40 @@
1
+ import { Scalar } from '../../nodes/Scalar.js';
2
+ import { stringifyNumber } from '../../stringify/stringifyNumber.js';
3
+
4
+ const floatNaN = {
5
+ identify: value => typeof value === 'number',
6
+ default: true,
7
+ tag: 'tag:yaml.org,2002:float',
8
+ test: /^(?:[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN))$/,
9
+ resolve: str => str.slice(-3).toLowerCase() === 'nan'
10
+ ? NaN
11
+ : str[0] === '-'
12
+ ? Number.NEGATIVE_INFINITY
13
+ : Number.POSITIVE_INFINITY,
14
+ stringify: stringifyNumber
15
+ };
16
+ const floatExp = {
17
+ identify: value => typeof value === 'number',
18
+ default: true,
19
+ tag: 'tag:yaml.org,2002:float',
20
+ format: 'EXP',
21
+ test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
22
+ resolve: str => parseFloat(str),
23
+ stringify: ({ value }) => Number(value).toExponential()
24
+ };
25
+ const float = {
26
+ identify: value => typeof value === 'number',
27
+ default: true,
28
+ tag: 'tag:yaml.org,2002:float',
29
+ test: /^[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*)$/,
30
+ resolve(str) {
31
+ const node = new Scalar(parseFloat(str));
32
+ const dot = str.indexOf('.');
33
+ if (dot !== -1 && str[str.length - 1] === '0')
34
+ node.minFractionDigits = str.length - dot - 1;
35
+ return node;
36
+ },
37
+ stringify: stringifyNumber
38
+ };
39
+
40
+ export { float, floatExp, floatNaN };
@@ -0,0 +1,38 @@
1
+ import { stringifyNumber } from '../../stringify/stringifyNumber.js';
2
+
3
+ const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
4
+ const intResolve = (str, offset, radix, { intAsBigInt }) => (intAsBigInt ? BigInt(str) : parseInt(str.substring(offset), radix));
5
+ function intStringify(node, radix, prefix) {
6
+ const { value } = node;
7
+ if (intIdentify(value) && value >= 0)
8
+ return prefix + value.toString(radix);
9
+ return stringifyNumber(node);
10
+ }
11
+ const intOct = {
12
+ identify: value => intIdentify(value) && value >= 0,
13
+ default: true,
14
+ tag: 'tag:yaml.org,2002:int',
15
+ format: 'OCT',
16
+ test: /^0o[0-7]+$/,
17
+ resolve: (str, _onError, opt) => intResolve(str, 2, 8, opt),
18
+ stringify: node => intStringify(node, 8, '0o')
19
+ };
20
+ const int = {
21
+ identify: intIdentify,
22
+ default: true,
23
+ tag: 'tag:yaml.org,2002:int',
24
+ test: /^[-+]?[0-9]+$/,
25
+ resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
26
+ stringify: stringifyNumber
27
+ };
28
+ const intHex = {
29
+ identify: value => intIdentify(value) && value >= 0,
30
+ default: true,
31
+ tag: 'tag:yaml.org,2002:int',
32
+ format: 'HEX',
33
+ test: /^0x[0-9a-fA-F]+$/,
34
+ resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
35
+ stringify: node => intStringify(node, 16, '0x')
36
+ };
37
+
38
+ export { int, intHex, intOct };
@@ -0,0 +1,23 @@
1
+ import { map } from '../common/map.js';
2
+ import { nullTag } from '../common/null.js';
3
+ import { seq } from '../common/seq.js';
4
+ import { string } from '../common/string.js';
5
+ import { boolTag } from './bool.js';
6
+ import { floatNaN, floatExp, float } from './float.js';
7
+ import { intOct, int, intHex } from './int.js';
8
+
9
+ const schema = [
10
+ map,
11
+ seq,
12
+ string,
13
+ nullTag,
14
+ boolTag,
15
+ intOct,
16
+ int,
17
+ intHex,
18
+ floatNaN,
19
+ floatExp,
20
+ float
21
+ ];
22
+
23
+ export { schema };
@@ -1,8 +1,7 @@
1
- import { Scalar } from '../nodes/Scalar.js';
2
- import { map } from './failsafe/map.js';
3
- import { seq } from './failsafe/seq.js';
1
+ import { Scalar } from '../../nodes/Scalar.js';
2
+ import { map } from '../common/map.js';
3
+ import { seq } from '../common/seq.js';
4
4
 
5
- /* global BigInt */
6
5
  function intIdentify(value) {
7
6
  return typeof value === 'bigint' || Number.isInteger(value);
8
7
  }
@@ -58,6 +57,6 @@ const jsonError = {
58
57
  return str;
59
58
  }
60
59
  };
61
- const json = [map, seq].concat(jsonScalars, jsonError);
60
+ const schema = [map, seq].concat(jsonScalars, jsonError);
62
61
 
63
- export { json };
62
+ export { schema };
@@ -0,0 +1,79 @@
1
+ import { map } from './common/map.js';
2
+ import { nullTag } from './common/null.js';
3
+ import { seq } from './common/seq.js';
4
+ import { string } from './common/string.js';
5
+ import { boolTag } from './core/bool.js';
6
+ import { float, floatExp, floatNaN } from './core/float.js';
7
+ import { int, intHex, intOct } from './core/int.js';
8
+ import { schema } from './core/schema.js';
9
+ import { schema as schema$1 } from './json/schema.js';
10
+ import { binary } from './yaml-1.1/binary.js';
11
+ import { omap } from './yaml-1.1/omap.js';
12
+ import { pairs } from './yaml-1.1/pairs.js';
13
+ import { schema as schema$2 } from './yaml-1.1/schema.js';
14
+ import { set } from './yaml-1.1/set.js';
15
+ import { floatTime, intTime, timestamp } from './yaml-1.1/timestamp.js';
16
+
17
+ const schemas = {
18
+ core: schema,
19
+ failsafe: [map, seq, string],
20
+ json: schema$1,
21
+ yaml11: schema$2,
22
+ 'yaml-1.1': schema$2
23
+ };
24
+ const tagsByName = {
25
+ binary,
26
+ bool: boolTag,
27
+ float,
28
+ floatExp,
29
+ floatNaN,
30
+ floatTime,
31
+ int,
32
+ intHex,
33
+ intOct,
34
+ intTime,
35
+ map,
36
+ null: nullTag,
37
+ omap,
38
+ pairs,
39
+ seq,
40
+ set,
41
+ timestamp
42
+ };
43
+ const coreKnownTags = {
44
+ 'tag:yaml.org,2002:binary': binary,
45
+ 'tag:yaml.org,2002:omap': omap,
46
+ 'tag:yaml.org,2002:pairs': pairs,
47
+ 'tag:yaml.org,2002:set': set,
48
+ 'tag:yaml.org,2002:timestamp': timestamp
49
+ };
50
+ function getTags(customTags, schemaName) {
51
+ let tags = schemas[schemaName];
52
+ if (!tags) {
53
+ const keys = Object.keys(schemas)
54
+ .filter(key => key !== 'yaml11')
55
+ .map(key => JSON.stringify(key))
56
+ .join(', ');
57
+ throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`);
58
+ }
59
+ if (Array.isArray(customTags)) {
60
+ for (const tag of customTags)
61
+ tags = tags.concat(tag);
62
+ }
63
+ else if (typeof customTags === 'function') {
64
+ tags = customTags(tags.slice());
65
+ }
66
+ return tags.map(tag => {
67
+ if (typeof tag !== 'string')
68
+ return tag;
69
+ const tagObj = tagsByName[tag];
70
+ if (tagObj)
71
+ return tagObj;
72
+ const keys = Object.keys(tagsByName)
73
+ .map(key => JSON.stringify(key))
74
+ .join(', ');
75
+ throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
76
+ });
77
+ }
78
+
79
+ export { coreKnownTags, getTags };
File without changes
@@ -0,0 +1,26 @@
1
+ import { Scalar } from '../../nodes/Scalar.js';
2
+
3
+ function boolStringify({ value, source }, ctx) {
4
+ const boolObj = value ? trueTag : falseTag;
5
+ if (source && boolObj.test.test(source))
6
+ return source;
7
+ return value ? ctx.options.trueStr : ctx.options.falseStr;
8
+ }
9
+ const trueTag = {
10
+ identify: value => value === true,
11
+ default: true,
12
+ tag: 'tag:yaml.org,2002:bool',
13
+ test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
14
+ resolve: () => new Scalar(true),
15
+ stringify: boolStringify
16
+ };
17
+ const falseTag = {
18
+ identify: value => value === false,
19
+ default: true,
20
+ tag: 'tag:yaml.org,2002:bool',
21
+ test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/i,
22
+ resolve: () => new Scalar(false),
23
+ stringify: boolStringify
24
+ };
25
+
26
+ export { falseTag, trueTag };
@@ -0,0 +1,43 @@
1
+ import { Scalar } from '../../nodes/Scalar.js';
2
+ import { stringifyNumber } from '../../stringify/stringifyNumber.js';
3
+
4
+ const floatNaN = {
5
+ identify: value => typeof value === 'number',
6
+ default: true,
7
+ tag: 'tag:yaml.org,2002:float',
8
+ test: /^[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/,
9
+ resolve: (str) => str.slice(-3).toLowerCase() === 'nan'
10
+ ? NaN
11
+ : str[0] === '-'
12
+ ? Number.NEGATIVE_INFINITY
13
+ : Number.POSITIVE_INFINITY,
14
+ stringify: stringifyNumber
15
+ };
16
+ const floatExp = {
17
+ identify: value => typeof value === 'number',
18
+ default: true,
19
+ tag: 'tag:yaml.org,2002:float',
20
+ format: 'EXP',
21
+ test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
22
+ resolve: (str) => parseFloat(str.replace(/_/g, '')),
23
+ stringify: ({ value }) => Number(value).toExponential()
24
+ };
25
+ const float = {
26
+ identify: value => typeof value === 'number',
27
+ default: true,
28
+ tag: 'tag:yaml.org,2002:float',
29
+ test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*$/,
30
+ resolve(str) {
31
+ const node = new Scalar(parseFloat(str.replace(/_/g, '')));
32
+ const dot = str.indexOf('.');
33
+ if (dot !== -1) {
34
+ const f = str.substring(dot + 1).replace(/_/g, '');
35
+ if (f[f.length - 1] === '0')
36
+ node.minFractionDigits = f.length;
37
+ }
38
+ return node;
39
+ },
40
+ stringify: stringifyNumber
41
+ };
42
+
43
+ export { float, floatExp, floatNaN };
@@ -0,0 +1,71 @@
1
+ import { stringifyNumber } from '../../stringify/stringifyNumber.js';
2
+
3
+ const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
4
+ function intResolve(str, offset, radix, { intAsBigInt }) {
5
+ const sign = str[0];
6
+ if (sign === '-' || sign === '+')
7
+ offset += 1;
8
+ str = str.substring(offset).replace(/_/g, '');
9
+ if (intAsBigInt) {
10
+ switch (radix) {
11
+ case 2:
12
+ str = `0b${str}`;
13
+ break;
14
+ case 8:
15
+ str = `0o${str}`;
16
+ break;
17
+ case 16:
18
+ str = `0x${str}`;
19
+ break;
20
+ }
21
+ const n = BigInt(str);
22
+ return sign === '-' ? BigInt(-1) * n : n;
23
+ }
24
+ const n = parseInt(str, radix);
25
+ return sign === '-' ? -1 * n : n;
26
+ }
27
+ function intStringify(node, radix, prefix) {
28
+ const { value } = node;
29
+ if (intIdentify(value)) {
30
+ const str = value.toString(radix);
31
+ return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
32
+ }
33
+ return stringifyNumber(node);
34
+ }
35
+ const intBin = {
36
+ identify: intIdentify,
37
+ default: true,
38
+ tag: 'tag:yaml.org,2002:int',
39
+ format: 'BIN',
40
+ test: /^[-+]?0b[0-1_]+$/,
41
+ resolve: (str, _onError, opt) => intResolve(str, 2, 2, opt),
42
+ stringify: node => intStringify(node, 2, '0b')
43
+ };
44
+ const intOct = {
45
+ identify: intIdentify,
46
+ default: true,
47
+ tag: 'tag:yaml.org,2002:int',
48
+ format: 'OCT',
49
+ test: /^[-+]?0[0-7_]+$/,
50
+ resolve: (str, _onError, opt) => intResolve(str, 1, 8, opt),
51
+ stringify: node => intStringify(node, 8, '0')
52
+ };
53
+ const int = {
54
+ identify: intIdentify,
55
+ default: true,
56
+ tag: 'tag:yaml.org,2002:int',
57
+ test: /^[-+]?[0-9][0-9_]*$/,
58
+ resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
59
+ stringify: stringifyNumber
60
+ };
61
+ const intHex = {
62
+ identify: intIdentify,
63
+ default: true,
64
+ tag: 'tag:yaml.org,2002:int',
65
+ format: 'HEX',
66
+ test: /^[-+]?0x[0-9a-fA-F_]+$/,
67
+ resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
68
+ stringify: node => intStringify(node, 16, '0x')
69
+ };
70
+
71
+ export { int, intBin, intHex, intOct };
File without changes
@@ -1,5 +1,6 @@
1
1
  import { isSeq, isPair, isMap } from '../../nodes/Node.js';
2
2
  import { Pair, createPair } from '../../nodes/Pair.js';
3
+ import { Scalar } from '../../nodes/Scalar.js';
3
4
  import { YAMLSeq } from '../../nodes/YAMLSeq.js';
4
5
 
5
6
  function resolvePairs(seq, onError) {
@@ -11,15 +12,17 @@ function resolvePairs(seq, onError) {
11
12
  else if (isMap(item)) {
12
13
  if (item.items.length > 1)
13
14
  onError('Each pair must have its own sequence indicator');
14
- const pair = item.items[0] || new Pair(null);
15
+ const pair = item.items[0] || new Pair(new Scalar(null));
15
16
  if (item.commentBefore)
16
- pair.commentBefore = pair.commentBefore
17
- ? `${item.commentBefore}\n${pair.commentBefore}`
17
+ pair.key.commentBefore = pair.key.commentBefore
18
+ ? `${item.commentBefore}\n${pair.key.commentBefore}`
18
19
  : item.commentBefore;
19
- if (item.comment)
20
- pair.comment = pair.comment
21
- ? `${item.comment}\n${pair.comment}`
20
+ if (item.comment) {
21
+ const cn = pair.value || pair.key;
22
+ cn.comment = cn.comment
23
+ ? `${item.comment}\n${cn.comment}`
22
24
  : item.comment;
25
+ }
23
26
  item = pair;
24
27
  }
25
28
  seq.items[i] = isPair(item) ? item : new Pair(item);
@@ -0,0 +1,37 @@
1
+ import { map } from '../common/map.js';
2
+ import { nullTag } from '../common/null.js';
3
+ import { seq } from '../common/seq.js';
4
+ import { string } from '../common/string.js';
5
+ import { binary } from './binary.js';
6
+ import { trueTag, falseTag } from './bool.js';
7
+ import { floatNaN, floatExp, float } from './float.js';
8
+ import { intBin, intOct, int, intHex } from './int.js';
9
+ import { omap } from './omap.js';
10
+ import { pairs } from './pairs.js';
11
+ import { set } from './set.js';
12
+ import { intTime, floatTime, timestamp } from './timestamp.js';
13
+
14
+ const schema = [
15
+ map,
16
+ seq,
17
+ string,
18
+ nullTag,
19
+ trueTag,
20
+ falseTag,
21
+ intBin,
22
+ intOct,
23
+ int,
24
+ intHex,
25
+ floatNaN,
26
+ floatExp,
27
+ float,
28
+ binary,
29
+ omap,
30
+ pairs,
31
+ set,
32
+ intTime,
33
+ floatTime,
34
+ timestamp
35
+ ];
36
+
37
+ export { schema };
File without changes
@@ -1,8 +1,9 @@
1
- import { isPair, isAlias, isNode, isScalar } from '../nodes/Node.js';
1
+ import { anchorIsValid } from '../doc/anchors.js';
2
+ import { isPair, isAlias, isNode, isScalar, isCollection } from '../nodes/Node.js';
2
3
  import { stringifyString } from './stringifyString.js';
3
4
 
4
5
  const createStringifyContext = (doc, options) => ({
5
- anchors: Object.create(null),
6
+ anchors: new Set(),
6
7
  doc,
7
8
  indent: '',
8
9
  indentStep: typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ',
@@ -19,7 +20,8 @@ const createStringifyContext = (doc, options) => ({
19
20
  nullStr: 'null',
20
21
  simpleKeys: false,
21
22
  singleQuote: false,
22
- trueStr: 'true'
23
+ trueStr: 'true',
24
+ verifyAliasOrder: true
23
25
  }, options)
24
26
  });
25
27
  function getTagObject(tags, item) {
@@ -50,9 +52,9 @@ function getTagObject(tags, item) {
50
52
  // needs to be called before value stringifier to allow for circular anchor refs
51
53
  function stringifyProps(node, tagObj, { anchors, doc }) {
52
54
  const props = [];
53
- const anchor = doc.anchors.getName(node);
54
- if (anchor) {
55
- anchors[anchor] = node;
55
+ const anchor = (isScalar(node) || isCollection(node)) && node.anchor;
56
+ if (anchor && anchorIsValid(anchor)) {
57
+ anchors.add(anchor);
56
58
  props.push(`&${anchor}`);
57
59
  }
58
60
  if (node.tag) {