yaml 2.3.0-3 → 2.3.0-5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/browser/dist/compose/compose-collection.js +46 -29
  2. package/browser/dist/compose/resolve-block-map.js +3 -2
  3. package/browser/dist/compose/resolve-block-seq.js +3 -2
  4. package/browser/dist/compose/resolve-flow-collection.js +3 -4
  5. package/browser/dist/doc/createNode.js +5 -1
  6. package/browser/dist/errors.js +1 -1
  7. package/browser/dist/nodes/YAMLMap.js +29 -1
  8. package/browser/dist/nodes/YAMLSeq.js +16 -0
  9. package/browser/dist/schema/common/map.js +2 -27
  10. package/browser/dist/schema/common/seq.js +2 -18
  11. package/browser/dist/schema/yaml-1.1/omap.js +7 -6
  12. package/browser/dist/schema/yaml-1.1/set.js +13 -12
  13. package/browser/dist/schema/yaml-1.1/timestamp.js +1 -1
  14. package/dist/compose/compose-collection.js +45 -28
  15. package/dist/compose/resolve-block-map.d.ts +2 -1
  16. package/dist/compose/resolve-block-map.js +3 -2
  17. package/dist/compose/resolve-block-seq.d.ts +2 -1
  18. package/dist/compose/resolve-block-seq.js +3 -2
  19. package/dist/compose/resolve-flow-collection.d.ts +2 -1
  20. package/dist/compose/resolve-flow-collection.js +3 -4
  21. package/dist/doc/createNode.js +5 -1
  22. package/dist/errors.d.ts +1 -1
  23. package/dist/errors.js +1 -1
  24. package/dist/nodes/Pair.d.ts +1 -1
  25. package/dist/nodes/YAMLMap.d.ts +6 -0
  26. package/dist/nodes/YAMLMap.js +28 -0
  27. package/dist/nodes/YAMLSeq.d.ts +2 -0
  28. package/dist/nodes/YAMLSeq.js +16 -0
  29. package/dist/schema/common/map.js +2 -27
  30. package/dist/schema/common/seq.js +2 -18
  31. package/dist/schema/types.d.ts +11 -3
  32. package/dist/schema/yaml-1.1/omap.d.ts +3 -0
  33. package/dist/schema/yaml-1.1/omap.js +7 -6
  34. package/dist/schema/yaml-1.1/set.d.ts +3 -1
  35. package/dist/schema/yaml-1.1/set.js +12 -11
  36. package/dist/schema/yaml-1.1/timestamp.js +1 -1
  37. package/dist/util.d.ts +1 -0
  38. package/package.json +1 -1
@@ -1,38 +1,50 @@
1
- import { isNode, isMap } from '../nodes/identity.js';
1
+ import { isNode } from '../nodes/identity.js';
2
2
  import { Scalar } from '../nodes/Scalar.js';
3
+ import { YAMLMap } from '../nodes/YAMLMap.js';
4
+ import { YAMLSeq } from '../nodes/YAMLSeq.js';
3
5
  import { resolveBlockMap } from './resolve-block-map.js';
4
6
  import { resolveBlockSeq } from './resolve-block-seq.js';
5
7
  import { resolveFlowCollection } from './resolve-flow-collection.js';
6
8
 
