Skip to content

Commit

Permalink
Merge pull request #1 from mimamch/parseOrThrow
Browse files Browse the repository at this point in the history
update v1.1.0
  • Loading branch information
mimamch committed Jul 18, 2023
2 parents 2228b56 + 4f1e6a7 commit de5a9ab
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
11 changes: 11 additions & 0 deletions dist/command-processor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ declare class CommandProcessor<T extends Commands> {
constructor(events: T);
/**
* Parses and executes a command with the given arguments.
* @deprecated use parse method instead
*
* @template E - The type of command to parse and execute.
* @param {E} command - The command to parse and execute.
* @param {...Parameters<T[E]>} args - The arguments to pass to the command handler.
* @returns {ReturnType<T[E]>} - The result of executing the command.
*/
parseCommand<E extends keyof T>(command: E, ...args: Parameters<T[E]>): ReturnType<T[E]>;
/**
* Parses and executes a command with the given arguments.
*
* @template E - The type of command to parse and execute.
* @param {E} command - The command to parse and execute.
* @param {...Parameters<T[E]>} args - The arguments to pass to the command handler.
* @returns {ReturnType<T[E]>} - The result of executing the command.
*/
parse<E extends keyof T>(command: E, ...args: Parameters<T[E]>): ReturnType<T[E]>;
parseOrThrow<E extends keyof T>(command: E, ...args: Parameters<T[E]>): ReturnType<T[E]>;
}
export default CommandProcessor;
export { CommandProcessor, Commands };
20 changes: 20 additions & 0 deletions dist/command-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@ class CommandProcessor {
}
/**
* Parses and executes a command with the given arguments.
* @deprecated use parse method instead
*
* @template E - The type of command to parse and execute.
* @param {E} command - The command to parse and execute.
* @param {...Parameters<T[E]>} args - The arguments to pass to the command handler.
* @returns {ReturnType<T[E]>} - The result of executing the command.
*/
parseCommand(command, ...args) {
var _a, _b;
return (_b = (_a = this.events)[command]) === null || _b === void 0 ? void 0 : _b.call(_a, args);
}
/**
* Parses and executes a command with the given arguments.
*
* @template E - The type of command to parse and execute.
* @param {E} command - The command to parse and execute.
* @param {...Parameters<T[E]>} args - The arguments to pass to the command handler.
* @returns {ReturnType<T[E]>} - The result of executing the command.
*/
parse(command, ...args) {
var _a, _b;
return (_b = (_a = this.events)[command]) === null || _b === void 0 ? void 0 : _b.call(_a, args);
}
parseOrThrow(command, ...args) {
if (!(command in this.events)) {
throw new Error(`command ${command.toString()} is not registered`);
}
return this.events[command](args);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mimamch/cmd",
"version": "1.0.6",
"version": "1.1.0",
"description": "A command processor library for handling interactive commands",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const commands = {
const commandProcessor = new CommandProcessor(commands);

// Execute the "/sayhello" command with the argument "mimamch"
const sayHelloResult = commandProcessor.parseCommand("/sayhello", "mimamch");
const sayHelloResult = commandProcessor.parse("/sayhello", "mimamch");
console.log(sayHelloResult);
```

Expand Down
26 changes: 26 additions & 0 deletions src/command-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CommandProcessor<T extends Commands> {

/**
* Parses and executes a command with the given arguments.
* @deprecated use parse method instead
*
* @template E - The type of command to parse and execute.
* @param {E} command - The command to parse and execute.
Expand All @@ -20,6 +21,31 @@ class CommandProcessor<T extends Commands> {
command: E,
...args: Parameters<T[E]>
): ReturnType<T[E]> {
return this.events[command]?.(args) as ReturnType<T[E]>;
}

/**
* Parses and executes a command with the given arguments.
*
* @template E - The type of command to parse and execute.
* @param {E} command - The command to parse and execute.
* @param {...Parameters<T[E]>} args - The arguments to pass to the command handler.
* @returns {ReturnType<T[E]>} - The result of executing the command.
*/
parse<E extends keyof T>(
command: E,
...args: Parameters<T[E]>
): ReturnType<T[E]> {
return this.events[command]?.(args) as ReturnType<T[E]>;
}

parseOrThrow<E extends keyof T>(
command: E,
...args: Parameters<T[E]>
): ReturnType<T[E]> {
if (!(command in this.events)) {
throw new Error(`command ${command.toString()} is not registered`);
}
return this.events[command](args) as ReturnType<T[E]>;
}
}
Expand Down

0 comments on commit de5a9ab

Please sign in to comment.