From ae87a2da7bb40fbb741bd35ec08ed3d96e798d94 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Thu, 18 Jan 2024 18:50:50 -0300 Subject: [PATCH] =?UTF-8?q?feat!:=20=F0=9F=92=A5=20=E2=9C=A8=20invert=20or?= =?UTF-8?q?der=20of=20array?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/try.d.ts | 2 +- src/try.js | 14 +++++++------- test/try.test.js | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/try.d.ts b/src/try.d.ts index 284a626..4377547 100644 --- a/src/try.d.ts +++ b/src/try.d.ts @@ -6,7 +6,7 @@ * **If a error occurred, the result is undefined**. * If there's not a error, error will be null and the result will be defined. */ -type WrappedResult = [Error, undefined] | [null, R]; +type WrappedResult = [R, null] | [undefined, Error]; /** * The returned function from {@link tryAsync}. diff --git a/src/try.js b/src/try.js index ae117fc..c78ee01 100644 --- a/src/try.js +++ b/src/try.js @@ -7,7 +7,7 @@ * **If a error occurred, the result is undefined**. * If there's not a error, error will be null and the result will be defined. * - * @typedef {[Error, undefined] | [null, R]} WrappedResult + * @typedef {[R, null] | [undefined, Error]} WrappedResult * @template R */ @@ -52,10 +52,10 @@ function tryAsync(func) { */ return async (...args) => { try { - return [null, await func(...args)]; + return [await func(...args), null]; } catch (error) { - if (error instanceof Error) return [error, undefined]; + if (error instanceof Error) return [undefined, error]; const errObj = new Error(error?.toString // eslint-disable-next-line @typescript-eslint/no-base-to-string @@ -63,7 +63,7 @@ function tryAsync(func) { : 'Could not stringify error', { cause: { value: error } }); - return [errObj, undefined]; + return [undefined, errObj]; } }; } @@ -108,10 +108,10 @@ function trySync(func) { */ return (...args) => { try { - return [null, func(...args)]; + return [func(...args), null]; } catch (error) { - if (error instanceof Error) return [error, undefined]; + if (error instanceof Error) return [undefined, error]; const errObj = new Error(error?.toString // eslint-disable-next-line @typescript-eslint/no-base-to-string @@ -119,7 +119,7 @@ function trySync(func) { : 'Could not stringify error', { cause: { value: error } }); - return [errObj, undefined]; + return [undefined, errObj]; } }; } diff --git a/test/try.test.js b/test/try.test.js index c3fbb03..9f931ae 100644 --- a/test/try.test.js +++ b/test/try.test.js @@ -6,26 +6,26 @@ import { tryA, tryS } from '../src/index.js'; describe.concurrent('Return values', () => { it('JSON parsing [Sync, Success]', ({ expect }) => { - const [error, json] = tryS(JSON.parse)('{ "hello": "world" }'); + const [json, error] = tryS(JSON.parse)('{ "hello": "world" }'); expect(error).toBe(null); expect(json).toEqual({ hello: 'world' }); }); it('JSON parsing [Sync, Error]', ({ expect }) => { - const [error, json] = tryS(JSON.parse)('{ "hello: "world" }'); + const [json, error] = tryS(JSON.parse)('{ "hello: "world" }'); expect(error?.name).toEqual('SyntaxError'); expect(error).toBeInstanceOf(Error); expect(json).toBe(undefined); }); it('Fetch function [Async, Success]', async ({ expect }) => { - const [error, res] = await tryA(fetch)('https://example.com'); + const [res, error] = await tryA(fetch)('https://example.com'); expect(error).toBe(null); expect(res?.status).toBe(200); }); it('Fetch function [Async, Error]', async ({ expect }) => { - const [error, res] = await tryA(fetch)('htps://example.com'); + const [res, error] = await tryA(fetch)('htps://example.com'); expect(error?.name).toEqual('TypeError'); expect(error).toBeInstanceOf(Error);