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 🙏

© 2025 – Pkg Stats / Ryan Hefner

naughty-util

v0.6.3

Published

Util functions for node.js

Readme

Naughty Util

license snyk npm version NPM Downloads NPM Downloads

Usage

  • Install: npm install naughty-util
  • Require: const utils = require('naughty-util')

Abstract utils

  • factorify<T, N, K extends keyof T>(dataset: T, nullable?: N): (key: K) => T[K] | N
  • factory<T extends { new(): any }>(Interface: T, ...params: ConstructorParameters<T>): () => InstanceType<T>

factorify helps remove tons of ifs and switches, also helps with strategies and commands.

const example = abstract.factorify({
  order: (user, id, message = "") => ({name: user.name, id, cancelled: false, message}),
  cancel: (user, id, message = "") => ({name: user.name, id, cancelled: true, message}),
  payments: (user, id, message = "") => ({name: user.name,}),
}, {user: null, id: 0});

const cancel = example("cancel");
const result = cancel({user: "John Doe", id: 5, message: "Service sucks"});
console.log(result); // {user: "John Doe", id: 5, cancelled: true, message: "Service sucks"}

const nullable = example("randomText");
const default = nullable({user: "John Doe", id: 5, message: "Test nullable"});
console.log(default); // {user: null, id: 0}

Array utils

  • valid(data: any): boolean
  • accessor<T>(array: Array<T>, meta: Record<string | symbol, number>): Array<T>

Access helps rid of unnamed values of the array.

const user = ["John Doe", 18, true];
const meta = {name: 0, age: 1, employed: 2};
array.accessor(user, meta);
console.log(user.name, user.age, user.employed); // "John Doe", 18, true

Async utils

  • promisify<F extends Callback>(fn: F): (...params: Parameters<F>) => any
  • compose<F extends CallbackAsync>(...fns: F[]): (...params: Parameters<F>) => Promise<any>
  • thenable<F extends Callback>(fn: F, ...params: Parameters<F>): Thenable<any>
  • pause(ms: number): Promise<void>
  • parallel<F extends CallbackAsync>(...fns: F[]): (...params: Parameters<F>) => Promise<any>

promisify allow to change callback last error first(errback) contract to Promise.

  const fsPromise = async.promisify(fs.readFile);
  const file = await fsPromise(__filename, "utf-8");
  console.log(file);

Buffer utils

  • random(length?: number): Thenable<Buffer>

Stream utils

  • read<T extends NodeJS.ReadableStream>(readable: T): Thenable<Buffer>

read helps with reading stream/readable, http socket for example.

  const handler = async(req, res) => {
    const body = await stream.read(req);
    console.log(body);
  };

number utils

  • safe(value: number): number
  • isSafe(value: number): boolean
  • positiveInt(value: number): boolean
  • cutFraction(value: number): number
  • total(dataset: number[]): number
  • average(dataset: number[]): number
  • percentRatio(amount: number, part: number): number
  • percentOf(base: number, percent: number): number

mixin utils

  • weakAssign<T extends object, M extends object>(target: T, mixin: M): M & T
  • forget<T extends object, K extends keyof T>(target: T, keys: K[]): any

date utils

  • verbalEpoch(input: string): number
  • verbal(input: string): number
  • unix(input: ConstructorParameters<DateConstructor>[0]): number
  • midnightUTC(input: ConstructorParameters<DateConstructor>[0]): number
  • midnight(input: ConstructorParameters<DateConstructor>[0]): number
  • difference(base: ConstructorParameters<DateConstructor>[0], target: ConstructorParameters<DateConstructor>[0]): number
  • reached(base: ConstructorParameters<DateConstructor>[0], target: ConstructorParameters<DateConstructor>[0]): boolean
  • DAY: number
  • HOUR: number
  • MINUTE: number
  • SECOND: number

HTTP utils

  • parseHost(host: string): string
  • parseCookies(cookie: string): Record<string, string>
  • createParams(params: string[][] | Record<string, string> | string | URLSearchParams): string
  • parseParams(params: string[][] | Record<string, string> | string | URLSearchParams): Record<string, string>
  • parseURL(pathname: string): URL

Misc utils

  • id<T>(entity: T): T
  • inRange<T extends string | number>(value: T, min: T, max: T): boolean
  • compose<F extends Callback>(...fns: F[]): (...params: Parameters<F>) => any
  • range(end: number, start?: number, step?: number): Generator<number>
  • partial<F extends Callback>(fn: F, ...params: Partial<Parameters<F>>): (...params: any) => any
  • projection<T extends [string, string | T, Callback], O extends object>(meta: T[], data: O): any

cache

  • cache({ ms, max }?: { ms?: number, max?: number }) => Set & Cache
  const store = cache({ max: 3, ms: 5000 });
  store("key", {data: 1});
  store("buffer", Buffer.from("Hello"));
  store("api-return", await someApi.get("something"));

  const data = store.get("key"); // {data: 1}

  store.limit(10); // max from 3 to 10;
  store.timeout(1000); // ms from 5000 to 1000

Part of the naughty stack