yaml 2.3.0-0 → 2.3.0-1

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 (93) hide show
  1. package/browser/dist/compose/compose-collection.js +1 -1
  2. package/browser/dist/compose/compose-scalar.js +1 -1
  3. package/browser/dist/compose/composer.js +1 -1
  4. package/browser/dist/compose/resolve-flow-collection.js +1 -1
  5. package/browser/dist/compose/util-map-includes.js +1 -1
  6. package/browser/dist/doc/Document.js +2 -4
  7. package/browser/dist/doc/anchors.js +1 -1
  8. package/browser/dist/doc/createNode.js +1 -1
  9. package/browser/dist/doc/directives.js +1 -1
  10. package/browser/dist/index.js +1 -1
  11. package/browser/dist/nodes/Alias.js +9 -2
  12. package/browser/dist/nodes/Collection.js +2 -1
  13. package/browser/dist/nodes/Node.js +23 -35
  14. package/browser/dist/nodes/Pair.js +1 -1
  15. package/browser/dist/nodes/Scalar.js +2 -1
  16. package/browser/dist/nodes/YAMLMap.js +1 -1
  17. package/browser/dist/nodes/YAMLSeq.js +1 -1
  18. package/browser/dist/nodes/addPairToJSMap.js +1 -1
  19. package/browser/dist/nodes/identity.js +36 -0
  20. package/browser/dist/nodes/toJS.js +1 -1
  21. package/browser/dist/schema/Schema.js +1 -1
  22. package/browser/dist/schema/common/map.js +1 -1
  23. package/browser/dist/schema/common/seq.js +1 -1
  24. package/browser/dist/schema/yaml-1.1/omap.js +2 -2
  25. package/browser/dist/schema/yaml-1.1/pairs.js +1 -1
  26. package/browser/dist/schema/yaml-1.1/set.js +1 -1
  27. package/browser/dist/stringify/stringify.js +1 -1
  28. package/browser/dist/stringify/stringifyCollection.js +1 -1
  29. package/browser/dist/stringify/stringifyDocument.js +1 -1
  30. package/browser/dist/stringify/stringifyPair.js +1 -1
  31. package/browser/dist/stringify/stringifyString.js +6 -6
  32. package/browser/dist/util.js +2 -0
  33. package/browser/dist/visit.js +1 -1
  34. package/dist/compose/compose-collection.d.ts +1 -1
  35. package/dist/compose/compose-collection.js +3 -3
  36. package/dist/compose/compose-scalar.js +7 -7
  37. package/dist/compose/composer.d.ts +1 -1
  38. package/dist/compose/composer.js +3 -3
  39. package/dist/compose/resolve-flow-collection.d.ts +1 -1
  40. package/dist/compose/resolve-flow-collection.js +2 -2
  41. package/dist/compose/util-map-includes.d.ts +3 -3
  42. package/dist/compose/util-map-includes.js +3 -3
  43. package/dist/doc/Document.d.ts +2 -1
  44. package/dist/doc/Document.js +12 -14
  45. package/dist/doc/anchors.d.ts +2 -2
  46. package/dist/doc/anchors.js +2 -2
  47. package/dist/doc/createNode.d.ts +1 -1
  48. package/dist/doc/createNode.js +8 -8
  49. package/dist/doc/directives.js +3 -3
  50. package/dist/index.d.ts +2 -1
  51. package/dist/index.js +9 -9
  52. package/dist/nodes/Alias.d.ts +1 -1
  53. package/dist/nodes/Alias.js +12 -5
  54. package/dist/nodes/Collection.d.ts +3 -2
  55. package/dist/nodes/Collection.js +10 -9
  56. package/dist/nodes/Node.d.ts +4 -17
  57. package/dist/nodes/Node.js +23 -51
  58. package/dist/nodes/Pair.d.ts +2 -2
  59. package/dist/nodes/Pair.js +4 -4
  60. package/dist/nodes/Scalar.js +2 -1
  61. package/dist/nodes/YAMLMap.d.ts +1 -1
  62. package/dist/nodes/YAMLMap.js +9 -9
  63. package/dist/nodes/YAMLSeq.d.ts +1 -1
  64. package/dist/nodes/YAMLSeq.js +5 -5
  65. package/dist/nodes/addPairToJSMap.js +7 -7
  66. package/dist/nodes/identity.d.ts +23 -0
  67. package/dist/nodes/identity.js +53 -0
  68. package/dist/nodes/toJS.d.ts +1 -4
  69. package/dist/nodes/toJS.js +2 -2
  70. package/dist/schema/Schema.d.ts +1 -1
  71. package/dist/schema/Schema.js +4 -4
  72. package/dist/schema/common/map.js +2 -2
  73. package/dist/schema/common/seq.js +2 -2
  74. package/dist/schema/core/schema.d.ts +1 -1
  75. package/dist/schema/json/schema.d.ts +1 -1
  76. package/dist/schema/tags.d.ts +1 -1
  77. package/dist/schema/yaml-1.1/omap.d.ts +1 -1
  78. package/dist/schema/yaml-1.1/omap.js +4 -4
  79. package/dist/schema/yaml-1.1/pairs.d.ts +1 -1
  80. package/dist/schema/yaml-1.1/pairs.js +5 -5
  81. package/dist/schema/yaml-1.1/schema.d.ts +1 -1
  82. package/dist/schema/yaml-1.1/set.js +5 -5
  83. package/dist/stringify/stringify.js +8 -8
  84. package/dist/stringify/stringifyCollection.js +8 -8
  85. package/dist/stringify/stringifyDocument.d.ts +3 -3
  86. package/dist/stringify/stringifyDocument.js +2 -2
  87. package/dist/stringify/stringifyPair.js +9 -9
  88. package/dist/stringify/stringifyString.js +6 -6
  89. package/dist/test-events.js +8 -8
  90. package/dist/util.d.ts +2 -0
  91. package/dist/util.js +4 -0
  92. package/dist/visit.js +18 -18
  93. package/package.json +5 -4
