File tree Expand file tree Collapse file tree 5 files changed +59
-2
lines changed Expand file tree Collapse file tree 5 files changed +59
-2
lines changed Original file line number Diff line number Diff line change @@ -6,13 +6,24 @@ declare class CommandProcessor<T extends Commands> {
6
6
constructor ( events : T ) ;
7
7
/**
8
8
* Parses and executes a command with the given arguments.
9
+ * @deprecated use parse method instead
9
10
*
10
11
* @template E - The type of command to parse and execute.
11
12
* @param {E } command - The command to parse and execute.
12
13
* @param {...Parameters<T[E]> } args - The arguments to pass to the command handler.
13
14
* @returns {ReturnType<T[E]> } - The result of executing the command.
14
15
*/
15
16
parseCommand < E extends keyof T > ( command : E , ...args : Parameters < T [ E ] > ) : ReturnType < T [ E ] > ;
17
+ /**
18
+ * Parses and executes a command with the given arguments.
19
+ *
20
+ * @template E - The type of command to parse and execute.
21
+ * @param {E } command - The command to parse and execute.
22
+ * @param {...Parameters<T[E]> } args - The arguments to pass to the command handler.
23
+ * @returns {ReturnType<T[E]> } - The result of executing the command.
24
+ */
25
+ parse < E extends keyof T > ( command : E , ...args : Parameters < T [ E ] > ) : ReturnType < T [ E ] > ;
26
+ parseOrThrow < E extends keyof T > ( command : E , ...args : Parameters < T [ E ] > ) : ReturnType < T [ E ] > ;
16
27
}
17
28
export default CommandProcessor ;
18
29
export { CommandProcessor , Commands } ;
Original file line number Diff line number Diff line change @@ -7,13 +7,33 @@ class CommandProcessor {
7
7
}
8
8
/**
9
9
* Parses and executes a command with the given arguments.
10
+ * @deprecated use parse method instead
10
11
*
11
12
* @template E - The type of command to parse and execute.
12
13
* @param {E } command - The command to parse and execute.
13
14
* @param {...Parameters<T[E]> } args - The arguments to pass to the command handler.
14
15
* @returns {ReturnType<T[E]> } - The result of executing the command.
15
16
*/
16
17
parseCommand ( command , ...args ) {
18
+ var _a , _b ;
19
+ return ( _b = ( _a = this . events ) [ command ] ) === null || _b === void 0 ? void 0 : _b . call ( _a , args ) ;
20
+ }
21
+ /**
22
+ * Parses and executes a command with the given arguments.
23
+ *
24
+ * @template E - The type of command to parse and execute.
25
+ * @param {E } command - The command to parse and execute.
26
+ * @param {...Parameters<T[E]> } args - The arguments to pass to the command handler.
27
+ * @returns {ReturnType<T[E]> } - The result of executing the command.
28
+ */
29
+ parse ( command , ...args ) {
30
+ var _a , _b ;
31
+ return ( _b = ( _a = this . events ) [ command ] ) === null || _b === void 0 ? void 0 : _b . call ( _a , args ) ;
32
+ }
33
+ parseOrThrow ( command , ...args ) {
34
+ if ( ! ( command in this . events ) ) {
35
+ throw new Error ( `command ${ command . toString ( ) } is not registered` ) ;
36
+ }
17
37
return this . events [ command ] ( args ) ;
18
38
}
19
39
}
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " @mimamch/cmd" ,
3
- "version" : " 1.0.6 " ,
3
+ "version" : " 1.1.0 " ,
4
4
"description" : " A command processor library for handling interactive commands" ,
5
5
"main" : " dist/index.js" ,
6
6
"types" : " dist/index.d.ts" ,
Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ const commands = {
45
45
const commandProcessor = new CommandProcessor (commands);
46
46
47
47
// Execute the "/sayhello" command with the argument "mimamch"
48
- const sayHelloResult = commandProcessor .parseCommand (" /sayhello" , " mimamch" );
48
+ const sayHelloResult = commandProcessor .parse (" /sayhello" , " mimamch" );
49
49
console .log (sayHelloResult);
50
50
```
51
51
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ class CommandProcessor<T extends Commands> {
10
10
11
11
/**
12
12
* Parses and executes a command with the given arguments.
13
+ * @deprecated use parse method instead
13
14
*
14
15
* @template E - The type of command to parse and execute.
15
16
* @param {E } command - The command to parse and execute.
@@ -20,6 +21,31 @@ class CommandProcessor<T extends Commands> {
20
21
command : E ,
21
22
...args : Parameters < T [ E ] >
22
23
) : ReturnType < T [ E ] > {
24
+ return this . events [ command ] ?.( args ) as ReturnType < T [ E ] > ;
25
+ }
26
+
27
+ /**
28
+ * Parses and executes a command with the given arguments.
29
+ *
30
+ * @template E - The type of command to parse and execute.
31
+ * @param {E } command - The command to parse and execute.
32
+ * @param {...Parameters<T[E]> } args - The arguments to pass to the command handler.
33
+ * @returns {ReturnType<T[E]> } - The result of executing the command.
34
+ */
35
+ parse < E extends keyof T > (
36
+ command : E ,
37
+ ...args : Parameters < T [ E ] >
38
+ ) : ReturnType < T [ E ] > {
39
+ return this . events [ command ] ?.( args ) as ReturnType < T [ E ] > ;
40
+ }
41
+
42
+ parseOrThrow < E extends keyof T > (
43
+ command : E ,
44
+ ...args : Parameters < T [ E ] >
45
+ ) : ReturnType < T [ E ] > {
46
+ if ( ! ( command in this . events ) ) {
47
+ throw new Error ( `command ${ command . toString ( ) } is not registered` ) ;
48
+ }
23
49
return this . events [ command ] ( args ) as ReturnType < T [ E ] > ;
24
50
}
25
51
}
You can’t perform that action at this time.
0 commit comments