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 +1 @@
1
- export declare const schema: (import("../types.js").ScalarTag | import("../types.js").CollectionTag)[];
1
+ export declare const schema: (import("../types.js").CollectionTag | import("../types.js").ScalarTag)[];
@@ -1,2 +1,2 @@
1
1
  import { CollectionTag, ScalarTag } from '../types.js';
2
- export declare const schema: (ScalarTag | CollectionTag)[];
2
+ export declare const schema: (CollectionTag | ScalarTag)[];
@@ -36,5 +36,5 @@ export declare const coreKnownTags: {
36
36
  test: RegExp;
37
37
  };
38
38
  };
39
- export declare function getTags(customTags: SchemaOptions['customTags'] | undefined, schemaName: string): (ScalarTag | CollectionTag)[];
39
+ export declare function getTags(customTags: SchemaOptions['customTags'] | undefined, schemaName: string): (CollectionTag | ScalarTag)[];
40
40
  export {};
@@ -1,5 +1,5 @@
1
- import { YAMLSeq } from '../../nodes/YAMLSeq.js';
2
1
  import { ToJSContext } from '../../nodes/toJS.js';
2
+ import { YAMLSeq } from '../../nodes/YAMLSeq.js';
3
3
  import { CollectionTag } from '../types.js';
4
4
  export declare class YAMLOMap extends YAMLSeq {
5
5
  static tag: string;
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
- var YAMLSeq = require('../../nodes/YAMLSeq.js');
3
+ var identity = require('../../nodes/identity.js');
4
4
  var toJS = require('../../nodes/toJS.js');
5
- var Node = require('../../nodes/Node.js');
6
5
  var YAMLMap = require('../../nodes/YAMLMap.js');
6
+ var YAMLSeq = require('../../nodes/YAMLSeq.js');
7
7
  var pairs = require('./pairs.js');
8
8
 
9
9
  class YAMLOMap extends YAMLSeq.YAMLSeq {
@@ -28,7 +28,7 @@ class YAMLOMap extends YAMLSeq.YAMLSeq {
28
28
  ctx.onCreate(map);
29
29
  for (const pair of this.items) {
30
30
  let key, value;
31
- if (Node.isPair(pair)) {
31
+ if (identity.isPair(pair)) {
32
32
  key = toJS.toJS(pair.key, '', ctx);
33
33
  value = toJS.toJS(pair.value, key, ctx);
34
34
  }
@@ -53,7 +53,7 @@ const omap = {
53
53
  const pairs$1 = pairs.resolvePairs(seq, onError);
54
54
  const seenKeys = [];
55
55
  for (const { key } of pairs$1.items) {
56
- if (Node.isScalar(key)) {
56
+ if (identity.isScalar(key)) {
57
57
  if (seenKeys.includes(key.value)) {
58
58
  onError(`Ordered maps must not include duplicate keys: ${key.value}`);
59
59
  }
@@ -1,5 +1,5 @@
1
1
  import type { CreateNodeContext } from '../../doc/createNode.js';
2
- import { ParsedNode } from '../../nodes/Node.js';
2
+ import type { ParsedNode } from '../../nodes/Node.js';
3
3
  import { Pair } from '../../nodes/Pair.js';
4
4
  import { YAMLMap } from '../../nodes/YAMLMap.js';
5
5
  import { YAMLSeq } from '../../nodes/YAMLSeq.js';
@@ -1,17 +1,17 @@
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 Scalar = require('../../nodes/Scalar.js');
6
6
  var YAMLSeq = require('../../nodes/YAMLSeq.js');
7
7
 
8
8
  function resolvePairs(seq, onError) {
9
- if (Node.isSeq(seq)) {
9
+ if (identity.isSeq(seq)) {
10
10
  for (let i = 0; i < seq.items.length; ++i) {
11
11
  let item = seq.items[i];
12
- if (Node.isPair(item))
12
+ if (identity.isPair(item))
13
13
  continue;
14
- else if (Node.isMap(item)) {
14
+ else if (identity.isMap(item)) {
15
15
  if (item.items.length > 1)
16
16
  onError('Each pair must have its own sequence indicator');
17
17
  const pair = item.items[0] || new Pair.Pair(new Scalar.Scalar(null));
@@ -27,7 +27,7 @@ function resolvePairs(seq, onError) {
27
27
  }
28
28
  item = pair;
29
29
  }
30
- seq.items[i] = Node.isPair(item) ? item : new Pair.Pair(item);
30
+ seq.items[i] = identity.isPair(item) ? item : new Pair.Pair(item);
31
31
  }
32
32
  }
33
33
  else
@@ -1 +1 @@
1
- export declare const schema: (import("../types.js").ScalarTag | import("../types.js").CollectionTag)[];
1
+ export declare const schema: (import("../types.js").CollectionTag | import("../types.js").ScalarTag)[];
@@ -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
 
@@ -11,7 +11,7 @@ class YAMLSet extends YAMLMap.YAMLMap {
11
11
  }
12
12
  add(key) {
13
13
  let pair;
14
- if (Node.isPair(key))
14
+ if (identity.isPair(key))
15
15
  pair = key;
16
16
  else if (key &&
17
17
  typeof key === 'object' &&
@@ -31,8 +31,8 @@ class YAMLSet extends YAMLMap.YAMLMap {
31
31
  */
32
32
  get(key, keepPair) {
33
33
  const pair = YAMLMap.findPair(this.items, key);
34
- return !keepPair && Node.isPair(pair)
35
- ? Node.isScalar(pair.key)
34
+ return !keepPair && identity.isPair(pair)
35
+ ? identity.isScalar(pair.key)
36
36
  ? pair.key.value
37
37
  : pair.key
38
38
  : pair;
@@ -68,7 +68,7 @@ const set = {
68
68
  default: false,
69
69
  tag: 'tag:yaml.org,2002:set',
70
70
  resolve(map, onError) {
71
- if (Node.isMap(map)) {
71
+ if (identity.isMap(map)) {
72
72
  if (map.hasAllNullValues(true))
73
73
  return Object.assign(new YAMLSet(), map);
74
74
  else
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var anchors = require('../doc/anchors.js');
4
- var Node = require('../nodes/Node.js');
4
+ var identity = require('../nodes/identity.js');
5
5
  var stringifyComment = require('./stringifyComment.js');
6
6
  var stringifyString = require('./stringifyString.js');
7
7
 
@@ -54,7 +54,7 @@ function getTagObject(tags, item) {
54
54
  }
55
55
  let tagObj = undefined;
56
56
  let obj;
57
- if (Node.isScalar(item)) {
57
+ if (identity.isScalar(item)) {
58
58
  obj = item.value;
59
59
  const match = tags.filter(t => t.identify?.(obj));
60
60
  tagObj =
@@ -75,7 +75,7 @@ function stringifyProps(node, tagObj, { anchors: anchors$1, doc }) {
75
75
  if (!doc.directives)
76
76
  return '';
77
77
  const props = [];
78
- const anchor = (Node.isScalar(node) || Node.isCollection(node)) && node.anchor;
78
+ const anchor = (identity.isScalar(node) || identity.isCollection(node)) && node.anchor;
79
79
  if (anchor && anchors.anchorIsValid(anchor)) {
80
80
  anchors$1.add(anchor);
81
81
  props.push(`&${anchor}`);
@@ -86,9 +86,9 @@ function stringifyProps(node, tagObj, { anchors: anchors$1, doc }) {
86
86
  return props.join(' ');
87
87
  }
88
88
  function stringify(item, ctx, onComment, onChompKeep) {
89
- if (Node.isPair(item))
89
+ if (identity.isPair(item))
90
90
  return item.toString(ctx, onComment, onChompKeep);
91
- if (Node.isAlias(item)) {
91
+ if (identity.isAlias(item)) {
92
92
  if (ctx.doc.directives)
93
93
  return item.toString(ctx);
94
94
  if (ctx.resolvedAliases?.has(item)) {
@@ -103,7 +103,7 @@ function stringify(item, ctx, onComment, onChompKeep) {
103
103
  }
104
104
  }
105
105
  let tagObj = undefined;
106
- const node = Node.isNode(item)
106
+ const node = identity.isNode(item)
107
107
  ? item
108
108
  : ctx.doc.createNode(item, { onTagObj: o => (tagObj = o) });
109
109
  if (!tagObj)
@@ -113,12 +113,12 @@ function stringify(item, ctx, onComment, onChompKeep) {
113
113
  ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1;
114
114
  const str = typeof tagObj.stringify === 'function'
115
115
  ? tagObj.stringify(node, ctx, onComment, onChompKeep)
116
- : Node.isScalar(node)
116
+ : identity.isScalar(node)
117
117
  ? stringifyString.stringifyString(node, ctx, onComment, onChompKeep)
118
118
  : node.toString(ctx, onComment, onChompKeep);
119
119
  if (!props)
120
120
  return str;
121
- return Node.isScalar(node) || str[0] === '{' || str[0] === '['
121
+ return identity.isScalar(node) || str[0] === '{' || str[0] === '['
122
122
  ? `${props} ${str}`
123
123
  : `${props}\n${ctx.indent}${str}`;
124
124
  }
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var Collection = require('../nodes/Collection.js');
4
- var Node = require('../nodes/Node.js');
4
+ var identity = require('../nodes/identity.js');
5
5
  var stringify = require('./stringify.js');
6
6
  var stringifyComment = require('./stringifyComment.js');
7
7
 
@@ -18,15 +18,15 @@ function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, fl
18
18
  for (let i = 0; i < items.length; ++i) {
19
19
  const item = items[i];
20
20
  let comment = null;
21
- if (Node.isNode(item)) {
21
+ if (identity.isNode(item)) {
22
22
  if (!chompKeep && item.spaceBefore)
23
23
  lines.push('');
24
24
  addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
25
25
  if (item.comment)
26
26
  comment = item.comment;
27
27
  }
28
- else if (Node.isPair(item)) {
29
- const ik = Node.isNode(item.key) ? item.key : null;
28
+ else if (identity.isPair(item)) {
29
+ const ik = identity.isNode(item.key) ? item.key : null;
30
30
  if (ik) {
31
31
  if (!chompKeep && ik.spaceBefore)
32
32
  lines.push('');
@@ -75,15 +75,15 @@ function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemInden
75
75
  for (let i = 0; i < items.length; ++i) {
76
76
  const item = items[i];
77
77
  let comment = null;
78
- if (Node.isNode(item)) {
78
+ if (identity.isNode(item)) {
79
79
  if (item.spaceBefore)
80
80
  lines.push('');
81
81
  addCommentBefore(ctx, lines, item.commentBefore, false);
82
82
  if (item.comment)
83
83
  comment = item.comment;
84
84
  }
85
- else if (Node.isPair(item)) {
86
- const ik = Node.isNode(item.key) ? item.key : null;
85
+ else if (identity.isPair(item)) {
86
+ const ik = identity.isNode(item.key) ? item.key : null;
87
87
  if (ik) {
88
88
  if (ik.spaceBefore)
89
89
  lines.push('');
@@ -91,7 +91,7 @@ function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemInden
91
91
  if (ik.comment)
92
92
  reqNewline = true;
93
93
  }
94
- const iv = Node.isNode(item.value) ? item.value : null;
94
+ const iv = identity.isNode(item.value) ? item.value : null;
95
95
  if (iv) {
96
96
  if (iv.comment)
97
97
  comment = iv.comment;
@@ -135,7 +135,7 @@ function stringifyFlowCollection({ comment, items }, ctx, { flowChars, itemInden
135
135
  }
136
136
  }
137
137
  if (comment) {
138
- str += stringifyComment.lineComment(str, commentString(comment), indent);
138
+ str += stringifyComment.lineComment(str, indent, commentString(comment));
139
139
  if (onComment)
140
140
  onComment();
141
141
  }
@@ -1,3 +1,4 @@
1
- import { Document } from '../doc/Document.js';
2
- import { ToStringOptions } from '../options.js';
3
- export declare function stringifyDocument(doc: Readonly<Document>, options: ToStringOptions): string;
1
+ import type { Document } from '../doc/Document.js';
2
+ import type { Node } from '../nodes/Node.js';
3
+ import type { ToStringOptions } from '../options.js';
4
+ export declare function stringifyDocument(doc: Readonly<Document<Node, boolean>>, options: ToStringOptions): string;
@@ -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 stringify = require('./stringify.js');
5
5
  var stringifyComment = require('./stringifyComment.js');
6
6
 
@@ -29,7 +29,7 @@ function stringifyDocument(doc, options) {
29
29
  let chompKeep = false;
30
30
  let contentComment = null;
31
31
  if (doc.contents) {
32
- if (Node.isNode(doc.contents)) {
32
+ if (identity.isNode(doc.contents)) {
33
33
  if (doc.contents.spaceBefore && hasDirectives)
34
34
  lines.push('');
35
35
  if (doc.contents.commentBefore) {
@@ -1,18 +1,18 @@
1
1
  'use strict';
2
2
 
3
- var Node = require('../nodes/Node.js');
3
+ var identity = require('../nodes/identity.js');
4
4
  var Scalar = require('../nodes/Scalar.js');
5
5
  var stringify = require('./stringify.js');
6
6
  var stringifyComment = require('./stringifyComment.js');
7
7
 
8
8
  function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
9
9
  const { allNullValues, doc, indent, indentStep, options: { commentString, indentSeq, simpleKeys } } = ctx;
10
- let keyComment = (Node.isNode(key) && key.comment) || null;
10
+ let keyComment = (identity.isNode(key) && key.comment) || null;
11
11
  if (simpleKeys) {
12
12
  if (keyComment) {
13
13
  throw new Error('With simple keys, key nodes cannot have comments');
14
14
  }
15
- if (Node.isCollection(key)) {
15
+ if (identity.isCollection(key)) {
16
16
  const msg = 'With simple keys, collection cannot be used as a key value';
17
17
  throw new Error(msg);
18
18
  }
@@ -20,8 +20,8 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
20
20
  let explicitKey = !simpleKeys &&
21
21
  (!key ||
22
22
  (keyComment && value == null && !ctx.inFlow) ||
23
- Node.isCollection(key) ||
24
- (Node.isScalar(key)
23
+ identity.isCollection(key) ||
24
+ (identity.isScalar(key)
25
25
  ? key.type === Scalar.Scalar.BLOCK_FOLDED || key.type === Scalar.Scalar.BLOCK_LITERAL
26
26
  : typeof key === 'object'));
27
27
  ctx = Object.assign({}, ctx, {
@@ -66,7 +66,7 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
66
66
  str += stringifyComment.lineComment(str, ctx.indent, commentString(keyComment));
67
67
  }
68
68
  let vsb, vcb, valueComment;
69
- if (Node.isNode(value)) {
69
+ if (identity.isNode(value)) {
70
70
  vsb = !!value.spaceBefore;
71
71
  vcb = value.commentBefore;
72
72
  valueComment = value.comment;
@@ -79,14 +79,14 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
79
79
  value = doc.createNode(value);
80
80
  }
81
81
  ctx.implicitKey = false;
82
- if (!explicitKey && !keyComment && Node.isScalar(value))
82
+ if (!explicitKey && !keyComment && identity.isScalar(value))
83
83
  ctx.indentAtStart = str.length + 1;
84
84
  chompKeep = false;
85
85
  if (!indentSeq &&
86
86
  indentStep.length >= 2 &&
87
87
  !ctx.inFlow &&
88
88
  !explicitKey &&
89
- Node.isSeq(value) &&
89
+ identity.isSeq(value) &&
90
90
  !value.flow &&
91
91
  !value.tag &&
92
92
  !value.anchor) {
@@ -110,7 +110,7 @@ function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
110
110
  ws += `\n${ctx.indent}`;
111
111
  }
112
112
  }
113
- else if (!explicitKey && Node.isCollection(value)) {
113
+ else if (!explicitKey && identity.isCollection(value)) {
114
114
  const vs0 = valueStr[0];
115
115
  const nl0 = valueStr.indexOf('\n');
116
116
  const hasNewline = nl0 !== -1;
@@ -3,8 +3,8 @@
3
3
  var Scalar = require('../nodes/Scalar.js');
4
4
  var foldFlowLines = require('./foldFlowLines.js');
5
5
 
6
- const getFoldOptions = (ctx) => ({
7
- indentAtStart: ctx.indentAtStart,
6
+ const getFoldOptions = (ctx, isBlock) => ({
7
+ indentAtStart: isBlock ? ctx.indent.length : ctx.indentAtStart,
8
8
  lineWidth: ctx.options.lineWidth,
9
9
  minContentWidth: ctx.options.minContentWidth
10
10
  });
@@ -117,7 +117,7 @@ function doubleQuotedString(value, ctx) {
117
117
  str = start ? str + json.slice(start) : json;
118
118
  return implicitKey
119
119
  ? str
120
- : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx));
120
+ : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_QUOTED, getFoldOptions(ctx, false));
121
121
  }
122
122
  function singleQuotedString(value, ctx) {
123
123
  if (ctx.options.singleQuote === false ||
@@ -129,7 +129,7 @@ function singleQuotedString(value, ctx) {
129
129
  const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
130
130
  return ctx.implicitKey
131
131
  ? res
132
- : foldFlowLines.foldFlowLines(res, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
132
+ : foldFlowLines.foldFlowLines(res, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx, false));
133
133
  }
134
134
  function quotedString(value, ctx) {
135
135
  const { singleQuote } = ctx.options;
@@ -148,6 +148,15 @@ function quotedString(value, ctx) {
148
148
  }
149
149
  return qs(value, ctx);
150
150
  }
151
+ // The negative lookbehind avoids a polynomial search,
152
+ // but isn't supported yet on Safari: https://caniuse.com/js-regexp-lookbehind
153
+ let blockEndNewlines;
154
+ try {
155
+ blockEndNewlines = new RegExp('(^|(?<!\n))\n+(?!\n|$)', 'g');
156
+ }
157
+ catch {
158
+ blockEndNewlines = /\n+(?!\n|$)/g;
159
+ }
151
160
  function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
152
161
  const { blockQuote, commentString, lineWidth } = ctx.options;
153
162
  // 1. Block can't end in whitespace unless the last line is non-empty.
@@ -191,7 +200,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
191
200
  value = value.slice(0, -end.length);
192
201
  if (end[end.length - 1] === '\n')
193
202
  end = end.slice(0, -1);
194
- end = end.replace(/\n+(?!\n|$)/g, `$&${indent}`);
203
+ end = end.replace(blockEndNewlines, `$&${indent}`);
195
204
  }
196
205
  // determine indent indicator from whitespace at value start
197
206
  let startWithSpace = false;
@@ -227,7 +236,7 @@ function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
227
236
  .replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
228
237
  // ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
229
238
  .replace(/\n+/g, `$&${indent}`);
230
- const body = foldFlowLines.foldFlowLines(`${start}${value}${end}`, indent, foldFlowLines.FOLD_BLOCK, getFoldOptions(ctx));
239
+ const body = foldFlowLines.foldFlowLines(`${start}${value}${end}`, indent, foldFlowLines.FOLD_BLOCK, getFoldOptions(ctx, true));
231
240
  return `${header}\n${indent}${body}`;
232
241
  }
233
242
  function plainString(item, ctx, onComment, onChompKeep) {
@@ -277,7 +286,7 @@ function plainString(item, ctx, onComment, onChompKeep) {
277
286
  }
278
287
  return implicitKey
279
288
  ? str
280
- : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx));
289
+ : foldFlowLines.foldFlowLines(str, indent, foldFlowLines.FOLD_FLOW, getFoldOptions(ctx, false));
281
290
  }
282
291
  function stringifyString(item, ctx, onComment, onChompKeep) {
283
292
  const { implicitKey, inFlow } = ctx;
@@ -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 publicApi = require('./public-api.js');
5
5
  var visit = require('./visit.js');
6
6
 
@@ -66,10 +66,10 @@ function addEvents(events, doc, errPos, node) {
66
66
  events.push('=VAL :');
67
67
  return;
68
68
  }
69
- if (errPos !== -1 && Node.isNode(node) && node.range[0] >= errPos)
69
+ if (errPos !== -1 && identity.isNode(node) && node.range[0] >= errPos)
70
70
  throw new Error();
71
71
  let props = '';
72
- let anchor = Node.isScalar(node) || Node.isCollection(node) ? node.anchor : undefined;
72
+ let anchor = identity.isScalar(node) || identity.isCollection(node) ? node.anchor : undefined;
73
73
  if (anchor) {
74
74
  if (/\d$/.test(anchor)) {
75
75
  const alt = anchor.replace(/\d$/, '');
@@ -78,9 +78,9 @@ function addEvents(events, doc, errPos, node) {
78
78
  }
79
79
  props = ` &${anchor}`;
80
80
  }
81
- if (Node.isNode(node) && node.tag)
81
+ if (identity.isNode(node) && node.tag)
82
82
  props += ` <${node.tag}>`;
83
- if (Node.isMap(node)) {
83
+ if (identity.isMap(node)) {
84
84
  const ev = node.flow ? '+MAP {}' : '+MAP';
85
85
  events.push(`${ev}${props}`);
86
86
  node.items.forEach(({ key, value }) => {
@@ -89,7 +89,7 @@ function addEvents(events, doc, errPos, node) {
89
89
  });
90
90
  events.push('-MAP');
91
91
  }
92
- else if (Node.isSeq(node)) {
92
+ else if (identity.isSeq(node)) {
93
93
  const ev = node.flow ? '+SEQ []' : '+SEQ';
94
94
  events.push(`${ev}${props}`);
95
95
  node.items.forEach(item => {
@@ -97,13 +97,13 @@ function addEvents(events, doc, errPos, node) {
97
97
  });
98
98
  events.push('-SEQ');
99
99
  }
100
- else if (Node.isPair(node)) {
100
+ else if (identity.isPair(node)) {
101
101
  events.push(`+MAP${props}`);
102
102
  addEvents(events, doc, errPos, node.key);
103
103
  addEvents(events, doc, errPos, node.value);
104
104
  events.push('-MAP');
105
105
  }
106
- else if (Node.isAlias(node)) {
106
+ else if (identity.isAlias(node)) {
107
107
  let alias = node.source;
108
108
  if (alias && /\d$/.test(alias)) {
109
109
  const alt = alias.replace(/\d$/, '');
package/dist/util.d.ts CHANGED
@@ -1,9 +1,11 @@
1
+ export { createNode, CreateNodeContext } from './doc/createNode.js';
1
2
  export { debug, LogLevelId, warn } from './log.js';
3
+ export { createPair } from './nodes/Pair.js';
2
4
  export { findPair } from './nodes/YAMLMap.js';
3
5
  export { toJS, ToJSContext } from './nodes/toJS.js';
4
6
  export { map as mapTag } from './schema/common/map.js';
5
7
  export { seq as seqTag } from './schema/common/seq.js';
6
8
  export { string as stringTag } from './schema/common/string.js';
7
- export { foldFlowLines } from './stringify/foldFlowLines';
9
+ export { foldFlowLines, FoldOptions } from './stringify/foldFlowLines';
8
10
  export { stringifyNumber } from './stringify/stringifyNumber.js';
9
11
  export { stringifyString } from './stringify/stringifyString.js';
package/dist/util.js CHANGED
@@ -1,6 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ var createNode = require('./doc/createNode.js');
3
4
  var log = require('./log.js');
5
+ var Pair = require('./nodes/Pair.js');
4
6
  var YAMLMap = require('./nodes/YAMLMap.js');
5
7
  var toJS = require('./nodes/toJS.js');
6
8
  var map = require('./schema/common/map.js');
@@ -12,8 +14,10 @@ var stringifyString = require('./stringify/stringifyString.js');
12
14
 
13
15
 
14
16
 
17
+ exports.createNode = createNode.createNode;
15
18
  exports.debug = log.debug;
16
19
  exports.warn = log.warn;
20
+ exports.createPair = Pair.createPair;
17
21
  exports.findPair = YAMLMap.findPair;
18
22
  exports.toJS = toJS.toJS;
19
23
  exports.mapTag = map.map;
package/dist/visit.js CHANGED
@@ -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
 
5
5
  const BREAK = Symbol('break visit');
6
6
  const SKIP = Symbol('skip children');
@@ -37,7 +37,7 @@ const REMOVE = Symbol('remove node');
37
37
  */
38
38
  function visit(node, visitor) {
39
39
  const visitor_ = initVisitor(visitor);
40
- if (Node.isDocument(node)) {
40
+ if (identity.isDocument(node)) {
41
41
  const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
42
42
  if (cd === REMOVE)
43
43
  node.contents = null;
@@ -56,12 +56,12 @@ visit.SKIP = SKIP;
56
56
  visit.REMOVE = REMOVE;
57
57
  function visit_(key, node, visitor, path) {
58
58
  const ctrl = callVisitor(key, node, visitor, path);
59
- if (Node.isNode(ctrl) || Node.isPair(ctrl)) {
59
+ if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
60
60
  replaceNode(key, path, ctrl);
61
61
  return visit_(key, ctrl, visitor, path);
62
62
  }
63
63
  if (typeof ctrl !== 'symbol') {
64
- if (Node.isCollection(node)) {
64
+ if (identity.isCollection(node)) {
65
65
  path = Object.freeze(path.concat(node));
66
66
  for (let i = 0; i < node.items.length; ++i) {
67
67
  const ci = visit_(i, node.items[i], visitor, path);
@@ -75,7 +75,7 @@ function visit_(key, node, visitor, path) {
75
75
  }
76
76
  }
77
77
  }
78
- else if (Node.isPair(node)) {
78
+ else if (identity.isPair(node)) {
79
79
  path = Object.freeze(path.concat(node));
80
80
  const ck = visit_('key', node.key, visitor, path);
81
81
  if (ck === BREAK)
@@ -124,7 +124,7 @@ function visit_(key, node, visitor, path) {
124
124
  */
125
125
  async function visitAsync(node, visitor) {
126
126
  const visitor_ = initVisitor(visitor);
127
- if (Node.isDocument(node)) {
127
+ if (identity.isDocument(node)) {
128
128
  const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
129
129
  if (cd === REMOVE)
130
130
  node.contents = null;
@@ -143,12 +143,12 @@ visitAsync.SKIP = SKIP;
143
143
  visitAsync.REMOVE = REMOVE;
144
144
  async function visitAsync_(key, node, visitor, path) {
145
145
  const ctrl = await callVisitor(key, node, visitor, path);
146
- if (Node.isNode(ctrl) || Node.isPair(ctrl)) {
146
+ if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
147
147
  replaceNode(key, path, ctrl);
148
148
  return visitAsync_(key, ctrl, visitor, path);
149
149
  }
150
150
  if (typeof ctrl !== 'symbol') {
151
- if (Node.isCollection(node)) {
151
+ if (identity.isCollection(node)) {
152
152
  path = Object.freeze(path.concat(node));
153
153
  for (let i = 0; i < node.items.length; ++i) {
154
154
  const ci = await visitAsync_(i, node.items[i], visitor, path);
@@ -162,7 +162,7 @@ async function visitAsync_(key, node, visitor, path) {
162
162
  }
163
163
  }
164
164
  }
165
- else if (Node.isPair(node)) {
165
+ else if (identity.isPair(node)) {
166
166
  path = Object.freeze(path.concat(node));
167
167
  const ck = await visitAsync_('key', node.key, visitor, path);
168
168
  if (ck === BREAK)
@@ -200,34 +200,34 @@ function initVisitor(visitor) {
200
200
  function callVisitor(key, node, visitor, path) {
201
201
  if (typeof visitor === 'function')
202
202
  return visitor(key, node, path);
203
- if (Node.isMap(node))
203
+ if (identity.isMap(node))
204
204
  return visitor.Map?.(key, node, path);
205
- if (Node.isSeq(node))
205
+ if (identity.isSeq(node))
206
206
  return visitor.Seq?.(key, node, path);
207
- if (Node.isPair(node))
207
+ if (identity.isPair(node))
208
208
  return visitor.Pair?.(key, node, path);
209
- if (Node.isScalar(node))
209
+ if (identity.isScalar(node))
210
210
  return visitor.Scalar?.(key, node, path);
211
- if (Node.isAlias(node))
211
+ if (identity.isAlias(node))
212
212
  return visitor.Alias?.(key, node, path);
213
213
  return undefined;
214
214
  }
215
215
  function replaceNode(key, path, node) {
216
216
  const parent = path[path.length - 1];
217
- if (Node.isCollection(parent)) {
217
+ if (identity.isCollection(parent)) {
218
218
  parent.items[key] = node;
219
219
  }
220
- else if (Node.isPair(parent)) {
220
+ else if (identity.isPair(parent)) {
221
221
  if (key === 'key')
222
222
  parent.key = node;
223
223
  else
224
224
  parent.value = node;
225
225
  }
226
- else if (Node.isDocument(parent)) {
226
+ else if (identity.isDocument(parent)) {
227
227
  parent.contents = node;
228
228
  }
229
229
  else {
230
- const pt = Node.isAlias(parent) ? 'alias' : 'scalar';
230
+ const pt = identity.isAlias(parent) ? 'alias' : 'scalar';
231
231
  throw new Error(`Cannot replace node with ${pt} parent`);
232
232
  }
233
233
  }