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

@pngwn/utils

v0.1.0

Published

Simple utility functions for when you need a simple utility function.

Downloads

4

Readme

utils

Simple utility functions for when you need a simple utility function. Tree shakeable, TS definitions and stuff.

They don't handle all edge cases but are mostly sufficient. Tests will give tell you what each function is supposed to cover. I add them as I need them.

Install

yarn add @pngwn/utils
# or
npm i @pngwn/utils

functions

  • debounce - squish multiple function calls into one
  • merge - deep merge for objects.
  • once - call a function only once.
  • throttle - call a fuction nor more than every x ms

debounce

debounce(callbackFn, waitTime = 0, callImmediately = false)

Squishes multiple function calls into one if those calls are within n milliseconds. Accepts 3 arguments: a function, a time in milliseconds and a boolean. The boolean dictates whether or not the function should be called immediately on first invocation. Only the Only the first argument(callbackFn) is required. Returns a function.

import { debounce } from '@pngwn/utils';

let count = 0;
const inc = debounce(() => count++, 50, true);

inc(); // count === 1
inc(); // count === 1
inc(); // count === 1
// wait 50ms: `count === 2`

const error = debounce(undefined);
// Error: Expected a function, instead got: undefined

merge

merge(obj1, obj2, obj3, ...)

Deep merge for objects (not arrays). Can take many objects as parameters. Concatenates arrays, treats Date and RegExp as primitives. Immutable. Returns an object.

import { merge } from '@pngwn/utils';

const obj1 = {
  key1: /abc/,
  key2: [1, 2, 3]
  key3 : { key4: 'hello' }
};

const obj2 = {
  key1: /efg/,
  key2: [4, 5, 6]
  key3: { key5: 'bye' }
};

const obj3  { key99: 'i\'m extra' }

// when merges are not possible: `obj3` overwrites `obj2` which overwrites `obj1`
const merged = merge(obj1, obj2, obj3);

merged === {
  key1: /efg/,
  key2: [1, 2, 3, 4, 5, 6]
  key3: { key4: 'hello', key5: 'bye' },
  key99: 'i\'m extra'
};

// check the tests for more examples/ information on what it can do

once

once(callbackFn)

Makes a function callable only once. Accepts a function as an argument. Subsequent calls to the function return the initial value which is stored after the first invocation. Returns a function.

import { once } from '@pngwn/utils';

let count = 0;
const inc = once(() => count++);
inc(); // count === 1
inc(); // count === 1

const upper = once(str => str.toUpperCase());
const firstCall = upper('hello'); // 'HELLO'
const secondCall = upper('bejeesus'); // 'HELLO'

const error = once(undefined);
// Error: 'Expected a function, instead got: undefined'

throttle

throttle(callbackFn, interval, callImmediately)`

Ensures a function is called no more than every n ms. Accepts 3 arguments: a function, a time in milliseconds and a boolean. The boolean dictates whether or not the function should be called immediately on first invocation. Only the Only the first argument(callbackFn) is required. Returns a function.

import { throttle } from '@pngwn/utils';

let count = 0;
const inc = throttle(() => count++, 50, true);

// the boolean casued the function to be called immediately, then the timer starts
// omitting it will cause the function to fire at the end of the timer

inc(); // count === 1
inc(); // count === 1
inc(); // count === 1

const error = throttle(undefined);
// Error: Expected a function, instead got: undefined