yaml 2.0.0-4 → 2.0.0-5

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 (210) 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 +20 -17
  5. package/browser/dist/compose/compose-scalar.js +20 -23
  6. package/browser/dist/compose/composer.js +105 -76
  7. package/browser/dist/compose/resolve-block-map.js +38 -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 +6 -7
  11. package/browser/dist/compose/resolve-flow-collection.js +153 -199
  12. package/browser/dist/compose/resolve-flow-scalar.js +49 -25
  13. package/browser/dist/compose/resolve-props.js +47 -35
  14. package/browser/dist/compose/util-contains-newline.js +15 -12
  15. package/browser/dist/doc/Document.js +50 -99
  16. package/browser/dist/doc/anchors.js +72 -0
  17. package/browser/dist/doc/createNode.js +27 -17
  18. package/browser/dist/errors.js +16 -12
  19. package/browser/dist/index.js +3 -3
  20. package/browser/dist/nodes/Alias.js +51 -28
  21. package/browser/dist/nodes/Collection.js +5 -83
  22. package/browser/dist/nodes/Node.js +2 -1
  23. package/browser/dist/nodes/Pair.js +9 -232
  24. package/browser/dist/nodes/YAMLMap.js +8 -4
  25. package/browser/dist/nodes/YAMLSeq.js +7 -4
  26. package/browser/dist/nodes/addPairToJSMap.js +103 -0
  27. package/browser/dist/nodes/toJS.js +12 -10
  28. package/browser/dist/options.js +0 -2
  29. package/browser/dist/parse/cst-scalar.js +219 -0
  30. package/browser/dist/parse/cst-stringify.js +61 -0
  31. package/browser/dist/parse/cst-visit.js +97 -0
  32. package/{dist/parse/tokens.js → browser/dist/parse/cst.js} +12 -7
  33. package/browser/dist/parse/lexer.js +99 -105
  34. package/browser/dist/parse/parser.js +219 -144
  35. package/browser/dist/public-api.js +11 -14
  36. package/browser/dist/schema/Schema.js +23 -0
  37. package/browser/dist/{tags/failsafe → schema/common}/map.js +0 -0
  38. package/browser/dist/schema/common/null.js +13 -0
  39. package/browser/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  40. package/browser/dist/{tags/failsafe → schema/common}/string.js +0 -0
  41. package/browser/dist/schema/core/bool.js +19 -0
  42. package/browser/dist/schema/core/float.js +40 -0
  43. package/browser/dist/schema/core/int.js +38 -0
  44. package/browser/dist/schema/core/schema.js +23 -0
  45. package/browser/dist/{tags/json.js → schema/json/schema.js} +5 -6
  46. package/browser/dist/schema/tags.js +79 -0
  47. package/browser/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  48. package/browser/dist/schema/yaml-1.1/bool.js +26 -0
  49. package/browser/dist/schema/yaml-1.1/float.js +43 -0
  50. package/browser/dist/schema/yaml-1.1/int.js +71 -0
  51. package/browser/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  52. package/browser/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  53. package/browser/dist/schema/yaml-1.1/schema.js +37 -0
  54. package/browser/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  55. package/browser/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  56. package/browser/dist/stringify/addComment.js +1 -7
  57. package/browser/dist/stringify/stringify.js +8 -6
  58. package/browser/dist/stringify/stringifyCollection.js +108 -0
  59. package/browser/dist/stringify/stringifyDocument.js +61 -0
  60. package/browser/dist/stringify/stringifyPair.js +113 -0
  61. package/browser/dist/stringify/stringifyString.js +53 -52
  62. package/browser/dist/visit.js +20 -1
  63. package/dist/compose/compose-collection.d.ts +4 -4
  64. package/dist/compose/compose-collection.js +12 -9
  65. package/dist/compose/compose-doc.d.ts +4 -3
  66. package/dist/compose/compose-doc.js +23 -6
  67. package/dist/compose/compose-node.d.ts +15 -7
  68. package/dist/compose/compose-node.js +20 -17
  69. package/dist/compose/compose-scalar.d.ts +4 -3
  70. package/dist/compose/compose-scalar.js +19 -22
  71. package/dist/compose/composer.d.ts +28 -23
  72. package/dist/compose/composer.js +106 -77
  73. package/dist/compose/resolve-block-map.d.ts +4 -4
  74. package/dist/compose/resolve-block-map.js +38 -29
  75. package/dist/compose/resolve-block-scalar.d.ts +5 -3
  76. package/dist/compose/resolve-block-scalar.js +14 -17
  77. package/dist/compose/resolve-block-seq.d.ts +4 -4
  78. package/dist/compose/resolve-block-seq.js +21 -17
  79. package/dist/compose/resolve-end.d.ts +3 -2
  80. package/dist/compose/resolve-end.js +6 -7
  81. package/dist/compose/resolve-flow-collection.d.ts +4 -5
  82. package/dist/compose/resolve-flow-collection.js +152 -198
  83. package/dist/compose/resolve-flow-scalar.d.ts +5 -3
  84. package/dist/compose/resolve-flow-scalar.js +49 -25
  85. package/dist/compose/resolve-props.d.ts +17 -10
  86. package/dist/compose/resolve-props.js +47 -35
  87. package/dist/compose/util-contains-newline.d.ts +1 -1
  88. package/dist/compose/util-contains-newline.js +15 -12
  89. package/dist/compose/util-empty-scalar-position.d.ts +1 -1
  90. package/dist/doc/Document.d.ts +27 -15
  91. package/dist/doc/Document.js +48 -97
  92. package/dist/doc/anchors.d.ts +24 -0
  93. package/dist/doc/anchors.js +77 -0
  94. package/dist/doc/createNode.d.ts +8 -10
  95. package/dist/doc/createNode.js +26 -16
  96. package/dist/errors.d.ts +11 -8
  97. package/dist/errors.js +16 -12
  98. package/dist/index.d.ts +5 -3
  99. package/dist/index.js +3 -3
  100. package/dist/nodes/Alias.d.ts +19 -9
  101. package/dist/nodes/Alias.js +51 -28
  102. package/dist/nodes/Collection.d.ts +3 -17
  103. package/dist/nodes/Collection.js +4 -82
  104. package/dist/nodes/Node.d.ts +6 -4
  105. package/dist/nodes/Node.js +2 -0
  106. package/dist/nodes/Pair.d.ts +6 -11
  107. package/dist/nodes/Pair.js +8 -231
  108. package/dist/nodes/Scalar.d.ts +6 -3
  109. package/dist/nodes/YAMLMap.d.ts +3 -3
  110. package/dist/nodes/YAMLMap.js +8 -4
  111. package/dist/nodes/YAMLSeq.d.ts +3 -3
  112. package/dist/nodes/YAMLSeq.js +7 -4
  113. package/dist/nodes/addPairToJSMap.d.ts +3 -0
  114. package/dist/nodes/addPairToJSMap.js +105 -0
  115. package/dist/nodes/toJS.d.ts +4 -5
  116. package/dist/nodes/toJS.js +12 -10
  117. package/dist/options.d.ts +20 -26
  118. package/dist/options.js +0 -2
  119. package/dist/parse/cst-scalar.d.ts +58 -0
  120. package/dist/parse/cst-scalar.js +223 -0
  121. package/dist/parse/cst-stringify.d.ts +8 -0
  122. package/dist/parse/cst-stringify.js +63 -0
  123. package/dist/parse/cst-visit.d.ts +39 -0
  124. package/dist/parse/cst-visit.js +99 -0
  125. package/dist/parse/{tokens.d.ts → cst.d.ts} +23 -7
  126. package/{browser/dist/parse/tokens.js → dist/parse/cst.js} +27 -1
  127. package/dist/parse/lexer.d.ts +4 -13
  128. package/dist/parse/lexer.js +100 -106
  129. package/dist/parse/parser.d.ts +30 -24
  130. package/dist/parse/parser.js +221 -146
  131. package/dist/public-api.d.ts +7 -7
  132. package/dist/public-api.js +11 -14
  133. package/dist/{doc → schema}/Schema.d.ts +5 -3
  134. package/dist/schema/Schema.js +25 -0
  135. package/dist/{tags/failsafe → schema/common}/map.d.ts +0 -0
  136. package/dist/{tags/failsafe → schema/common}/map.js +0 -0
  137. package/dist/schema/common/null.d.ts +4 -0
  138. package/dist/schema/common/null.js +15 -0
  139. package/dist/{tags/failsafe → schema/common}/seq.d.ts +0 -0
  140. package/dist/{tags/failsafe → schema/common}/seq.js +1 -1
  141. package/dist/{tags/failsafe → schema/common}/string.d.ts +0 -0
  142. package/dist/{tags/failsafe → schema/common}/string.js +0 -0
  143. package/dist/schema/core/bool.d.ts +4 -0
  144. package/dist/schema/core/bool.js +21 -0
  145. package/dist/schema/core/float.d.ts +4 -0
  146. package/dist/schema/core/float.js +44 -0
  147. package/dist/schema/core/int.d.ts +4 -0
  148. package/dist/schema/core/int.js +42 -0
  149. package/dist/schema/core/schema.d.ts +1 -0
  150. package/dist/schema/core/schema.js +25 -0
  151. package/dist/schema/json/schema.d.ts +2 -0
  152. package/dist/{tags/json.js → schema/json/schema.js} +5 -6
  153. package/dist/schema/tags.d.ts +41 -0
  154. package/dist/schema/tags.js +82 -0
  155. package/dist/{tags → schema}/types.d.ts +3 -7
  156. package/dist/{tags → schema}/yaml-1.1/binary.d.ts +0 -0
  157. package/dist/{tags → schema}/yaml-1.1/binary.js +0 -0
  158. package/dist/schema/yaml-1.1/bool.d.ts +7 -0
  159. package/dist/schema/yaml-1.1/bool.js +29 -0
  160. package/dist/schema/yaml-1.1/float.d.ts +4 -0
  161. package/dist/schema/yaml-1.1/float.js +47 -0
  162. package/dist/schema/yaml-1.1/int.d.ts +5 -0
  163. package/dist/schema/yaml-1.1/int.js +76 -0
  164. package/dist/{tags → schema}/yaml-1.1/omap.d.ts +0 -0
  165. package/dist/{tags → schema}/yaml-1.1/omap.js +0 -0
  166. package/dist/{tags → schema}/yaml-1.1/pairs.d.ts +3 -2
  167. package/dist/{tags → schema}/yaml-1.1/pairs.js +9 -6
  168. package/dist/schema/yaml-1.1/schema.d.ts +1 -0
  169. package/dist/schema/yaml-1.1/schema.js +39 -0
  170. package/dist/{tags → schema}/yaml-1.1/set.d.ts +1 -1
  171. package/dist/{tags → schema}/yaml-1.1/set.js +0 -0
  172. package/dist/{tags → schema}/yaml-1.1/timestamp.d.ts +0 -0
  173. package/dist/{tags → schema}/yaml-1.1/timestamp.js +0 -0
  174. package/dist/stringify/addComment.js +0 -7
  175. package/dist/stringify/stringify.d.ts +1 -2
  176. package/dist/stringify/stringify.js +8 -6
  177. package/dist/stringify/stringifyCollection.d.ts +21 -0
  178. package/dist/stringify/stringifyCollection.js +110 -0
  179. package/dist/stringify/stringifyDocument.d.ts +3 -0
  180. package/dist/stringify/stringifyDocument.js +63 -0
  181. package/dist/stringify/stringifyPair.d.ts +3 -0
  182. package/dist/stringify/stringifyPair.js +115 -0
  183. package/dist/stringify/stringifyString.js +53 -52
  184. package/dist/test-events.d.ts +1 -2
  185. package/dist/test-events.js +25 -12
  186. package/dist/util.d.ts +1 -1
  187. package/dist/visit.d.ts +10 -1
  188. package/dist/visit.js +20 -1
  189. package/package.json +4 -3
  190. package/browser/dist/doc/Anchors.js +0 -102
  191. package/browser/dist/doc/Schema.js +0 -27
  192. package/browser/dist/doc/getSchemaTags.js +0 -30
  193. package/browser/dist/tags/core.js +0 -109
  194. package/browser/dist/tags/failsafe/index.js +0 -7
  195. package/browser/dist/tags/index.js +0 -34
  196. package/browser/dist/tags/yaml-1.1/index.js +0 -152
  197. package/dist/doc/Anchors.d.ts +0 -36
  198. package/dist/doc/Anchors.js +0 -104
  199. package/dist/doc/Schema.js +0 -29
  200. package/dist/doc/getSchemaTags.d.ts +0 -3
  201. package/dist/doc/getSchemaTags.js +0 -32
  202. package/dist/tags/core.d.ts +0 -14
  203. package/dist/tags/core.js +0 -119
  204. package/dist/tags/failsafe/index.d.ts +0 -1
  205. package/dist/tags/failsafe/index.js +0 -9
  206. package/dist/tags/index.d.ts +0 -31
  207. package/dist/tags/index.js +0 -37
  208. package/dist/tags/json.d.ts +0 -2
  209. package/dist/tags/yaml-1.1/index.d.ts +0 -2
  210. package/dist/tags/yaml-1.1/index.js +0 -154
