UNPKG

2.7 kBJavaScriptView Raw
1'use strict';
2
3var map = require('./common/map.js');
4var _null = require('./common/null.js');
5var seq = require('./common/seq.js');
6var string = require('./common/string.js');
7var bool = require('./core/bool.js');
8var float = require('./core/float.js');
9var int = require('./core/int.js');
10var schema = require('./core/schema.js');
11var schema$1 = require('./json/schema.js');
12var binary = require('./yaml-1.1/binary.js');
13var omap = require('./yaml-1.1/omap.js');
14var pairs = require('./yaml-1.1/pairs.js');
15var schema$2 = require('./yaml-1.1/schema.js');
16var set = require('./yaml-1.1/set.js');
17var timestamp = require('./yaml-1.1/timestamp.js');
18
19const schemas = new Map([
20 ['core', schema.schema],
21 ['failsafe', [map.map, seq.seq, string.string]],
22 ['json', schema$1.schema],
23 ['yaml11', schema$2.schema],
24 ['yaml-1.1', schema$2.schema]
25]);
26const tagsByName = {
27 binary: binary.binary,
28 bool: bool.boolTag,
29 float: float.float,
30 floatExp: float.floatExp,
31 floatNaN: float.floatNaN,
32 floatTime: timestamp.floatTime,
33 int: int.int,
34 intHex: int.intHex,
35 intOct: int.intOct,
36 intTime: timestamp.intTime,
37 map: map.map,
38 null: _null.nullTag,
39 omap: omap.omap,
40 pairs: pairs.pairs,
41 seq: seq.seq,
42 set: set.set,
43 timestamp: timestamp.timestamp
44};
45const coreKnownTags = {
46 'tag:yaml.org,2002:binary': binary.binary,
47 'tag:yaml.org,2002:omap': omap.omap,
48 'tag:yaml.org,2002:pairs': pairs.pairs,
49 'tag:yaml.org,2002:set': set.set,
50 'tag:yaml.org,2002:timestamp': timestamp.timestamp
51};
52function getTags(customTags, schemaName) {
53 let tags = schemas.get(schemaName);
54 if (!tags) {
55 if (Array.isArray(customTags))
56 tags = [];
57 else {
58 const keys = Array.from(schemas.keys())
59 .filter(key => key !== 'yaml11')
60 .map(key => JSON.stringify(key))
61 .join(', ');
62 throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`);
63 }
64 }
65 if (Array.isArray(customTags)) {
66 for (const tag of customTags)
67 tags = tags.concat(tag);
68 }
69 else if (typeof customTags === 'function') {
70 tags = customTags(tags.slice());
71 }
72 return tags.map(tag => {
73 if (typeof tag !== 'string')
74 return tag;
75 const tagObj = tagsByName[tag];
76 if (tagObj)
77 return tagObj;
78 const keys = Object.keys(tagsByName)
79 .map(key => JSON.stringify(key))
80 .join(', ');
81 throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
82 });
83}
84
85exports.coreKnownTags = coreKnownTags;
86exports.getTags = getTags;