1 | 'use strict';
|
---|
2 |
|
---|
3 | var YAMLException = require('./exception');
|
---|
4 |
|
---|
5 | var 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 |
|
---|
18 | var YAML_NODE_KINDS = [
|
---|
19 | 'scalar',
|
---|
20 | 'sequence',
|
---|
21 | 'mapping'
|
---|
22 | ];
|
---|
23 |
|
---|
24 | function 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 |
|
---|
38 | function 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.options = options; // keep original options in case user wants to extend this type later
|
---|
49 | this.tag = tag;
|
---|
50 | this.kind = options['kind'] || null;
|
---|
51 | this.resolve = options['resolve'] || function () { return true; };
|
---|
52 | this.construct = options['construct'] || function (data) { return data; };
|
---|
53 | this.instanceOf = options['instanceOf'] || null;
|
---|
54 | this.predicate = options['predicate'] || null;
|
---|
55 | this.represent = options['represent'] || null;
|
---|
56 | this.representName = options['representName'] || null;
|
---|
57 | this.defaultStyle = options['defaultStyle'] || null;
|
---|
58 | this.multi = options['multi'] || false;
|
---|
59 | this.styleAliases = compileStyleAliases(options['styleAliases'] || null);
|
---|
60 |
|
---|
61 | if (YAML_NODE_KINDS.indexOf(this.kind) === -1) {
|
---|
62 | throw new YAMLException('Unknown kind "' + this.kind + '" is specified for "' + tag + '" YAML type.');
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | module.exports = Type;
|
---|