npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@zmqzzz/tool

v1.0.0

Published

Cosmos 前端通用工具函数库

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

添加新功能

  1. src/ 目录下创建相应的模块文件
  2. src/index.ts 中导出新功能
  3. 添加测试用例
  4. 更新文档

项目结构

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 lint

3. 发布到 npm

# 发布到 npm
npm publish

# 如果是 scoped package,需要指定访问权限
npm publish --access public

4. 发布到私有仓库(可选)

# 配置私有仓库
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 功能

🤝 贡献指南

  1. Fork 项目
  2. 创建功能分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 打开 Pull Request

📄 许可证

MIT License - 详见 LICENSE 文件

👥 团队

Cosmos Frontend Team

🔗 相关链接