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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@hhfe/utils

v0.1.3

Published

通用工具函数库

Readme

@hhfe/utils

一个功能完整的通用工具函数库,提供日期、对象、字符串、验证等常用工具函数。

安装

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

使用

import { formatDate, capitalize, isEmail } from '@hhfe/utils';

// 使用日期工具函数
const formattedDate = formatDate(new Date()); // "2023-01-01 12:00:00"

// 使用字符串工具函数
const text = capitalize('hello world'); // "Hello world"

// 使用验证工具函数
const valid = isEmail('[email protected]'); // true

API 文档

日期工具 (Date)

formatDate(date, format?)

格式化日期

  • 参数:
    • date: Date | string | number - 日期对象或日期字符串或时间戳
    • format: string - 格式化模板,默认为 YYYY-MM-DD HH:mm:ss
  • 返回: string - 格式化后的日期字符串
formatDate(new Date()); // "2023-01-01 12:00:00"
formatDate(Date.now(), 'YYYY年MM月DD日'); // "2023年01月01日"

getRelativeTime(date, baseDate?)

获取相对时间

  • 参数:
    • date: Date | string | number - 目标日期
    • baseDate: Date | string | number - 基准日期,默认为当前时间
  • 返回: string - 相对时间字符串
getRelativeTime(new Date(Date.now() - 60 * 1000)); // "1分钟前"
getRelativeTime(new Date(Date.now() - 3600 * 1000)); // "1小时前"

getDateRange(type)

获取日期范围

  • 参数:
    • type: 'today' | 'yesterday' | 'week' | 'month' | 'year' - 范围类型
  • 返回: [Date, Date] - 日期范围数组
const [start, end] = getDateRange('today'); // 今天的开始和结束时间
const [weekStart, weekEnd] = getDateRange('week'); // 本周的开始和结束时间

对象工具 (Object)

deepClone(obj)

深拷贝对象

  • 参数:
    • obj: T - 要拷贝的对象
  • 返回: T - 拷贝后的对象
const original = { a: 1, b: { c: 2 } };
const cloned = deepClone(original);

deepMerge(target, ...sources)

合并对象

  • 参数:
    • target: T - 目标对象
    • sources: S[] - 源对象数组
  • 返回: T - 合并后的对象
const merged = deepMerge({ a: 1 }, { b: 2 }, { c: 3 });
// { a: 1, b: 2, c: 3 }

removeProps(obj, keys)

移除对象中的指定属性

  • 参数:
    • obj: T - 目标对象
    • keys: K[] - 要移除的属性名数组
  • 返回: Omit<T, K> - 移除属性后的对象
const result = removeProps({ a: 1, b: 2, c: 3 }, ['b', 'c']);
// { a: 1 }

pickProps(obj, keys)

提取对象中的指定属性

  • 参数:
    • obj: T - 目标对象
    • keys: K[] - 要提取的属性名数组
  • 返回: Pick<T, K> - 提取属性后的对象
const result = pickProps({ a: 1, b: 2, c: 3 }, ['a', 'c']);
// { a: 1, c: 3 }

isEmpty(obj)

判断对象是否为空

  • 参数:
    • obj: unknown - 要判断的对象
  • 返回: boolean - 是否为空
isEmpty(null); // true
isEmpty(''); // true
isEmpty([]); // true
isEmpty({}); // true
isEmpty({ a: 1 }); // false

字符串工具 (String)

capitalize(str)

首字母大写

  • 参数:
    • str: string - 要处理的字符串
  • 返回: string - 首字母大写后的字符串
capitalize('hello world'); // "Hello world"

camelToKebab(str)

驼峰转连字符

  • 参数:
    • str: string - 要处理的字符串
  • 返回: string - 转换后的字符串
camelToKebab('backgroundColor'); // "background-color"

kebabToCamel(str)

连字符转驼峰

  • 参数:
    • str: string - 要处理的字符串
  • 返回: string - 转换后的字符串
kebabToCamel('background-color'); // "backgroundColor"

truncate(str, maxLength, suffix?)

截取字符串

  • 参数:
    • str: string - 要处理的字符串
    • maxLength: number - 最大长度
    • suffix: string - 后缀,默认为 ...
  • 返回: string - 截取后的字符串
truncate('Hello world', 5); // "Hello..."
truncate('Hello world', 5, '...'); // "Hello..."

randomString(length?)

生成随机字符串

  • 参数:
    • length: number - 长度,默认为 16
  • 返回: string - 随机字符串
randomString(); // "A1b2C3d4E5f6G7h8"
randomString(8); // "Ab1Cd2Ef"

验证工具 (Validate)

isMobile(value)

验证手机号

  • 参数:
    • value: string - 要验证的值
  • 返回: boolean - 是否通过验证
isMobile('13812345678'); // true
isMobile('123456789'); // false

isEmail(value)

验证邮箱

  • 参数:
    • value: string - 要验证的值
  • 返回: boolean - 是否通过验证
isEmail('[email protected]'); // true
isEmail('invalid-email'); // false

isUrl(value)

验证URL

  • 参数:
    • value: string - 要验证的值
  • 返回: boolean - 是否通过验证
isUrl('https://example.com'); // true
isUrl('not-a-url'); // false

isIdCard(value)

验证身份证号

  • 参数:
    • value: string - 要验证的值
  • 返回: boolean - 是否通过验证
isIdCard('110101199001011234'); // true
isIdCard('123456'); // false

isIp(value)

验证IP地址

  • 参数:
    • value: string - 要验证的值
  • 返回: boolean - 是否通过验证
isIp('192.168.1.1'); // true
isIp('999.999.999.999'); // false

isNumber(value)

验证是否为数字

  • 参数:
    • value: unknown - 要验证的值
  • 返回: boolean - 是否通过验证
isNumber(123); // true
isNumber('123'); // false
isNumber(NaN); // false

isInteger(value)

验证是否为整数

  • 参数:
    • value: unknown - 要验证的值
  • 返回: boolean - 是否通过验证
isInteger(123); // true
isInteger(123.45); // false
isInteger('123'); // false

依赖

  • dayjs - 轻量级日期处理库
  • lodash-es - 现代 JavaScript 实用程序库

开发

# 安装依赖
pnpm install

# 构建
pnpm build

# 类型检查
pnpm type-check

许可证

MIT