UNPKG

3.44 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3function topicOf(id) {
4 if (!id)
5 return '';
6 return id
7 .split(':')
8 .slice(0, -1)
9 .join(':');
10}
11function keyOf(id) {
12 if (!id)
13 return '';
14 return id
15 .split(':')
16 .slice(-1)
17 .join(':');
18}
19class TopicBase {
20 constructor() {
21 this.subtopics = {};
22 this.commands = {};
23 }
24 findTopic(id) {
25 let key = keyOf(id);
26 let parentID = topicOf(id);
27 if (parentID) {
28 let parent = this.findTopic(parentID);
29 return parent && parent.subtopics[key];
30 }
31 return this.subtopics[key];
32 }
33 findCommand(id) {
34 const topic = this.findTopic(topicOf(id));
35 if (topic)
36 return topic.findCommand(id);
37 return this.commands[keyOf(id)];
38 }
39}
40exports.TopicBase = TopicBase;
41class Topic extends TopicBase {
42 constructor(opts) {
43 super();
44 this.name = opts.name;
45 this.description = opts.description;
46 this.hidden = !!opts.hidden;
47 this.commands = opts.commands || {};
48 }
49}
50exports.Topic = Topic;
51function topicsToArray(input, base) {
52 if (!input)
53 return [];
54 if (Array.isArray(input))
55 return input;
56 base = base ? `${base}:` : '';
57 return Object.keys(input).map(k => new Topic(Object.assign({}, input[k], { name: `${base}${k}` })));
58}
59exports.topicsToArray = topicsToArray;
60function commandsToArray(input, base) {
61 if (!input)
62 return [];
63 if (Array.isArray(input))
64 return input;
65 base = base ? `${base}:` : '';
66 return Object.keys(input).map(k => input[k]);
67}
68exports.commandsToArray = commandsToArray;
69class RootTopic extends TopicBase {
70 constructor() {
71 super(...arguments);
72 this.subtopics = {};
73 this.commands = {};
74 this.allCommands = [];
75 this.allTopics = [];
76 }
77 findCommand(id) {
78 for (let c of this.allCommands) {
79 if (c.aliases.find(a => a === id))
80 return c;
81 }
82 return super.findCommand(id);
83 }
84 addTopics(topics) {
85 for (let t of topicsToArray(topics)) {
86 if (!t.name)
87 continue;
88 let topic = this.findOrCreateTopic(t.name);
89 this.mergeTopics(topic, t);
90 this.addTopics(topicsToArray(t.subtopics, t.name));
91 this.addCommands(commandsToArray(t.commands, t.name));
92 }
93 }
94 addCommands(commands) {
95 if (!commands)
96 return;
97 for (const c of commands) {
98 this.allCommands.push(c);
99 let topicID = topicOf(c.id);
100 let topic = topicID ? this.findOrCreateTopic(topicID) : this;
101 topic.commands[keyOf(c.id)] = c;
102 }
103 }
104 findOrCreateTopic(name) {
105 let key = keyOf(name);
106 let parentID = topicOf(name);
107 let topic = this;
108 if (parentID) {
109 let parent = this.findOrCreateTopic(parentID);
110 topic = parent;
111 }
112 let topics = topic.subtopics;
113 if (!topics[key]) {
114 topics[key] = new Topic({ name });
115 this.allTopics.push(topics[key]);
116 }
117 return topics[key];
118 }
119 mergeTopics(a, b) {
120 a.description = b.description || a.description;
121 a.hidden = b.hidden || a.hidden;
122 }
123}
124exports.RootTopic = RootTopic;