A collection of useful JavaScript utility functions for common programming tasks
npm install js-utils-toolkit-helper一个功能丰富的JavaScript工具函数库,提供日常开发中常用的工具函数。
``bash`
npm install js-utils-toolkit
`javascript
import { stringUtils, arrayUtils, objectUtils, validationUtils, dateUtils, mathUtils } from 'js-utils-toolkit';
// 或者导入默认导出
import utils from 'js-utils-toolkit';
`
`javascript`
const { stringUtils, arrayUtils, objectUtils, validationUtils, dateUtils, mathUtils } = require('js-utils-toolkit');
`javascript
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)); // '这是一...'
`
`javascript
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])); // 随机排序的数组
`
`javascript
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']);
`
`javascript
import { validationUtils } from 'js-utils-toolkit';
// 邮箱验证
console.log(validationUtils.isEmail('test@example.com')); // true
// 手机号验证(中国)
console.log(validationUtils.isPhone('13800138000')); // true
// 身份证验证(中国)
console.log(validationUtils.isIdCard('110101199001011234')); // true
// URL验证
console.log(validationUtils.isUrl('https://example.com')); // true
`
`javascript
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分钟前'
`
`javascript
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'
`
- 转换为驼峰命名
- toPascalCase(str) - 转换为帕斯卡命名
- toSnakeCase(str) - 转换为蛇形命名
- truncate(str, length, suffix) - 字符串截断$3
- unique(arr) - 数组去重
- chunk(arr, size) - 数组分块
- flatten(arr, depth) - 数组扁平化
- shuffle(arr) - 数组随机排序$3
- deepClone(obj) - 深度克隆对象
- merge(...objects) - 合并多个对象
- pick(obj, keys) - 选取指定属性
- omit(obj, keys) - 排除指定属性$3
- isEmail(email) - 邮箱验证
- isPhone(phone) - 手机号验证(中国)
- isIdCard(idCard) - 身份证验证(中国)
- isUrl(url) - URL验证$3
- formatDate(date, format) - 日期格式化
- getRelativeTime(date) - 相对时间$3
- randomInt(min, max) - 随机整数
- clamp(value, min, max) - 数值限制
- formatNumber(num) - 数字格式化浏览器支持
- Chrome 60+
- Firefox 55+
- Safari 12+
- Edge 79+
开发
`bash
克隆项目
git clone https://github.com/yourusername/js-utils-toolkit.git安装依赖
npm install运行测试
npm test
``欢迎提交 Issue 和 Pull Request!
MIT License