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

utility-kit

v0.5.0

Published

An easy to use library that provides miscellaneous but very useful functions that are commonly used in most javascript projects.

Downloads

24

Readme

utility-kit

An easy to use library that provides miscellaneous but very useful functions that are commonly used in most javascript projects.

Features

  • Generate random number
  • Pick random element from array
  • Generate random name
  • Generate OTP
  • Probability
  • Minimum/Maximum element of an array
  • Wait/Sleep function
  • Retry wrapper with incremental delay

Installation

To install utility-kit

  # with npm:
  npm install utility-kit --save

  # with yarn:
  yarn add utility-kit

  # with pnpm:
  pnpm add utility-kit

  # with bun:
  bun add utility-kit

Usage

utility-kit can be used in almost any javascript project to achieve the above mentioned features with no extra effort. The functions provided by utility-kit may be categorised into 4 categories:

Random

import { random, randomNumber, randomElement, randomAdjective, randomAnimal, randomName, generateOTP } from "utility-kit";

console.log(random()); // A drop-in and secure replacement for Math.random(), offering truly random numbers generated using cryptographic sources.

console.log(random(16)); // Generates a truly random number using 16 bytes, providing a wider range and higher precision. The number of bytes ranges from 1-127 with 8 being default.

console.log(randomNumber(10, 20)); // Any number between 10 and 20 (both numbers included) will be logged

console.log(randomElement([3, 2, 10, 7, 8])); // Any one random element of the array will be logged

console.log(randomAdjective()); // Any one adjective from a list of 25 adjective will be logged

console.log(randomAnimal()); // Any one animal from a list of 200 animals will be logged

console.log(randomName("-")); // A combination of randomAdjective() and randomAnimal() will be logged with a "-" separator in between. Default separator is " "

console.log(generateOTP(6)); // A 6 digit string OTP will be logged

Math

import { probability, minimumNumber, maximumNumber } from "utility-kit";

if (probability(0.5)) console.log(true); // There is a 50% chance that true will be logged

const array = [89, 6, 99, 2, 50, 10];
console.log(minimumNumber(array)); // 2
console.log(maximumNumber(array)); // 99

Time

import { wait } from "utility-kit";

async function test(time) {
  const ref = Date.now();
  await wait(time);
  return Date.now() - ref;
}

test(1000).then((time) => console.log(time)); // ~1000

Utility

import { probability, retry, retryAsync } from "utility-kit";

// Some error prone callback
function errorProne() {
  if (probability(0.5)) {
    console.log("fail");
    throw new Error("Something went wrong");
  }
  console.log("success");
  return "hurray";
}
retry(errorProne); // "fail" will be logged at most 3 (default) times until "success" is logged
retry(errorProne, { retries: 8 }); // "fail" will be logged at most 8 times until "success" is logged
retry(errorProne, {
  // onSuccess is a function with a single parameter which will have the value that is returned by the callback function and will be invoked only in case on success. Here, "hurray" will also be logged after "success".
  onSuccess: (result) => console.log(result),
  // onError is a function with a single parameter which will contain the information about error that occured in callback function and will be invoked in case of failure. Here, error will be "Something went wrong"
  onError: (error) => console.log(error),
});

// For functions with non-zero parameters
function readChunk(file, chunkNumber);
const chunk = retry(() => readChunk(file, 2));
console.log(chunk); // either 2nd chunk of file in case of success or undefined in case of failure

// For handling asynchronous callbacks or retrying after some incremental delay
async function uploadFile(file); // A function that uploads a file asynchronously, resolving to the file link on success or rejecting on failure.
const link = await retryAsync(async () => await uploadFile(file), { retries: 4 });
console.log(link); // link to the uploaded file if file upload successful in any of the 5(1+4) tries or undefined in case of failure

// In below example, if file upload fails, then retryAsync will wait for 1000ms (initialDelay) before retrying to upload the file and this wait will increase by 500ms (delayIncrement) for every next retry.
await retryAsync(async () => await uploadFile(file), {
  initialDelay: 1000, // In ms, default is 0
  delayIncement: 500, // In ms, default is 0
  onSuccess: console.log,
  onError: console.log,
});

Author

Sahil Aggarwal