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

@stylobate/utils

v0.1.0

Published

通用工具函数集合:debounce/throttle、retry、storage、url、wait/sleep、result 类型等业务高频复用的轻量工具。

Readme

@stylobate/utils

业务高频复用的轻量工具集。零运行时依赖,全 ESM,tree-shakeable(sideEffects: false)。

安装

pnpm add @stylobate/utils

API 一览

| 模块 | 导出 | 用途 | | ----------- | ---------------------------------------- | ------------------------------------- | | 时序 | sleep, debounce, throttle | 异步等待、节流防抖(带 cancel/flush) | | 异步控制 | retry | 指数退避 + 抖动重试 | | Result 类型 | Result, ok, err, safe | 把异常变成数据 | | Storage | safeLocalStorage, safeSessionStorage | 自动 JSON、SSR 安全 | | URL | buildQuery, withQuery | 拼装/覆盖 query string |

示例

sleep

import { sleep } from '@stylobate/utils';

await sleep(300);
await sleep(1000, { signal: controller.signal }); // 可被中止

debounce / throttle

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

const onSearch = debounce((q: string) => fetch(`/api?q=${q}`), 200);
input.addEventListener('input', (e) => onSearch((e.target as HTMLInputElement).value));

// 立即执行最后一次:
onSearch.flush();
// 取消所有 pending:
onSearch.cancel();

retry

import { retry } from '@stylobate/utils';

const data = await retry(() => fetch('/api').then((r) => r.json()), {
  retries: 5,
  initialDelay: 300,
  shouldRetry: (e) => e instanceof TypeError, // 只在网络错误时重试
});

Result

import { safe } from '@stylobate/utils';

const r = await safe(() => api.fetchUser(id));
if (!r.ok) {
  logger.warn('fetchUser failed', r.error);
  return fallback;
}
return r.value;

Storage

import { safeLocalStorage } from '@stylobate/utils';

safeLocalStorage.set('settings', { theme: 'dark' });
const settings = safeLocalStorage.get<{ theme: string }>('settings');
// SSR 或浏览器禁用 storage 时返回 null,不抛错

URL

import { buildQuery, withQuery } from '@stylobate/utils';

buildQuery({ a: 1, b: undefined, c: ['x', 'y'] }); // 'a=1&c=x&c=y'
withQuery('/users?page=1', { page: 2, size: 20 }); // '/users?page=2&size=20'

设计原则

  • 零依赖:不引入 lodash、qs 等大块头。需要单点能力,优先在这里加,不在业务里造轮子
  • SSR 安全:所有访问 window/storage 的工具都做了 typeof 守卫
  • 可取消:异步工具尽量接受 AbortSignal
  • 小颗粒:每个文件一个职责,便于摇树