UNPKG

1.75 kBJavaScriptView Raw
1'use strict';
2
3var YAMLException = require('./exception');
4
5var TYPE_CONSTRUCTOR_OPTIONS = [
6 'kind',
7 'multi',
8 'resolve',
9 'construct',
10 'instanceOf',
11 'predicate',
12 'represent',
13 'representName',
14 'defaultStyle',
15 'styleAliases'
16];
17
18var YAML_NODE_KINDS = [
19 'scalar',
20 'sequence',
21 'mapping'
22];
23
24function compileStyleAliases(map) {
25 var result = {};
26
27 if (map !== null) {
28 Object.keys(map).forEach(function (style) {
29 map[style].forEach(function (alias) {
30 result[String(alias)] = style;
31 });
32 });
33 }
34
35 return result;
36}
37
38function Type(tag, options) {
39 options = options || {};
40
41 Object.keys(options).forEach(function (name) {
42 if (TYPE_CONSTRUCTOR_OPTIONS.indexOf(name) === -1) {
43 throw new YAMLException('Unknown option "' + name + '" is met in definition of "' + tag + '" YAML type.');
44 }
45 });
46
47 // TODO: Add tag format check.
48 this.tag = tag;
49 this.kind = options['kind'] || null;
50 this.resolve = options['resolve'] || function () { return true; };
51 this.construct = options['construct'] || function (data) { return data; };
52 this.instanceOf = options['instanceOf'] || null;
53 this.predicate = options['predicate'] || null;
54 this.represent = options['represent'] || null;
55 this.representName = options['representName'] || null;
56 this.defaultStyle = options['defaultStyle'] || null;
57 this.multi = options['multi'] || false;
58 this.styleAliases = compileStyleAliases(options['styleAliases'] || null);
59
60 if (YAML_NODE_KINDS.indexOf(this.kind) === -1) {
61 throw new YAMLException('Unknown kind "' + this.kind + '" is specified for "' + tag + '" YAML type.');
62 }
63}
64
65module.exports = Type;