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