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

@lyohhhh/utils

v0.2.21

Published

utils

Downloads

11

Readme

@lyohhhh/utils

轻量的 TypeScript 实用函数集合,支持 ESM/CJS 与类型声明。

安装命令(任选其一):

# pnpm
pnpm add @lyohhhh/utils -S

# npm
npm install @lyohhhh/utils --save

# yarn
yarn add @lyohhhh/utils

# bun
bun add @lyohhhh/utils

快速上手

// ESM
import { debounce, isEmpty } from '@lyohhhh/utils';

// CJS
// const { debounce, isEmpty } = require('@lyohhhh/utils');

const fn = debounce(() => console.log('hi'), 300);
console.log(isEmpty('')); // true

API

缓存 cache

提供通用缓存与 Web 缓存(localStorage)封装,支持过期时间。

import { createCache, createWebCache } from '@lyohhhh/utils';

// Web(浏览器)使用
const webCache = createWebCache('app', 1000); // key 前缀 + 过期毫秒
webCache.set('token', 'abc');
webCache.get('token'); // 'abc' 或过期后为 null
webCache.remove('token');
webCache.clear(); // 清空所有(委托给 localStorage.clear)

// 自定义存储实现(Node/服务端)
const store = new Map<string, string>();
const cache = createCache({
  key: 'ns',
  expire: 500,
  getItem: store.has,
  setItem: store.set,
  removeItem: store.delete,
  clear: store.clear,
});

插件 plugins

import { debounce, throttle } from '@lyohhhh/utils';

// 防抖(默认 leading=true)
const onResize = debounce(() => console.log('resize'), 300, true);

// 节流
const onScroll = throttle(() => console.log('scroll'), 200);

HTTP 客户端 http

基于 axios 的轻封装,提供默认配置、拦截器钩子、取消重复请求、失败自动重试、URL 前缀拼接等能力。

import createAxios, { Axios } from '@lyohhhh/utils';

// 创建实例(可覆盖默认项)
const request = createAxios({
  baseURL: 'https://api.example.com',
  timeout: 10000,
  requestOptions: {
    urlPrefix: '/v1',
    isReturnDefaultResponse: true, // 如需拿到完整 AxiosResponse
  },
  axiosHooks: {
    requestInterceptorsHook(config) {
      // 注入 token
      config.headers = {
        ...config.headers,
        Authorization: 'Bearer token',
      };
      return config;
    },
    responseInterceptorsHook(response) {
      // 可在此做统一错误码处理/数据提取
      return response;
    },
  },
});

// 发起请求
async function demo() {
  const res = await request.get({ url: '/users', params: { page: 1 } });
  console.log(res.data);
}

可用配置(节选):

  • requestOptions.urlPrefix: 接口前缀拼接(如 /api/v1
  • requestOptions.ignoreCancelToken: 是否忽略重复请求合并/取消
  • requestOptions.isOpenRetry / retryCount: 网络失败自动重试
  • axiosHooks.*: 请求/响应拦截、错误处理钩子

函数工具 func

若干常用函数工具:对象序列化、深合并、深拷贝、类型守卫等。

import {
  objectToQuery,
  merge,
  deepClone,
  isObject,
  isFunction,
} from '@lyohhhh/utils';

// 对象转查询串(会过滤 null/undefined/'')
objectToQuery({ a: 1, b: undefined, c: '', d: 0 }); // 'a=1&d=0'

// 深度合并(对象递归、数组覆盖、非对象忽略)
const a = { x: 1, y: { z: 2, arr: [1, 2] } };
const b = { y: { z: 3, arr: [3] }, k: 9 };
merge(a, b); // { x:1, y:{ z:3, arr:[3] }, k:9 }

// 深拷贝(含 Date/Array/循环引用)
const obj: any = { a: 1, b: { c: [1, 2] } };
obj.self = obj;
const cloned = deepClone(obj);

// 类型守卫
isObject({}); // true
isFunction(() => {}); // true

校验 validate

import {
  isExternal,
  isLinkHttp,
  isLinkTel,
  isLinkMailto,
  isEmpty,
  isValidPassword,
  isValidTransactionPassword,
} from '@lyohhhh/utils';

isExternal('mailto:[email protected]'); // true
isLinkHttp('https://a.com'); // true
isLinkTel('tel:123456'); // true
isLinkMailto('mailto:[email protected]'); // true
isEmpty(''); // true
isValidPassword('Abc123'); // true(6-16 位,至少两类字符,不能含中文/空白)
isValidTransactionPassword('123456'); // true(6 位纯数字)

导出与类型

  • ESM:dist/index.mjs,CJS:dist/index.cjs;类型:dist/index.d.ts
  • 支持按需导入:import { throttle } from '@lyohhhh/utils'