@zmqzzz/tool
v1.0.0
Published
Cosmos 前端通用工具函数库
Maintainers
Readme
@cosmos-fe/tool
Cosmos 前端通用工具函数库,提供常用的异步处理、对象操作、日期格式化等功能。
📦 安装
npm install @cosmos-fe/tool
# 或
yarn add @cosmos-fe/tool🚀 快速开始
默认导入(推荐)
import cosmosFe from '@cosmos-fe/tool'
// 使用异步请求处理
const success = await cosmosFe.asyncFetch(
() => api.getUserInfo(),
{
loadingAction: '获取用户信息',
onSuccess: (data) => console.log('成功:', data),
onError: (error) => console.error('错误:', error)
},
message // antd message 实例
)
// 过滤对象空值
const cleanData = cosmosFe.filterEmptyValue({
name: '张三',
age: 25,
email: '',
phone: null,
address: undefined
})
// 结果: { name: '张三', age: 25 }
// 格式化时长(不显示秒)
const duration = cosmosFe.formatDuringWithoutSec(3661) // "1小时1分钟"按需导入
import { asyncFetch, filterEmptyValue, formatDuringWithoutSec } from '@cosmos-fe/tool'
// 使用异步请求处理
const success = await asyncFetch(
() => api.getUserInfo(),
{
loadingAction: '获取用户信息',
onSuccess: (data) => console.log('成功:', data),
onError: (error) => console.error('错误:', error)
},
message
)
// 过滤对象空值
const cleanData = filterEmptyValue({
name: '张三',
age: 25,
email: '',
phone: null
})
// 格式化时长
const duration = formatDuringWithoutSec(3661)类型导入
import type { AsyncFetchHooks } from '@cosmos-fe/tool'
const hooks: AsyncFetchHooks<UserInfo> = {
loadingAction: '获取用户信息',
onSuccess: (data) => console.log(data),
onError: (error) => console.error(error)
}📚 API 文档
asyncFetch
异步请求处理函数,支持加载状态、成功/失败回调等。
asyncFetch<T>(
callApi: () => Promise<T>,
hooks?: AsyncFetchHooks<T>,
messageInstance?: any
): Promise<boolean>参数:
callApi: 返回 Promise 的 API 调用函数hooks: 钩子函数配置messageInstance: antd message 实例(可选)
返回值: Promise<boolean> - 请求是否成功
filterEmptyValue
过滤对象中的空值(null、undefined、空字符串)。
filterEmptyValue<T extends Record<string, any>>(obj: T): Partial<T>formatDuringWithoutSec
格式化时长,不显示秒数。
formatDuringWithoutSec(seconds: number): string🛠️ 开发指南
环境准备
# 安装依赖
yarn install
# 开发模式
yarn dev
# 构建
yarn build
# 测试
yarn test
# 代码检查
yarn lint添加新功能
- 在
src/目录下创建相应的模块文件 - 在
src/index.ts中导出新功能 - 添加测试用例
- 更新文档
项目结构
src/
├── async/ # 异步相关功能
│ └── index.ts
├── object/ # 对象操作功能
│ └── filterEmptyValue.ts
├── date/ # 日期处理功能
│ └── formatDuringWithoutSec.ts
└── index.ts # 主入口文件📦 发布流程
1. 版本管理
# 更新版本号
npm version patch # 补丁版本 (1.0.0 -> 1.0.1)
npm version minor # 次要版本 (1.0.0 -> 1.1.0)
npm version major # 主要版本 (1.0.0 -> 2.0.0)2. 构建和测试
# 构建项目
yarn build
# 运行测试
yarn test
# 代码检查
yarn lint3. 发布到 npm
# 发布到 npm
npm publish
# 如果是 scoped package,需要指定访问权限
npm publish --access public4. 发布到私有仓库(可选)
# 配置私有仓库
npm config set registry https://your-private-registry.com
# 发布到私有仓库
npm publish🔧 配置说明
package.json 关键配置
main: CommonJS 入口文件module: ES Module 入口文件types: TypeScript 类型定义文件files: 发布时包含的文件peerDependencies: 对等依赖(antd)
TypeScript 配置
- 严格模式启用
- 支持 ES2018
- 生成声明文件和源码映射
Rollup 配置
- 支持 CommonJS 和 ES Module 双格式
- 外部化 antd 依赖
- 生成源码映射
🧪 测试
# 运行所有测试
yarn test
# 监听模式
yarn test --watch
# 生成覆盖率报告
yarn test --coverage📝 更新日志
v1.0.0
- 初始版本发布
- 支持 asyncFetch、filterEmptyValue、formatDuringWithoutSec 功能
🤝 贡献指南
- Fork 项目
- 创建功能分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 打开 Pull Request
📄 许可证
MIT License - 详见 LICENSE 文件
👥 团队
Cosmos Frontend Team
