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 🙏

© 2024 – Pkg Stats / Ryan Hefner

convert

v5.7.0

Published

The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript

Downloads

150,830

Readme

Convert

The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.

bundle size npm monthly downloads

npm install convert
# or
yarn add convert

More installation examples below.

convert(5, "miles").to("km");
convertMany("4d 16h").to("minutes");

Features

  • Full build time and runtime validation of conversions
  • Web frameworks like Next.js and Vite know that Convert is side-effect free, so conversions will be precalculated at build-time, absolutely zero conversion code is sent to clients
  • Runs anywhere (browsers, Node, Bun, etc), full ES3 backwards-compatibility
  • Tiny bundle size and 0 dependencies
  • Supports bigints

Usage

Generated API documentation for the latest version is available online.

View docs.

// ESM:
import convert from "convert";
// CJS:
const { convert } = require("convert");

// 360 seconds into minutes
convert(360, "seconds").to("minutes");
// -> 6

// BigInt support
convert(20n, "hours").to("minutes");
// -> 1200n

// Format to the best unit automatically
convert(5500, "meters").to("best");
// -> { quantity: 5.5, unit: 'km', toString: () => '5.5 km' }

// You can also do length, data, volume, mass, temperature, and more
convert(5, "kilometers").to("nautical miles");
convert(12, "pounds").to("ounces");
convert(8192, "bytes").to("KiB");
convert(10, "atmospheres").to("kPa");
convert(451, "fahrenheit").to("celsius");

Converting many units

import { convertMany } from "convert";
const { convertMany } = require("convert");

// Convert 1 day and 8 hours into ms
convertMany("1d8h").to("ms");

Converting to best unit

import convert from "convert";
const { convert } = require("convert");

// Convert into the best unit
const duration = convert(36, "h").to("best");
// -> { quantity: 1.5, unit: 'd', toString: () => '1.5 d' }

// The toString() method means you can automatically cast the object to a string without any issues
"duration is " + duration;
// -> duration is 1.5d

// You can also specify to use a specific kind of units (metric or imperial, metric is default)
convert(3.5, "km").to("best"); // -> { quantity: 3.5, unit: 'km', toString: () => '3.5 km' }
convert(3.5, "km").to("best", "metric"); // -> { quantity: 3.5, unit: 'km', toString: () => '3.5 km' }
convert(3.5, "km").to("best", "imperial"); // -> { quantity: 2.17, unit: 'mi', toString: () => '3.5 mi' }

ms shorthand

import { ms } from "convert";
const { ms } = require("convert");

// Convert a duration into milliseconds
ms("1d 2h 30min");
// -> 95400000

// Convert milliseconds to a string
ms(86400000);
// -> '1d'

Installation

Package manager

Convert is published as convert on npm.

npm install convert
# or
yarn add convert

And then import it in your code like this:

// CommonJS
const { convert } = require("convert");

// ESM
import convert from "convert";

Browsers

Pick your favorite CDN:

ES Modules via CDN

<script type="module">
	import convert from "https://cdn.skypack.dev/convert@5";
	import convert from "https://esm.run/convert@5";
	import convert from "https://cdn.jsdelivr.net/npm/convert@5";
	import convert from "https://unpkg.com/convert@5";
</script>

UMD (global)

<script src="https://cdn.jsdelivr.net/npm/convert@5/dist/index.js"></script>
<script src="https://unpkg.com/convert@5/dist/index.js"></script>

Alternatives

Convert is better than other unit conversion libraries because it's faster and smaller than them, while having the same features. Benchmarks of popular unit conversion libraries, including Convert are available here.

Convert is the fastest, taking less than a microsecond for all functions. That's a little over 3 million convert() calls per second.

Bundle size comparison

npm bundle size of convert

npm bundle size of safe-units

npm bundle size of convert-units

npm bundle size of js-quantities

npm bundle size of uom + npm bundle size of uom-units