📜🛠️mahmud-js-tools🛠️📜
mahmud-js-tools 是一个简单而实用的 JavaScript 工具库,包含多种常用的工具函数,如🎯深拷贝对象、🎯防抖函数、🎯比较对象、🎯节流函数、🎯生成随机数、🎯格式化时间、🎯验证 URL、🎯数组去重、🎯首字母大写、🎯驼峰命名转换、🎯打乱数组顺序和🎯异步延迟等。
English | 简体中文
特性
- 🚀 高性能虚拟滚动 - 轻松渲染百万级数据,保持流畅的滚动体验
- 🔄 动态高度适配 - 自动处理超大数据集,防止DOM高度溢出
- 🎯 简单易用API - 直观的接口设计,快速集成到任何项目
- 📦 轻量级 - 无依赖,体积小,加载快
- 🔧 高度可定制 - 支持自定义渲染函数,满足各种UI需求
目录结构
. ├── dist │ ├── index.js │ └── index.js.map ├── src │ ├── index.js │ └── test.js ├── .gitignore ├── LICENSE ├── README.md └── package.json
功能列表
函数名 | 描述 | 示例 |
---|---|---|
deepClone |
深拷贝对象 | deepClone({a: 1}) |
cloneArray |
深拷贝数组 | cloneArray([1, {b: 2}]) |
debounce |
防抖函数 | debounce(fn, 300) |
throttle |
节流函数 | throttle(fn, 300) |
isEqual |
比较两个对象是否相等 | isEqual(obj1, obj2) |
random |
生成指定范围的随机数 | random(1, 10) |
formatTime |
格式化时间戳为可读字符串 | formatTime(1620000000) |
isUrl |
验证 URL 是否有效 | isUrl('https://...') |
unique |
数组去重 | unique([1, 2, 2, 3]) |
capitalize |
将字符串首字母大写 | capitalize('hello') |
camelCase |
将字符串转换为驼峰命名法 | camelCase('hello-world') |
shuffle |
打乱数组顺序 | shuffle([1, 2, 3, 4]) |
sleep |
异步延迟(Promise) | await sleep(1000) |
📜安装
您可以使用 npm 来安装这个库:
npm install mahmud-js-tools
🛠️CDN
<!-- 生产环境(压缩版) -->
<!-- <script src="https://unpkg.com/mahmud-js-tools@1.0.4/dist/index.min.js"></script> -->
<script src="https://unpkg.com/mahmud-js-tools@1.0.12/dist/index.js"></script>
<!-- 开发环境(未压缩) -->
<!-- <script src="https://unpkg.com/mahmud-js-tools@1.0.4/dist/index.js"></script> -->
<script src="https://unpkg.com/mahmud-js-tools@1.0.12/dist/index.js"></script>
🚀使用
deepClone 用于深拷贝对象。
const { deepClone } = require('mahmud-js-tools');
const obj = { a: 1, b: { c: 2 } };
const cloned = deepClone(obj);
console.log(cloned); // { a: 1, b: { c: 2 } }
console.log(cloned === obj); // false
cloneArray 用于深拷贝数组。
const { cloneArray } = require("mahmud-js-tools");
const arr = [1, { b: 2 }];
const clonedArr = cloneArray(arr);
console.log(clonedArr); // [1, { b: 2 }]
console.log(clonedArr === arr); // false
debounce 用于防抖函数。
const { debounce } = require("mahmud-js-tools");
const mockFn = jest.fn();
const debouncedFn = debounce(mockFn, 100);
debouncedFn();
setTimeout(() => {
console.log(mockFn.mock.calls.length); // 1
}, 200);
throttle 用于节流函数。
const { throttle } = require("mahmud-js-tools");
const mockFn = jest.fn();
const throttledFn = throttle(mockFn, 100);
throttledFn();
throttledFn();
setTimeout(() => {
console.log(mockFn.mock.calls.length); // 1
}, 200);
isEqual 用于比较两个对象是否相等。
const { isEqual } = require("mahmud-js-tools");
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
const obj3 = { a: 1, b: { c: 3 } };
console.log(isEqual(obj1, obj2)); // true
console.log(isEqual(obj1, obj3)); // false
random 用于生成指定范围的随机数。
const { random } = require("mahmud-js-tools");
console.log(random(1, 10)); // 随机数在 1 到 10 之间
formatTime 用于格式化时间戳为可读字符串。
const { formatTime } = require("mahmud-js-tools");
console.log(formatTime(1620000000000)); // 2021-05-03 00:00:00
isUrl 用于验证 URL 是否有效。
const { isUrl } = require("mahmud-js-tools");
console.log(isUrl("https://google.com")); // true
console.log(isUrl("not-a-url")); // false
unique 用于数组去重。
const { unique } = require("mahmud-js-tools");
console.log(unique([1, 2, 2, 3])); // [1, 2, 3]
capitalize 用于将字符串首字母大写。
const { capitalize } = require("mahmud-js-tools");
console.log(capitalize("hello")); // Hello
camelCase 用于将字符串转换为驼峰命名法。
const { camelCase } = require("mahmud-js-tools");
console.log(camelCase("hello-world")); // helloWorld
shuffle 用于打乱数组顺序。
const { shuffle } = require("mahmud-js-tools");
const arr = [1, 2, 3, 4];
const shuffledArr = shuffle(arr);
console.log(shuffledArr); // 随机打乱后的数组
sleep 用于异步延迟。
const { sleep } = require("mahmud-js-tools");
async function testSleep() {
const start = Date.now();
await sleep(1000);
const end = Date.now();
console.log(end - start); // 1000 或更接近的值
}
testSleep();
🧪单元测试
项目使用 Jest 进行完整测试,运行以下命令执行测试:
npm run test
🤝贡献指南
欢迎对这个项目进行贡献!请遵循以下步骤:
- Fork 本仓库。
- 创建新分支 (
git checkout -b feature/your-feature
)。 - 提交更改 (
git commit -m 'Add some feature'
)。 - 推送分支 (
git push origin feature/your-feature
)。 提交 Pull Request。
🎯 欢迎访问我的技术博客:- © https://gitee.com/mahmudtjcu
- 🎯 欢迎访问我的Gitee:- © https://www.cnblogs.com/mahmud
📜许可证
本项目使用 MIT 许可证。