@zpeak/utils
v1.0.13
Published
@zpeak/utils 工具函数库,提供常用的工具函数,包括文件操作、字符串处理、数组处理、对象处理、日期处理、数字处理、网络工具等。
Maintainers
Readme
@zpeak/utils
zpeak 工具函数库,提供常用的工具函数,包括文件操作、字符串处理、数组处理、对象处理、日期处理、数字处理、网络工具等。
安装
# 使用 npm
npm install @zpeak/utils
# 使用 yarn
yarn add @zpeak/utils
# 使用 pnpm
pnpm add @zpeak/utils目录结构
src/
├── file/ # 文件操作相关工具函数
├── string/ # 字符串处理相关工具函数
├── array/ # 数组处理相关工具函数
├── object/ # 对象处理相关工具函数
├── date/ # 日期处理相关工具函数
├── number/ # 数字处理相关工具函数
├── network/ # 网络相关工具函数
├── common/ # 通用工具函数
└── index.ts # 主入口文件,导出所有工具函数功能模块
file 模块
copyToTarget(source, dest): 将目录复制到另一个目录
string 模块
capitalize(str): 首字母大写camelToKebab(str): 驼峰转连字符
array 模块
unique(arr): 数组去重groupBy(arr, key): 数组分组
object 模块
deepClone(obj): 对象深拷贝merge(target, ...sources): 合并对象
date 模块
formatDate(date, format): 格式化日期
number 模块
formatNumber(num, decimals): 数字格式化clamp(num, min, max): 数字范围限制
network 模块
isValidUrl(url): 检查 URL 是否有效
common 模块
generateId(length): 生成随机 ID
使用示例
导入所有工具函数
import * as utils from '@zpeak/utils';
// 使用文件操作工具
await utils.copyToTarget('./source', './dest');
// 使用字符串工具
const capitalized = utils.capitalize('hello');
const kebabCase = utils.camelToKebab('helloWorld');
// 使用数组工具
const uniqueArray = utils.unique([1, 2, 2, 3]);
const grouped = utils.groupBy([{ age: 18 }, { age: 20 }], item => item.age);
// 使用对象工具
const cloned = utils.deepClone({ a: 1, b: { c: 2 } });
const merged = utils.merge({ a: 1 }, { b: 2 });
// 使用日期工具
const formattedDate = utils.formatDate(new Date());
// 使用数字工具
const formattedNumber = utils.formatNumber(123.456);
const clamped = utils.clamp(5, 1, 10);
// 使用网络工具
const isValid = utils.isValidUrl('https://example.com');
// 使用通用工具
const id = utils.generateId();按需导入
import { copyToTarget } from '@zpeak/utils';
import { capitalize, camelToKebab } from '@zpeak/utils';
import { unique, groupBy } from '@zpeak/utils';
import { deepClone, merge } from '@zpeak/utils';
import { formatDate } from '@zpeak/utils';
import { formatNumber, clamp } from '@zpeak/utils';
import { isValidUrl } from '@zpeak/utils';
import { generateId } from '@zpeak/utils';构建
# 使用 tsc 构建
pnpm build
# 使用 tsup 构建
pnpm build:tsup测试
pnpm test许可证
ISC