@@ -2,6 +2,7 @@
2
2
 
3
3
  var Node = require('./nodes/Node.js');
4
4
  var publicApi = require('./public-api.js');
5
+ var visit = require('./visit.js');
5
6
 
6
7
  const scalarChar = {
7
8
  BLOCK_FOLDED: '>',
@@ -10,11 +11,22 @@ const scalarChar = {
10
11
  QUOTE_DOUBLE: '"',
11
12
  QUOTE_SINGLE: "'"
12
13
  };
14
+ function anchorExists(doc, anchor) {
15
+ let found = false;
16
+ visit.visit(doc, {
17
+ Value(_key, node) {
18
+ if (node.anchor === anchor) {
19
+ found = true;
20
+ return visit.visit.BREAK;
21
+ }
22
+ }
23
+ });
24
+ return found;
25
+ }
13
26
  // test harness for yaml-test-suite event tests
14
- function testEvents(src, options) {
27
+ function testEvents(src) {
15
28
  var _a;
16
- const opt = Object.assign({ version: '1.2' }, options);
17
- const docs = publicApi.parseAllDocuments(src, opt);
29
+ const docs = publicApi.parseAllDocuments(src);
18
30
  const errDoc = docs.find(doc => doc.errors.length > 0);
19
31
  const error = errDoc ? errDoc.errors[0].message : null;
20
32
  const events = ['+STR'];
@@ -24,17 +36,18 @@ function testEvents(src, options) {
24
36
  let root = doc.contents;
25
37
  if (Array.isArray(root))
26
38
  root = root[0];
27
- const [rootStart, rootEnd] = doc.range || [0, 0];
39
+ // eslint-disable-next-line no-sparse-arrays
40
+ const [rootStart, , rootEnd] = doc.range || [0, , 0];
28
41
  const error = doc.errors[0];
29
- if (error && (!error.offset || error.offset < rootStart))
42
+ if (error && (!error.pos || error.pos[0] < rootStart))
30
43
  throw new Error();
31
44
  let docStart = '+DOC';
32
45
  if (doc.directives.marker)
33
46
  docStart += ' ---';
34
- else if (doc.contents && doc.contents.range[1] === doc.contents.range[0])
47
+ else if (doc.contents && doc.contents.range[2] === doc.contents.range[0])
35
48
  continue;
36
49
  events.push(docStart);
37
- addEvents(events, doc, (_a = error === null || error === void 0 ? void 0 : error.offset) !== null && _a !== void 0 ? _a : -1, root);
50
+ addEvents(events, doc, (_a = error === null || error === void 0 ? void 0 : error.pos[0]) !== null && _a !== void 0 ? _a : -1, root);
38
51
  let docEnd = '-DOC';
39
52
  if (rootEnd) {
40
53
  const post = src.slice(rootStart, rootEnd);
@@ -58,16 +71,16 @@ function addEvents(events, doc, errPos, node) {
58
71
  if (errPos !== -1 && Node.isNode(node) && node.range[0] >= errPos)
59
72
  throw new Error();
60
73
  let props = '';
61
- let anchor = Node.isNode(node) ? doc.anchors.getName(node) : undefined;
74
+ let anchor = Node.isScalar(node) || Node.isCollection(node) ? node.anchor : undefined;
62
75
  if (anchor) {
63
76
  if (/\d$/.test(anchor)) {
64
77
  const alt = anchor.replace(/\d$/, '');
65
- if (doc.anchors.getNode(alt))
78
+ if (anchorExists(doc, alt))
66
79
  anchor = alt;
67
80
  }
68
81
  props = ` &${anchor}`;
69
82
  }
70
- if (node.tag)
83
+ if (Node.isNode(node) && node.tag)
71
84
  props += ` <${node.tag}>`;
72
85
  if (Node.isMap(node)) {
73
86
  events.push(`+MAP${props}`);
@@ -91,10 +104,10 @@ function addEvents(events, doc, errPos, node) {
91
104
  events.push('-MAP');
92
105
  }
93
106
  else if (Node.isAlias(node)) {
94
- let alias = doc.anchors.getName(node.source);
107
+ let alias = node.source;
95
108
  if (alias && /\d$/.test(alias)) {
96
109
  const alt = alias.replace(/\d$/, '');
97
- if (doc.anchors.getNode(alt))
110
+ if (anchorExists(doc, alt))
98
111
  alias = alt;
99
112
  }
100
113
  events.push(`=ALI${props} *${alias}`);
package/dist/util.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { debug, LogLevelId, warn } from './log.js';
2
2
  export { findPair } from './nodes/YAMLMap.js';
3
- export { toJS, ToJSAnchorValue, ToJSContext } from './nodes/toJS.js';
3
+ export { toJS, ToJSContext } from './nodes/toJS.js';
4
4
  export { foldFlowLines } from './stringify/foldFlowLines';
5
5
  export { stringifyNumber } from './stringify/stringifyNumber.js';
6
6
  export { stringifyString } from './stringify/stringifyString.js';
package/dist/visit.d.ts CHANGED
@@ -8,10 +8,13 @@ import type { YAMLSeq } from './nodes/YAMLSeq.js';
8
8
  export declare type visitorFn<T> = (key: number | 'key' | 'value' | null, node: T, path: readonly (Document | Node | Pair)[]) => void | symbol | number | Node | Pair;
9
9
  export declare type visitor = visitorFn<unknown> | {
10
10
  Alias?: visitorFn<Alias>;
11
+ Collection?: visitorFn<YAMLMap | YAMLSeq>;
11
12
  Map?: visitorFn<YAMLMap>;
13
+ Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
12
14
  Pair?: visitorFn<Pair>;
13
15
  Scalar?: visitorFn<Scalar>;
14
16
  Seq?: visitorFn<YAMLSeq>;
17
+ Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
15
18
  };
16
19
  /**
17
20
  * Apply a visitor to an AST node or document.
@@ -38,14 +41,20 @@ export declare type visitor = visitorFn<unknown> | {
38
41
  * If `visitor` is a single function, it will be called with all values
39
42
  * encountered in the tree, including e.g. `null` values. Alternatively,
40
43
  * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
41
- * `Alias` and `Scalar` node.
44
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
45
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
46
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
47
+ * specific defined one will be used for each node.
42
48
  */
43
49
  export declare function visit(node: Node | Document | null, visitor: visitorFn<unknown> | {
44
50
  Alias?: visitorFn<Alias>;
51
+ Collection?: visitorFn<YAMLMap | YAMLSeq>;
45
52
  Map?: visitorFn<YAMLMap>;
53
+ Node?: visitorFn<Alias | Scalar | YAMLMap | YAMLSeq>;
46
54
  Pair?: visitorFn<Pair>;
47
55
  Scalar?: visitorFn<Scalar>;
48
56
  Seq?: visitorFn<YAMLSeq>;
57
+ Value?: visitorFn<Scalar | YAMLMap | YAMLSeq>;
49
58
  }): void;
50
59
  export declare namespace visit {
51
60
  var BREAK: symbol;
package/dist/visit.js CHANGED
@@ -30,9 +30,28 @@ const REMOVE = Symbol('remove node');
30
30
  * If `visitor` is a single function, it will be called with all values
31
31
  * encountered in the tree, including e.g. `null` values. Alternatively,
32
32
  * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
33
- * `Alias` and `Scalar` node.
33
+ * `Alias` and `Scalar` node. To define the same visitor function for more than
34
+ * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
35
+ * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
36
+ * specific defined one will be used for each node.
34
37
  */
35
38
  function visit(node, visitor) {
39
+ if (typeof visitor === 'object' &&
40
+ (visitor.Collection || visitor.Node || visitor.Value)) {
41
+ visitor = Object.assign({
42
+ Alias: visitor.Node,
43
+ Map: visitor.Node,
44
+ Scalar: visitor.Node,
45
+ Seq: visitor.Node
46
+ }, visitor.Value && {
47
+ Map: visitor.Value,
48
+ Scalar: visitor.Value,
49
+ Seq: visitor.Value
50
+ }, visitor.Collection && {
51
+ Map: visitor.Collection,
52
+ Seq: visitor.Collection
53
+ }, visitor);
54
+ }
36
55
  if (Node.isDocument(node)) {
37
56
  const cd = _visit(null, node.contents, visitor, Object.freeze([node]));
38
57
  if (cd === REMOVE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.0.0-4",
3
+ "version": "2.0.0-5",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "repository": "github:eemeli/yaml",
@@ -46,6 +46,7 @@
46
46
  "test": "jest --config config/jest.config.js",
47
47
  "test:browsers": "cd playground && npm test",
48
48
  "test:dist": "npm run build:node && jest --config config/jest.config.js",
49
+ "test:dist:types": "tsc --allowJs --moduleResolution node --noEmit --target es2017 dist/index.js",
49
50
  "test:types": "tsc --noEmit",
50
51
  "docs:install": "cd docs-slate && bundle install",
51
52
  "docs:deploy": "cd docs-slate && ./deploy.sh",
@@ -53,7 +54,7 @@
53
54
  "preversion": "npm test && npm run build",
54
55
  "prepublishOnly": "npm run clean && npm test && npm run build"
55
56
  },
56
- "browserslist": "> 0.5%, not dead, not ie 11",
57
+ "browserslist": "defaults, not ie 11",
57
58
  "prettier": {
58
59
  "arrowParens": "avoid",
59
60
  "semi": false,
@@ -70,6 +71,7 @@
70
71
  "@rollup/plugin-replace": "^2.3.4",
71
72
  "@rollup/plugin-typescript": "^8.1.1",
72
73
  "@types/jest": "^26.0.20",
74
+ "@types/node": "^14.14.37",
73
75
  "@typescript-eslint/eslint-plugin": "^4.15.2",
74
76
  "@typescript-eslint/parser": "^4.15.2",
75
77
  "babel-jest": "^26.6.3",
@@ -81,7 +83,6 @@
81
83
  "jest-ts-webcompat-resolver": "^1.0.0",
82
84
  "prettier": "^2.2.1",
83
85
  "rollup": "^2.38.2",
84
- "rollup-plugin-copy": "^3.3.0",
85
86
  "tslib": "^2.1.0",
86
87
  "typescript": "^4.1.3"
87
88
  },
@@ -1,102 +0,0 @@
1
- import { Alias } from '../nodes/Alias.js';
2
- import { isAlias, isMap, isScalar, isCollection } from '../nodes/Node.js';
3
- import { Pair } from '../nodes/Pair.js';
4
- import { Scalar } from '../nodes/Scalar.js';
5
- import { YAMLSeq } from '../nodes/YAMLSeq.js';
6
-
7
- class Anchors {
8
- constructor(prefix) {
9
- this.map = Object.create(null);
10
- this.prefix = prefix;
11
- }
12
- /**
13
- * Create a new `Alias` node, adding the required anchor for `node`.
14
- * If `name` is empty, a new anchor name will be generated.
15
- */
16
- createAlias(node, name) {
17
- this.setAnchor(node, name);
18
- return new Alias(node);
19
- }
20
- /**
21
- * Create a new merge `Pair` with the given source nodes.
22
- * Non-`Alias` sources will be automatically wrapped.
23
- */
24
- createMergePair(...sources) {
25
- const key = new Scalar(Pair.MERGE_KEY);
26
- const items = sources.map(s => {
27
- if (isAlias(s)) {
28
- if (isMap(s.source))
29
- return s;
30
- }
31
- else if (isMap(s)) {
32
- return this.createAlias(s);
33
- }
34
- throw new Error('Merge sources must be Map nodes or their Aliases');
35
- });
36
- if (items.length === 1)
37
- return new Pair(key, items[0]);
38
- const seq = new YAMLSeq();
39
- seq.items = items;
40
- return new Pair(key, seq);
41
- }
42
- /** The anchor name associated with `node`, if set. */
43
- getName(node) {
44
- return Object.keys(this.map).find(a => this.map[a] === node);
45
- }
46
- /** List of all defined anchor names. */
47
- getNames() {
48
- return Object.keys(this.map);
49
- }
50
- /** The node associated with the anchor `name`, if set. */
51
- getNode(name) {
52
- return this.map[name];
53
- }
54
- /**
55
- * Find an available anchor name with the given `prefix` and a
56
- * numerical suffix.
57
- */
58
- newName(prefix) {
59
- if (!prefix)
60
- prefix = this.prefix;
61
- const names = Object.keys(this.map);
62
- for (let i = 1; true; ++i) {
63
- const name = `${prefix}${i}`;
64
- if (!names.includes(name))
65
- return name;
66
- }
67
- }
68
- /**
69
- * Associate an anchor with `node`. If `name` is empty, a new name will be generated.
70
- * To remove an anchor, use `setAnchor(null, name)`.
71
- */
72
- setAnchor(node, name) {
73
- const { map } = this;
74
- if (!node) {
75
- if (!name)
76
- return null;
77
- delete map[name];
78
- return name;
79
- }
80
- if (!isScalar(node) && !isCollection(node))
81
- throw new Error('Anchors may only be set for Scalar, Seq and Map nodes');
82
- if (name) {
83
- if (/[\x00-\x19\s,[\]{}]/.test(name))
84
- throw new Error('Anchor names must not contain whitespace or control characters');
85
- const prevNode = map[name];
86
- if (prevNode && prevNode !== node)
87
- map[this.newName(name)] = prevNode;
88
- }
89
- const prevName = Object.keys(map).find(a => map[a] === node);
90
- if (prevName) {
91
- if (!name || prevName === name)
92
- return prevName;
93
- delete map[prevName];
94
- }
95
- else if (!name)
96
- name = this.newName();
97
- map[name] = node;
98
- return name;
99
- }
100
- }
101
-
102
- export { Anchors };
@@ -1,27 +0,0 @@
1
- import { tags, schemas } from '../tags/index.js';
2
- import { getSchemaTags } from './getSchemaTags.js';
3
-
4
- const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
5
- const coreKnownTags = {
6
- 'tag:yaml.org,2002:binary': tags.binary,
7
- 'tag:yaml.org,2002:omap': tags.omap,
8
- 'tag:yaml.org,2002:pairs': tags.pairs,
9
- 'tag:yaml.org,2002:set': tags.set,
10
- 'tag:yaml.org,2002:timestamp': tags.timestamp
11
- };
12
- class Schema {
13
- constructor({ customTags, merge, resolveKnownTags, schema, sortMapEntries }) {
14
- // Used by createNode(), to avoid circular dependencies
15
- this.map = tags.map;
16
- this.seq = tags.seq;
17
- this.merge = !!merge;
18
- this.name = schema || 'core';
19
- this.knownTags = resolveKnownTags ? coreKnownTags : {};
20
- this.tags = getSchemaTags(schemas, tags, customTags, this.name);
21
- // Used by createMap()
22
- this.sortMapEntries =
23
- sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null;
24
- }
25
- }
26
-
27
- export { Schema };
@@ -1,30 +0,0 @@
1
- function getSchemaTags(schemas, knownTags, customTags, schemaName) {
2
- const schemaId = schemaName.replace(/\W/g, ''); // 'yaml-1.1' -> 'yaml11'
3
- let tags = schemas[schemaId];
4
- if (!tags) {
5
- const keys = Object.keys(schemas)
6
- .map(key => JSON.stringify(key))
7
- .join(', ');
8
- throw new Error(`Unknown schema "${schemaName}"; use one of ${keys}`);
9
- }
10
- if (Array.isArray(customTags)) {
11
- for (const tag of customTags)
12
- tags = tags.concat(tag);
13
- }
14
- else if (typeof customTags === 'function') {
15
- tags = customTags(tags.slice());
16
- }
17
- return tags.map(tag => {
18
- if (typeof tag !== 'string')
19
- return tag;
20
- const tagObj = knownTags[tag];
21
- if (tagObj)
22
- return tagObj;
23
- const keys = Object.keys(knownTags)
24
- .map(key => JSON.stringify(key))
25
- .join(', ');
26
- throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
27
- });
28
- }
29
-
30
- export { getSchemaTags };
@@ -1,109 +0,0 @@
1
- import { Scalar } from '../nodes/Scalar.js';
2
- import { stringifyNumber } from '../stringify/stringifyNumber.js';
3
- import { failsafe } from './failsafe/index.js';
4
-
5
- const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
6
- const intResolve = (str, offset, radix, { intAsBigInt }) => (intAsBigInt ? BigInt(str) : parseInt(str.substring(offset), radix));
7
- function intStringify(node, radix, prefix) {
8
- const { value } = node;
9
- if (intIdentify(value) && value >= 0)
10
- return prefix + value.toString(radix);
11
- return stringifyNumber(node);
12
- }
13
- const nullObj = {
14
- identify: value => value == null,
15
- createNode: () => new Scalar(null),
16
- default: true,
17
- tag: 'tag:yaml.org,2002:null',
18
- test: /^(?:~|[Nn]ull|NULL)?$/,
19
- resolve: () => new Scalar(null),
20
- stringify: ({ source }, ctx) => source && nullObj.test.test(source) ? source : ctx.options.nullStr
21
- };
22
- const boolObj = {
23
- identify: value => typeof value === 'boolean',
24
- default: true,
25
- tag: 'tag:yaml.org,2002:bool',
26
- test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
27
- resolve: str => new Scalar(str[0] === 't' || str[0] === 'T'),
28
- stringify({ source, value }, ctx) {
29
- if (source && boolObj.test.test(source)) {
30
- const sv = source[0] === 't' || source[0] === 'T';
31
- if (value === sv)
32
- return source;
33
- }
34
- return value ? ctx.options.trueStr : ctx.options.falseStr;
35
- }
36
- };
37
- const octObj = {
38
- identify: value => intIdentify(value) && value >= 0,
39
- default: true,
40
- tag: 'tag:yaml.org,2002:int',
41
- format: 'OCT',
42
- test: /^0o[0-7]+$/,
43
- resolve: (str, _onError, opt) => intResolve(str, 2, 8, opt),
44
- stringify: node => intStringify(node, 8, '0o')
45
- };
46
- const intObj = {
47
- identify: intIdentify,
48
- default: true,
49
- tag: 'tag:yaml.org,2002:int',
50
- test: /^[-+]?[0-9]+$/,
51
- resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
52
- stringify: stringifyNumber
53
- };
54
- const hexObj = {
55
- identify: value => intIdentify(value) && value >= 0,
56
- default: true,
57
- tag: 'tag:yaml.org,2002:int',
58
- format: 'HEX',
59
- test: /^0x[0-9a-fA-F]+$/,
60
- resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
61
- stringify: node => intStringify(node, 16, '0x')
62
- };
63
- const nanObj = {
64
- identify: value => typeof value === 'number',
65
- default: true,
66
- tag: 'tag:yaml.org,2002:float',
67
- test: /^(?:[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN))$/,
68
- resolve: str => str.slice(-3).toLowerCase() === 'nan'
69
- ? NaN
70
- : str[0] === '-'
71
- ? Number.NEGATIVE_INFINITY
72
- : Number.POSITIVE_INFINITY,
73
- stringify: stringifyNumber
74
- };
75
- const expObj = {
76
- identify: value => typeof value === 'number',
77
- default: true,
78
- tag: 'tag:yaml.org,2002:float',
79
- format: 'EXP',
80
- test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
81
- resolve: str => parseFloat(str),
82
- stringify: ({ value }) => Number(value).toExponential()
83
- };
84
- const floatObj = {
85
- identify: value => typeof value === 'number',
86
- default: true,
87
- tag: 'tag:yaml.org,2002:float',
88
- test: /^[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*)$/,
89
- resolve(str) {
90
- const node = new Scalar(parseFloat(str));
91
- const dot = str.indexOf('.');
92
- if (dot !== -1 && str[str.length - 1] === '0')
93
- node.minFractionDigits = str.length - dot - 1;
94
- return node;
95
- },
96
- stringify: stringifyNumber
97
- };
98
- const core = failsafe.concat([
99
- nullObj,
100
- boolObj,
101
- octObj,
102
- intObj,
103
- hexObj,
104
- nanObj,
105
- expObj,
106
- floatObj
107
- ]);
108
-
109
- export { boolObj, core, expObj, floatObj, hexObj, intObj, nanObj, nullObj, octObj };
@@ -1,7 +0,0 @@
1
- import { map } from './map.js';
2
- import { seq } from './seq.js';
3
- import { string } from './string.js';
4
-
5
- const failsafe = [map, seq, string];
6
-
7
- export { failsafe };
@@ -1,34 +0,0 @@
1
- import { boolObj, floatObj, expObj, nanObj, intObj, hexObj, octObj, nullObj, core } from './core.js';
2
- import { failsafe } from './failsafe/index.js';
3
- import { json } from './json.js';
4
- import { yaml11 } from './yaml-1.1/index.js';
5
- import { map } from './failsafe/map.js';
6
- import { seq } from './failsafe/seq.js';
7
- import { binary } from './yaml-1.1/binary.js';
8
- import { omap } from './yaml-1.1/omap.js';
9
- import { pairs } from './yaml-1.1/pairs.js';
10
- import { set } from './yaml-1.1/set.js';
11
- import { floatTime, intTime, timestamp } from './yaml-1.1/timestamp.js';
12
-
13
- const schemas = { core, failsafe, json, yaml11 };
14
- const tags = {
15
- binary,
16
- bool: boolObj,
17
- float: floatObj,
18
- floatExp: expObj,
19
- floatNaN: nanObj,
20
- floatTime,
21
- int: intObj,
22
- intHex: hexObj,
23
- intOct: octObj,
24
- intTime,
25
- map,
26
- null: nullObj,
27
- omap,
28
- pairs,
29
- seq,
30
- set,
31
- timestamp
32
- };
33
-
34
- export { schemas, tags };
@@ -1,152 +0,0 @@
1
- import { Scalar } from '../../nodes/Scalar.js';
2
- import { stringifyNumber } from '../../stringify/stringifyNumber.js';
3
- import { failsafe } from '../failsafe/index.js';
4
- import { binary } from './binary.js';
5
- import { omap } from './omap.js';
6
- import { pairs } from './pairs.js';
7
- import { set } from './set.js';
8
- import { intTime, floatTime, timestamp } from './timestamp.js';
9
-
10
- const nullObj = {
11
- identify: value => value == null,
12
- createNode: () => new Scalar(null),
13
- default: true,
14
- tag: 'tag:yaml.org,2002:null',
15
- test: /^(?:~|[Nn]ull|NULL)?$/,
16
- resolve: () => new Scalar(null),
17
- stringify: ({ source }, ctx) => source && nullObj.test.test(source) ? source : ctx.options.nullStr
18
- };
19
- function boolStringify({ value, source }, ctx) {
20
- const boolObj = value ? trueObj : falseObj;
21
- if (source && boolObj.test.test(source))
22
- return source;
23
- return value ? ctx.options.trueStr : ctx.options.falseStr;
24
- }
25
- const trueObj = {
26
- identify: value => value === true,
27
- default: true,
28
- tag: 'tag:yaml.org,2002:bool',
29
- test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
30
- resolve: () => new Scalar(true),
31
- stringify: boolStringify
32
- };
33
- const falseObj = {
34
- identify: value => value === false,
35
- default: true,
36
- tag: 'tag:yaml.org,2002:bool',
37
- test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/i,
38
- resolve: () => new Scalar(false),
39
- stringify: boolStringify
40
- };
41
- const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
42
- function intResolve(str, offset, radix, { intAsBigInt }) {
43
- const sign = str[0];
44
- if (sign === '-' || sign === '+')
45
- offset += 1;
46
- str = str.substring(offset).replace(/_/g, '');
47
- if (intAsBigInt) {
48
- switch (radix) {
49
- case 2:
50
- str = `0b${str}`;
51
- break;
52
- case 8:
53
- str = `0o${str}`;
54
- break;
55
- case 16:
56
- str = `0x${str}`;
57
- break;
58
- }
59
- const n = BigInt(str);
60
- return sign === '-' ? BigInt(-1) * n : n;
61
- }
62
- const n = parseInt(str, radix);
63
- return sign === '-' ? -1 * n : n;
64
- }
65
- function intStringify(node, radix, prefix) {
66
- const { value } = node;
67
- if (intIdentify(value)) {
68
- const str = value.toString(radix);
69
- return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
70
- }
71
- return stringifyNumber(node);
72
- }
73
- const yaml11 = failsafe.concat([
74
- nullObj,
75
- trueObj,
76
- falseObj,
77
- {
78
- identify: intIdentify,
79
- default: true,
80
- tag: 'tag:yaml.org,2002:int',
81
- format: 'BIN',
82
- test: /^[-+]?0b[0-1_]+$/,
83
- resolve: (str, _onError, opt) => intResolve(str, 2, 2, opt),
84
- stringify: node => intStringify(node, 2, '0b')
85
- },
86
- {
87
- identify: intIdentify,
88
- default: true,
89
- tag: 'tag:yaml.org,2002:int',
90
- format: 'OCT',
91
- test: /^[-+]?0[0-7_]+$/,
92
- resolve: (str, _onError, opt) => intResolve(str, 1, 8, opt),
93
- stringify: node => intStringify(node, 8, '0')
94
- },
95
- {
96
- identify: intIdentify,
97
- default: true,
98
- tag: 'tag:yaml.org,2002:int',
99
- test: /^[-+]?[0-9][0-9_]*$/,
100
- resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
101
- stringify: stringifyNumber
102
- },
103
- {
104
- identify: intIdentify,
105
- default: true,
106
- tag: 'tag:yaml.org,2002:int',
107
- format: 'HEX',
108
- test: /^[-+]?0x[0-9a-fA-F_]+$/,
109
- resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
110
- stringify: node => intStringify(node, 16, '0x')
111
- },
112
- {
113
- identify: value => typeof value === 'number',
114
- default: true,
115
- tag: 'tag:yaml.org,2002:float',
116
- test: /^[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/,
117
- resolve: (str) => str.slice(-3).toLowerCase() === 'nan'
118
- ? NaN
119
- : str[0] === '-'
120
- ? Number.NEGATIVE_INFINITY
121
- : Number.POSITIVE_INFINITY,
122
- stringify: stringifyNumber
123
- },
124
- {
125
- identify: value => typeof value === 'number',
126
- default: true,
127
- tag: 'tag:yaml.org,2002:float',
128
- format: 'EXP',
129
- test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
130
- resolve: (str) => parseFloat(str.replace(/_/g, '')),
131
- stringify: ({ value }) => Number(value).toExponential()
132
- },
133
- {
134
- identify: value => typeof value === 'number',
135
- default: true,
136
- tag: 'tag:yaml.org,2002:float',
137
- test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*$/,
138
- resolve(str) {
139
- const node = new Scalar(parseFloat(str.replace(/_/g, '')));
140
- const dot = str.indexOf('.');
141
- if (dot !== -1) {
142
- const f = str.substring(dot + 1).replace(/_/g, '');
143
- if (f[f.length - 1] === '0')
144
- node.minFractionDigits = f.length;
145
- }
146
- return node;
147
- },
148
- stringify: stringifyNumber
149
- }
150
- ], binary, omap, pairs, set, intTime, floatTime, timestamp);
151
-
152
- export { yaml11 };