feat!: 💥 invert order of array

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-01-18 18:50:50 -03:00
parent 02490a2502
commit ae87a2da7b
3 changed files with 12 additions and 12 deletions

2
src/try.d.ts vendored
View File

@@ -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<R> = [Error, undefined] | [null, R];
type WrappedResult<R> = [R, null] | [undefined, Error];
/**
* The returned function from {@link tryAsync}.

View File

@@ -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<R>
* @typedef {[R, null] | [undefined, Error]} WrappedResult<R>
* @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];
}
};
}

View File

@@ -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);