The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.
Repository
Current version released
2 months ago
Versions
- v5.6.0Latest
- v5.5.1
- v5.5.0
- v5.4.1
- v5.4.0
- v5.3.0
- v5.2.0
- v5.1.0
- v5.0.0
- v4.14.1
- v4.14.0
- v4.13.2
- v4.13.2
- v4.13.1
- v4.13.1
- v4.13.0
- v4.12.0
- v4.11.0
- v4.10.0
- v4.9.0
- v4.8.0
- v4.7.1
- v4.6.2
- v4.6.1
- v4.6.0
- v4.5.0
- v4.4.2
- v4.4.2
- v4.4.1
- v4.4.0
- v4.3.2
- v4.3.1
- v4.3.0
- v4.2.5
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.2
- v4.0.1
- v4.0.0
- v3.1.0
- v3.0.0
- v2.2.1
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.0
- v1.9.1
- v1.9.0
- v1.8.3
- v1.8.2
- v1.8.1
- v1.8.0
- v1.7.1
- v1.7.0
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.4
- v1.1.3
- v1.1.2
Convert
The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.
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.
// 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.5km' }
// 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.5d' }
// 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.5km' }
convert(3.5, "km").to("best", "metric"); // -> { quantity: 3.5, unit: 'km', toString: () => '3.5km' }
convert(3.5, "km").to("best", "imperial"); // -> { quantity: 2.17, unit: 'mi', toString: () => '3.5mi' }
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.