From 4f1e6a749ef66b41ac2c5c25369e91bb405d5763 Mon Sep 17 00:00:00 2001 From: Muhammad Imam Choirudin Date: Tue, 18 Jul 2023 23:49:41 +0700 Subject: [PATCH] update v1.1.0 --- dist/command-processor.d.ts | 11 +++++++++++ dist/command-processor.js | 20 ++++++++++++++++++++ package.json | 2 +- readme.md | 2 +- src/command-processor.ts | 26 ++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/dist/command-processor.d.ts b/dist/command-processor.d.ts index 801fd68..de97ff8 100644 --- a/dist/command-processor.d.ts +++ b/dist/command-processor.d.ts @@ -6,6 +6,7 @@ declare class CommandProcessor { 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. @@ -13,6 +14,16 @@ declare class CommandProcessor { * @returns {ReturnType} - The result of executing the command. */ parseCommand(command: E, ...args: Parameters): ReturnType; + /** + * 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} args - The arguments to pass to the command handler. + * @returns {ReturnType} - The result of executing the command. + */ + parse(command: E, ...args: Parameters): ReturnType; + parseOrThrow(command: E, ...args: Parameters): ReturnType; } export default CommandProcessor; export { CommandProcessor, Commands }; diff --git a/dist/command-processor.js b/dist/command-processor.js index 7b52041..a895438 100644 --- a/dist/command-processor.js +++ b/dist/command-processor.js @@ -7,6 +7,7 @@ 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. @@ -14,6 +15,25 @@ class CommandProcessor { * @returns {ReturnType} - 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} args - The arguments to pass to the command handler. + * @returns {ReturnType} - 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); } } diff --git a/package.json b/package.json index 1931af9..7cbed54 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index c9a285d..c877674 100644 --- a/readme.md +++ b/readme.md @@ -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); ``` diff --git a/src/command-processor.ts b/src/command-processor.ts index 756c375..2928425 100644 --- a/src/command-processor.ts +++ b/src/command-processor.ts @@ -10,6 +10,7 @@ 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. @@ -20,6 +21,31 @@ class CommandProcessor { command: E, ...args: Parameters ): ReturnType { + return this.events[command]?.(args) as ReturnType; + } + + /** + * 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} args - The arguments to pass to the command handler. + * @returns {ReturnType} - The result of executing the command. + */ + parse( + command: E, + ...args: Parameters + ): ReturnType { + return this.events[command]?.(args) as ReturnType; + } + + parseOrThrow( + command: E, + ...args: Parameters + ): ReturnType { + if (!(command in this.events)) { + throw new Error(`command ${command.toString()} is not registered`); + } return this.events[command](args) as ReturnType; } }