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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@js-toolkit/utils

v1.62.1

Published

Typescript helper types and utils

Readme

@js-toolkit/utils

npm package license

TypeScript utilities: helper functions, classes, FP data structures, and type helpers. Zero dependencies (optional @js-toolkit/node-utils for build minification).

Install

yarn add @js-toolkit/utils
# or
npm install @js-toolkit/utils

Import

Use subpath imports for tree-shaking:

import { EventEmitter } from '@js-toolkit/utils/EventEmitter';
import { debounce } from '@js-toolkit/utils/debounce';
import { getErrorMessage } from '@js-toolkit/utils/getErrorMessage';
import type { DeepPartial, RequiredStrict } from '@js-toolkit/utils/types';

API Overview

Classes and primitives

| Module | Description | |--------|-------------| | EventEmitter | Typed event emitter | | Mutex | Async mutex with acquire/release | | Queue | FIFO queue | | CacheMap | Map with getOrCreate / getOrQueue | | CancellablePromise | Promise with cancel support | | DataEventEmitter | Event emitter with typed data payloads | | Option, Try, Sink | FP data structures | | List | Immutable list (collections/immutable) |

Functions

| Module | Description | |--------|-------------| | noop | No-op function | | clear | Clear array in place | | debounce | Debounced function with isPending | | delay | Delayed execution with cancel | | delayed | Delayed promise | | getErrorMessage | Extract message from unknown error | | getAwaiter | Deferred promise (resolve/reject later) | | promisify | Wrap sync fn in Promise | | tryGet | Try thunk, return fallback on error | | hasIn, hasOwn | Safe property checks | | isEmptyObject | Check if object has no own keys | | getEnumName, getEnumValues | Enum helpers | | createDisposable | Create Symbol.dispose object | | memoizeAsync | Async memoization | | wait | Promise-based delay | | escapeRegExp | Escape regex special chars |

Types

| Type | Description | |------|-------------| | DeepPartial, DeepRequired | Recursive partial/required | | RequiredStrict, OptionalToUndefined | Strict optional handling | | Merge, Diff, OmitStrict | Object type utilities | | Keys, TupleToUnion, ArrayItem | Key and array helpers | | MapToKey, GettersToProps | Mapped types | | UnreachableCaseError | Exhaustive switch helper |

Decorators

| Module | Description | |--------|-------------| | @bind | Bind method to instance |

Logging

| Module | Description | |--------|-------------| | log | Logger with plugins (ConsolePlugin, LocalStoragePlugin) |

Usage Examples

EventEmitter

import { EventEmitter } from '@js-toolkit/utils/EventEmitter';

interface Events {
  data: [payload: string];
  error: [err: Error];
}

const emitter = new EventEmitter<Events>();
emitter.on('data', (payload: string) => console.log(payload));
emitter.emit('data', 'hello');

Mutex

import { Mutex } from '@js-toolkit/utils/Mutex';

const mutex = new Mutex<string>();
const release = await mutex.acquire('worker-1');
try {
  // critical section
} finally {
  release[Symbol.dispose]();
}

tryGet and getErrorMessage

import { tryGet } from '@js-toolkit/utils/tryGet';
import { getErrorMessage } from '@js-toolkit/utils/getErrorMessage';

const input = '{"key":"value"}'; // e.g. from API or user
const value = tryGet(() => JSON.parse(input) as Record<string, unknown>, null);
if (value === null) {
  // parse failed
}

try {
  await fetch('/api/data'); // or any async operation
} catch (err: unknown) {
  console.error(getErrorMessage(err));
}

Exhaustive switch with UnreachableCaseError

import { UnreachableCaseError } from '@js-toolkit/utils/UnreachableCaseError';

type Status = 'idle' | 'loading' | 'done';

function handle(status: Status): string {
  switch (status) {
    case 'idle':
      return 'idle';
    case 'loading':
      return 'loading';
    case 'done':
      return 'done';
    default:
      throw new UnreachableCaseError(status);
  }
}

Option (FP)

import { Option } from '@js-toolkit/utils/fp/Option';

const value: string | null = null; // or from API, form, etc.
const opt = Option.of(value);
if (opt.nonEmpty()) {
  console.log(opt.get());
}
const result = opt.getOrElse('default');

debounce

import { debounce } from '@js-toolkit/utils/debounce';

async function fetchResults(query: string): Promise<void> {
  /* fetch and render */
}

const debouncedSearch = debounce((query: string) => void fetchResults(query), 300);
debouncedSearch('foo');
if (debouncedSearch.isPending) {
  // call is queued
}
debouncedSearch.cancel();

getAwaiter

import { getAwaiter } from '@js-toolkit/utils/getAwaiter';

const awaiter = getAwaiter<number>();
setTimeout(() => awaiter.resolve(42), 1000);
const value = await awaiter.wait(2000); // 42

Types

import type { DeepPartial, RequiredStrict, Keys } from '@js-toolkit/utils/types';

interface Config {
  a: number;
  b?: string;
}
type PartialConfig = DeepPartial<Config>;
type StrictConfig = RequiredStrict<Config>;
type ConfigKeys = Keys<Config>; // 'a' | 'b'

@bind decorator

import { bind } from '@js-toolkit/utils/decorators/bind';

class Handler {
  @bind
  handleClick() {
    // this is always bound
  }
}

Testing

yarn test

Repository

https://github.com/js-toolkit/utils