-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandValue.js
62 lines (60 loc) · 1.42 KB
/
CommandValue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const VAL_REGISTRY = {}
class CommandValue
{
constructor(name, values)
{
this.name = name;
if(name in VAL_REGISTRY)
{
console.warn("Overwriting previously defined CommandValue with name \"%s\".", name);
}
VAL_REGISTRY[name] = this;
this.values = values;
}
static get REGISTRY()
{
return VAL_REGISTRY;
}
static getRegex(valueType)
{
let value = VAL_REGISTRY[valueType];
return value ? value.regex : null;
}
get regex()
{
return new RegExp(this.getCompletionValues("").join("|"), "i");
}
getCompletionValues(input)
{
var vals = [];
for(let key in this.values)
{
let val = this.values[key];
if(Array.isArray(val))
{
for(let v of val)
{
if(!input || v.startsWith(input))
{
vals.push(v);
}
}
}
else if(val instanceof RegExp && (!input || val.test(input)))
{
vals.push(key);
}
else
{
vals.push(val);
}
}
return vals;
}
getValue(input)
{
let vals = this.getCompletionValues(input);
return vals.length == 1 ? vals[0] : null;
}
}
module.exports = CommandValue;