elegant-tools-js
v1.0.2
Published
A powerful TypeScript utility library with browser fingerprinting, debouncing, throttling, and more
Maintainers
Readme
elegant-tools-js
一个强大的 TypeScript 工具库,提供浏览器指纹识别、防抖、节流等常用工具函数。
特性
- 浏览器指纹识别 - 基于多维度特征生成唯一标识
- 防抖 & 节流 - 优化事件处理性能
- 深拷贝 - 完整的对象克隆支持
- 唯一ID生成 - 时间戳+随机数组合
- 日期格式化 - 灵活的日期格式化
- 数组操作 - 智能去重工具
- URL工具 - 查询字符串解析与生成
- 设备检测 - 移动设备识别
- 随机工具 - 随机数与字符串生成
安装
npm install elegant-tools-js
# 或
yarn add elegant-tools-js快速开始
ES6+ 模块
import { Utils } from 'elegant-tools-js';
// 防抖 - 避免频繁触发
const debouncedFunction = Utils.debounce(() => {
console.log('防抖执行');
}, 1000);
window.addEventListener('resize', debouncedFunction);
// 节流 - 控制执行频率
const throttledFunction = Utils.throttle(() => {
console.log('节流执行');
}, 1000);
window.addEventListener('scroll', throttledFunction);
// 浏览器指纹 - 获取唯一标识
const fingerprint = Utils.getBrowserFingerprint();
console.log('浏览器指纹:', fingerprint);
// 深拷贝对象
const original = { a: 1, b: { c: 2 } };
const copy = Utils.deepClone(original);
// 生成唯一ID
const id = Utils.generateId();
// 格式化日期
Utils.formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss');
// 数组去重
Utils.uniqueArray([1, 2, 2, 3]); // [1, 2, 3]
// 检查移动设备
if (Utils.isMobile()) {
console.log('移动设备');
}
// 随机数
Utils.random(1, 100); // 1-100 之间的随机整数
// 驼峰转短横线
Utils.camelToKebab('helloWorld'); // 'hello-world'CommonJS
const { Utils } = require('elegant-tools-js');
// 使用方式与 ES6+ 模块相同主要 API
浏览器指纹
Utils.getBrowserFingerprint(): string获取基于浏览器特征的指纹信息,特征包括 User Agent、屏幕信息、时区、硬件信息等。
事件优化
// 防抖
Utils.debounce(func, wait)
// 节流
Utils.throttle(func, limit)对象操作
// 深拷贝
Utils.deepClone(obj)
// 生成唯一ID
Utils.generateId()日期处理
// 格式化日期
Utils.formatDate(date, format)支持 YYYY, MM, DD, HH, mm, ss 占位符。
数组工具
// 数组去重
Utils.uniqueArray(arr)
// 根据键去重
Utils.uniqueArrayByKey(arr, key)URL 工具
// 解析查询字符串
Utils.parseQuery(query)
// 生成查询字符串
Utils.stringifyQuery(params)设备检测
Utils.isMobile(): boolean