diff --git a/src/cmd.spec.ts b/src/cmd.spec.ts index ade0d2e..29a1a7d 100644 --- a/src/cmd.spec.ts +++ b/src/cmd.spec.ts @@ -283,342 +283,4 @@ describe("cmd", () => { expect(message.name).toBe("error"); }); }); - - describe("ofFunc", () => { - describe("either", () => { - it("returns one function", () => { - // arrange - const result = cmd.ofFunc.either(jest.fn(), successMsg, errorMsg); - - // assert - expect(result).toHaveLength(1); - expect(result[0]).toBeInstanceOf(Function); - }); - - it("runs the task", () => { - // arrange - const task = jest.fn(); - const result = cmd.ofFunc.either(task, successMsg, errorMsg); - - // act - result[0]?.(() => null); - - // assert - expect(task).toHaveBeenCalledTimes(1); - }); - - it("dispatches the success message", () => { - // arrange - const task = jest.fn(); - const result = cmd.ofFunc.either(task, successMsg, errorMsg); - - // act - let message: Message = { name: "none" }; - - result[0]?.((msg) => { - message = msg; - }); - - // assert - expect(message.name).toBe("success"); - }); - - it("dispatches the error message", () => { - // arrange - const task = jest.fn(() => { - throw new Error("error"); - }); - const result = cmd.ofFunc.either(task, successMsg, errorMsg); - - // act - let message: Message = { name: "none" }; - - result[0]?.((msg) => { - message = msg; - }); - - // assert - expect(message.name).toBe("error"); - }); - }); - - describe("perform", () => { - it("returns one function", () => { - // arrange - const result = cmd.ofFunc.perform(jest.fn(), successMsg); - - // assert - expect(result).toHaveLength(1); - expect(result[0]).toBeInstanceOf(Function); - }); - - it("runs the task", () => { - // arrange - const task = jest.fn(); - const result = cmd.ofFunc.perform(task, successMsg); - - // act - result[0]?.(() => null); - - // assert - expect(task).toHaveBeenCalledTimes(1); - }); - - it("dispatches the success message", () => { - // arrange - const task = jest.fn(); - const result = cmd.ofFunc.perform(task, successMsg); - - // act - let message: Message = { name: "none" }; - - result[0]?.((msg) => { - message = msg; - }); - - // assert - expect(message.name).toBe("success"); - }); - - it("ignores an error", () => { - // arrange - const task = jest.fn(() => { - throw new Error("error"); - }); - const result = cmd.ofFunc.perform(task, successMsg); - - // act - let message: Message = { name: "none" }; - - result[0]?.((msg) => { - message = msg; - }); - - // assert - expect(message.name).toBe("none"); - }); - }); - - describe("attempt", () => { - it("returns one function", () => { - // arrange - const result = cmd.ofFunc.attempt(jest.fn(), errorMsg); - - // assert - expect(result).toHaveLength(1); - expect(result[0]).toBeInstanceOf(Function); - }); - - it("runs the task", () => { - // arrange - const task = jest.fn(); - const result = cmd.ofFunc.attempt(task, errorMsg); - - // act - result[0]?.(() => null); - - // assert - expect(task).toHaveBeenCalledTimes(1); - }); - - it("ignores the success", () => { - // arrange - const task = jest.fn(); - const result = cmd.ofFunc.attempt(task, errorMsg); - - // act - let message: Message = { name: "none" }; - - result[0]?.((msg) => { - message = msg; - }); - - // assert - expect(message.name).toBe("none"); - }); - - it("dispatches the error message", () => { - // arrange - const task = jest.fn(() => { - throw new Error("error"); - }); - const result = cmd.ofFunc.attempt(task, errorMsg); - - // act - let message: Message = { name: "none" }; - - result[0]?.((msg) => { - message = msg; - }); - - // assert - expect(message.name).toBe("error"); - }); - }); - }); - - describe("ofPromise", () => { - describe("either", () => { - it("returns one function", () => { - // arrange - const result = cmd.ofPromise.either(asyncResolve, successMsg, errorMsg); - - // assert - expect(result).toHaveLength(1); - expect(result[0]).toBeInstanceOf(Function); - }); - - it("runs the task", () => { - // arrange - const task = jest.fn(asyncResolve); - const result = cmd.ofPromise.either(task, successMsg, errorMsg); - - // act - result[0]?.(() => null); - - // assert - expect(task).toHaveBeenCalledTimes(1); - }); - - it("dispatches the success message", async () => { - // arrange - const task = jest.fn(asyncResolve); - const result = cmd.ofPromise.either(task, successMsg, errorMsg); - - // act - const act = async (): Promise => - new Promise((resolve) => { - result[0]?.(resolve); - }); - const message = await act(); - - // assert - expect(message.name).toBe("success"); - }); - - it("dispatches the error message", async () => { - // arrange - const task = jest.fn(async () => { - throw new Error("error"); - }); - const result = cmd.ofPromise.either(task, successMsg, errorMsg); - - // act - const act = async (): Promise => - new Promise((resolve) => { - result[0]?.(resolve); - }); - const message = await act(); - - // assert - expect(message.name).toBe("error"); - }); - }); - - describe("perform", () => { - it("returns one function", () => { - // arrange - const result = cmd.ofPromise.perform(asyncResolve, successMsg); - - // assert - expect(result).toHaveLength(1); - expect(result[0]).toBeInstanceOf(Function); - }); - - it("runs the task", () => { - // arrange - const task = jest.fn(asyncResolve); - const result = cmd.ofPromise.perform(task, successMsg); - - // act - result[0]?.(() => null); - - // assert - expect(task).toHaveBeenCalledTimes(1); - }); - - it("dispatches the success message", async () => { - // arrange - const task = jest.fn(asyncResolve); - const result = cmd.ofPromise.perform(task, successMsg); - - // act - const act = async (): Promise => - new Promise((resolve) => { - result[0]?.(resolve); - }); - const message = await act(); - - // assert - expect(message.name).toBe("success"); - }); - - it("ignores an error", async () => { - // arrange - const task = jest.fn(async () => { - throw new Error("error"); - }); - const result = cmd.ofPromise.perform(task, successMsg); - - // act - const succeeds = (): void => result[0]?.(jest.fn()); - - // assert - expect(succeeds).not.toThrow(); - }); - }); - - describe("attempt", () => { - it("returns one function", () => { - // arrange - const result = cmd.ofPromise.attempt(asyncResolve, errorMsg); - - // assert - expect(result).toHaveLength(1); - expect(result[0]).toBeInstanceOf(Function); - }); - - it("runs the task", () => { - // arrange - const task = jest.fn(asyncResolve); - const result = cmd.ofPromise.attempt(task, errorMsg); - - // act - result[0]?.(() => null); - - // assert - expect(task).toHaveBeenCalledTimes(1); - }); - - it("ignores the success", async () => { - // arrange - const task = jest.fn(asyncResolve); - const result = cmd.ofPromise.attempt(task, errorMsg); - - // act - result[0]?.(jest.fn()); - - // assert - expect(task).toHaveBeenCalledWith(); - }); - - it("dispatches the error message", async () => { - // arrange - const task = jest.fn(async () => { - throw new Error("error"); - }); - const result = cmd.ofPromise.attempt(task, errorMsg); - - // act - const act = async (): Promise => - new Promise((resolve) => { - result[0]?.(resolve); - }); - const message = await act(); - - // assert - expect(message.name).toBe("error"); - }); - }); - }); }); diff --git a/src/cmd.ts b/src/cmd.ts index 9260c0b..aa12b3e 100644 --- a/src/cmd.ts +++ b/src/cmd.ts @@ -104,156 +104,6 @@ const cmd = { return [bind]; }, - - /** - * Provides functionalities to create commands from simple functions. - * @deprecated Use `ofEither`, `ofSuccess`, or `ofError` instead. - */ - ofFunc: { - /** - * Creates a command out of a simple function and maps the result. - * @param task The function to call. - * @param ofSuccess Creates the message to dispatch after a successful call of the task. - * @param ofError Creates the message to dispatch when an error occurred. - * @param args The parameters of the task. - */ - either( - task: (...args: TArgs) => TReturn, - ofSuccess: (result: TReturn) => TSuccessMessage, - ofError: (error: Error) => TErrorMessage, - ...args: TArgs - ): Cmd { - const bind = (dispatch: Dispatch): void => { - try { - const result = task(...args); - - dispatch(ofSuccess(result)); - } catch (ex: unknown) { - dispatch(ofError(ex as Error)); - } - }; - - return [bind]; - }, - - /** - * Creates a command out of a simple function and ignores the error case. - * @param task The function to call. - * @param ofSuccess Creates the message to dispatch after a successful call of the task. - * @param args The parameters of the task. - */ - perform( - task: (...args: TArgs) => TReturn, - ofSuccess: (result: TReturn) => TSuccessMessage, - ...args: TArgs - ): Cmd { - const bind = (dispatch: Dispatch, fallback?: FallbackHandler): void => { - try { - const result = task(...args); - - dispatch(ofSuccess(result)); - } catch { - fallback?.(); - } - }; - - return [bind]; - }, - - /** - * Creates a command out of a simple function and ignores the success case. - * @param task The function to call. - * @param ofError Creates the message to dispatch when an error occurred. - * @param args The parameters of the task. - */ - attempt( - task: (...args: TArgs) => unknown, - ofError: (error: Error) => TErrorMessage, - ...args: TArgs - ): Cmd { - const bind = (dispatch: Dispatch, fallback?: FallbackHandler): void => { - try { - task(...args); - - if (fallback) { - fallback(); - } - } catch (ex: unknown) { - dispatch(ofError(ex as Error)); - } - }; - - return [bind]; - }, - }, - - /** - * Provides functionalities to create commands from async functions. - * @deprecated Use `ofEither`, `ofSuccess`, or `ofError` instead. - */ - ofPromise: { - /** - * Creates a command out of an async function and maps the result. - * @param task The async function to call. - * @param ofSuccess Creates the message to dispatch when the promise is resolved. - * @param ofError Creates the message to dispatch when the promise is rejected. - * @param args The parameters of the task. - */ - either( - task: (...args: TArgs) => Promise, - ofSuccess: (result: TReturn) => TSuccessMessage, - ofError: (error: Error) => TErrorMessage, - ...args: TArgs - ): Cmd { - const bind = (dispatch: Dispatch): void => { - task(...args) - .then((result) => dispatch(ofSuccess(result))) - .catch((ex: unknown) => dispatch(ofError(ex as Error))); - }; - - return [bind]; - }, - - /** - * Creates a command out of an async function and ignores the error case. - * @param task The async function to call. - * @param ofSuccess Creates the message to dispatch when the promise is resolved. - * @param args The parameters of the task. - */ - perform( - task: (...args: TArgs) => Promise, - ofSuccess: (result: TReturn) => TSuccessMessage, - ...args: TArgs - ): Cmd { - const bind = (dispatch: Dispatch, fallback: FallbackHandler = defaultFallbackHandler): void => { - task(...args) - .then((result) => dispatch(ofSuccess(result))) - .catch(() => fallback()); - }; - - return [bind]; - }, - - /** - * Creates a command out of an async function and ignores the success case. - * @param task The async function to call. - * @param ofError Creates the message to dispatch when the promise is rejected. - * @param args The parameters of the task. - */ - attempt( - task: (...args: TArgs) => Promise, - ofError: (error: Error) => TErrorMessage, - ...args: TArgs - ): Cmd { - const bind = (dispatch: Dispatch, fallback?: FallbackHandler): void => { - task(...args) - .then(() => fallback?.()) - .catch((ex: unknown) => dispatch(ofError(ex as Error))); - }; - - return [bind]; - }, - }, }; function defaultFallbackHandler(): void {