Schema.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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.name = (typeof schema === 'string' && schema) || 'core';
  16. this.knownTags = resolveKnownTags ? tags.coreKnownTags : {};
  17. this.tags = tags.getTags(customTags, this.name, merge);
  18. this.toStringOptions = toStringDefaults ?? null;
  19. Object.defineProperty(this, identity.MAP, { value: map.map });
  20. Object.defineProperty(this, identity.SCALAR, { value: string.string });
  21. Object.defineProperty(this, identity.SEQ, { value: seq.seq });
  22. // Used by createMap()
  23. this.sortMapEntries =
  24. typeof sortMapEntries === 'function'
  25. ? sortMapEntries
  26. : sortMapEntries === true
  27. ? sortMapEntriesByKey
  28. : null;
  29. }
  30. clone() {
  31. const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
  32. copy.tags = this.tags.slice();
  33. return copy;
  34. }
  35. }
  36. exports.Schema = Schema;