js-utils-toolkit-helper
v1.0.1
Published
A collection of useful JavaScript utility functions for common programming tasks
Maintainers
Readme
JavaScript Utility Toolkit
一个功能丰富的JavaScript工具函数库,提供日常开发中常用的工具函数。
安装
npm install js-utils-toolkit使用方法
ES6 模块导入
import { stringUtils, arrayUtils, objectUtils, validationUtils, dateUtils, mathUtils } from 'js-utils-toolkit';
// 或者导入默认导出
import utils from 'js-utils-toolkit';CommonJS 导入
const { stringUtils, arrayUtils, objectUtils, validationUtils, dateUtils, mathUtils } = require('js-utils-toolkit');功能特性
1. 字符串工具 (stringUtils)
import { stringUtils } from 'js-utils-toolkit';
// 驼峰命名
console.log(stringUtils.toCamelCase('hello-world')); // 'helloWorld'
// 帕斯卡命名
console.log(stringUtils.toPascalCase('hello-world')); // 'HelloWorld'
// 蛇形命名
console.log(stringUtils.toSnakeCase('helloWorld')); // 'hello_world'
// 字符串截断
console.log(stringUtils.truncate('这是一个很长的字符串', 5)); // '这是一...'2. 数组工具 (arrayUtils)
import { arrayUtils } from 'js-utils-toolkit';
// 数组去重
console.log(arrayUtils.unique([1, 2, 2, 3, 3, 3])); // [1, 2, 3]
// 数组分块
console.log(arrayUtils.chunk([1, 2, 3, 4, 5], 2)); // [[1, 2], [3, 4], [5]]
// 数组扁平化
console.log(arrayUtils.flatten([1, [2, [3, 4]], 5])); // [1, 2, 3, 4, 5]
// 数组随机排序
console.log(arrayUtils.shuffle([1, 2, 3, 4, 5])); // 随机排序的数组3. 对象工具 (objectUtils)
import { objectUtils } from 'js-utils-toolkit';
const obj = { a: 1, b: { c: 2 }, d: [3, 4] };
// 深度克隆
const cloned = objectUtils.deepClone(obj);
// 对象合并
const merged = objectUtils.merge({ a: 1 }, { b: 2 }, { c: 3 });
// 选取属性
const picked = objectUtils.pick({ a: 1, b: 2, c: 3 }, ['a', 'b']);
// 排除属性
const omitted = objectUtils.omit({ a: 1, b: 2, c: 3 }, ['a']);4. 验证工具 (validationUtils)
import { validationUtils } from 'js-utils-toolkit';
// 邮箱验证
console.log(validationUtils.isEmail('[email protected]')); // true
// 手机号验证(中国)
console.log(validationUtils.isPhone('13800138000')); // true
// 身份证验证(中国)
console.log(validationUtils.isIdCard('110101199001011234')); // true
// URL验证
console.log(validationUtils.isUrl('https://example.com')); // true5. 日期工具 (dateUtils)
import { dateUtils } from 'js-utils-toolkit';
const date = new Date('2023-01-01');
// 日期格式化
console.log(dateUtils.formatDate(date, 'YYYY-MM-DD HH:mm:ss'));
// 相对时间
const pastDate = new Date(Date.now() - 30 * 60 * 1000); // 30分钟前
console.log(dateUtils.getRelativeTime(pastDate)); // '30分钟前'6. 数学工具 (mathUtils)
import { mathUtils } from 'js-utils-toolkit';
// 随机整数
console.log(mathUtils.randomInt(1, 100)); // 1-100之间的随机整数
// 数值限制
console.log(mathUtils.clamp(150, 0, 100)); // 100
// 数字格式化
console.log(mathUtils.formatNumber(1234567)); // '1,234,567'API 文档
stringUtils
toCamelCase(str)- 转换为驼峰命名toPascalCase(str)- 转换为帕斯卡命名toSnakeCase(str)- 转换为蛇形命名truncate(str, length, suffix)- 字符串截断
arrayUtils
unique(arr)- 数组去重chunk(arr, size)- 数组分块flatten(arr, depth)- 数组扁平化shuffle(arr)- 数组随机排序
objectUtils
deepClone(obj)- 深度克隆对象merge(...objects)- 合并多个对象pick(obj, keys)- 选取指定属性omit(obj, keys)- 排除指定属性
validationUtils
isEmail(email)- 邮箱验证isPhone(phone)- 手机号验证(中国)isIdCard(idCard)- 身份证验证(中国)isUrl(url)- URL验证
dateUtils
formatDate(date, format)- 日期格式化getRelativeTime(date)- 相对时间
mathUtils
randomInt(min, max)- 随机整数clamp(value, min, max)- 数值限制formatNumber(num)- 数字格式化
浏览器支持
- Chrome 60+
- Firefox 55+
- Safari 12+
- Edge 79+
开发
# 克隆项目
git clone https://github.com/HrCode-w/npm1.git
# 安装依赖
npm install
# 运行测试
npm test贡献
欢迎提交 Issue 和 Pull Request!
许可证
MIT License
更新日志
v1.0.0
- 初始版本发布
- 包含6大类工具函数
- 支持ES6模块和CommonJS
