UNPKG

921 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.buildTypeTree = void 0;
4function buildTypeTree(schemas) {
5 const tree = buildTree();
6 for (const type of schemas) {
7 setTypeToTree(tree, type.id.toNames(), type);
8 }
9 if (tree.children.size === 0) {
10 throw new Error('There is no schema in the input contents.');
11 }
12 return tree;
13}
14exports.buildTypeTree = buildTypeTree;
15function setTypeToTree(tree, paths, type) {
16 if (paths.length === 0) {
17 throw new Error('The paths is nothing for set the type to TypeTree.');
18 }
19 let t = tree;
20 for (const key of paths) {
21 let next = t.children.get(key);
22 if (next === undefined) {
23 next = buildTree();
24 t.children.set(key, next);
25 }
26 t = next;
27 }
28 t.schema = type;
29}
30function buildTree() {
31 return {
32 children: new Map(),
33 };
34}