Skip to content

Commit 4f1e6a7

Browse files
committed
update v1.1.0
1 parent 2228b56 commit 4f1e6a7

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

dist/command-processor.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@ declare class CommandProcessor<T extends Commands> {
66
constructor(events: T);
77
/**
88
* Parses and executes a command with the given arguments.
9+
* @deprecated use parse method instead
910
*
1011
* @template E - The type of command to parse and execute.
1112
* @param {E} command - The command to parse and execute.
1213
* @param {...Parameters<T[E]>} args - The arguments to pass to the command handler.
1314
* @returns {ReturnType<T[E]>} - The result of executing the command.
1415
*/
1516
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]>;
1627
}
1728
export default CommandProcessor;
1829
export { CommandProcessor, Commands };

dist/command-processor.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,33 @@ class CommandProcessor {
77
}
88
/**
99
* Parses and executes a command with the given arguments.
10+
* @deprecated use parse method instead
1011
*
1112
* @template E - The type of command to parse and execute.
1213
* @param {E} command - The command to parse and execute.
1314
* @param {...Parameters<T[E]>} args - The arguments to pass to the command handler.
1415
* @returns {ReturnType<T[E]>} - The result of executing the command.
1516
*/
1617
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+
}
1737
return this.events[command](args);
1838
}
1939
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mimamch/cmd",
3-
"version": "1.0.6",
3+
"version": "1.1.0",
44
"description": "A command processor library for handling interactive commands",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const commands = {
4545
const commandProcessor = new CommandProcessor(commands);
4646

4747
// Execute the "/sayhello" command with the argument "mimamch"
48-
const sayHelloResult = commandProcessor.parseCommand("/sayhello", "mimamch");
48+
const sayHelloResult = commandProcessor.parse("/sayhello", "mimamch");
4949
console.log(sayHelloResult);
5050
```
5151

src/command-processor.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CommandProcessor<T extends Commands> {
1010

1111
/**
1212
* Parses and executes a command with the given arguments.
13+
* @deprecated use parse method instead
1314
*
1415
* @template E - The type of command to parse and execute.
1516
* @param {E} command - The command to parse and execute.
@@ -20,6 +21,31 @@ class CommandProcessor<T extends Commands> {
2021
command: E,
2122
...args: Parameters<T[E]>
2223
): 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+
}
2349
return this.events[command](args) as ReturnType<T[E]>;
2450
}
2551
}

0 commit comments

Comments
 (0)