@micl/helper
v0.1.4
Published
micl helper
Readme
@micl/helper
Micl 项目的辅助工具模块,通过原型扩展提供常用工具函数。
✨ 特性
- 原型扩展方式,无需导入单独函数即可使用
- 字节编解码功能
- 元数据管理
- 对象操作工具
- 数组工具
- Promise 延迟执行工具
📦 安装
npm install @micl/helper
# or
pnpm add @micl/helper
# or
yarn add @micl/helper📚 模块导出
只需 import '@micl/helper' 即可自动扩展原生对象原型。
| 扩展对象 | 方法 | 描述 |
|----------|------|------|
| Buffer | encode() | 编码数据为 Buffer |
| Buffer | decode() | 从 Buffer 解码数据 |
| Object | omit(keys) | 移除对象指定属性 |
| Object | defineMetadata(key, value) | 定义元数据 |
| Object | getMetadata(key) | 获取元数据 |
| Array | findMaxObj(filter, key) | 查找指定键的最大值对象 |
| Promise | sleep(ms) | 延迟执行 |
🎯 使用示例
字节编解码
import '@micl/helper';
// 编码字符串
const str = 'hello world';
const encoded = (str as any).encode();
console.log(encoded); // <Buffer 01 00 00 00 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>
// 解码字符串
const decoded = encoded.decode();
console.log(decoded); // 'hello world'
// 编码数字
const num = 3.1415926;
const numEncoded = (num as any).encode();
const numDecoded = numEncoded.decode();
console.log(numDecoded); // 3.1415926
// 编码对象
const obj = { name: 'test', value: 123 };
const objEncoded = (obj as any).encode();
const objDecoded = objEncoded.decode();
console.log(objDecoded); // { name: 'test', value: 123 }对象操作
import '@micl/helper';
// omit - 移除属性
const original = { a: 1, b: 2, c: 3 };
const result = original.omit(['a', 'c']);
console.log(result); // { b: 2 }
// 元数据管理
const obj = {};
obj.defineMetadata('key', 'value');
const metadata = obj.getMetadata('key');
console.log(metadata); // 'value'数组操作
import '@micl/helper';
// 查找最大值对象
const arr = [
{ id: 1, score: 85 },
{ id: 2, score: 95 },
{ id: 3, score: 78 },
];
const maxItem = arr.findMaxObj(item => item.id > 0, 'score');
console.log(maxItem); // { id: 2, score: 95 }Promise 延迟
import '@micl/helper';
// 延迟执行
console.log('开始');
await Promise.sleep(1000);
console.log('1秒后');� API 参考
Buffer.encode()
编码数据为 Buffer 格式。
返回: Buffer - 编码后的字节数据
Buffer.decode()
从 Buffer 解码数据。
返回: 解码后的原始数据(string | number | object)
Object.omit(keys)
移除对象指定属性,返回新对象。
参数:
keys- 要移除的属性名数组
返回: 新对象
Object.defineMetadata(key, value)
在对象上定义元数据。
参数:
key- 元数据键名value- 元数据值
Object.getMetadata(key)
获取对象的元数据。
参数:
key- 元数据键名
返回: 元数据值
Array.findMaxObj(filter, key)
在数组中查找满足过滤条件且指定键值最大的对象。
参数:
filter- 过滤函数key- 比较最大值的键名
返回: 找到的对象
Promise.sleep(ms)
延迟指定时间后 resolve。
参数:
ms- 延迟毫秒数
返回: Promise
🛠 开发
构建
cd helper
pnpm build🤝 贡献
欢迎提交 Issue 和 Pull Request 来完善这个模块。
📄 许可证
本项目采用 ISC 许可证 - 查看 LICENSE 文件了解详情。
Copyright (c) alexgogoing [email protected]
📞 支持
如有问题或建议,请提交 Issue 或联系维护者。
@micl/helper - 简洁高效的原型扩展工具集 🚀
