npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kikiutils/wrappers

v1.1.2

Published

Utils function wrappers.

Downloads

69

Readme

@kikiutils/wrappers

npm version license

Table of contents

Description

This package provides some useful function wrappers.

Prerequisites

This package requires Node v16 or above.

To check your installed version, run the following command.

$ node -v
v20.5.1

Installation

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

Wrappers

All wrappers support passing in async or sync function.

tryAndGetBoolean

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

tryAndGetData

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

Notice

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

Versioning

We adhere to Semantic Versioning for this project.

For the versions available, see the versions on npm.

Authors

License

MIT License © kiki-kanri