实用的 JavaScript 字符串和数组工具函数集合
npm install su-js-utilsbash
npm install su-js-utils
`
使用方法
`javascript
// 方式 1: 使用命名空间
import {
stringUtils,
arrayUtils,
dateUtils,
domUtils,
verifyUtils,
} from "su-js-utils";
// 方式 2: 直接导入具体函数
import { capitalize, unique, format } from "su-js-utils";
`
API 文档
$3
`javascript
import { stringUtils } from "su-js-utils";
// 首字母大写
stringUtils.capitalize("hello"); // => "Hello"
// 驼峰转短横线
stringUtils.camelToKebab("helloWorld"); // => "hello-world"
// 短横线转驼峰
stringUtils.kebabToCamel("hello-world"); // => "helloWorld"
// 截断字符串
stringUtils.truncate("Hello World", 8); // => "Hello..."
// 移除两端字符
stringUtils.trim(" hello "); // => "hello"
`
$3
`javascript
import { arrayUtils } from "su-js-utils";
// 安全获取元素
arrayUtils.get([1, 2, 3], 1); // => 2
// 去重
arrayUtils.unique([1, 1, 2, 2, 3]); // => [1, 2, 3]
// 分组
arrayUtils.chunk([1, 2, 3, 4], 2); // => [[1, 2], [3, 4]]
// 扁平化
arrayUtils.flatten([1, [2, [3, 4]], 5]); // => [1, 2, [3, 4], 5]
`
$3
`javascript
import { dateUtils } from "su-js-utils";
// 格式化日期
dateUtils.format(new Date(), "YYYY-MM-DD"); // => "2024-03-15"
// 相对时间
dateUtils.relativeTime("2024-03-14"); // => "1天前"
// 判断闰年
dateUtils.isLeapYear("2024"); // => true
// 获取月份天数
dateUtils.getDaysInMonth("2024-02"); // => 29
// 判断周末
dateUtils.isWeekend("2024-03-16"); // => true
`
$3
`javascript
import { domUtils, $ } from "su-js-utils";
// 选择器
const button = $.get("#myButton");
const items = $.getAll(".item");
// 类名操作
domUtils.className.add(button, "active");
domUtils.className.toggle(button, "disabled");
// 样式操作
domUtils.style.set(button, { backgroundColor: "#fff" });
domUtils.style.show(button);
// 事件操作
domUtils.event.on(button, "click", () => {});
domUtils.event.once(button, "click", () => {});
// 动画操作
await domUtils.animation.fadeIn(element);
await domUtils.animation.slideDown(element);
`
$3
`javascript
import { verifyUtils } from "su-js-utils";
// 字符串验证
verifyUtils.string.isEmail("test@example.com"); // => true
verifyUtils.string.isPhone("13812345678"); // => true
// 密码验证
verifyUtils.string.validatePassword("Password123!", {
minLength: 8,
requireNumber: true,
requireSpecialChar: true,
});
// 数字验证
verifyUtils.number.isInteger(123); // => true
verifyUtils.number.inRange(5, 1, 10); // => true
// 日期验证
verifyUtils.date.isFuture(new Date()); // => false
verifyUtils.date.inRange(date, start, end);
`
按需引入
可以只引入需要的工具函数,减小打包体积:
`javascript
import { capitalize } from "su-js-utils/stringUtils";
import { unique } from "su-js-utils/arrayUtils";
import { format } from "su-js-utils/dateUtils";
`
浏览器支持
- Chrome >= 49
- Firefox >= 45
- Safari >= 10
- Edge >= 14
- IE >= 11 (需要 polyfill)
开发
`bash
安装依赖
npm install
构建
npm run build
测试
npm test
``