/
helper.ts
46 lines (37 loc) · 903 Bytes
/
helper.ts
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
interface StoreFunction {
(...args: any[]): string;
}
interface Store {
[key: string]: StoreFunction
}
class Helper {
public store: Store;
constructor() {
this.store = {};
}
/**
* @returns {Store} - The plugin store
*/
list(): Store {
return this.store;
}
/**
* Get helper plugin function by name
* @param {String} name - The name of the helper plugin
* @returns {StoreFunction}
*/
get(name: string): StoreFunction {
return this.store[name];
}
/**
* Register a helper plugin
* @param {String} name - The name of the helper plugin
* @param {StoreFunction} fn - The helper plugin function
*/
register(name: string, fn: StoreFunction): void {
if (!name) throw new TypeError('name is required');
if (typeof fn !== 'function') throw new TypeError('fn must be a function');
this.store[name] = fn;
}
}
export = Helper;