Schema.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { MAP, SCALAR, SEQ } from '../nodes/identity.js';
  2. import { map } from './common/map.js';
  3. import { seq } from './common/seq.js';
  4. import { string } from './common/string.js';
  5. import { getTags, coreKnownTags } from './tags.js';
  6. const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
  7. class Schema {
  8. constructor({ compat, customTags, merge, resolveKnownTags, schema, sortMapEntries, toStringDefaults }) {
  9. this.compat = Array.isArray(compat)
  10. ? getTags(compat, 'compat')
  11. : compat
  12. ? getTags(null, compat)
  13. : null;
  14. this.merge = !!merge;
  15. this.name = (typeof schema === 'string' && schema) || 'core';
  16. this.knownTags = resolveKnownTags ? coreKnownTags : {};
  17. this.tags = getTags(customTags, this.name);
  18. this.toStringOptions = toStringDefaults ?? null;
  19. Object.defineProperty(this, MAP, { value: map });
  20. Object.defineProperty(this, SCALAR, { value: string });
  21. Object.defineProperty(this, SEQ, { value: 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. export { Schema };