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