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

handy-js-utils

v1.0.0

Published

一个实用的JavaScript工具库,包含字符串、数组、对象、日期和数学处理的常用函数

Readme

Handy JS Utils

一个实用的JavaScript工具库,包含常用的字符串、数组、对象、日期和数学处理函数。

安装

npm install handy-js-utils

使用方法

CommonJS

const handyUtils = require('handy-js-utils');

// 使用字符串工具
console.log(handyUtils.string.capitalize('hello world')); // "Hello world"

ES6 模块

import handyUtils from 'handy-js-utils';

// 使用数组工具
console.log(handyUtils.array.unique([1, 2, 2, 3, 3, 4])); // [1, 2, 3, 4]

浏览器环境

<script src="path/to/handy-js-utils/index.js"></script>
<script>
  console.log(HandyUtils.string.capitalize('hello world'));
</script>

API 文档

字符串工具 (string)

capitalize(str)

将字符串首字母大写

  • 参数: str (string) - 输入字符串
  • 返回: (string) - 首字母大写的字符串
handyUtils.string.capitalize('hello world'); // "Hello world"

toCamelCase(str)

将字符串转换为驼峰命名格式

  • 参数: str (string) - 输入字符串
  • 返回: (string) - 驼峰命名格式的字符串
handyUtils.string.toCamelCase('hello-world-test'); // "helloWorldTest"
handyUtils.string.toCamelCase('hello_world_test'); // "helloWorldTest"

randomString(length)

生成随机字符串

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

数组工具 (array)

unique(arr)

数组去重

  • 参数: arr (Array) - 输入数组
  • 返回: (Array) - 去重后的数组
handyUtils.array.unique([1, 2, 2, 3, 3, 4]); // [1, 2, 3, 4]

chunk(arr, size)

将数组分块

  • 参数:
    • arr (Array) - 输入数组
    • size (number) - 每块大小,默认为1
  • 返回: (Array) - 分块后的二维数组
handyUtils.array.chunk([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]]

shuffle(arr)

随机打乱数组

  • 参数: arr (Array) - 输入数组
  • 返回: (Array) - 打乱后的数组
handyUtils.array.shuffle([1, 2, 3, 4, 5]); // [3, 1, 5, 2, 4] (随机结果)

对象工具 (object)

deepClone(obj)

深拷贝对象

  • 参数: obj (*) - 输入对象
  • 返回: (*) - 深拷贝后的对象
const original = { a: 1, b: { c: 2 } };
const cloned = handyUtils.object.deepClone(original);
cloned.b.c = 3;
console.log(original.b.c); // 2 (原对象未被修改)

get(obj, path, defaultValue)

获取对象深层属性值

  • 参数:
    • obj (Object) - 目标对象
    • path (string) - 属性路径,如 'a.b.c'
    • defaultValue (*) - 默认值,默认为undefined
  • 返回: (*) - 属性值或默认值
const obj = { a: { b: { c: 'value' } } };
handyUtils.object.get(obj, 'a.b.c'); // "value"
handyUtils.object.get(obj, 'a.b.d', 'default'); // "default"

isEmpty(obj)

检查对象是否为空

  • 参数: obj (Object) - 输入对象
  • 返回: (boolean) - 是否为空
handyUtils.object.isEmpty({}); // true
handyUtils.object.isEmpty([]); // true
handyUtils.object.isEmpty(''); // true
handyUtils.object.isEmpty({ a: 1 }); // false

日期工具 (date)

format(date, format)

格式化日期

  • 参数:
    • date (Date|string|number) - 日期
    • format (string) - 格式字符串,默认为 'YYYY-MM-DD HH:mm:ss'
  • 返回: (string) - 格式化后的日期字符串
handyUtils.date.format(new Date(), 'YYYY-MM-DD'); // "2023-12-01"
handyUtils.date.format(new Date(), 'YYYY年MM月DD日'); // "2023年12月01日"

timeAgo(date)

获取相对时间描述

  • 参数: date (Date|string|number) - 日期
  • 返回: (string) - 相对时间描述
const pastDate = new Date(Date.now() - 2 * 60 * 60 * 1000); // 2小时前
handyUtils.date.timeAgo(pastDate); // "2小时前"

数学工具 (math)

randomInt(min, max)

生成指定范围的随机整数

  • 参数:
    • min (number) - 最小值
    • max (number) - 最大值
  • 返回: (number) - 随机整数
handyUtils.math.randomInt(1, 10); // 1-10之间的随机整数

formatNumber(num)

数字格式化(添加千分位分隔符)

  • 参数: num (number) - 数字
  • 返回: (string) - 格式化后的字符串
handyUtils.math.formatNumber(1234567); // "1,234,567"

round(num, digits)

保留指定小数位数

  • 参数:
    • num (number) - 数字
    • digits (number) - 小数位数,默认为2
  • 返回: (number) - 处理后的数字
handyUtils.math.round(3.14159, 2); // 3.14
handyUtils.math.round(3.14159, 4); // 3.1416

示例

运行内置示例:

handyUtils.example();

或者直接运行:

npm test

许可证

MIT