@@ -2,11 +2,10 @@
2
2
 
3
3
  var Alias = require('../nodes/Alias.js');
4
4
  var Collection = require('../nodes/Collection.js');
5
- var Node = require('../nodes/Node.js');
5
+ var identity = require('../nodes/identity.js');
6
6
  var Pair = require('../nodes/Pair.js');
7
7
  var toJS = require('../nodes/toJS.js');
8
8
  var Schema = require('../schema/Schema.js');
9
- var stringify = require('../stringify/stringify.js');
10
9
  var stringifyDocument = require('../stringify/stringifyDocument.js');
11
10
  var anchors = require('./anchors.js');
12
11
  var applyReviver = require('./applyReviver.js');
@@ -23,7 +22,7 @@ class Document {
23
22
  this.errors = [];
24
23
  /** Warnings encountered during parsing. */
25
24
  this.warnings = [];
26
- Object.defineProperty(this, Node.NODE_TYPE, { value: Node.DOC });
25
+ Object.defineProperty(this, identity.NODE_TYPE, { value: identity.DOC });
27
26
  let _replacer = null;
28
27
  if (typeof replacer === 'function' || Array.isArray(replacer)) {
29
28
  _replacer = replacer;
@@ -62,7 +61,7 @@ class Document {
62
61
  */
63
62
  clone() {
64
63
  const copy = Object.create(Document.prototype, {
65
- [Node.NODE_TYPE]: { value: Node.DOC }
64
+ [identity.NODE_TYPE]: { value: identity.DOC }
66
65
  });
67
66
  copy.commentBefore = this.commentBefore;
68
67
  copy.comment = this.comment;
@@ -73,7 +72,7 @@ class Document {
73
72
  copy.directives = this.directives.clone();
74
73
  copy.schema = this.schema.clone();
75
74
  // @ts-expect-error We can't really know that this matches Contents.
76
- copy.contents = Node.isNode(this.contents)
75
+ copy.contents = identity.isNode(this.contents)
77
76
  ? this.contents.clone(copy.schema)
78
77
  : this.contents;
79
78
  if (this.range)
@@ -139,7 +138,7 @@ class Document {
139
138
  sourceObjects
140
139
  };
141
140
  const node = createNode.createNode(value, tag, ctx);
142
- if (flow && Node.isCollection(node))
141
+ if (flow && identity.isCollection(node))
143
142
  node.flow = true;
144
143
  setAnchors();
145
144
  return node;
@@ -182,7 +181,7 @@ class Document {
182
181
  * `true` (collections are always returned intact).
183
182
  */
184
183
  get(key, keepScalar) {
185
- return Node.isCollection(this.contents)
184
+ return identity.isCollection(this.contents)
186
185
  ? this.contents.get(key, keepScalar)
187
186
  : undefined;
188
187
  }
@@ -193,10 +192,10 @@ class Document {
193
192
  */
194
193
  getIn(path, keepScalar) {
195
194
  if (Collection.isEmptyPath(path))
196
- return !keepScalar && Node.isScalar(this.contents)
195
+ return !keepScalar && identity.isScalar(this.contents)
197
196
  ? this.contents.value
198
197
  : this.contents;
199
- return Node.isCollection(this.contents)
198
+ return identity.isCollection(this.contents)
200
199
  ? this.contents.getIn(path, keepScalar)
201
200
  : undefined;
202
201
  }
@@ -204,7 +203,7 @@ class Document {
204
203
  * Checks if the document includes a value with the key `key`.
205
204
  */
206
205
  has(key) {
207
- return Node.isCollection(this.contents) ? this.contents.has(key) : false;
206
+ return identity.isCollection(this.contents) ? this.contents.has(key) : false;
208
207
  }
209
208
  /**
210
209
  * Checks if the document includes a value at `path`.
@@ -212,7 +211,7 @@ class Document {
212
211
  hasIn(path) {
213
212
  if (Collection.isEmptyPath(path))
214
213
  return this.contents !== undefined;
215
- return Node.isCollection(this.contents) ? this.contents.hasIn(path) : false;
214
+ return identity.isCollection(this.contents) ? this.contents.hasIn(path) : false;
216
215
  }
217
216
  /**
218
217
  * Sets a value in this document. For `!!set`, `value` needs to be a
@@ -297,8 +296,7 @@ class Document {
297
296
  keep: !json,
298
297
  mapAsMap: mapAsMap === true,
299
298
  mapKeyWarned: false,
300
- maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100,
301
- stringify: stringify.stringify
299
+ maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
302
300
  };
303
301
  const res = toJS.toJS(this.contents, jsonArg ?? '', ctx);
304
302
  if (typeof onAnchor === 'function')
@@ -330,7 +328,7 @@ class Document {
330
328
  }
331
329
  }
332
330
  function assertCollection(contents) {
333
- if (Node.isCollection(contents))
331
+ if (identity.isCollection(contents))
334
332
  return true;
335
333
  throw new Error('Expected a YAML collection as document contents');
336
334
  }
@@ -1,4 +1,4 @@
1
- import { Node } from '../nodes/Node.js';
1
+ import type { Node } from '../nodes/Node.js';
2
2
  import type { Document } from './Document.js';
3
3
  /**
4
4
  * Verify that the input string is a valid anchor.
@@ -19,6 +19,6 @@ export declare function createNodeAnchors(doc: Document<Node, boolean>, prefix:
19
19
  setAnchors: () => void;
20
20
  sourceObjects: Map<unknown, {
21
21
  anchor: string | null;
22
- node: Node<unknown> | null;
22
+ node: Node | null;
23
23
  }>;
24
24
  };
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var Node = require('../nodes/Node.js');
3
+ var identity = require('../nodes/identity.js');
4
4
  var visit = require('../visit.js');
5
5
 
6
6
  /**
@@ -57,7 +57,7 @@ function createNodeAnchors(doc, prefix) {
57
57
  const ref = sourceObjects.get(source);
58
58
  if (typeof ref === 'object' &&
59
59
  ref.anchor &&
60
- (Node.isScalar(ref.node) || Node.isCollection(ref.node))) {
60
+ (identity.isScalar(ref.node) || identity.isCollection(ref.node))) {
61
61
  ref.node.anchor = ref.anchor;
62
62
  }
63
63
  else {
@@ -1,4 +1,4 @@
1
- import { Node } from '../nodes/Node.js';
1
+ import type { Node } from '../nodes/Node.js';
2
2
  import type { Schema } from '../schema/Schema.js';
3
3
  import type { CollectionTag, ScalarTag } from '../schema/types.js';
4
4
  import type { Replacer } from './Document.js';
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var Alias = require('../nodes/Alias.js');
4
- var Node = require('../nodes/Node.js');
4
+ var identity = require('../nodes/identity.js');
5
5
  var Scalar = require('../nodes/Scalar.js');
6
6
 
7
7
  const defaultTagPrefix = 'tag:yaml.org,2002:';
@@ -16,12 +16,12 @@ function findTagObject(value, tagName, tags) {
16
16
  return tags.find(t => t.identify?.(value) && !t.format);
17
17
  }
18
18
  function createNode(value, tagName, ctx) {
19
- if (Node.isDocument(value))
19
+ if (identity.isDocument(value))
20
20
  value = value.contents;
21
- if (Node.isNode(value))
21
+ if (identity.isNode(value))
22
22
  return value;
23
- if (Node.isPair(value)) {
24
- const map = ctx.schema[Node.MAP].createNode?.(ctx.schema, null, ctx);
23
+ if (identity.isPair(value)) {
24
+ const map = ctx.schema[identity.MAP].createNode?.(ctx.schema, null, ctx);
25
25
  map.items.push(value);
26
26
  return map;
27
27
  }
@@ -65,10 +65,10 @@ function createNode(value, tagName, ctx) {
65
65
  }
66
66
  tagObj =
67
67
  value instanceof Map
68
- ? schema[Node.MAP]
68
+ ? schema[identity.MAP]
69
69
  : Symbol.iterator in Object(value)
70
- ? schema[Node.SEQ]
71
- : schema[Node.MAP];
70
+ ? schema[identity.SEQ]
71
+ : schema[identity.MAP];
72
72
  }
73
73
  if (onTagObj) {
74
74
  onTagObj(tagObj);
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var Node = require('../nodes/Node.js');
3
+ var identity = require('../nodes/identity.js');
4
4
  var visit = require('../visit.js');
5
5
 
6
6
  const escapeChars = {
@@ -146,10 +146,10 @@ class Directives {
146
146
  : [];
147
147
  const tagEntries = Object.entries(this.tags);
148
148
  let tagNames;
149
- if (doc && tagEntries.length > 0 && Node.isNode(doc.contents)) {
149
+ if (doc && tagEntries.length > 0 && identity.isNode(doc.contents)) {
150
150
  const tags = {};
151
151
  visit.visit(doc.contents, (_key, node) => {
152
- if (Node.isNode(node) && node.tag)
152
+ if (identity.isNode(node) && node.tag)
153
153
  tags[node.tag] = true;
154
154
  });
155
155
  tagNames = Object.keys(tags);
package/dist/index.d.ts CHANGED
@@ -3,7 +3,8 @@ export { Document } from './doc/Document.js';
3
3
  export { Schema } from './schema/Schema.js';
4
4
  export { ErrorCode, YAMLError, YAMLParseError, YAMLWarning } from './errors.js';
5
5
  export { Alias } from './nodes/Alias.js';
6
- export { isAlias, isCollection, isDocument, isMap, isNode, isPair, isScalar, isSeq, Node, ParsedNode, Range } from './nodes/Node.js';
6
+ export { isAlias, isCollection, isDocument, isMap, isNode, isPair, isScalar, isSeq } from './nodes/identity.js';
7
+ export { Node, ParsedNode, Range } from './nodes/Node.js';
7
8
  export { Pair } from './nodes/Pair.js';
8
9
  export { Scalar } from './nodes/Scalar.js';
9
10
  export { YAMLMap } from './nodes/YAMLMap.js';
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ var Document = require('./doc/Document.js');
5
5
  var Schema = require('./schema/Schema.js');
6
6
  var errors = require('./errors.js');
7
7
  var Alias = require('./nodes/Alias.js');
8
- var Node = require('./nodes/Node.js');
8
+ var identity = require('./nodes/identity.js');
9
9
  var Pair = require('./nodes/Pair.js');
10
10
  var Scalar = require('./nodes/Scalar.js');
11
11
  var YAMLMap = require('./nodes/YAMLMap.js');
@@ -26,14 +26,14 @@ exports.YAMLError = errors.YAMLError;
26
26
  exports.YAMLParseError = errors.YAMLParseError;
27
27
  exports.YAMLWarning = errors.YAMLWarning;
28
28
  exports.Alias = Alias.Alias;
29
- exports.isAlias = Node.isAlias;
30
- exports.isCollection = Node.isCollection;
31
- exports.isDocument = Node.isDocument;
32
- exports.isMap = Node.isMap;
33
- exports.isNode = Node.isNode;
34
- exports.isPair = Node.isPair;
35
- exports.isScalar = Node.isScalar;
36
- exports.isSeq = Node.isSeq;
29
+ exports.isAlias = identity.isAlias;
30
+ exports.isCollection = identity.isCollection;
31
+ exports.isDocument = identity.isDocument;
32
+ exports.isMap = identity.isMap;
33
+ exports.isNode = identity.isNode;
34
+ exports.isPair = identity.isPair;
35
+ exports.isScalar = identity.isScalar;
36
+ exports.isSeq = identity.isSeq;
37
37
  exports.Pair = Pair.Pair;
38
38
  exports.Scalar = Scalar.Scalar;
39
39
  exports.YAMLMap = YAMLMap.YAMLMap;
@@ -3,7 +3,7 @@ import type { FlowScalar } from '../parse/cst.js';
3
3
  import type { StringifyContext } from '../stringify/stringify.js';
4
4
  import { NodeBase, Range } from './Node.js';
5
5
  import type { Scalar } from './Scalar';
6
- import type { ToJSContext } from './toJS.js';
6
+ import { ToJSContext } from './toJS.js';
7
7
  import type { YAMLMap } from './YAMLMap.js';
8
8
  import type { YAMLSeq } from './YAMLSeq.js';
9
9
  export declare namespace Alias {
@@ -2,11 +2,13 @@
2
2
 
3
3
  var anchors = require('../doc/anchors.js');
4
4
  var visit = require('../visit.js');
5
+ var identity = require('./identity.js');
5
6
  var Node = require('./Node.js');
7
+ var toJS = require('./toJS.js');
6
8
 
7
9
  class Alias extends Node.NodeBase {
8
10
  constructor(source) {
9
- super(Node.ALIAS);
11
+ super(identity.ALIAS);
10
12
  this.source = source;
11
13
  Object.defineProperty(this, 'tag', {
12
14
  set() {
@@ -39,7 +41,12 @@ class Alias extends Node.NodeBase {
39
41
  const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
40
42
  throw new ReferenceError(msg);
41
43
  }
42
- const data = anchors.get(source);
44
+ let data = anchors.get(source);
45
+ if (!data) {
46
+ // Resolve anchors for Node.prototype.toJS()
47
+ toJS.toJS(source, null, ctx);
48
+ data = anchors.get(source);
49
+ }
43
50
  /* istanbul ignore if */
44
51
  if (!data || data.res === undefined) {
45
52
  const msg = 'This should not happen: Alias anchor was not resolved?';
@@ -71,12 +78,12 @@ class Alias extends Node.NodeBase {
71
78
  }
72
79
  }
73
80
  function getAliasCount(doc, node, anchors) {
74
- if (Node.isAlias(node)) {
81
+ if (identity.isAlias(node)) {
75
82
  const source = node.resolve(doc);
76
83
  const anchor = anchors && source && anchors.get(source);
77
84
  return anchor ? anchor.count * anchor.aliasCount : 0;
78
85
  }
79
- else if (Node.isCollection(node)) {
86
+ else if (identity.isCollection(node)) {
80
87
  let count = 0;
81
88
  for (const item of node.items) {
82
89
  const c = getAliasCount(doc, item, anchors);
@@ -85,7 +92,7 @@ function getAliasCount(doc, node, anchors) {
85
92
  }
86
93
  return count;
87
94
  }
88
- else if (Node.isPair(node)) {
95
+ else if (identity.isPair(node)) {
89
96
  const kc = getAliasCount(doc, node.key, anchors);
90
97
  const vc = getAliasCount(doc, node.value, anchors);
91
98
  return Math.max(kc, vc);
@@ -1,6 +1,7 @@
1
1
  import type { Schema } from '../schema/Schema.js';
2
- import { NodeBase, NODE_TYPE } from './Node.js';
3
- export declare function collectionFromPath(schema: Schema, path: unknown[], value: unknown): import("./Node.js").Node<unknown>;
2
+ import { NODE_TYPE } from './identity.js';
3
+ import { NodeBase } from './Node.js';
4
+ export declare function collectionFromPath(schema: Schema, path: unknown[], value: unknown): import("./Node.js").Node;
4
5
  export declare const isEmptyPath: (path: Iterable<unknown> | null | undefined) => path is null | undefined;
5
6
  export declare abstract class Collection extends NodeBase {
6
7
  static maxFlowStringSingleLineLength: number;
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var createNode = require('../doc/createNode.js');
4
+ var identity = require('./identity.js');
4
5
  var Node = require('./Node.js');
5
6
 
6
7
  function collectionFromPath(schema, path, value) {
@@ -49,7 +50,7 @@ class Collection extends Node.NodeBase {
49
50
  const copy = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
50
51
  if (schema)
51
52
  copy.schema = schema;
52
- copy.items = copy.items.map(it => Node.isNode(it) || Node.isPair(it) ? it.clone(schema) : it);
53
+ copy.items = copy.items.map(it => identity.isNode(it) || identity.isPair(it) ? it.clone(schema) : it);
53
54
  if (this.range)
54
55
  copy.range = this.range.slice();
55
56
  return copy;
@@ -65,7 +66,7 @@ class Collection extends Node.NodeBase {
65
66
  else {
66
67
  const [key, ...rest] = path;
67
68
  const node = this.get(key, true);
68
- if (Node.isCollection(node))
69
+ if (identity.isCollection(node))
69
70
  node.addIn(rest, value);
70
71
  else if (node === undefined && this.schema)
71
72
  this.set(key, collectionFromPath(this.schema, rest, value));
@@ -82,7 +83,7 @@ class Collection extends Node.NodeBase {
82
83
  if (rest.length === 0)
83
84
  return this.delete(key);
84
85
  const node = this.get(key, true);
85
- if (Node.isCollection(node))
86
+ if (identity.isCollection(node))
86
87
  return node.deleteIn(rest);
87
88
  else
88
89
  throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
@@ -96,18 +97,18 @@ class Collection extends Node.NodeBase {
96
97
  const [key, ...rest] = path;
97
98
  const node = this.get(key, true);
98
99
  if (rest.length === 0)
99
- return !keepScalar && Node.isScalar(node) ? node.value : node;
100
+ return !keepScalar && identity.isScalar(node) ? node.value : node;
100
101
  else
101
- return Node.isCollection(node) ? node.getIn(rest, keepScalar) : undefined;
102
+ return identity.isCollection(node) ? node.getIn(rest, keepScalar) : undefined;
102
103
  }
103
104
  hasAllNullValues(allowScalar) {
104
105
  return this.items.every(node => {
105
- if (!Node.isPair(node))
106
+ if (!identity.isPair(node))
106
107
  return false;
107
108
  const n = node.value;
108
109
  return (n == null ||
109
110
  (allowScalar &&
110
- Node.isScalar(n) &&
111
+ identity.isScalar(n) &&
111
112
  n.value == null &&
112
113
  !n.commentBefore &&
113
114
  !n.comment &&
@@ -122,7 +123,7 @@ class Collection extends Node.NodeBase {
122
123
  if (rest.length === 0)
123
124
  return this.has(key);
124
125
  const node = this.get(key, true);
125
- return Node.isCollection(node) ? node.hasIn(rest) : false;
126
+ return identity.isCollection(node) ? node.hasIn(rest) : false;
126
127
  }
127
128
  /**
128
129
  * Sets a value in this collection. For `!!set`, `value` needs to be a
@@ -135,7 +136,7 @@ class Collection extends Node.NodeBase {
135
136
  }
136
137
  else {
137
138
  const node = this.get(key, true);
138
- if (Node.isCollection(node))
139
+ if (identity.isCollection(node))
139
140
  node.setIn(rest, value);
140
141
  else if (node === undefined && this.schema)
141
142
  this.set(key, collectionFromPath(this.schema, rest, value));
@@ -1,8 +1,9 @@
1
1
  import type { Document } from '../doc/Document.js';
2
+ import type { ToJSOptions } from '../options.js';
2
3
  import { Token } from '../parse/cst.js';
3
4
  import type { StringifyContext } from '../stringify/stringify.js';
4
5
  import type { Alias } from './Alias.js';
5
- import type { Pair } from './Pair.js';
6
+ import { NODE_TYPE } from './identity.js';
6
7
  import type { Scalar } from './Scalar.js';
7
8
  import type { YAMLMap } from './YAMLMap.js';
8
9
  import type { YAMLSeq } from './YAMLSeq.js';
@@ -15,22 +16,6 @@ export type NodeType<T> = T extends string | number | bigint | boolean | null |
15
16
  } ? YAMLMap<NodeType<keyof T>, NodeType<T[keyof T]>> : Node;
16
17
  export type ParsedNode = Alias.Parsed | Scalar.Parsed | YAMLMap.Parsed | YAMLSeq.Parsed;
17
18
  export type Range = [number, number, number];
18
- export declare const ALIAS: unique symbol;
19
- export declare const DOC: unique symbol;
20
- export declare const MAP: unique symbol;
21
- export declare const PAIR: unique symbol;
22
- export declare const SCALAR: unique symbol;
23
- export declare const SEQ: unique symbol;
24
- export declare const NODE_TYPE: unique symbol;
25
- export declare const isAlias: (node: any) => node is Alias;
26
- export declare const isDocument: <T extends Node<unknown> = Node<unknown>>(node: any) => node is Document<T, true>;
27
- export declare const isMap: <K = unknown, V = unknown>(node: any) => node is YAMLMap<K, V>;
28
- export declare const isPair: <K = unknown, V = unknown>(node: any) => node is Pair<K, V>;
29
- export declare const isScalar: <T = unknown>(node: any) => node is Scalar<T>;
30
- export declare const isSeq: <T = unknown>(node: any) => node is YAMLSeq<T>;
31
- export declare function isCollection<K = unknown, V = unknown>(node: any): node is YAMLMap<K, V> | YAMLSeq<V>;
32
- export declare function isNode<T = unknown>(node: any): node is Node<T>;
33
- export declare const hasAnchor: <K = unknown, V = unknown>(node: unknown) => node is Scalar<V> | YAMLMap<K, V> | YAMLSeq<V>;
34
19
  export declare abstract class NodeBase {
35
20
  readonly [NODE_TYPE]: symbol;
36
21
  /** A comment on or immediately after this */
@@ -56,4 +41,6 @@ export declare abstract class NodeBase {
56
41
  constructor(type: symbol);
57
42
  /** Create a copy of this node. */
58
43
  clone(): NodeBase;
44
+ /** A plain JavaScript representation of this node. */
45
+ toJS(doc: Document<Node, boolean>, { mapAsMap, maxAliasCount, onAnchor, reviver }?: ToJSOptions): any;
59
46
  }
@@ -1,42 +1,12 @@
1
1
  'use strict';
2
2
 
3
- const ALIAS = Symbol.for('yaml.alias');
4
- const DOC = Symbol.for('yaml.document');
5
- const MAP = Symbol.for('yaml.map');
6
- const PAIR = Symbol.for('yaml.pair');
7
- const SCALAR = Symbol.for('yaml.scalar');
8
- const SEQ = Symbol.for('yaml.seq');
9
- const NODE_TYPE = Symbol.for('yaml.node.type');
10
- const isAlias = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS;
11
- const isDocument = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC;
12
- const isMap = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP;
13
- const isPair = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR;
14
- const isScalar = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR;
15
- const isSeq = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ;
16
- function isCollection(node) {
17
- if (node && typeof node === 'object')
18
- switch (node[NODE_TYPE]) {
19
- case MAP:
20
- case SEQ:
21
- return true;
22
- }
23
- return false;
24
- }
25
- function isNode(node) {
26
- if (node && typeof node === 'object')
27
- switch (node[NODE_TYPE]) {
28
- case ALIAS:
29
- case MAP:
30
- case SCALAR:
31
- case SEQ:
32
- return true;
33
- }
34
- return false;
35
- }
36
- const hasAnchor = (node) => (isScalar(node) || isCollection(node)) && !!node.anchor;
3
+ var applyReviver = require('../doc/applyReviver.js');
4
+ var identity = require('./identity.js');
5
+ var toJS = require('./toJS.js');
6
+
37
7
  class NodeBase {
38
8
  constructor(type) {
39
- Object.defineProperty(this, NODE_TYPE, { value: type });
9
+ Object.defineProperty(this, identity.NODE_TYPE, { value: type });
40
10
  }
41
11
  /** Create a copy of this node. */
42
12
  clone() {
@@ -45,22 +15,24 @@ class NodeBase {
45
15
  copy.range = this.range.slice();
46
16
  return copy;
47
17
  }
18
+ /** A plain JavaScript representation of this node. */
19
+ toJS(doc, { mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
20
+ const ctx = {
21
+ anchors: new Map(),
22
+ doc,
23
+ keep: true,
24
+ mapAsMap: mapAsMap === true,
25
+ mapKeyWarned: false,
26
+ maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
27
+ };
28
+ const res = toJS.toJS(this, '', ctx);
29
+ if (typeof onAnchor === 'function')
30
+ for (const { count, res } of ctx.anchors.values())
31
+ onAnchor(res, count);
32
+ return typeof reviver === 'function'
33
+ ? applyReviver.applyReviver(reviver, { '': res }, '', res)
34
+ : res;
35
+ }
48
36
  }
49
37
 
50
- exports.ALIAS = ALIAS;
51
- exports.DOC = DOC;
52
- exports.MAP = MAP;
53
- exports.NODE_TYPE = NODE_TYPE;
54
38
  exports.NodeBase = NodeBase;
55
- exports.PAIR = PAIR;
56
- exports.SCALAR = SCALAR;
57
- exports.SEQ = SEQ;
58
- exports.hasAnchor = hasAnchor;
59
- exports.isAlias = isAlias;
60
- exports.isCollection = isCollection;
61
- exports.isDocument = isDocument;
62
- exports.isMap = isMap;
63
- exports.isNode = isNode;
64
- exports.isPair = isPair;
65
- exports.isScalar = isScalar;
66
- exports.isSeq = isSeq;
@@ -3,9 +3,9 @@ import type { CollectionItem } from '../parse/cst.js';
3
3
  import type { Schema } from '../schema/Schema.js';
4
4
  import type { StringifyContext } from '../stringify/stringify.js';
5
5
  import { addPairToJSMap } from './addPairToJSMap.js';
6
- import { NODE_TYPE } from './Node.js';
6
+ import { NODE_TYPE } from './identity.js';
7
7
  import type { ToJSContext } from './toJS.js';
8
- export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<import("./Node.js").Node<unknown>, import("./Alias.js").Alias | import("./Scalar.js").Scalar<unknown> | import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./YAMLSeq.js").YAMLSeq<unknown>>;
8
+ export declare function createPair(key: unknown, value: unknown, ctx: CreateNodeContext): Pair<import("./Node.js").Node, import("./Alias.js").Alias | import("./Scalar.js").Scalar<unknown> | import("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./YAMLSeq.js").YAMLSeq<unknown>>;
9
9
  export declare class Pair<K = unknown, V = unknown> {
10
10
  readonly [NODE_TYPE]: symbol;
11
11
  /** Always Node or null when parsed, but can be set to anything. */
@@ -3,7 +3,7 @@
3
3
  var createNode = require('../doc/createNode.js');
4
4
  var stringifyPair = require('../stringify/stringifyPair.js');
5
5
  var addPairToJSMap = require('./addPairToJSMap.js');
6
- var Node = require('./Node.js');
6
+ var identity = require('./identity.js');
7
7
 
8
8
  function createPair(key, value, ctx) {
9
9
  const k = createNode.createNode(key, undefined, ctx);
@@ -12,15 +12,15 @@ function createPair(key, value, ctx) {
12
12
  }
13
13
  class Pair {
14
14
  constructor(key, value = null) {
15
- Object.defineProperty(this, Node.NODE_TYPE, { value: Node.PAIR });
15
+ Object.defineProperty(this, identity.NODE_TYPE, { value: identity.PAIR });
16
16
  this.key = key;
17
17
  this.value = value;
18
18
  }
19
19
  clone(schema) {
20
20
  let { key, value } = this;
21
- if (Node.isNode(key))
21
+ if (identity.isNode(key))
22
22
  key = key.clone(schema);
23
- if (Node.isNode(value))
23
+ if (identity.isNode(value))
24
24
  value = value.clone(schema);
25
25
  return new Pair(key, value);
26
26
  }
@@ -1,12 +1,13 @@
1
1
  'use strict';
2
2
 
3
+ var identity = require('./identity.js');
3
4
  var Node = require('./Node.js');
4
5
  var toJS = require('./toJS.js');
5
6
 
6
7
  const isScalarValue = (value) => !value || (typeof value !== 'function' && typeof value !== 'object');
7
8
  class Scalar extends Node.NodeBase {
8
9
  constructor(value) {
9
- super(Node.SCALAR);
10
+ super(identity.SCALAR);
10
11
  this.value = value;
11
12
  }
12
13
  toJSON(arg, ctx) {
@@ -2,7 +2,7 @@ import type { BlockMap, FlowCollection } from '../parse/cst.js';
2
2
  import type { Schema } from '../schema/Schema.js';
3
3
  import type { StringifyContext } from '../stringify/stringify.js';
4
4
  import { Collection } from './Collection.js';
5
- import { ParsedNode, Range } from './Node.js';
5
+ import type { ParsedNode, Range } from './Node.js';
6
6
  import { Pair } from './Pair.js';
7
7
  import { Scalar } from './Scalar.js';
8
8
  import type { ToJSContext } from './toJS.js';