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

@hsinyau/utils

v0.1.7

Published

My utils library

Readme

@hsinyau/utils

一个现代化的自用 TypeScript 工具函数库。

✨ 特性

  • 🚀 现代化: 使用 TypeScript 编写,支持 ESM
  • 📦 轻量级: 零依赖(除了 React 相关工具)
  • 🎯 类型安全: 完整的 TypeScript 类型定义
  • 🧪 测试覆盖: 使用 Vitest 进行单元测试
  • 📚 文档完善: 详细的 JSDoc 注释和使用示例

📦 安装

npm install @hsinyau/utils
# 或
yarn add @hsinyau/utils
# 或
pnpm add @hsinyau/utils

🚀 快速开始

import { camelize, cn, timeAgo } from '@hsinyau/utils'

// 字符串处理
camelize('foo-bar') // 'fooBar'

// 时间处理
timeAgo(new Date('2024-01-01')) // '3个月前'

// React 工具
cn('text-red-500', 'bg-blue-500') // 合并 Tailwind CSS 类名

📚 API 文档

字符串工具 (String Utils)

camelize(str: string): string

将下划线或连字符分隔的字符串转换为驼峰命名。

import { camelize } from '@hsinyau/utils'

camelize('foo-bar') // 'fooBar'
camelize('foo_bar') // 'fooBar'
camelize('fooBar') // 'fooBar'

dasherize(str: string): string

将驼峰命名转换为连字符分隔的形式。

import { dasherize } from '@hsinyau/utils'

dasherize('fooBar') // 'foo-bar'
dasherize('fooBarBaz') // 'foo-bar-baz'

underscored(str: string): string

将驼峰命名转换为下划线分隔的形式。

import { underscored } from '@hsinyau/utils'

underscored('fooBar') // 'foo_bar'
underscored('fooBarBaz') // 'foo_bar_baz'

random(size?: number, dict?: string): string

生成指定长度的随机字符串。

import { random } from '@hsinyau/utils'

random(16) // '9aXMo5NadcPvfA1b'
random(8, 'abc_-123') // 'c_13-2ac'

ensurePrefix(prefix: string, str: string): string

确保字符串以指定前缀开头。

import { ensurePrefix } from '@hsinyau/utils'

ensurePrefix('http://', 'www.google.com') // 'http://www.google.com'
ensurePrefix('https://', 'https://www.google.com') // 'https://www.google.com'

ensureSuffix(suffix: string, str: string): string

确保字符串以指定后缀结尾。

import { ensureSuffix } from '@hsinyau/utils'

ensureSuffix('.png', 'example') // 'example.png'
ensureSuffix('.png', 'example.png') // 'example.png'

padZero(num: number, length: number, position?: 'start' | 'end'): string

用零填充数字到指定长度。

import { padZero } from '@hsinyau/utils'

padZero(13579, 10) // '0000013579'
padZero(13579, 8, 'end') // '13579000'

slash(str: string): string

将反斜杠转换为正斜杠。

import { slash } from '@hsinyau/utils'

slash('\\a\\b\\c') // '/a/b/c'

时间工具 (Time Utils)

timeAgo(time: number | Date | string, currentTime?: number | Date | string): string

将时间转换为相对时间字符串(如"3分钟前")。

import { timeAgo } from '@hsinyau/utils'

timeAgo(new Date('2024-01-01')) // '3个月前'
timeAgo(Date.now() - 60000) // '1分钟前'
timeAgo(Date.now() + 3600000) // '1小时后'

timestamp(): number

获取当前时间戳。

import { timestamp } from '@hsinyau/utils'

timestamp() // 1718332800000

isValidDate(date: any): boolean

检查是否为有效的日期。

import { isValidDate } from '@hsinyau/utils'

isValidDate(new Date()) // true
isValidDate('invalid') // false

isValidTimestamp(timestamp: any): boolean

检查是否为有效的时间戳。

import { isValidTimestamp } from '@hsinyau/utils'

isValidTimestamp(Date.now()) // true
isValidTimestamp('invalid') // false

normalizeTime(time: any, options?: NormalizeTimeOptions): string | number | Date

标准化时间格式。

import { normalizeTime } from '@hsinyau/utils'

normalizeTime('2024-01-01', { format: 'timestamp' }) // 1704067200000
normalizeTime(1704067200000, { format: 'date' }) // '2024-01-01T00:00:00.000Z'

React 工具 (React Utils)

cn(...inputs: ClassValue[]): string

合并 CSS 类名,支持 Tailwind CSS 类名去重。

import { cn } from '@hsinyau/utils'

cn('text-red-500', 'bg-blue-500') // 'text-red-500 bg-blue-500'
cn('text-red-500', 'text-blue-500') // 'text-blue-500' (后面的覆盖前面的)

🛠️ 开发

环境要求

  • Node.js >= 20.18.0
  • pnpm >= 10.11.0

安装依赖

pnpm install

开发命令

# 构建
pnpm build

# 开发模式(监听文件变化)
pnpm dev

# 运行测试
pnpm test

# 类型检查
pnpm typecheck

# 代码检查
pnpm lint

# 代码检查并自动修复
pnpm lint:fix

发布

pnpm release

📄 许可证

MIT License - 详见 LICENSE 文件