yaml 2.2.1 → 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 (100) hide show
  1. package/README.md +4 -0
  2. package/browser/dist/compose/compose-collection.js +1 -1
  3. package/browser/dist/compose/compose-doc.js +1 -0
  4. package/browser/dist/compose/compose-scalar.js +1 -1
  5. package/browser/dist/compose/composer.js +1 -1
  6. package/browser/dist/compose/resolve-flow-collection.js +1 -1
  7. package/browser/dist/compose/util-map-includes.js +1 -1
  8. package/browser/dist/doc/Document.js +12 -10
  9. package/browser/dist/doc/anchors.js +1 -1
  10. package/browser/dist/doc/createNode.js +1 -1
  11. package/browser/dist/doc/directives.js +1 -1
  12. package/browser/dist/index.js +1 -1
  13. package/browser/dist/nodes/Alias.js +9 -2
  14. package/browser/dist/nodes/Collection.js +2 -1
  15. package/browser/dist/nodes/Node.js +23 -35
  16. package/browser/dist/nodes/Pair.js +1 -1
  17. package/browser/dist/nodes/Scalar.js +2 -1
  18. package/browser/dist/nodes/YAMLMap.js +1 -1
  19. package/browser/dist/nodes/YAMLSeq.js +1 -1
  20. package/browser/dist/nodes/addPairToJSMap.js +1 -1
  21. package/browser/dist/nodes/identity.js +36 -0
  22. package/browser/dist/nodes/toJS.js +1 -1
  23. package/browser/dist/schema/Schema.js +1 -1
  24. package/browser/dist/schema/common/map.js +1 -1
  25. package/browser/dist/schema/common/seq.js +1 -1
  26. package/browser/dist/schema/tags.js +1 -1
  27. package/browser/dist/schema/yaml-1.1/omap.js +2 -2
  28. package/browser/dist/schema/yaml-1.1/pairs.js +1 -1
  29. package/browser/dist/schema/yaml-1.1/set.js +1 -1
  30. package/browser/dist/stringify/stringify.js +1 -1
  31. package/browser/dist/stringify/stringifyCollection.js +2 -2
  32. package/browser/dist/stringify/stringifyDocument.js +1 -1
  33. package/browser/dist/stringify/stringifyPair.js +1 -1
  34. package/browser/dist/stringify/stringifyString.js +16 -7
  35. package/browser/dist/util.js +2 -0
  36. package/browser/dist/visit.js +1 -1
  37. package/dist/compose/compose-collection.d.ts +1 -1
  38. package/dist/compose/compose-collection.js +3 -3
  39. package/dist/compose/compose-doc.d.ts +2 -1
  40. package/dist/compose/compose-doc.js +1 -0
  41. package/dist/compose/compose-scalar.js +7 -7
  42. package/dist/compose/composer.d.ts +5 -5
  43. package/dist/compose/composer.js +3 -3
  44. package/dist/compose/resolve-flow-collection.d.ts +1 -1
  45. package/dist/compose/resolve-flow-collection.js +2 -2
  46. package/dist/compose/util-map-includes.d.ts +3 -3
  47. package/dist/compose/util-map-includes.js +3 -3
  48. package/dist/doc/Document.d.ts +11 -9
  49. package/dist/doc/Document.js +22 -20
  50. package/dist/doc/anchors.d.ts +4 -4
  51. package/dist/doc/anchors.js +2 -2
  52. package/dist/doc/createNode.d.ts +1 -1
  53. package/dist/doc/createNode.js +8 -8
  54. package/dist/doc/directives.js +3 -3
  55. package/dist/index.d.ts +2 -1
  56. package/dist/index.js +9 -9
  57. package/dist/nodes/Alias.d.ts +1 -1
  58. package/dist/nodes/Alias.js +12 -5
  59. package/dist/nodes/Collection.d.ts +3 -2
  60. package/dist/nodes/Collection.js +10 -9
  61. package/dist/nodes/Node.d.ts +5 -18
  62. package/dist/nodes/Node.js +23 -51
  63. package/dist/nodes/Pair.d.ts +2 -2
  64. package/dist/nodes/Pair.js +4 -4
  65. package/dist/nodes/Scalar.js +2 -1
  66. package/dist/nodes/YAMLMap.d.ts +1 -1
  67. package/dist/nodes/YAMLMap.js +9 -9
  68. package/dist/nodes/YAMLSeq.d.ts +1 -1
  69. package/dist/nodes/YAMLSeq.js +5 -5
  70. package/dist/nodes/addPairToJSMap.js +7 -7
  71. package/dist/nodes/identity.d.ts +23 -0
  72. package/dist/nodes/identity.js +53 -0
  73. package/dist/nodes/toJS.d.ts +2 -5
  74. package/dist/nodes/toJS.js +2 -2
  75. package/dist/public-api.d.ts +3 -3
  76. package/dist/schema/Schema.d.ts +1 -1
  77. package/dist/schema/Schema.js +4 -4
  78. package/dist/schema/common/map.js +2 -2
  79. package/dist/schema/common/seq.js +2 -2
  80. package/dist/schema/core/schema.d.ts +1 -1
  81. package/dist/schema/json/schema.d.ts +1 -1
  82. package/dist/schema/tags.d.ts +1 -1
  83. package/dist/schema/yaml-1.1/omap.d.ts +1 -1
  84. package/dist/schema/yaml-1.1/omap.js +4 -4
  85. package/dist/schema/yaml-1.1/pairs.d.ts +1 -1
  86. package/dist/schema/yaml-1.1/pairs.js +5 -5
  87. package/dist/schema/yaml-1.1/schema.d.ts +1 -1
  88. package/dist/schema/yaml-1.1/set.js +5 -5
  89. package/dist/stringify/stringify.js +8 -8
  90. package/dist/stringify/stringifyCollection.js +9 -9
  91. package/dist/stringify/stringifyDocument.d.ts +4 -3
  92. package/dist/stringify/stringifyDocument.js +2 -2
  93. package/dist/stringify/stringifyPair.js +9 -9
  94. package/dist/stringify/stringifyString.js +16 -7
  95. package/dist/test-events.js +8 -8
  96. package/dist/util.d.ts +3 -1
  97. package/dist/util.js +4 -0
  98. package/dist/visit.js +18 -18
  99. package/package.json +13 -6
  100. package/util.d.ts +0 -3
