UNPKG

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