@hilime/lodash-mini
v1.0.0
Published
一个使用 esbuild 构建的工具函数库
Maintainers
Readme
@hilime/lodash-mini
一个使用 esbuild 构建的轻量级工具函数库,支持多种模块格式
✨ 特性
- 🚀 快速构建 - 使用 esbuild 构建,速度极快
- 📦 多格式支持 - 支持 ESM、CommonJS、IIFE 三种格式
- 🔧 TypeScript 支持 - 完整的类型定义
- 🪶 轻量级 - 打包后仅 2-3KB
- 🌳 Tree-shaking 友好 - 支持按需导入
📦 安装
npm install @hilime/lodash-mini🚀 快速开始
ESM (推荐)
import { isArray, chunk, debounce } from '@hilime/lodash-mini';
// 或者导入所有功能
import limeLodash from '@hilime/lodash-mini';CommonJS
const { isArray, chunk, debounce } = require('@hilime/lodash-mini');
// 或者导入所有功能
const limeLodash = require('@hilime/lodash-mini');浏览器 (IIFE)
<script src="path/to/@hilime/lodash-mini/dist/index.iife.js"></script>
<script>
// 全局变量 LimeLodash 可用
console.log(LimeLodash.isArray([1, 2, 3])); // true
</script>📚 API 文档
类型检查
isArray(value): boolean
检查值是否为数组
isArray([1, 2, 3]); // true
isArray('hello'); // falseisObject(value): boolean
检查值是否为对象(非数组)
isObject({}); // true
isObject([]); // falseisEmpty(value): boolean
检查值是否为空
isEmpty([]); // true
isEmpty(''); // true
isEmpty({}); // true
isEmpty([1]); // false数组操作
chunk(array, size): array[]
将数组分割成指定大小的块
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]flatten(array): array
展平多维数组
flatten([1, [2, 3], [4, [5]]]); // [1, 2, 3, 4, 5]uniq(array): array
数组去重
uniq([1, 2, 2, 3, 3]); // [1, 2, 3]groupBy(array, keyFn): object
根据指定函数对数组元素进行分组
const people = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 30 }
];
groupBy(people, person => person.age);
// { '25': [{ name: 'Bob', age: 25 }], '30': [{ name: 'Alice', age: 30 }, { name: 'Charlie', age: 30 }] }对象操作
pick(object, keys): object
从对象中挑选指定的属性
pick({ a: 1, b: 2, c: 3 }, ['a', 'c']); // { a: 1, c: 3 }omit(object, keys): object
从对象中排除指定的属性
omit({ a: 1, b: 2, c: 3 }, ['b']); // { a: 1, c: 3 }merge(target, ...sources): object
深度合并对象
merge({ a: 1, b: { x: 1 } }, { b: { y: 2 }, c: 3 });
// { a: 1, b: { x: 1, y: 2 }, c: 3 }字符串操作
capitalize(str): string
首字母大写
capitalize('hello'); // 'Hello'camelCase(str): string
转换为驼峰命名
camelCase('hello-world'); // 'helloWorld'kebabCase(str): string
转换为短横线命名
kebabCase('helloWorld'); // 'hello-world'性能工具
debounce(func, wait): function
防抖函数
const debouncedFn = debounce(() => {
console.log('执行');
}, 1000);
// 在1秒内多次调用,只会执行一次
debouncedFn();
debouncedFn();
debouncedFn();throttle(func, wait): function
节流函数
const throttledFn = throttle(() => {
console.log('执行');
}, 1000);
// 每秒最多执行一次
throttledFn();
throttledFn(); // 被忽略🏗️ 开发
构建
# 构建所有格式
npm run build
# 开发模式构建(包含 sourcemap)
npm run build:dev
# 监听模式
npm run dev测试
npm test清理
npm run clean📊 构建产物
dist/index.esm.js- ESM 格式 (~2.3KB)dist/index.cjs.js- CommonJS 格式 (~2.8KB)dist/index.iife.js- IIFE 格式 (~2.7KB)dist/index.d.ts- TypeScript 类型定义 (~1.5KB)
🤝 贡献
欢迎提交 Issue 和 Pull Request!
📄 许可证
MIT
🔗 相关链接
- esbuild - 超快的 JavaScript 打包工具
- TypeScript - JavaScript 的超集