@@ -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,36 +1,21 @@
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';
9
10
  export type Node<T = unknown> = Alias | Scalar<T> | YAMLMap<unknown, T> | YAMLSeq<T>;
10
11
  /** Utility type mapper */
11
- export type NodeType<T> = T extends string | number | bigint | boolean | null ? Scalar<T> : T extends Array<any> ? YAMLSeq<NodeType<T[number]>> : T extends {
12
+ export type NodeType<T> = T extends string | number | bigint | boolean | null | undefined ? Scalar<T> : T extends Date ? Scalar<string | Date> : T extends Array<any> ? YAMLSeq<NodeType<T[number]>> : T extends {
12
13
  [key: string]: any;
13
14
  } ? YAMLMap<NodeType<keyof T>, NodeType<T[keyof T]>> : T extends {
14
15
  [key: number]: any;
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>;
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';
@@ -3,17 +3,17 @@
3
3
  var stringifyCollection = require('../stringify/stringifyCollection.js');
4
4
  var addPairToJSMap = require('./addPairToJSMap.js');
5
5
  var Collection = require('./Collection.js');
6
- var Node = require('./Node.js');
6
+ var identity = require('./identity.js');
7
7
  var Pair = require('./Pair.js');
8
8
  var Scalar = require('./Scalar.js');
9
9
 
10
10
  function findPair(items, key) {
11
- const k = Node.isScalar(key) ? key.value : key;
11
+ const k = identity.isScalar(key) ? key.value : key;
12
12
  for (const it of items) {
13
- if (Node.isPair(it)) {
13
+ if (identity.isPair(it)) {
14
14
  if (it.key === key || it.key === k)
15
15
  return it;
16
- if (Node.isScalar(it.key) && it.key.value === k)
16
+ if (identity.isScalar(it.key) && it.key.value === k)
17
17
  return it;
18
18
  }
19
19
  }
@@ -24,7 +24,7 @@ class YAMLMap extends Collection.Collection {
24
24
  return 'tag:yaml.org,2002:map';
25
25
  }
26
26
  constructor(schema) {
27
- super(Node.MAP, schema);
27
+ super(identity.MAP, schema);
28
28
  this.items = [];
29
29
  }
30
30
  /**
@@ -35,7 +35,7 @@ class YAMLMap extends Collection.Collection {
35
35
  */
36
36
  add(pair, overwrite) {
37
37
  let _pair;
38
- if (Node.isPair(pair))
38
+ if (identity.isPair(pair))
39
39
  _pair = pair;
40
40
  else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
41
41
  // In TypeScript, this never happens.
@@ -49,7 +49,7 @@ class YAMLMap extends Collection.Collection {
49
49
  if (!overwrite)
50
50
  throw new Error(`Key ${_pair.key} already set`);
51
51
  // For scalars, keep the old node & its comments and anchors
52
- if (Node.isScalar(prev.value) && Scalar.isScalarValue(_pair.value))
52
+ if (identity.isScalar(prev.value) && Scalar.isScalarValue(_pair.value))
53
53
  prev.value.value = _pair.value;
54
54
  else
55
55
  prev.value = _pair.value;
@@ -75,7 +75,7 @@ class YAMLMap extends Collection.Collection {
75
75
  get(key, keepScalar) {
76
76
  const it = findPair(this.items, key);
77
77
  const node = it?.value;
78
- return (!keepScalar && Node.isScalar(node) ? node.value : node) ?? undefined;
78
+ return (!keepScalar && identity.isScalar(node) ? node.value : node) ?? undefined;
79
79
  }
80
80
  has(key) {
81
81
  return !!findPair(this.items, key);
@@ -100,7 +100,7 @@ class YAMLMap extends Collection.Collection {
100
100
  if (!ctx)
101
101
  return JSON.stringify(this);
102
102
  for (const item of this.items) {
103
- if (!Node.isPair(item))
103
+ if (!identity.isPair(item))
104
104
  throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
105
105
  }
106
106
  if (!ctx.allNullValues && this.hasAllNullValues(false))
@@ -2,7 +2,7 @@ import type { BlockSequence, 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 type { Pair } from './Pair.js';
7
7
  import { Scalar } from './Scalar.js';
8
8
  import { ToJSContext } from './toJS.js';
@@ -2,7 +2,7 @@
2
2
 
3
3
  var stringifyCollection = require('../stringify/stringifyCollection.js');
4
4
  var Collection = require('./Collection.js');
5
- var Node = require('./Node.js');
5
+ var identity = require('./identity.js');
6
6
  var Scalar = require('./Scalar.js');
7
7
  var toJS = require('./toJS.js');
8
8
 
@@ -11,7 +11,7 @@ class YAMLSeq extends Collection.Collection {
11
11
  return 'tag:yaml.org,2002:seq';
12
12
  }
13
13
  constructor(schema) {
14
- super(Node.SEQ, schema);
14
+ super(identity.SEQ, schema);
15
15
  this.items = [];
16
16
  }
17
17
  add(value) {
@@ -37,7 +37,7 @@ class YAMLSeq extends Collection.Collection {
37
37
  if (typeof idx !== 'number')
38
38
  return undefined;
39
39
  const it = this.items[idx];
40
- return !keepScalar && Node.isScalar(it) ? it.value : it;
40
+ return !keepScalar && identity.isScalar(it) ? it.value : it;
41
41
  }
42
42
  /**
43
43
  * Checks if the collection includes a value with the key `key`.
@@ -61,7 +61,7 @@ class YAMLSeq extends Collection.Collection {
61
61
  if (typeof idx !== 'number')
62
62
  throw new Error(`Expected a valid index, not ${key}.`);
63
63
  const prev = this.items[idx];
64
- if (Node.isScalar(prev) && Scalar.isScalarValue(value))
64
+ if (identity.isScalar(prev) && Scalar.isScalarValue(value))
65
65
  prev.value = value;
66
66
  else
67
67
  this.items[idx] = value;
@@ -88,7 +88,7 @@ class YAMLSeq extends Collection.Collection {
88
88
  }
89
89
  }
90
90
  function asItemIndex(key) {
91
- let idx = Node.isScalar(key) ? key.value : key;
91
+ let idx = identity.isScalar(key) ? key.value : key;
92
92
  if (idx && typeof idx === 'string')
93
93
  idx = Number(idx);
94
94
  return typeof idx === 'number' && Number.isInteger(idx) && idx >= 0
@@ -2,15 +2,15 @@
2
2
 
3
3
  var log = require('../log.js');
4
4
  var stringify = require('../stringify/stringify.js');
5
- var Node = require('./Node.js');
5
+ var identity = require('./identity.js');
6
6
  var Scalar = require('./Scalar.js');
7
7
  var toJS = require('./toJS.js');
8
8
 
9
9
  const MERGE_KEY = '<<';
10
10
  function addPairToJSMap(ctx, map, { key, value }) {
11
11
  if (ctx?.doc.schema.merge && isMergeKey(key)) {
12
- value = Node.isAlias(value) ? value.resolve(ctx.doc) : value;
13
- if (Node.isSeq(value))
12
+ value = identity.isAlias(value) ? value.resolve(ctx.doc) : value;
13
+ if (identity.isSeq(value))
14
14
  for (const it of value.items)
15
15
  mergeToJSMap(ctx, map, it);
16
16
  else if (Array.isArray(value))
@@ -44,7 +44,7 @@ function addPairToJSMap(ctx, map, { key, value }) {
44
44
  return map;
45
45
  }
46
46
  const isMergeKey = (key) => key === MERGE_KEY ||
47
- (Node.isScalar(key) &&
47
+ (identity.isScalar(key) &&
48
48
  key.value === MERGE_KEY &&
49
49
  (!key.type || key.type === Scalar.Scalar.PLAIN));
50
50
  // If the value associated with a merge key is a single mapping node, each of
@@ -55,8 +55,8 @@ const isMergeKey = (key) => key === MERGE_KEY ||
55
55
  // Keys in mapping nodes earlier in the sequence override keys specified in
56
56
  // later mapping nodes. -- http://yaml.org/type/merge.html
57
57
  function mergeToJSMap(ctx, map, value) {
58
- const source = ctx && Node.isAlias(value) ? value.resolve(ctx.doc) : value;
59
- if (!Node.isMap(source))
58
+ const source = ctx && identity.isAlias(value) ? value.resolve(ctx.doc) : value;
59
+ if (!identity.isMap(source))
60
60
  throw new Error('Merge sources must be maps or map aliases');
61
61
  const srcMap = source.toJSON(null, ctx, Map);
62
62
  for (const [key, value] of srcMap) {
@@ -83,7 +83,7 @@ function stringifyKey(key, jsKey, ctx) {
83
83
  return '';
84
84
  if (typeof jsKey !== 'object')
85
85
  return String(jsKey);
86
- if (Node.isNode(key) && ctx && ctx.doc) {
86
+ if (identity.isNode(key) && ctx && ctx.doc) {
87
87
  const strCtx = stringify.createStringifyContext(ctx.doc, {});
88
88
  strCtx.anchors = new Set();
89
89
  for (const node of ctx.anchors.keys())
@@ -0,0 +1,23 @@
1
+ import type { Document } from '../doc/Document.js';
2
+ import type { Alias } from './Alias.js';
3
+ import type { Node } from './Node.js';
4
+ import type { Pair } from './Pair.js';
5
+ import type { Scalar } from './Scalar.js';
6
+ import type { YAMLMap } from './YAMLMap.js';
7
+ import type { YAMLSeq } from './YAMLSeq.js';
8
+ export declare const ALIAS: unique symbol;
9
+ export declare const DOC: unique symbol;
10
+ export declare const MAP: unique symbol;
11
+ export declare const PAIR: unique symbol;
12
+ export declare const SCALAR: unique symbol;
13
+ export declare const SEQ: unique symbol;
14
+ export declare const NODE_TYPE: unique symbol;
15
+ export declare const isAlias: (node: any) => node is Alias;
16
+ export declare const isDocument: <T extends Node = Node>(node: any) => node is Document<T, true>;
17
+ export declare const isMap: <K = unknown, V = unknown>(node: any) => node is YAMLMap<K, V>;
18
+ export declare const isPair: <K = unknown, V = unknown>(node: any) => node is Pair<K, V>;
19
+ export declare const isScalar: <T = unknown>(node: any) => node is Scalar<T>;
20
+ export declare const isSeq: <T = unknown>(node: any) => node is YAMLSeq<T>;
21
+ export declare function isCollection<K = unknown, V = unknown>(node: any): node is YAMLMap<K, V> | YAMLSeq<V>;
22
+ export declare function isNode<T = unknown>(node: any): node is Node<T>;
23
+ export declare const hasAnchor: <K = unknown, V = unknown>(node: unknown) => node is Scalar<V> | YAMLMap<K, V> | YAMLSeq<V>;
@@ -0,0 +1,53 @@
1
+ 'use strict';
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;
37
+
38
+ exports.ALIAS = ALIAS;
39
+ exports.DOC = DOC;
40
+ exports.MAP = MAP;
41
+ exports.NODE_TYPE = NODE_TYPE;
42
+ exports.PAIR = PAIR;
43
+ exports.SCALAR = SCALAR;
44
+ exports.SEQ = SEQ;
45
+ exports.hasAnchor = hasAnchor;
46
+ exports.isAlias = isAlias;
47
+ exports.isCollection = isCollection;
48
+ exports.isDocument = isDocument;
49
+ exports.isMap = isMap;
50
+ exports.isNode = isNode;
51
+ exports.isPair = isPair;
52
+ exports.isScalar = isScalar;
53
+ exports.isSeq = isSeq;
@@ -1,6 +1,5 @@
1
1
  import type { Document } from '../doc/Document.js';
2
- import type { stringify } from '../stringify/stringify.js';
3
- import { Node } from './Node.js';
2
+ import type { Node } from './Node.js';
4
3
  export interface AnchorData {
5
4
  aliasCount: number;
6
5
  count: number;
@@ -8,14 +7,12 @@ export interface AnchorData {
8
7
  }
9
8
  export interface ToJSContext {
10
9
  anchors: Map<Node, AnchorData>;
11
- doc: Document;
10
+ doc: Document<Node, boolean>;
12
11
  keep: boolean;
13
12
  mapAsMap: boolean;
14
13
  mapKeyWarned: boolean;
15
14
  maxAliasCount: number;
16
15
  onCreate?: (res: unknown) => void;
17
- /** Requiring this directly in Pair would create circular dependencies */
18
- stringify: typeof stringify;
19
16
  }
20
17
  /**
21
18
  * Recursively convert any node or its contents to native JavaScript
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var Node = require('./Node.js');
3
+ var identity = require('./identity.js');
4
4
 
5
5
  /**
6
6
  * Recursively convert any node or its contents to native JavaScript
@@ -18,7 +18,7 @@ function toJS(value, arg, ctx) {
18
18
  return value.map((v, i) => toJS(v, String(i), ctx));
19
19
  if (value && typeof value.toJSON === 'function') {
20
20
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
21
- if (!ctx || !Node.hasAnchor(value))
21
+ if (!ctx || !identity.hasAnchor(value))
22
22
  return value.toJSON(arg, ctx);
23
23
  const data = { aliasCount: 0, count: 1, res: undefined };
24
24
  ctx.anchors.set(value, data);
@@ -1,7 +1,7 @@
1
1
  import { Composer } from './compose/composer.js';
2
2
  import type { Reviver } from './doc/applyReviver.js';
3
3
  import { Document, Replacer } from './doc/Document.js';
4
- import type { ParsedNode } from './nodes/Node.js';
4
+ import type { Node, ParsedNode } from './nodes/Node.js';
5
5
  import type { CreateNodeOptions, DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions, ToStringOptions } from './options.js';
6
6
  export interface EmptyStream extends Array<Document.Parsed>, ReturnType<Composer['streamInfo']> {
7
7
  empty: true;
@@ -15,9 +15,9 @@ export interface EmptyStream extends Array<Document.Parsed>, ReturnType<Composer
15
15
  * EmptyStream and contain additional stream information. In
16
16
  * TypeScript, you should use `'empty' in docs` as a type guard for it.
17
17
  */
18
- export declare function parseAllDocuments<T extends ParsedNode = ParsedNode>(source: string, options?: ParseOptions & DocumentOptions & SchemaOptions): Document.Parsed<T>[] | EmptyStream;
18
+ export declare function parseAllDocuments<Contents extends Node = ParsedNode, Strict extends boolean = true>(source: string, options?: ParseOptions & DocumentOptions & SchemaOptions): Array<Contents extends ParsedNode ? Document.Parsed<Contents, Strict> : Document<Contents, Strict>> | EmptyStream;
19
19
  /** Parse an input string into a single YAML.Document */
20
- export declare function parseDocument<T extends ParsedNode = ParsedNode>(source: string, options?: ParseOptions & DocumentOptions & SchemaOptions): Document.Parsed<T>;
20
+ export declare function parseDocument<Contents extends Node = ParsedNode, Strict extends boolean = true>(source: string, options?: ParseOptions & DocumentOptions & SchemaOptions): Contents extends ParsedNode ? Document.Parsed<Contents, Strict> : Document<Contents, Strict>;
21
21
  /**
22
22
  * Parse an input string into JavaScript.
23
23
  *
@@ -1,4 +1,4 @@
1
- import { MAP, SCALAR, SEQ } from '../nodes/Node.js';
1
+ import { MAP, SCALAR, SEQ } from '../nodes/identity.js';
2
2
  import type { Pair } from '../nodes/Pair.js';
3
3
  import type { SchemaOptions, ToStringOptions } from '../options.js';
4
4
  import type { CollectionTag, ScalarTag } from './types.js';
@@ -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 map = require('./common/map.js');
5
5
  var seq = require('./common/seq.js');
6
6
  var string = require('./common/string.js');
@@ -19,9 +19,9 @@ class Schema {
19
19
  this.knownTags = resolveKnownTags ? tags.coreKnownTags : {};
20
20
  this.tags = tags.getTags(customTags, this.name);
21
21
  this.toStringOptions = toStringDefaults ?? null;
22
- Object.defineProperty(this, Node.MAP, { value: map.map });
23
- Object.defineProperty(this, Node.SCALAR, { value: string.string });
24
- Object.defineProperty(this, Node.SEQ, { value: seq.seq });
22
+ Object.defineProperty(this, identity.MAP, { value: map.map });
23
+ Object.defineProperty(this, identity.SCALAR, { value: string.string });
24
+ Object.defineProperty(this, identity.SEQ, { value: seq.seq });
25
25
  // Used by createMap()
26
26
  this.sortMapEntries =
27
27
  typeof sortMapEntries === 'function'
@@ -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 Pair = require('../../nodes/Pair.js');
5
5
  var YAMLMap = require('../../nodes/YAMLMap.js');
6
6
 
@@ -35,7 +35,7 @@ const map = {
35
35
  nodeClass: YAMLMap.YAMLMap,
36
36
  tag: 'tag:yaml.org,2002:map',
37
37
  resolve(map, onError) {
38
- if (!Node.isMap(map))
38
+ if (!identity.isMap(map))
39
39
  onError('Expected a mapping for this tag');
40
40
  return map;
41
41
  }
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var createNode = require('../../doc/createNode.js');
4
- var Node = require('../../nodes/Node.js');
4
+ var identity = require('../../nodes/identity.js');
5
5
  var YAMLSeq = require('../../nodes/YAMLSeq.js');
6
6
 
7
7
  function createSeq(schema, obj, ctx) {
@@ -26,7 +26,7 @@ const seq = {
26
26
  nodeClass: YAMLSeq.YAMLSeq,
27
27
  tag: 'tag:yaml.org,2002:seq',
28
28
  resolve(seq, onError) {
29
- if (!Node.isSeq(seq))
29
+ if (!identity.isSeq(seq))
30
30
  onError('Expected a sequence for this tag');
31
31
  return seq;
32
32
  }