7
- function composeCollection(CN, ctx, token, tagToken, onError) {
8
- let coll;
9
- switch (token.type) {
10
- case 'block-map': {
11
- coll = resolveBlockMap(CN, ctx, token, onError);
12
- break;
13
- }
14
- case 'block-seq': {
15
- coll = resolveBlockSeq(CN, ctx, token, onError);
16
- break;
17
- }
18
- case 'flow-collection': {
19
- coll = resolveFlowCollection(CN, ctx, token, onError);
20
- break;
21
- }
22
- }
23
- if (!tagToken)
24
- return coll;
25
- const tagName = ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
26
- if (!tagName)
27
- return coll;
28
- // Cast needed due to: https://github.com/Microsoft/TypeScript/issues/3841
9
+ function resolveCollection(CN, ctx, token, onError, tagName, tag) {
10
+ const coll = token.type === 'block-map'
11
+ ? resolveBlockMap(CN, ctx, token, onError, tag)
12
+ : token.type === 'block-seq'
13
+ ? resolveBlockSeq(CN, ctx, token, onError, tag)
14
+ : resolveFlowCollection(CN, ctx, token, onError, tag);
29
15
  const Coll = coll.constructor;
16
+ // If we got a tagName matching the class, or the tag name is '!',
17
+ // then use the tagName from the node class used to create it.
30
18
  if (tagName === '!' || tagName === Coll.tagName) {
31
19
  coll.tag = Coll.tagName;
32
20
  return coll;
33
21
  }
34
- const expType = isMap(coll) ? 'map' : 'seq';
35
- let tag = ctx.schema.tags.find(t => t.collection === expType && t.tag === tagName);
22
+ if (tagName)
23
+ coll.tag = tagName;
24
+ return coll;
25
+ }
26
+ function composeCollection(CN, ctx, token, tagToken, onError) {
27
+ const tagName = !tagToken
28
+ ? null
29
+ : ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
30
+ const expType = token.type === 'block-map'
31
+ ? 'map'
32
+ : token.type === 'block-seq'
33
+ ? 'seq'
34
+ : token.start.source === '{'
35
+ ? 'map'
36
+ : 'seq';
37
+ // shortcut: check if it's a generic YAMLMap or YAMLSeq
38
+ // before jumping into the custom tag logic.
39
+ if (!tagToken ||
40
+ !tagName ||
41
+ tagName === '!' ||
42
+ (tagName === YAMLMap.tagName && expType === 'map') ||
43
+ (tagName === YAMLSeq.tagName && expType === 'seq') ||
44
+ !expType) {
45
+ return resolveCollection(CN, ctx, token, onError, tagName);
46
+ }
47
+ let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
36
48
  if (!tag) {
37
49
  const kt = ctx.schema.knownTags[tagName];
38
50
  if (kt && kt.collection === expType) {
@@ -40,12 +52,17 @@ function composeCollection(CN, ctx, token, tagToken, onError) {
40
52
  tag = kt;
41
53
  }
42
54
  else {
43
- onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
44
- coll.tag = tagName;
45
- return coll;
55
+ if (kt?.collection) {
56
+ onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
57
+ }
58
+ else {
59
+ onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
60
+ }
61
+ return resolveCollection(CN, ctx, token, onError, tagName);
46
62
  }
47
63
  }
48
- const res = tag.resolve(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options);
64
+ const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
65
+ const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
49
66
  const node = isNode(res)
50
67
  ? res
51
68
  : new Scalar(res);
@@ -6,8 +6,9 @@ import { flowIndentCheck } from './util-flow-indent-check.js';
6
6
  import { mapIncludes } from './util-map-includes.js';
7
7
 
8
8
  const startColMsg = 'All mapping items must start at the same column';
9
- function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
10
- const map = new YAMLMap(ctx.schema);
9
+ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) {
10
+ const NodeClass = tag?.nodeClass ?? YAMLMap;
11
+ const map = new NodeClass(ctx.schema);
11
12
  if (ctx.atRoot)
12
13
  ctx.atRoot = false;
13
14
  let offset = bm.offset;
@@ -2,8 +2,9 @@ import { YAMLSeq } from '../nodes/YAMLSeq.js';
2
2
  import { resolveProps } from './resolve-props.js';
3
3
  import { flowIndentCheck } from './util-flow-indent-check.js';
4
4
 
5
- function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) {
6
- const seq = new YAMLSeq(ctx.schema);
5
+ function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, tag) {
6
+ const NodeClass = tag?.nodeClass ?? YAMLSeq;
7
+ const seq = new NodeClass(ctx.schema);
7
8
  if (ctx.atRoot)
8
9
  ctx.atRoot = false;
9
10
  let offset = bs.offset;
@@ -9,12 +9,11 @@ import { mapIncludes } from './util-map-includes.js';
9
9
 
10
10
  const blockMsg = 'Block collections are not allowed within flow collections';
11
11
  const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
12
- function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError) {
12
+ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError, tag) {
13
13
  const isMap = fc.start.source === '{';
14
14
  const fcName = isMap ? 'flow map' : 'flow sequence';
15
- const coll = isMap
16
- ? new YAMLMap(ctx.schema)
17
- : new YAMLSeq(ctx.schema);
15
+ const NodeClass = (tag?.nodeClass ?? (isMap ? YAMLMap : YAMLSeq));
16
+ const coll = new NodeClass(ctx.schema);
18
17
  coll.flow = true;
19
18
  const atRoot = ctx.atRoot;
20
19
  if (atRoot)
@@ -74,9 +74,13 @@ function createNode(value, tagName, ctx) {
74
74
  }
75
75
  const node = tagObj?.createNode
76
76
  ? tagObj.createNode(ctx.schema, value, ctx)
77
- : new Scalar(value);
77
+ : typeof tagObj?.nodeClass?.from === 'function'
78
+ ? tagObj.nodeClass.from(ctx.schema, value, ctx)
79
+ : new Scalar(value);
78
80
  if (tagName)
79
81
  node.tag = tagName;
82
+ else if (!tagObj.default)
83
+ node.tag = tagObj.tag;
80
84
  if (ref)
81
85
  ref.node = node;
82
86
  return node;
@@ -47,7 +47,7 @@ const prettifyError = (src, lc) => (error) => {
47
47
  let count = 1;
48
48
  const end = error.linePos[1];
49
49
  if (end && end.line === line && end.col > col) {
50
- count = Math.min(end.col - col, 80 - ci);
50
+ count = Math.max(1, Math.min(end.col - col, 80 - ci));
51
51
  }
52
52
  const pointer = ' '.repeat(ci) + '^'.repeat(count);
53
53
  error.message += `:\n\n${lineStr}\n${pointer}\n`;
@@ -2,7 +2,7 @@ import { stringifyCollection } from '../stringify/stringifyCollection.js';
2
2
  import { addPairToJSMap } from './addPairToJSMap.js';
3
3
  import { Collection } from './Collection.js';
4
4
  import { isPair, isScalar, MAP } from './identity.js';
5
- import { Pair } from './Pair.js';
5
+ import { Pair, createPair } from './Pair.js';
6
6
  import { isScalarValue } from './Scalar.js';
7
7
 
8
8
  function findPair(items, key) {
@@ -25,6 +25,34 @@ class YAMLMap extends Collection {
25
25
  super(MAP, schema);
26
26
  this.items = [];
27
27
  }
28
+ /**
29
+ * A generic collection parsing method that can be extended
30
+ * to other node classes that inherit from YAMLMap
31
+ */
32
+ static from(schema, obj, ctx) {
33
+ const { keepUndefined, replacer } = ctx;
34
+ const map = new this(schema);
35
+ const add = (key, value) => {
36
+ if (typeof replacer === 'function')
37
+ value = replacer.call(obj, key, value);
38
+ else if (Array.isArray(replacer) && !replacer.includes(key))
39
+ return;
40
+ if (value !== undefined || keepUndefined)
41
+ map.items.push(createPair(key, value, ctx));
42
+ };
43
+ if (obj instanceof Map) {
44
+ for (const [key, value] of obj)
45
+ add(key, value);
46
+ }
47
+ else if (obj && typeof obj === 'object') {
48
+ for (const key of Object.keys(obj))
49
+ add(key, obj[key]);
50
+ }
51
+ if (typeof schema.sortMapEntries === 'function') {
52
+ map.items.sort(schema.sortMapEntries);
53
+ }
54
+ return map;
55
+ }
28
56
  /**
29
57
  * Adds a value to the collection.
30
58
  *
@@ -1,3 +1,4 @@
1
+ import { createNode } from '../doc/createNode.js';
1
2
  import { stringifyCollection } from '../stringify/stringifyCollection.js';
2
3
  import { Collection } from './Collection.js';
3
4
  import { SEQ, isScalar } from './identity.js';
@@ -84,6 +85,21 @@ class YAMLSeq extends Collection {
84
85
  onComment
85
86
  });
86
87
  }
88
+ static from(schema, obj, ctx) {
89
+ const { replacer } = ctx;
90
+ const seq = new this(schema);
91
+ if (obj && Symbol.iterator in Object(obj)) {
92
+ let i = 0;
93
+ for (let it of obj) {
94
+ if (typeof replacer === 'function') {
95
+ const key = obj instanceof Set ? it : String(i++);
96
+ it = replacer.call(obj, key, it);
97
+ }
98
+ seq.items.push(createNode(it, undefined, ctx));
99
+ }
100
+ }
101
+ return seq;
102
+ }
87
103
  }
88
104
  function asItemIndex(key) {
89
105
  let idx = isScalar(key) ? key.value : key;
@@ -1,34 +1,8 @@
1
1
  import { isMap } from '../../nodes/identity.js';
2
- import { createPair } from '../../nodes/Pair.js';
3
2
  import { YAMLMap } from '../../nodes/YAMLMap.js';
4
3
 
5
- function createMap(schema, obj, ctx) {
6
- const { keepUndefined, replacer } = ctx;
7
- const map = new YAMLMap(schema);
8
- const add = (key, value) => {
9
- if (typeof replacer === 'function')
10
- value = replacer.call(obj, key, value);
11
- else if (Array.isArray(replacer) && !replacer.includes(key))
12
- return;
13
- if (value !== undefined || keepUndefined)
14
- map.items.push(createPair(key, value, ctx));
15
- };
16
- if (obj instanceof Map) {
17
- for (const [key, value] of obj)
18
- add(key, value);
19
- }
20
- else if (obj && typeof obj === 'object') {
21
- for (const key of Object.keys(obj))
22
- add(key, obj[key]);
23
- }
24
- if (typeof schema.sortMapEntries === 'function') {
25
- map.items.sort(schema.sortMapEntries);
26
- }
27
- return map;
28
- }
29
4
  const map = {
30
5
  collection: 'map',
31
- createNode: createMap,
32
6
  default: true,
33
7
  nodeClass: YAMLMap,
34
8
  tag: 'tag:yaml.org,2002:map',
@@ -36,7 +10,8 @@ const map = {
36
10
  if (!isMap(map))
37
11
  onError('Expected a mapping for this tag');
38
12
  return map;
39
- }
13
+ },
14
+ createNode: (schema, obj, ctx) => YAMLMap.from(schema, obj, ctx)
40
15
  };
41
16
 
42
17
  export { map };
@@ -1,25 +1,8 @@
1
- import { createNode } from '../../doc/createNode.js';
2
1
  import { isSeq } from '../../nodes/identity.js';
3
2
  import { YAMLSeq } from '../../nodes/YAMLSeq.js';
4
3
 
5
- function createSeq(schema, obj, ctx) {
6
- const { replacer } = ctx;
7
- const seq = new YAMLSeq(schema);
8
- if (obj && Symbol.iterator in Object(obj)) {
9
- let i = 0;
10
- for (let it of obj) {
11
- if (typeof replacer === 'function') {
12
- const key = obj instanceof Set ? it : String(i++);
13
- it = replacer.call(obj, key, it);
14
- }
15
- seq.items.push(createNode(it, undefined, ctx));
16
- }
17
- }
18
- return seq;
19
- }
20
4
  const seq = {
21
5
  collection: 'seq',
22
- createNode: createSeq,
23
6
  default: true,
24
7
  nodeClass: YAMLSeq,
25
8
  tag: 'tag:yaml.org,2002:seq',
@@ -27,7 +10,8 @@ const seq = {
27
10
  if (!isSeq(seq))
28
11
  onError('Expected a sequence for this tag');
29
12
  return seq;
30
- }
13
+ },
14
+ createNode: (schema, obj, ctx) => YAMLSeq.from(schema, obj, ctx)
31
15
  };
32
16
 
33
17
  export { seq };
@@ -39,6 +39,12 @@ class YAMLOMap extends YAMLSeq {
39
39
  }
40
40
  return map;
41
41
  }
42
+ static from(schema, iterable, ctx) {
43
+ const pairs = createPairs(schema, iterable, ctx);
44
+ const omap = new this();
45
+ omap.items = pairs.items;
46
+ return omap;
47
+ }
42
48
  }
43
49
  YAMLOMap.tag = 'tag:yaml.org,2002:omap';
44
50
  const omap = {
@@ -62,12 +68,7 @@ const omap = {
62
68
  }
63
69
  return Object.assign(new YAMLOMap(), pairs);
64
70
  },
65
- createNode(schema, iterable, ctx) {
66
- const pairs = createPairs(schema, iterable, ctx);
67
- const omap = new YAMLOMap();
68
- omap.items = pairs.items;
69
- return omap;
70
- }
71
+ createNode: (schema, iterable, ctx) => YAMLOMap.from(schema, iterable, ctx)
71
72
  };
72
73
 
73
74
  export { YAMLOMap, omap };
@@ -1,5 +1,5 @@
1
1
  import { isMap, isPair, isScalar } from '../../nodes/identity.js';
2
- import { createPair, Pair } from '../../nodes/Pair.js';
2
+ import { Pair, createPair } from '../../nodes/Pair.js';
3
3
  import { YAMLMap, findPair } from '../../nodes/YAMLMap.js';
4
4
 
5
5
  class YAMLSet extends YAMLMap {
@@ -57,6 +57,17 @@ class YAMLSet extends YAMLMap {
57
57
  else
58
58
  throw new Error('Set items must all have null values');
59
59
  }
60
+ static from(schema, iterable, ctx) {
61
+ const { replacer } = ctx;
62
+ const set = new this(schema);
63
+ if (iterable && Symbol.iterator in Object(iterable))
64
+ for (let value of iterable) {
65
+ if (typeof replacer === 'function')
66
+ value = replacer.call(iterable, value, value);
67
+ set.items.push(createPair(value, null, ctx));
68
+ }
69
+ return set;
70
+ }
60
71
  }
61
72
  YAMLSet.tag = 'tag:yaml.org,2002:set';
62
73
  const set = {
@@ -65,6 +76,7 @@ const set = {
65
76
  nodeClass: YAMLSet,
66
77
  default: false,
67
78
  tag: 'tag:yaml.org,2002:set',
79
+ createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx),
68
80
  resolve(map, onError) {
69
81
  if (isMap(map)) {
70
82
  if (map.hasAllNullValues(true))
@@ -75,17 +87,6 @@ const set = {
75
87
  else
76
88
  onError('Expected a mapping for this tag');
77
89
  return map;
78
- },
79
- createNode(schema, iterable, ctx) {
80
- const { replacer } = ctx;
81
- const set = new YAMLSet(schema);
82
- if (iterable && Symbol.iterator in Object(iterable))
83
- for (let value of iterable) {
84
- if (typeof replacer === 'function')
85
- value = replacer.call(iterable, value, value);
86
- set.items.push(createPair(value, null, ctx));
87
- }
88
- return set;
89
90
  }
90
91
  };
91
92
 
@@ -43,7 +43,7 @@ function stringifySexagesimal(node) {
43
43
  }
44
44
  return (sign +
45
45
  parts
46
- .map(n => (n < 10 ? '0' + String(n) : String(n)))
46
+ .map(n => String(n).padStart(2, '0'))
47
47
  .join(':')
48
48
  .replace(/000000\d*$/, '') // % 60 may introduce error
49
49
  );
@@ -2,39 +2,51 @@
2
2
 
3
3
  var identity = require('../nodes/identity.js');
4
4
  var Scalar = require('../nodes/Scalar.js');
5
+ var YAMLMap = require('../nodes/YAMLMap.js');
6
+ var YAMLSeq = require('../nodes/YAMLSeq.js');
5
7
  var resolveBlockMap = require('./resolve-block-map.js');
6
8
  var resolveBlockSeq = require('./resolve-block-seq.js');
7
9
  var resolveFlowCollection = require('./resolve-flow-collection.js');
8
10
 
9
- function composeCollection(CN, ctx, token, tagToken, onError) {
10
- let coll;
11
- switch (token.type) {
12
- case 'block-map': {
13
- coll = resolveBlockMap.resolveBlockMap(CN, ctx, token, onError);
14
- break;
15
- }
16
- case 'block-seq': {
17
- coll = resolveBlockSeq.resolveBlockSeq(CN, ctx, token, onError);
18
- break;
19
- }
20
- case 'flow-collection': {
21
- coll = resolveFlowCollection.resolveFlowCollection(CN, ctx, token, onError);
22
- break;
23
- }
24
- }
25
- if (!tagToken)
26
- return coll;
27
- const tagName = ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
28
- if (!tagName)
29
- return coll;
30
- // Cast needed due to: https://github.com/Microsoft/TypeScript/issues/3841
11
+ function resolveCollection(CN, ctx, token, onError, tagName, tag) {
12
+ const coll = token.type === 'block-map'
13
+ ? resolveBlockMap.resolveBlockMap(CN, ctx, token, onError, tag)
14
+ : token.type === 'block-seq'
15
+ ? resolveBlockSeq.resolveBlockSeq(CN, ctx, token, onError, tag)
16
+ : resolveFlowCollection.resolveFlowCollection(CN, ctx, token, onError, tag);
31
17
  const Coll = coll.constructor;
18
+ // If we got a tagName matching the class, or the tag name is '!',
19
+ // then use the tagName from the node class used to create it.
32
20
  if (tagName === '!' || tagName === Coll.tagName) {
33
21
  coll.tag = Coll.tagName;
34
22
  return coll;
35
23
  }
36
- const expType = identity.isMap(coll) ? 'map' : 'seq';
37
- let tag = ctx.schema.tags.find(t => t.collection === expType && t.tag === tagName);
24
+ if (tagName)
25
+ coll.tag = tagName;
26
+ return coll;
27
+ }
28
+ function composeCollection(CN, ctx, token, tagToken, onError) {
29
+ const tagName = !tagToken
30
+ ? null
31
+ : ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
32
+ const expType = token.type === 'block-map'
33
+ ? 'map'
34
+ : token.type === 'block-seq'
35
+ ? 'seq'
36
+ : token.start.source === '{'
37
+ ? 'map'
38
+ : 'seq';
39
+ // shortcut: check if it's a generic YAMLMap or YAMLSeq
40
+ // before jumping into the custom tag logic.
41
+ if (!tagToken ||
42
+ !tagName ||
43
+ tagName === '!' ||
44
+ (tagName === YAMLMap.YAMLMap.tagName && expType === 'map') ||
45
+ (tagName === YAMLSeq.YAMLSeq.tagName && expType === 'seq') ||
46
+ !expType) {
47
+ return resolveCollection(CN, ctx, token, onError, tagName);
48
+ }
49
+ let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
38
50
  if (!tag) {
39
51
  const kt = ctx.schema.knownTags[tagName];
40
52
  if (kt && kt.collection === expType) {
@@ -42,12 +54,17 @@ function composeCollection(CN, ctx, token, tagToken, onError) {
42
54
  tag = kt;
43
55
  }
44
56
  else {
45
- onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
46
- coll.tag = tagName;
47
- return coll;
57
+ if (kt?.collection) {
58
+ onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
59
+ }
60
+ else {
61
+ onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
62
+ }
63
+ return resolveCollection(CN, ctx, token, onError, tagName);
48
64
  }
49
65
  }
50
- const res = tag.resolve(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options);
66
+ const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
67
+ const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
51
68
  const node = identity.isNode(res)
52
69
  ? res
53
70
  : new Scalar.Scalar(res);
@@ -1,6 +1,7 @@
1
1
  import type { ParsedNode } from '../nodes/Node.js';
2
2
  import { YAMLMap } from '../nodes/YAMLMap.js';
3
3
  import type { BlockMap } from '../parse/cst.js';
4
+ import { CollectionTag } from '../schema/types.js';
4
5
  import type { ComposeContext, ComposeNode } from './compose-node.js';
5
6
  import type { ComposeErrorHandler } from './composer.js';
6
- export declare function resolveBlockMap({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bm: BlockMap, onError: ComposeErrorHandler): YAMLMap.Parsed<ParsedNode, ParsedNode | null>;
7
+ export declare function resolveBlockMap({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bm: BlockMap, onError: ComposeErrorHandler, tag?: CollectionTag): YAMLMap.Parsed<ParsedNode, ParsedNode | null>;
@@ -8,8 +8,9 @@ var utilFlowIndentCheck = require('./util-flow-indent-check.js');
8
8
  var utilMapIncludes = require('./util-map-includes.js');
9
9
 
10
10
  const startColMsg = 'All mapping items must start at the same column';
11
- function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError) {
12
- const map = new YAMLMap.YAMLMap(ctx.schema);
11
+ function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) {
12
+ const NodeClass = tag?.nodeClass ?? YAMLMap.YAMLMap;
13
+ const map = new NodeClass(ctx.schema);
13
14
  if (ctx.atRoot)
14
15
  ctx.atRoot = false;
15
16
  let offset = bm.offset;
@@ -1,5 +1,6 @@
1
1
  import { YAMLSeq } from '../nodes/YAMLSeq.js';
2
2
  import type { BlockSequence } from '../parse/cst.js';
3
+ import { CollectionTag } from '../schema/types.js';
3
4
  import type { ComposeContext, ComposeNode } from './compose-node.js';
4
5
  import type { ComposeErrorHandler } from './composer.js';
5
- export declare function resolveBlockSeq({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bs: BlockSequence, onError: ComposeErrorHandler): YAMLSeq.Parsed<import("../index.js").ParsedNode>;
6
+ export declare function resolveBlockSeq({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, bs: BlockSequence, onError: ComposeErrorHandler, tag?: CollectionTag): YAMLSeq.Parsed<import("../index.js").ParsedNode>;
@@ -4,8 +4,9 @@ var YAMLSeq = require('../nodes/YAMLSeq.js');
4
4
  var resolveProps = require('./resolve-props.js');
5
5
  var utilFlowIndentCheck = require('./util-flow-indent-check.js');
6
6
 
7
- function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError) {
8
- const seq = new YAMLSeq.YAMLSeq(ctx.schema);
7
+ function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, tag) {
8
+ const NodeClass = tag?.nodeClass ?? YAMLSeq.YAMLSeq;
9
+ const seq = new NodeClass(ctx.schema);
9
10
  if (ctx.atRoot)
10
11
  ctx.atRoot = false;
11
12
  let offset = bs.offset;
@@ -1,6 +1,7 @@
1
1
  import { YAMLMap } from '../nodes/YAMLMap.js';
2
2
  import { YAMLSeq } from '../nodes/YAMLSeq.js';
3
3
  import type { FlowCollection } from '../parse/cst.js';
4
+ import { CollectionTag } from '../schema/types.js';
4
5
  import type { ComposeContext, ComposeNode } from './compose-node.js';
5
6
  import type { ComposeErrorHandler } from './composer.js';
6
- export declare function resolveFlowCollection({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, fc: FlowCollection, onError: ComposeErrorHandler): YAMLMap.Parsed<import("../index.js").ParsedNode, import("../index.js").ParsedNode | null> | YAMLSeq.Parsed<import("../index.js").ParsedNode>;
7
+ export declare function resolveFlowCollection({ composeNode, composeEmptyNode }: ComposeNode, ctx: ComposeContext, fc: FlowCollection, onError: ComposeErrorHandler, tag?: CollectionTag): YAMLMap.Parsed<import("../index.js").ParsedNode, import("../index.js").ParsedNode | null> | YAMLSeq.Parsed<import("../index.js").ParsedNode>;
@@ -11,12 +11,11 @@ var utilMapIncludes = require('./util-map-includes.js');
11
11
 
12
12
  const blockMsg = 'Block collections are not allowed within flow collections';
13
13
  const isBlock = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
14
- function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError) {
14
+ function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError, tag) {
15
15
  const isMap = fc.start.source === '{';
16
16
  const fcName = isMap ? 'flow map' : 'flow sequence';
17
- const coll = isMap
18
- ? new YAMLMap.YAMLMap(ctx.schema)
19
- : new YAMLSeq.YAMLSeq(ctx.schema);
17
+ const NodeClass = (tag?.nodeClass ?? (isMap ? YAMLMap.YAMLMap : YAMLSeq.YAMLSeq));
18
+ const coll = new NodeClass(ctx.schema);
20
19
  coll.flow = true;
21
20
  const atRoot = ctx.atRoot;
22
21
  if (atRoot)
@@ -76,9 +76,13 @@ function createNode(value, tagName, ctx) {
76
76
  }
77
77
  const node = tagObj?.createNode
78
78
  ? tagObj.createNode(ctx.schema, value, ctx)
79
- : new Scalar.Scalar(value);
79
+ : typeof tagObj?.nodeClass?.from === 'function'
80
+ ? tagObj.nodeClass.from(ctx.schema, value, ctx)
81
+ : new Scalar.Scalar(value);
80
82
  if (tagName)
81
83
  node.tag = tagName;
84
+ else if (!tagObj.default)
85
+ node.tag = tagObj.tag;
82
86
  if (ref)
83
87
  ref.node = node;
84
88
  return node;
package/dist/errors.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { LineCounter } from './parse/line-counter';
2
- export type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN';
2
+ export type ErrorCode = 'ALIAS_PROPS' | 'BAD_ALIAS' | 'BAD_DIRECTIVE' | 'BAD_DQ_ESCAPE' | 'BAD_INDENT' | 'BAD_PROP_ORDER' | 'BAD_SCALAR_START' | 'BLOCK_AS_IMPLICIT_KEY' | 'BLOCK_IN_FLOW' | 'DUPLICATE_KEY' | 'IMPOSSIBLE' | 'KEY_OVER_1024_CHARS' | 'MISSING_CHAR' | 'MULTILINE_IMPLICIT_KEY' | 'MULTIPLE_ANCHORS' | 'MULTIPLE_DOCS' | 'MULTIPLE_TAGS' | 'TAB_AS_INDENT' | 'TAG_RESOLVE_FAILED' | 'UNEXPECTED_TOKEN' | 'BAD_COLLECTION_TYPE';
3
3
  export type LinePos = {
4
4
  line: number;
5
5
  col: number;
package/dist/errors.js CHANGED
@@ -49,7 +49,7 @@ const prettifyError = (src, lc) => (error) => {
49
49
  let count = 1;
50
50
  const end = error.linePos[1];
51
51
  if (end && end.line === line && end.col > col) {
52
- count = Math.min(end.col - col, 80 - ci);
52
+ count = Math.max(1, Math.min(end.col - col, 80 - ci));
53
53
  }
54
54
  const pointer = ' '.repeat(ci) + '^'.repeat(count);
55
55
  error.message += `:\n\n${lineStr}\n${pointer}\n`;
@@ -5,7 +5,7 @@ import type { StringifyContext } from '../stringify/stringify.js';
5
5
  import { addPairToJSMap } from './addPairToJSMap.js';
6
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, 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("./YAMLMap.js").YAMLMap<unknown, unknown> | import("./Scalar.js").Scalar<unknown> | import("./Alias.js").Alias | 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. */
@@ -1,6 +1,7 @@
1
1
  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
+ import { CreateNodeContext } from '../util.js';
4
5
  import { Collection } from './Collection.js';
5
6
  import type { ParsedNode, Range } from './Node.js';
6
7
  import { Pair } from './Pair.js';
@@ -19,6 +20,11 @@ export declare class YAMLMap<K = unknown, V = unknown> extends Collection {
19
20
  static get tagName(): 'tag:yaml.org,2002:map';
20
21
  items: Pair<K, V>[];
21
22
  constructor(schema?: Schema);
23
+ /**
24
+ * A generic collection parsing method that can be extended
25
+ * to other node classes that inherit from YAMLMap
26
+ */
27
+ static from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLMap<unknown, unknown>;
22
28
  /**
23
29
  * Adds a value to the collection.
24
30
  *
@@ -27,6 +27,34 @@ class YAMLMap extends Collection.Collection {
27
27
  super(identity.MAP, schema);
28
28
  this.items = [];
29
29
  }
30
+ /**
31
+ * A generic collection parsing method that can be extended
32
+ * to other node classes that inherit from YAMLMap
33
+ */
34
+ static from(schema, obj, ctx) {
35
+ const { keepUndefined, replacer } = ctx;
36
+ const map = new this(schema);
37
+ const add = (key, value) => {
38
+ if (typeof replacer === 'function')
39
+ value = replacer.call(obj, key, value);
40
+ else if (Array.isArray(replacer) && !replacer.includes(key))
41
+ return;
42
+ if (value !== undefined || keepUndefined)
43
+ map.items.push(Pair.createPair(key, value, ctx));
44
+ };
45
+ if (obj instanceof Map) {
46
+ for (const [key, value] of obj)
47
+ add(key, value);
48
+ }
49
+ else if (obj && typeof obj === 'object') {
50
+ for (const key of Object.keys(obj))
51
+ add(key, obj[key]);
52
+ }
53
+ if (typeof schema.sortMapEntries === 'function') {
54
+ map.items.sort(schema.sortMapEntries);
55
+ }
56
+ return map;
57
+ }
30
58
  /**
31
59
  * Adds a value to the collection.
32
60
  *
@@ -1,3 +1,4 @@
1
+ import { CreateNodeContext } from '../doc/createNode.js';
1
2
  import type { BlockSequence, FlowCollection } from '../parse/cst.js';
2
3
  import type { Schema } from '../schema/Schema.js';
3
4
  import type { StringifyContext } from '../stringify/stringify.js';
@@ -55,4 +56,5 @@ export declare class YAMLSeq<T = unknown> extends Collection {
55
56
  set(key: unknown, value: T): void;
56
57
  toJSON(_?: unknown, ctx?: ToJSContext): unknown[];
57
58
  toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
59
+ static from(schema: Schema, obj: unknown, ctx: CreateNodeContext): YAMLSeq<unknown>;
58
60
  }
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var createNode = require('../doc/createNode.js');
3
4
  var stringifyCollection = require('../stringify/stringifyCollection.js');
4
5
  var Collection = require('./Collection.js');
5
6
  var identity = require('./identity.js');
@@ -86,6 +87,21 @@ class YAMLSeq extends Collection.Collection {
86
87
  onComment
87
88
  });
88
89
  }
90
+ static from(schema, obj, ctx) {
91
+ const { replacer } = ctx;
92
+ const seq = new this(schema);
93
+ if (obj && Symbol.iterator in Object(obj)) {
94
+ let i = 0;
95
+ for (let it of obj) {
96
+ if (typeof replacer === 'function') {
97
+ const key = obj instanceof Set ? it : String(i++);
98
+ it = replacer.call(obj, key, it);
99
+ }
100
+ seq.items.push(createNode.createNode(it, undefined, ctx));
101
+ }
102
+ }
103
+ return seq;
104
+ }
89
105
  }
90
106
  function asItemIndex(key) {
91
107
  let idx = identity.isScalar(key) ? key.value : key;
@@ -1,36 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  var identity = require('../../nodes/identity.js');
4
- var Pair = require('../../nodes/Pair.js');
5
4
  var YAMLMap = require('../../nodes/YAMLMap.js');
6
5
 
7
- function createMap(schema, obj, ctx) {
8
- const { keepUndefined, replacer } = ctx;
9
- const map = new YAMLMap.YAMLMap(schema);
10
- const add = (key, value) => {
11
- if (typeof replacer === 'function')
12
- value = replacer.call(obj, key, value);
13
- else if (Array.isArray(replacer) && !replacer.includes(key))
14
- return;
15
- if (value !== undefined || keepUndefined)
16
- map.items.push(Pair.createPair(key, value, ctx));
17
- };
18
- if (obj instanceof Map) {
19
- for (const [key, value] of obj)
20
- add(key, value);
21
- }
22
- else if (obj && typeof obj === 'object') {
23
- for (const key of Object.keys(obj))
24
- add(key, obj[key]);
25
- }
26
- if (typeof schema.sortMapEntries === 'function') {
27
- map.items.sort(schema.sortMapEntries);
28
- }
29
- return map;
30
- }
31
6
  const map = {
32
7
  collection: 'map',
33
- createNode: createMap,
34
8
  default: true,
35
9
  nodeClass: YAMLMap.YAMLMap,
36
10
  tag: 'tag:yaml.org,2002:map',
@@ -38,7 +12,8 @@ const map = {
38
12
  if (!identity.isMap(map))
39
13
  onError('Expected a mapping for this tag');
40
14
  return map;
41
- }
15
+ },
16
+ createNode: (schema, obj, ctx) => YAMLMap.YAMLMap.from(schema, obj, ctx)
42
17
  };
43
18
 
44
19
  exports.map = map;
@@ -1,27 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var createNode = require('../../doc/createNode.js');
4
3
  var identity = require('../../nodes/identity.js');
5
4
  var YAMLSeq = require('../../nodes/YAMLSeq.js');
6
5
 
7
- function createSeq(schema, obj, ctx) {
8
- const { replacer } = ctx;
9
- const seq = new YAMLSeq.YAMLSeq(schema);
10
- if (obj && Symbol.iterator in Object(obj)) {
11
- let i = 0;
12
- for (let it of obj) {
13
- if (typeof replacer === 'function') {
14
- const key = obj instanceof Set ? it : String(i++);
15
- it = replacer.call(obj, key, it);
16
- }
17
- seq.items.push(createNode.createNode(it, undefined, ctx));
18
- }
19
- }
20
- return seq;
21
- }
22
6
  const seq = {
23
7
  collection: 'seq',
24
- createNode: createSeq,
25
8
  default: true,
26
9
  nodeClass: YAMLSeq.YAMLSeq,
27
10
  tag: 'tag:yaml.org,2002:seq',
@@ -29,7 +12,8 @@ const seq = {
29
12
  if (!identity.isSeq(seq))
30
13
  onError('Expected a sequence for this tag');
31
14
  return seq;
32
- }
15
+ },
16
+ createNode: (schema, obj, ctx) => YAMLSeq.YAMLSeq.from(schema, obj, ctx)
33
17
  };
34
18
 
35
19
  exports.seq = seq;
@@ -1,11 +1,11 @@
1
1
  import type { CreateNodeContext } from '../doc/createNode.js';
2
- import type { Schema } from './Schema.js';
3
2
  import type { Node } from '../nodes/Node.js';
4
3
  import type { Scalar } from '../nodes/Scalar.js';
5
4
  import type { YAMLMap } from '../nodes/YAMLMap.js';
6
5
  import type { YAMLSeq } from '../nodes/YAMLSeq.js';
7
6
  import type { ParseOptions } from '../options.js';
8
7
  import type { StringifyContext } from '../stringify/stringify.js';
8
+ import type { Schema } from './Schema.js';
9
9
  interface TagBase {
10
10
  /**
11
11
  * An optional factory function, used e.g. by collections when wrapping JS objects as AST nodes.
@@ -71,12 +71,20 @@ export interface CollectionTag extends TagBase {
71
71
  /**
72
72
  * The `Node` child class that implements this tag.
73
73
  * If set, used to select this tag when stringifying.
74
+ *
75
+ * If the class provides a static `from` method, then that
76
+ * will be used if the tag object doesn't have a `createNode` method.
74
77
  */
75
- nodeClass?: new () => Node;
78
+ nodeClass?: {
79
+ new (schema?: Schema): Node;
80
+ from?: (schema: Schema, obj: unknown, ctx: CreateNodeContext) => Node;
81
+ };
76
82
  /**
77
83
  * Turns a value into an AST node.
78
84
  * If returning a non-`Node` value, the output will be wrapped as a `Scalar`.
85
+ *
86
+ * Note: this is required if nodeClass is not provided.
79
87
  */
80
- resolve(value: YAMLMap.Parsed | YAMLSeq.Parsed, onError: (message: string) => void, options: ParseOptions): unknown;
88
+ resolve?: (value: YAMLMap.Parsed | YAMLSeq.Parsed, onError: (message: string) => void, options: ParseOptions) => unknown;
81
89
  }
82
90
  export {};
@@ -1,5 +1,7 @@
1
1
  import { ToJSContext } from '../../nodes/toJS.js';
2
2
  import { YAMLSeq } from '../../nodes/YAMLSeq.js';
3
+ import { CreateNodeContext } from '../../util.js';
4
+ import type { Schema } from '../Schema.js';
3
5
  import { CollectionTag } from '../types.js';
4
6
  export declare class YAMLOMap extends YAMLSeq {
5
7
  static tag: string;
@@ -21,5 +23,6 @@ export declare class YAMLOMap extends YAMLSeq {
21
23
  * but TypeScript won't allow widening the signature of a child method.
22
24
  */
23
25
  toJSON(_?: unknown, ctx?: ToJSContext): unknown[];
26
+ static from(schema: Schema, iterable: unknown, ctx: CreateNodeContext): YAMLOMap;
24
27
  }
25
28
  export declare const omap: CollectionTag;
@@ -41,6 +41,12 @@ class YAMLOMap extends YAMLSeq.YAMLSeq {
41
41
  }
42
42
  return map;
43
43
  }
44
+ static from(schema, iterable, ctx) {
45
+ const pairs$1 = pairs.createPairs(schema, iterable, ctx);
46
+ const omap = new this();
47
+ omap.items = pairs$1.items;
48
+ return omap;
49
+ }
44
50
  }
45
51
  YAMLOMap.tag = 'tag:yaml.org,2002:omap';
46
52
  const omap = {
@@ -64,12 +70,7 @@ const omap = {
64
70
  }
65
71
  return Object.assign(new YAMLOMap(), pairs$1);
66
72
  },
67
- createNode(schema, iterable, ctx) {
68
- const pairs$1 = pairs.createPairs(schema, iterable, ctx);
69
- const omap = new YAMLOMap();
70
- omap.items = pairs$1.items;
71
- return omap;
72
- }
73
+ createNode: (schema, iterable, ctx) => YAMLOMap.from(schema, iterable, ctx)
73
74
  };
74
75
 
75
76
  exports.YAMLOMap = YAMLOMap;
@@ -1,9 +1,10 @@
1
- import type { Schema } from '../../schema/Schema.js';
2
1
  import { Pair } from '../../nodes/Pair.js';
3
2
  import { Scalar } from '../../nodes/Scalar.js';
4
3
  import { ToJSContext } from '../../nodes/toJS.js';
5
4
  import { YAMLMap } from '../../nodes/YAMLMap.js';
5
+ import type { Schema } from '../../schema/Schema.js';
6
6
  import type { StringifyContext } from '../../stringify/stringify.js';
7
+ import { CreateNodeContext } from '../../util.js';
7
8
  import type { CollectionTag } from '../types.js';
8
9
  export declare class YAMLSet<T = unknown> extends YAMLMap<T, Scalar<null> | null> {
9
10
  static tag: string;
@@ -22,5 +23,6 @@ export declare class YAMLSet<T = unknown> extends YAMLMap<T, Scalar<null> | null
22
23
  set(key: T, value: null): void;
23
24
  toJSON(_?: unknown, ctx?: ToJSContext): any;
24
25
  toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
26
+ static from(schema: Schema, iterable: unknown, ctx: CreateNodeContext): YAMLSet<unknown>;
25
27
  }
26
28
  export declare const set: CollectionTag;
@@ -59,6 +59,17 @@ class YAMLSet extends YAMLMap.YAMLMap {
59
59
  else
60
60
  throw new Error('Set items must all have null values');
61
61
  }
62
+ static from(schema, iterable, ctx) {
63
+ const { replacer } = ctx;
64
+ const set = new this(schema);
65
+ if (iterable && Symbol.iterator in Object(iterable))
66
+ for (let value of iterable) {
67
+ if (typeof replacer === 'function')
68
+ value = replacer.call(iterable, value, value);
69
+ set.items.push(Pair.createPair(value, null, ctx));
70
+ }
71
+ return set;
72
+ }
62
73
  }
63
74
  YAMLSet.tag = 'tag:yaml.org,2002:set';
64
75
  const set = {
@@ -67,6 +78,7 @@ const set = {
67
78
  nodeClass: YAMLSet,
68
79
  default: false,
69
80
  tag: 'tag:yaml.org,2002:set',
81
+ createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx),
70
82
  resolve(map, onError) {
71
83
  if (identity.isMap(map)) {
72
84
  if (map.hasAllNullValues(true))
@@ -77,17 +89,6 @@ const set = {
77
89
  else
78
90
  onError('Expected a mapping for this tag');
79
91
  return map;
80
- },
81
- createNode(schema, iterable, ctx) {
82
- const { replacer } = ctx;
83
- const set = new YAMLSet(schema);
84
- if (iterable && Symbol.iterator in Object(iterable))
85
- for (let value of iterable) {
86
- if (typeof replacer === 'function')
87
- value = replacer.call(iterable, value, value);
88
- set.items.push(Pair.createPair(value, null, ctx));
89
- }
90
- return set;
91
92
  }
92
93
  };
93
94
 
@@ -45,7 +45,7 @@ function stringifySexagesimal(node) {
45
45
  }
46
46
  return (sign +
47
47
  parts
48
- .map(n => (n < 10 ? '0' + String(n) : String(n)))
48
+ .map(n => String(n).padStart(2, '0'))
49
49
  .join(':')
50
50
  .replace(/000000\d*$/, '') // % 60 may introduce error
51
51
  );
package/dist/util.d.ts CHANGED
@@ -7,5 +7,6 @@ export { map as mapTag } from './schema/common/map.js';
7
7
  export { seq as seqTag } from './schema/common/seq.js';
8
8
  export { string as stringTag } from './schema/common/string.js';
9
9
  export { foldFlowLines, FoldOptions } from './stringify/foldFlowLines';
10
+ export { StringifyContext } from './stringify/stringify.js';
10
11
  export { stringifyNumber } from './stringify/stringifyNumber.js';
11
12
  export { stringifyString } from './stringify/stringifyString.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yaml",
3
- "version": "2.3.0-3",
3
+ "version": "2.3.0-5",
4
4
  "license": "ISC",
5
5
  "author": "Eemeli Aro <eemeli@gmail.com>",
6
6
  "repository": "github:eemeli/yaml",