Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

type.js 1.5KB

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