@shihengtech/utils
v0.0.11
Published
A collection of utility tools for frontend projects
Readme
@shihengtech/utils
一个轻量级的 JavaScript/TypeScript 工具函数库。
✨ 特性
- 🚀 轻量高效 - 精简的代码实现,最小化包体积
- 📦 双格式支持 - 同时支持 ESM 和 CommonJS
- 🔒 类型安全 - 使用 TypeScript 编写,提供完整的类型定义
- 🌲 Tree Shaking - 支持按需引入,减少打包体积
- ✅ 完善测试 - 高覆盖率的单元测试
📦 安装
# npm
npm install @shihengtech/utils
# pnpm
pnpm add @shihengtech/utils
# yarn
yarn add @shihengtech/utils🔨 使用
fnRunner - 带重试的异步执行器
import { fnRunner } from '@shihengtech/utils'
// 执行异步函数,失败后最多重试 3 次
const result = await fnRunner(
async () => fetch('/api/data').then(r => r.json()),
3
)createQueryWithCache - 带缓存的查询函数
import { createQueryWithCache } from '@shihengtech/utils'
const fetchUser = createQueryWithCache(
'user',
async (id: string) => fetch(`/api/users/${id}`).then(r => r.json()),
{ maxAge: 60000 } // 缓存 1 分钟
)
const user = await fetchUser('123')ReplaySubject - 响应式数据流
import { ReplaySubject } from '@shihengtech/utils'
const subject = new ReplaySubject<number>(1)
subject.subscribe(value => console.log(value))
subject.next(1) // 输出: 1
subject.next(2) // 输出: 2📖 文档
查看完整文档:文档站点
🛠️ 开发
# 安装依赖
pnpm install
# 开发模式(监听文件变化)
pnpm dev
# 构建
pnpm build
# 运行测试
pnpm test
# 运行测试(单次)
pnpm test:run
# 运行测试(覆盖率)
pnpm test:coverage
# 启动文档开发服务器
pnpm docs:dev
# 构建文档
pnpm docs:build📊 测试覆盖率
| 模块 | 语句 | 分支 | 函数 | 行 | | -------------------- | ------ | ------ | ------ | ------ | | 总计 | 92.83% | 87.09% | 88.23% | 92.83% | | ReplaySubject | 100% | 100% | 100% | 100% | | createQueryWithCache | 96.38% | 88.88% | 100% | 96.38% | | fnRunner | 81.25% | 83.33% | 100% | 81.25% |
运行 pnpm test:coverage 查看详细覆盖率报告。
📁 项目结构
@shihengtech/utils/
├── src/
│ ├── index.ts # 入口文件
│ ├── class/ # 类
│ │ └── ReplaySubject/ # 响应式数据流
│ └── utils/ # 工具函数
│ ├── fnRunner/ # 重试执行器
│ └── createQueryWithCache/ # 缓存查询
├── docs/ # VitePress 文档
├── dist/ # 构建输出
└── package.json📄 License
MIT
