Schema.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var identity = require('../nodes/identity.js');
  3. var map = require('./common/map.js');
  4. var seq = require('./common/seq.js');
  5. var string = require('./common/string.js');
  6. var tags = require('./tags.js');
  7. const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
  8. class Schema {
  9. constructor({ compat, customTags, merge, resolveKnownTags, schema, sortMapEntries, toStringDefaults }) {
  10. this.compat = Array.isArray(compat)
  11. ? tags.getTags(compat, 'compat')
  12. : compat
  13. ? tags.getTags(null, compat)
  14. : null;
  15. this.merge = !!merge;
  16. this.name = (typeof schema === 'string' && schema) || 'core';
  17. this.knownTags = resolveKnownTags ? tags.coreKnownTags : {};
  18. this.tags = tags.getTags(customTags, this.name);
  19. this.toStringOptions = toStringDefaults ?? null;
  20. Object.defineProperty(this, identity.MAP, { value: map.map });
  21. Object.defineProperty(this, identity.SCALAR, { value: string.string });
  22. Object.defineProperty(this, identity.SEQ, { value: seq.seq });
  23. // Used by createMap()
  24. this.sortMapEntries =
  25. typeof sortMapEntries === 'function'
  26. ? sortMapEntries
  27. : sortMapEntries === true
  28. ? sortMapEntriesByKey
  29. : null;
  30. }
  31. clone() {
  32. const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
  33. copy.tags = this.tags.slice();
  34. return copy;
  35. }
  36. }
  37. exports.Schema = Schema;