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

smart-unit

v2.1.2

Published

Elegant unit conversion utility with automatic unit selection and high-precision support

Readme

smart-unit

优雅的 JavaScript & TypeScript 单位转换工具

npm version npm downloads bundle size test license


smart-unit 是一个轻量级的自动单位转换工具,支持智能格式化输出。

import SmartUnit from 'smart-unit';

const size = new SmartUnit(['B', 'KB', 'MB', 'GB'], { baseDigit: 1024 });

size.format(1024 * 1024 * 100);  // "100MB"
size.format(1536);               // "1.5KB"
size.parse('2.5GB');             // 2684354560

特性

  • 🎯 智能格式化 — 自动选择最优单位进行展示
  • 🔢 双向转换 — 支持单位格式化(format)和反向解析(parse
  • 高性能 — 极小开销,支持 Node.js 和浏览器
  • 🧮 高精度 — 可选 decimal.js 集成,支持任意精度计算
  • 📦 TypeScript 优先 — 完整的类型安全
  • 🪶 轻量级 — 核心功能,体积小巧
  • 测试完善 — 100+ 测试用例,覆盖各种边缘情况

安装

npm install smart-unit

在线体验

在 CodeSandbox 中编辑

快速开始

固定比例单位

import SmartUnit from 'smart-unit';

const fileSize = new SmartUnit(['B', 'KB', 'MB', 'GB', 'TB'], {
  baseDigit: 1024,
});

fileSize.format(1024 * 1024 * 100);  // => "100MB"
fileSize.format(1536);               // => "1.5KB"

可变比例单位

const length = new SmartUnit(['mm', 10, 'cm', 100, 'm', 1000, 'km']);

length.format(1500);     // => "1.5m"
length.format(1500000);  // => "1.5km"

高精度与 BigInt 支持

import { SmartUnitPrecision } from 'smart-unit/precision'

const bigLength = new SmartUnitPrecision(
  ['mm', 10, 'cm', 100, 'm', 1000, 'km', 1000, 'Mm', 1000, 'Gm', 1000, 'Tm']
);

// 支持 BigInt 和超出 JS 安全整数范围的数值
bigLength.format(10n ** 18n);  // => "1000Tm"

解析单位字符串

const time = new SmartUnit(['ms', 1000, 's', 60, 'm', 60, 'h']);

time.parse('90s');   // => 90000 (ms)
time.parse('2.5h');  // => 9000000 (ms)

链式单位

const time = new SmartUnit(['ms', 1000, 's', 60, 'm', 60, 'h']);

time.formatChain(63000) // => 1m3s
time.formatChain(3663000);  // => 1h1m3s

国际化

const i18nMap = {
  ms: 'ms',
  s: 'seconds',
  m: 'minutes',
  h: 'hours',
}
const t = (unit: keyof typeof i18nMap) => i18nMap[unit]

const timeI18n = new SmartUnit(['ms', 1000, 's', 60, 'm', 60, 'h'], {
  separator: ' ',
}).withConvert(t)

timeI18n.formatChain(90000) // 1minutes 30seconds
timeI18n.formatChain(9000000) // 2hours 30minutes

使用 TypeScript

SmartUnit拥有完整的类型安全支持,适用于 TypeScript 项目。

import SmartUnit, { type GetUnitNames } from 'smart-unit'

const time = new SmartUnit(['ms', 1000, 's', 60, 'm', 60, 'h'])
// 使用工具函数导出类型
type TimeUnits = GetUnitNames<typeof time> // => type TimeUnits = "m" | "ms" | "s" | "h"

// t函数必须能接受所有TimeUnits单位,否则将导致类型错误
const timeI18n = time.withConvert(t)
// toBase的unit参数收到TimeUnits类型约束
time.toBase(60, 'h')

贡献

欢迎贡献!请随时提交 issue 或 pull request。

许可证

MIT © flycran