This package provides some useful function wrappers.
This package requires Node v16 or above.
To check your installed version, run the following command.
$ node -v
v20.5.1
Before installing, please read the prerequisites.
To install and use this package, run:
$ npm i @kikiutils/wrappers # Npm
$ pnpm add @kikiutils/wrappers # Pnpm
$ yarn add @kikiutils/wrappers # Yarn
All wrappers support passing in async or sync function.
Run the function use try/catch.
Returns false if there was an error. Otherwise return true.
import { tryAndGetBoolean } from '@kikiutils/wrappers';
// Async usage
const wrappedFunc = tryAndGetBoolean(async (from: string, to: string) => {
// ...some actions.
return [from, to];
});
const asyncResult = await wrappedFunc('./from', './to');
// true - successfully executed
// false - an error occurred
// Sync usage
const wrappedFuncSync = tryAndGetBoolean((from: string, to: string) => {
// ...some actions.
return [from, to];
});
const syncResult = wrappedFuncSync('./from', './to');
// true - successfully executed
// false - an error occurred
Run the function use try/catch.
If an error occurs, returns undefined or the set value. Otherwise return the function result.
import { tryAndGetData } from '@kikiutils/wrappers';
import fs from 'fs';
import fsp from 'fs/promises';
// Async usage
const wrappedFunc = tryAndGetData(async (path: string) => {
// ...some actions.
return await fsp.readFile(path);
});
const asyncResult = await wrappedFunc('./package.json');
// Buffer - successfully read
// undefined - an error occurred
// Sync usage
const wrappedFuncSync = tryAndGetData((path: string) => {
// ...some actions.
return fs.readFileSync(path);
});
const syncResult = wrappedFuncSync('./package.json');
// Buffer - successfully read
// undefined - an error occurred
// With onErrorValue
const wrappedFunc = tryAndGetData(async (path: string) => {
// ...some actions.
return await fsp.readFile(path);
}, false);
const asyncResult = await wrappedFunc('./package.json');
// Buffer - successfully read
// false - an error occurred
If you're using typescript, wrapping a function with overloads will break the original type hints and checks for overloads.
But there is a compromise on the parameters and return types of the overload function, see this issue.
If you have a better solution or a solution for ReturnType, feel free to open an issue and raise it, thanks!
To solve this problem, you may need to wrap the function to be wrapped and specify the input type explicitly.
import { tryAndGetData } from '@kikiutils/wrappers';
import fsp from 'fs/promises';
// Original function
const oFile = await fsp.readFile('./package.json');
// oFile type is Buffer
// Wrapped function
const wrappedReadFile = tryAndGetData(fsp.readFile);
const wFile = await wrappedReadFile('./package.json');
// wFile type is string | Buffer | undefined
// Set params to get current type.
const cWrappedReadFile = tryAndGetData(async (path: string) => await fsp.readFile(path));
const cWFile = await cWrappedReadFile('./package.json');
// cWFile type is Buffer | undefined
We adhere to Semantic Versioning for this project.
For the versions available, see the versions on npm.
- kiki-kanri - kiki-kanri
MIT License © kiki-kanri