一个轻量级的 JavaScript 工具函数库,提供字符串、数组、日期、DOM、验证等常用工具函数。
- 🚀 轻量级,无依赖
- 📦 支持 ES Module 和 CommonJS
- 🔧 提供完整的 TypeScript 类型定义
- 💪 包含多种常用工具函数
- 🎯 模块化设计,按需引入
npm install su-js-utilsyarn add su-js-utilspnpm add su-js-utilsimport suJsUtils from "su-js-utils";
// 使用字符串工具
suJsUtils.string.capitalize("hello"); // => "Hello"
// 使用数组工具
suJsUtils.array.unique([1, 1, 2]); // => [1, 2]
// 使用日期工具
suJsUtils.date.format(new Date(), "YYYY-MM-DD"); // => "2024-03-15"import { string, array, date, dom, verify } from "su-js-utils";
string.capitalize("hello"); // => "Hello"
array.unique([1, 1, 2]); // => [1, 2]
date.format(new Date(), "YYYY-MM-DD"); // => "2024-03-15"import { capitalize, unique, format } from "su-js-utils";
capitalize("hello"); // => "Hello"
unique([1, 1, 2]); // => [1, 2]
format(new Date()); // => "2024-03-15"import { string } from "su-js-utils";
// 首字母大写
string.capitalize("hello"); // => "Hello"
// 驼峰转短横线
string.camelToKebab("helloWorld"); // => "hello-world"
// 短横线转驼峰
string.kebabToCamel("hello-world"); // => "helloWorld"
// 截断字符串
string.truncate("Hello World", 8); // => "Hello..."
// 移除两端字符
string.trim(" hello "); // => "hello"import { array } from "su-js-utils";
// 安全获取元素
array.get([1, 2, 3], 1); // => 2
// 去重
array.unique([1, 1, 2, 2, 3]); // => [1, 2, 3]
// 分组
array.chunk([1, 2, 3, 4], 2); // => [[1, 2], [3, 4]]
// 扁平化
array.flatten([1, [2, [3, 4]], 5]); // => [1, 2, [3, 4], 5]import { date } from "su-js-utils";
// 格式化日期
date.format(new Date(), "YYYY-MM-DD"); // => "2024-03-15"
// 相对时间
date.relativeTime("2024-03-14"); // => "1天前"
// 判断闰年
date.isLeapYear("2024"); // => true
// 获取月份天数
date.getDaysInMonth("2024-02"); // => 29
// 判断周末
date.isWeekend("2024-03-16"); // => trueimport { dom } from "su-js-utils";
// 选择器
const button = dom.selector.get("#myButton");
const items = dom.selector.getAll(".item");
// 类名操作
dom.className.add(button, "active");
dom.className.toggle(button, "disabled");
// 样式操作
dom.style.set(button, { backgroundColor: "#fff" });
dom.style.show(button);
// 事件操作
dom.event.on(button, "click", () => {});
dom.event.once(button, "click", () => {});
// 动画操作
await dom.animation.fadeIn(element);
await dom.animation.slideDown(element);import { verify } from "su-js-utils";
// 字符串验证
verify.string.isEmail("test@example.com"); // => true
verify.string.isPhone("13812345678"); // => true
// 密码验证
verify.string.validatePassword("Password123!", {
minLength: 8,
requireNumber: true,
requireSpecialChar: true,
});
// 数字验证
verify.number.isInteger(123); // => true
verify.number.inRange(5, 1, 10); // => true
// 日期验证
verify.date.isFuture(new Date()); // => false
verify.date.inRange(date, start, end); // => true- Chrome >= 49
- Firefox >= 45
- Safari >= 10
- Edge >= 14
- IE >= 11 (需要 polyfill)
# 安装依赖
npm install
# 构建
npm run build
# 测试
npm test欢迎提交 Issue 和 Pull Request。
GitHub: https://github.com/suhaihui/su-js-utils
MIT © suhaihui
- 优化 TypeScript 类型支持
- 修复模块导出问题
- 完善文档和示例
- 新增 DOM 工具函数
- 新增动画相关功能
- 优化日期工具函数
- 完善类型定义
- 新增验证工具函数
- 优化字符串工具函数
- 修复已知问题
- 新增日期工具函数
- 优化数组工具函数
- 完善文档