Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
814aa085d1 | ||
|
|
be5ea204ce | ||
|
|
4c3e60cf85
|
||
|
|
c04b7d5c8c | ||
|
|
a4f857e71e
|
||
|
|
ecfbfc7ea4
|
||
|
|
ae87a2da7b
|
||
|
|
02490a2502
|
||
|
|
3444a1c5da
|
||
|
|
f19a5ec2b1
|
||
|
|
0895f7c1c2 | ||
|
|
b76750bbe9 | ||
|
|
d003151b0f | ||
|
|
731ff705a2
|
||
|
|
138dc210e1
|
||
|
|
65a4f3de22 | ||
|
|
5581ae7841 | ||
|
|
8266d1e67a | ||
|
|
7f49118630
|
||
|
|
5626da8b78
|
||
|
|
53d7c3b408
|
||
|
|
49832f676a
|
||
|
|
b97ca55a68
|
||
|
|
4bf4326da8
|
||
|
|
472d01ec35 |
2
.github/workflows/release-preview.yml
vendored
2
.github/workflows/release-preview.yml
vendored
@@ -11,7 +11,7 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
if: ${{ github.event.workflow_run.conclusion == 'success' && github.repository == 'loreddev/marknow' }}
|
if: ${{ github.event.workflow_run.conclusion == 'success' && github.repository == 'loreddev/lilbetter.js' }}
|
||||||
name: Release preview
|
name: Release preview
|
||||||
env:
|
env:
|
||||||
HUSKY: 0
|
HUSKY: 0
|
||||||
|
|||||||
0
.husky/pre-commit
Normal file → Executable file
0
.husky/pre-commit
Normal file → Executable file
0
.husky/prepare-commit-msg
Normal file → Executable file
0
.husky/prepare-commit-msg
Normal file → Executable file
4
.lintstagedrc
Normal file
4
.lintstagedrc
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"gitDir": "./.git",
|
||||||
|
"*": "eslint --fix"
|
||||||
|
}
|
||||||
18
CHANGELOG.md
18
CHANGELOG.md
@@ -1,5 +1,23 @@
|
|||||||
# lilbetter.js
|
# lilbetter.js
|
||||||
|
|
||||||
|
## 0.1.0
|
||||||
|
|
||||||
|
### Minor Changes
|
||||||
|
|
||||||
|
- ecfbfc7: Created the tryAsync (tryA) and trySync (tryS) functions, said can be used for wrapping and calling functions that can throw error. Said error can then be handled using in a Go-like fashion.
|
||||||
|
|
||||||
|
## 0.0.3
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 138dc21: Populated package.json
|
||||||
|
|
||||||
|
## 0.0.2
|
||||||
|
|
||||||
|
### Patch Changes
|
||||||
|
|
||||||
|
- 4bf4326: Fix github workflow
|
||||||
|
|
||||||
## 0.0.1
|
## 0.0.1
|
||||||
|
|
||||||
### Patch Changes
|
### Patch Changes
|
||||||
|
|||||||
@@ -1,23 +1,25 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"checkJs": true,
|
"checkJs": true,
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"module": "ES2022",
|
"module": "ES2022",
|
||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"alwaysStrict": true
|
"alwaysStrict": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"./src/**/*.js",
|
"./src/**/*",
|
||||||
"./eslint.config.js"
|
"./test/**/*.js",
|
||||||
],
|
"./eslint.config.js",
|
||||||
"exclude": [
|
"./commitlint.config.cjs"
|
||||||
"./node_modules/**",
|
],
|
||||||
".dist"
|
"exclude": [
|
||||||
]
|
"./node_modules/**",
|
||||||
|
".dist"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
86
package.json
86
package.json
@@ -1,29 +1,61 @@
|
|||||||
{
|
{
|
||||||
"name": "lilbetter.js",
|
"name": "lilbetter.js",
|
||||||
"version": "0.0.1",
|
"version": "0.1.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "./src/index.js",
|
"main": "./src/index.js",
|
||||||
"scripts": {
|
"browser": "./src/index.js",
|
||||||
"test": "vitest",
|
"source": "./src/index.js",
|
||||||
"prepare": "husky install",
|
"module": "./src/index.js",
|
||||||
"lint": "eslint .",
|
"homepage": "https://github.com/LoredDev/lilbetter.js",
|
||||||
"lint:fix": "eslint . --fix",
|
"repository": {
|
||||||
"release": "changeset publish"
|
"type": "git",
|
||||||
},
|
"url": "https://github.com/LoredDev/lilbetter.js"
|
||||||
"type": "module",
|
},
|
||||||
"keywords": [],
|
"exports": {
|
||||||
"author": "",
|
".": {
|
||||||
"license": "MIT",
|
"import": "./src/index.js",
|
||||||
"publishConfig": {
|
"default": "./src/index.js",
|
||||||
"access": "public"
|
"types": "./src/index.d.ts"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"./try": {
|
||||||
"@changesets/cli": "^2.27.1",
|
"import": "./src/try.js",
|
||||||
"@commitlint/config-conventional": "^18.4.4",
|
"default": "./src/try.js",
|
||||||
"@commitlint/types": "^18.4.4",
|
"types": "./src/try.d.ts"
|
||||||
"@eslegant/js": "0.0.0-next-20230910004746",
|
}
|
||||||
"eslint": "^8.56.0",
|
},
|
||||||
"husky": "^8.0.3",
|
"files": [
|
||||||
"vitest": "^1.2.1"
|
"./src/**/*.js",
|
||||||
}
|
"./src/**/*.d.ts"
|
||||||
|
],
|
||||||
|
"scripts": {
|
||||||
|
"test": "vitest",
|
||||||
|
"prepare": "husky install",
|
||||||
|
"lint": "eslint .",
|
||||||
|
"lint:fix": "eslint . --fix",
|
||||||
|
"release": "changeset publish"
|
||||||
|
},
|
||||||
|
"type": "module",
|
||||||
|
"keywords": [],
|
||||||
|
"author": {
|
||||||
|
"email": "contact.guz013@gmail.com",
|
||||||
|
"name": "Gustavo \"Guz\" L. de Mello",
|
||||||
|
"url": "https://guz.one"
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18",
|
||||||
|
"pnpm": ">=8"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@changesets/cli": "^2.27.1",
|
||||||
|
"@commitlint/config-conventional": "^18.4.4",
|
||||||
|
"@commitlint/types": "^18.4.4",
|
||||||
|
"@eslegant/js": "0.0.0-next-20230910004746",
|
||||||
|
"eslint": "^8.56.0",
|
||||||
|
"husky": "^8.0.3",
|
||||||
|
"vitest": "^1.2.1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
src/index.d.ts
vendored
Normal file
3
src/index.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
export * from './try.d.ts';
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
export * from './try.js';
|
||||||
|
|
||||||
|
|||||||
94
src/try.d.ts
vendored
Normal file
94
src/try.d.ts
vendored
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* The WrappedResult type returned by the wrapped function in {@link trySync}
|
||||||
|
* and {@link tryAsync}.
|
||||||
|
*
|
||||||
|
* **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> = [R, null] | [undefined, Error];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The returned function from {@link tryAsync}.
|
||||||
|
*
|
||||||
|
* @param args - The arguments of the function.
|
||||||
|
* - The arguments of the wrapped function.
|
||||||
|
* @returns
|
||||||
|
* - The final tuple containing the Error object (if one occured) and the resulting value.
|
||||||
|
*/
|
||||||
|
type WrappedAsyncFunction<F> = (...args: Parameters<F>) =>
|
||||||
|
Promise<WrappedResult<Awaited<ReturnType<F>>>>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function-sugar/Syntax-sugar for handling functions that can throw errors. Wrapping then
|
||||||
|
* into a try-catch block / "curried function" that returns a "tuple as array" of error and
|
||||||
|
* value, which can be used for handling the error using a Go-like fashion. **This function
|
||||||
|
* is for asynchronous operations,** for synchronous ones, see {@link trySync}.
|
||||||
|
*
|
||||||
|
* **If there's a error, the result is undefined**.
|
||||||
|
* If there's not a error, error will be null and the result will be defined.
|
||||||
|
*
|
||||||
|
* @param func
|
||||||
|
* - The function to be executed.
|
||||||
|
* @returns
|
||||||
|
* - The function to be immediately called with the wrapped function's arguments.
|
||||||
|
* @example
|
||||||
|
* const [error, res] = await tryAsync(fetch)("https://example.com");
|
||||||
|
* if (error !== null) {
|
||||||
|
* // error handling...
|
||||||
|
* console.log(error);
|
||||||
|
* }
|
||||||
|
* // continue the logic...
|
||||||
|
*/
|
||||||
|
function tryAsync<
|
||||||
|
F extends (...args: Parameters<F>) => (ReturnType<F> extends Promise
|
||||||
|
? ReturnType<F>
|
||||||
|
: Promise<ReturnType<F>>
|
||||||
|
),
|
||||||
|
>(func: F): WrappedAsyncFunction<F>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The returned function from {@link trySync}.
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
* - The arguments of the wrapped function.
|
||||||
|
* @returns
|
||||||
|
* - The final tuple containing the Error object (if one occured) and the resulting value.
|
||||||
|
*/
|
||||||
|
type WrappedFunction<F> = (...args: Parameters<F>) =>
|
||||||
|
WrappedResult<ReturnType<F>>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function-sugar/Syntax-sugar for handling functions that can throw errors. Wrapping then
|
||||||
|
* into a try-catch block / "curried function" that returns a "tuple as array" of error and
|
||||||
|
* value, which can be used for handling the error using a Go-like fashion. **This function
|
||||||
|
* is for synchronous operations,** for asynchronous ones, see {@link tryAsync}.
|
||||||
|
*
|
||||||
|
* **If there's a error, the result is undefined**.
|
||||||
|
* If there's not a error, error will be null and the result will be defined.
|
||||||
|
*
|
||||||
|
* @param func
|
||||||
|
* - The function to be executed.
|
||||||
|
* @returns
|
||||||
|
* - The function to be immediately called with the wrapped function's arguments.
|
||||||
|
* @example
|
||||||
|
* const [error, json] = trySync(JSON.parse)('{ "hello": "world" }');
|
||||||
|
* if (error !== null) {
|
||||||
|
* // error handling...
|
||||||
|
* console.log(error);
|
||||||
|
* }
|
||||||
|
* // continue the logic...
|
||||||
|
*/
|
||||||
|
function trySync<
|
||||||
|
F extends (...args: Parameters<F>) => ReturnType<F>,
|
||||||
|
>(func: F): WrappedFunction<F>;
|
||||||
|
|
||||||
|
export {
|
||||||
|
type WrappedAsyncFunction,
|
||||||
|
type WrappedFunction,
|
||||||
|
type WrappedResult,
|
||||||
|
tryAsync as tryA,
|
||||||
|
tryAsync,
|
||||||
|
trySync as tryS,
|
||||||
|
trySync,
|
||||||
|
};
|
||||||
133
src/try.js
Normal file
133
src/try.js
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
/* eslint-disable no-secrets/no-secrets */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The WrappedResult type returned by the wrapped function in {@link trySync}
|
||||||
|
* and {@link tryAsync}.
|
||||||
|
*
|
||||||
|
* **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 {[R, null] | [undefined, Error]} WrappedResult<R>
|
||||||
|
* @template R
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The returned function from {@link tryAsync}.
|
||||||
|
*
|
||||||
|
* @typedef {(...args: Parameters<F>) => Promise<WrappedResult<Awaited<ReturnType<F>>>>}
|
||||||
|
* WrappedAsyncFunction
|
||||||
|
* @template {(...args: Parameters<F>) => ReturnType<F>} F
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function-sugar/Syntax-sugar for handling functions that can throw errors. Wrapping then
|
||||||
|
* into a try-catch block / "curried function" that returns a "tuple as array" of error and
|
||||||
|
* value, which can be used for handling the error using a Go-like fashion. **This function
|
||||||
|
* is for asynchronous operations,** for synchronous ones, see {@link trySync}.
|
||||||
|
*
|
||||||
|
* **If there's a error, the result is undefined**.
|
||||||
|
* If there's not a error, error will be null and the result will be defined.
|
||||||
|
*
|
||||||
|
* @template {(...args: Parameters<F>) => Promise<Awaited<ReturnType<F>>>} F
|
||||||
|
* @param {F} func
|
||||||
|
* - The function to be executed.
|
||||||
|
* @returns {WrappedAsyncFunction<F>}
|
||||||
|
* - The function to be immediately called with the wrapped function's arguments.
|
||||||
|
* @example
|
||||||
|
* const [error, res] = await tryAsync(fetch)("https://example.com");
|
||||||
|
* if (error !== null) {
|
||||||
|
* // error handling...
|
||||||
|
* console.log(error);
|
||||||
|
* }
|
||||||
|
* // continue the logic...
|
||||||
|
*/
|
||||||
|
function tryAsync(func) {
|
||||||
|
/**
|
||||||
|
* The returned function from {@link tryAsync}.
|
||||||
|
*
|
||||||
|
* @param {Parameters<F>} args - The arguments of the function.
|
||||||
|
* - The arguments of the wrapped function.
|
||||||
|
* @returns {Promise<WrappedResult<Awaited<ReturnType<F>>>>}
|
||||||
|
* - The final tuple containing the Error object (if one occured) and the resulting value.
|
||||||
|
*/
|
||||||
|
return async (...args) => {
|
||||||
|
try {
|
||||||
|
return [await func(...args), null];
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
if (error instanceof Error) return [undefined, error];
|
||||||
|
|
||||||
|
const errObj = new Error(error?.toString
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
||||||
|
? `Stringified error to: ${error.toString()}`
|
||||||
|
: 'Could not stringify error',
|
||||||
|
{ cause: { value: error } });
|
||||||
|
|
||||||
|
return [undefined, errObj];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The returned function from {@link trySync}.
|
||||||
|
*
|
||||||
|
* @typedef {(...args: Parameters<F>) => WrappedResult<ReturnType<F>>} WrappedFunction
|
||||||
|
* @template {(...args: Parameters<F>) => ReturnType<F>} F
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function-sugar/Syntax-sugar for handling functions that can throw errors. Wrapping then
|
||||||
|
* into a try-catch block / "curried function" that returns a "tuple as array" of error and
|
||||||
|
* value, which can be used for handling the error using a Go-like fashion. **This function
|
||||||
|
* is for synchronous operations,** for asynchronous ones, see {@link tryAsync}.
|
||||||
|
*
|
||||||
|
* **If there's a error, the result is undefined**.
|
||||||
|
* If there's not a error, error will be null and the result will be defined.
|
||||||
|
*
|
||||||
|
* @template {(...args: Parameters<F>) => ReturnType<F>} F
|
||||||
|
* @param {F} func
|
||||||
|
* - The function to be executed.
|
||||||
|
* @returns {WrappedFunction<F>}
|
||||||
|
* - The function to be immediately called with the wrapped function's arguments.
|
||||||
|
* @example
|
||||||
|
* const [error, json] = trySync(JSON.parse)('{ "hello": "world" }');
|
||||||
|
* if (error !== null) {
|
||||||
|
* // error handling...
|
||||||
|
* console.log(error);
|
||||||
|
* }
|
||||||
|
* // continue the logic...
|
||||||
|
*/
|
||||||
|
function trySync(func) {
|
||||||
|
/**
|
||||||
|
* The returned function from {@link trySync}.
|
||||||
|
*
|
||||||
|
* @param {Parameters<F>} args
|
||||||
|
* - The arguments of the wrapped function.
|
||||||
|
* @returns {WrappedResult<ReturnType<F>>}
|
||||||
|
* - The final tuple containing the Error object (if one occured) and the resulting value.
|
||||||
|
*/
|
||||||
|
return (...args) => {
|
||||||
|
try {
|
||||||
|
return [func(...args), null];
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
if (error instanceof Error) return [undefined, error];
|
||||||
|
|
||||||
|
const errObj = new Error(error?.toString
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
||||||
|
? `Stringified error to: ${error.toString()}`
|
||||||
|
: 'Could not stringify error',
|
||||||
|
{ cause: { value: error } });
|
||||||
|
|
||||||
|
return [undefined, errObj];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
tryAsync as tryA,
|
||||||
|
tryAsync,
|
||||||
|
trySync as tryS,
|
||||||
|
trySync,
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
import { expect, test } from 'vitest';
|
|
||||||
|
|
||||||
test('placeholder', () => {
|
|
||||||
expect(1).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
35
test/try.test.js
Normal file
35
test/try.test.js
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/* eslint-disable import/no-relative-parent-imports */
|
||||||
|
// eslint-disable-next-line n/no-unpublished-import
|
||||||
|
import { describe, it } from 'vitest';
|
||||||
|
|
||||||
|
import { tryA, tryS } from '../src/index.js';
|
||||||
|
|
||||||
|
describe.concurrent('Return values', () => {
|
||||||
|
it('JSON parsing [Sync, Success]', ({ expect }) => {
|
||||||
|
const [json, error] = tryS(JSON.parse)('{ "hello": "world" }');
|
||||||
|
|
||||||
|
expect(error).toBe(null);
|
||||||
|
expect(json).toEqual({ hello: 'world' });
|
||||||
|
});
|
||||||
|
it('JSON parsing [Sync, Error]', ({ expect }) => {
|
||||||
|
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 [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 [res, error] = await tryA(fetch)('htps://example.com');
|
||||||
|
|
||||||
|
expect(error?.name).toEqual('TypeError');
|
||||||
|
expect(error).toBeInstanceOf(Error);
|
||||||
|
expect(res).toBe(undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
Reference in New Issue
Block a user