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

utiljs-box

v1.0.2

Published

工具函数库utiljs-box 包含 tryJson、sleep、removeNull 等函数

Readme

utiljs-box

一个轻量级的 TypeScript 工具函数库,提供常用的实用工具,让开发更高效。

特性

  • ✅ TypeScript 编写,完整的类型定义
  • ✅ 支持 ESM 和 CommonJS 双格式
  • ✅ 按需加载,减小打包体积
  • ✅ 100% 测试覆盖率
  • ✅ 简洁易用的 API
  • ✅ 支持浏览器和 Node.js 环境

安装

# 使用 yarn
yarn add utiljs-box

# 使用 npm
npm install utiljs-box

# 使用 pnpm
pnpm add utiljs-box

使用

// ESM
import { tryJson, sleep, removeNull } from 'utiljs-box'

// CommonJS
const { tryJson, sleep, removeNull } = require('utiljs-box')

API 文档

1. tryJson

安全解析 JSON 字符串,避免解析失败抛出异常。

类型

<T>(str: string, defaultValue?: T): T | null

参数

  • str:要解析的 JSON 字符串
  • defaultValue:解析失败时返回的默认值,默认为 null

返回值

  • 解析成功返回解析后的对象
  • 解析失败返回默认值

示例

const { tryJson } = require('utiljs-box')

const jsonStr = '{"name": "张三", "age": 18}'
const invalidJson = '{"name": "张三", "age": 18,'

console.log(tryJson(jsonStr)) // { name: '张三', age: 18 }
console.log(tryJson(invalidJson)) // null
console.log(tryJson(invalidJson, { name: '默认名称' })) // { name: '默认名称' }

2. sleep

异步延迟函数,返回一个 Promise,用于在异步函数中等待指定时间。

类型

(ms: number): Promise<void>

参数

  • ms:延迟的毫秒数

返回值

  • Promise 对象

示例

const { sleep } = require('utiljs-box')

async function demo() {
  console.log('开始执行')
  await sleep(1000) // 延迟 1 秒
  console.log('1 秒后执行')
  await sleep(2000) // 延迟 2 秒
  console.log('总共延迟 3 秒')
}

demo()

3. removeNull

移除对象中所有值为 null 的属性,支持嵌套对象。

注意:数组中的 null 值不会被移除。

类型

(obj: any): any

参数

  • obj:要处理的对象

返回值

  • 移除 null 值后的新对象

示例

const { removeNull } = require('utiljs-box')

const data = {
  name: '张三',
  age: 18,
  address: null,
  contacts: {
    phone: '13800138000',
    email: null,
    wechat: 'zhangsan'
  },
  hobbies: ['篮球', null, '游泳', null, '编程'] // 数组中的 null 不会被移除
}

console.log(removeNull(data))
// {
//   name: '张三',
//   age: 18,
//   contacts: {
//     phone: '13800138000',
//     wechat: 'zhangsan'
//   },
//   hobbies: ['篮球', null, '游泳', null, '编程']
// }

示例

查看 examples 目录下的示例文件:

# 运行 JavaScript 示例
node examples/main.js

# 运行 TypeScript 示例
# 先安装 ts-node
# npm install -g ts-node
# ts-node examples/main.ts

开发

安装依赖

yarn install

运行测试

yarn test

构建

yarn build

开发模式

yarn dev

测试覆盖率

-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-------------------|---------|----------|---------|---------|-------------------
All files          |     100 |    93.75 |     100 |     100 | 
 sleep.ts          |     100 |      100 |     100 |     100 | 
 tryJson.ts        |     100 |      100 |     100 |     100 | 
 removeNull.ts     |     100 |    91.66 |     100 |     100 | 18
-------------------|---------|----------|---------|---------|-------------------

许可证

MIT

贡献

欢迎提交 Issue 和 Pull Request!

作者

hyc

仓库地址

https://github.com/hyc8801/utilbox