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

@zh12233/math-library

v1.0.0

Published

A TypeScript math library built with Vite

Readme

Math Library

一个基于 TypeScript 和 Vite 构建的简单数学库,提供基础的算术运算和数组操作功能。

✨ 特性

  • 🚀 使用 Vite 构建超快的开发体验
  • 📦 支持 ES Module 和 UMD 格式
  • 🧪 完整的单元测试(使用 Vitest)
  • 📝 完整的 TypeScript 类型定义
  • 📊 内置代码覆盖率报告
  • 🔍 ESLint + Prettier 代码规范
  • 📖 友好的 API 文档

📦 安装

npm install math-library

yarn add math-library

pnpm add math-library

🚀 使用方法

ES Modules

// 导入单个函数
import { add, multiply, average } from 'math-library';

console.log(add(2, 3)); // 5
console.log(multiply(4, 5)); // 20
console.log(average([1, 2, 3, 4, 5])); // 3

CommonJS

const MathLibrary = require('math-library');

console.log(MathLibrary.subtract(10, 3)); // 7
console.log(MathLibrary.divide(20, 4)); // 5

使用命名空间对象

import MathLibrary from 'math-library';

console.log(MathLibrary.sum([1, 2, 3, 4, 5])); // 15
console.log(MathLibrary.max([1, 2, 3, 4, 5])); // 5
console.log(MathLibrary.min([1, 2, 3, 4, 5])); // 1

📚 API 文档

基础算术运算

add(a, b)

计算两个数的和。

add(a: number, b: number): number

示例:

add(2, 3); // 5
add(-1, 4); // 3

subtract(a, b)

计算两个数的差(a - b)。

subtract(a: number, b: number): number

示例:

subtract(5, 3); // 2
subtract(10, -2); // 12

multiply(a, b)

计算两个数的积。

multiply(a: number, b: number): number

示例:

multiply(2, 3); // 6
multiply(-2, 4); // -8

divide(a, b)

计算两个数的商(a ÷ b)。

divide(a: number, b: number): number

注意: 当除数为 0 时会抛出 RangeError

示例:

divide(6, 2); // 3
divide(10, 4); // 2.5
divide(5, 0); // Error: 除数不能为零

数组操作

sum(numbers)

计算数组中所有数字的总和。

sum(numbers: number[]): number

示例:

sum([1, 2, 3, 4, 5]); // 15
sum([10, -5, 3]); // 8
sum([]); // 0

average(numbers)

计算数组中所有数字的平均值。

average(numbers: number[]): number

注意: 空数组会抛出 RangeError

示例:

average([1, 2, 3, 4, 5]); // 3
average([10, 20, 30]); // 20
average([]); // Error: 数组不能为空

max(numbers)

找到数组中的最大值。

max(numbers: number[]): number

注意: 空数组会抛出 RangeError

示例:

max([1, 5, 3, 9, 2]); // 9
max([-1, -5, -2]); // -1

min(numbers)

找到数组中的最小值。

min(numbers: number[]): number

注意: 空数组会抛出 RangeError

示例:

min([1, 5, 3, 9, 2]); // 1
min([-1, -5, -2]); // -5

👨‍💻 开发

环境要求

  • Node.js >= 18.0.0
  • pnpm(推荐)/ npm / yarn

安装依赖

pnpm install

开发模式

pnpm dev

构建

pnpm build

构建产物会输出到 dist 目录:

dist/
├── index.js           # ES Module 格式
├── index.umd.cjs      # UMD 格式
├── index.d.ts         # TypeScript 类型定义
└── *.js.map          # Source maps

测试

运行测试:

# 运行所有测试
pnpm test

# 测试 UI(可视化界面)
pnpm test:ui

# 生成覆盖率报告
pnpm test:coverage

代码质量

# 代码检查
pnpm lint

# 代码格式化
pnpm format

📊 项目结构

math-library/
├── src/
│   ├── index.ts           # 主入口文件,所有函数的实现
│   └── index.test.ts      # 测试文件
├── dist/                  # 构建输出目录(自动生成)
├── coverage/              # 测试覆盖率报告(自动生成)
├── .eslintrc.cjs          # ESLint 配置
├── .prettierrc            # Prettier 配置
├── .gitignore             # Git 忽略文件
├── package.json           # 项目配置
├── tsconfig.json          # TypeScript 配置
├── tsconfig.node.json     # Node.js TypeScript 配置
├── vite.config.ts         # Vite 构建配置
└── README.md              # 项目文档

📚 发布到 NPM

如果您想将此包发布到 npm,请查看详细的发布指南:

快速发布命令:

# 1. 登录 npm
npm login

# 2. 构建项目
npm run build

# 3. 发布到 npm
npm publish

更多详情请参考 docs/NPM_PUBLISH_GUIDE.md

🎯 技术栈

  • TypeScript - 类型安全的 JavaScript 超集
  • Vite - 现代化的前端构建工具
  • Vitest - 基于 Vite 的单元测试框架
  • ESLint - JavaScript/TypeScript 代码检查工具
  • Prettier - 代码格式化工具

📝 许可证

MIT License

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📧 联系方式

如有问题或建议,请提交 Issue。