Bytes is a JavaScript library for parsing, formatting, converting, and calculating byte sizes with native Intl support.
With Bun:
bun add @batou.dev/bytes
With npm:
npm install @batou.dev/bytes
Bytes supports Node.js 22 and later, works with ES modules and CommonJS, and includes TypeScript declarations.
ES modules:
import { Bytes } from "@batou.dev/bytes";
CommonJS:
const { Bytes } = require("@batou.dev/bytes");
Format byte values:
Bytes.fromBytes(1024).toString(); // 1 KiB
Bytes.fromString("1234.56kB").toFormat("%.3m"); // 1.235 MB
Parse localized values:
Bytes.fromString("1,234.56 GiB", { locale: "en" }).toDecimal({ locale: "fr" }); // 1,33 TB
Convert to numeric values:
Bytes.fromString("1234.56 GiB").valueOf(); // 1325598706237
Bytes.fromString("12.345 MiB").as("kilobytes"); // 12944.67072
Calculate with byte sizes:
Bytes.fromString("123.45 MB").add("1.23 MiB").toBinary(); // 118.96 MiB
This project uses Bun.
bun install
Run the full verification suite with:
bun run lint
bun run typecheck
bun run build
bun run test
bun run test:package
Generate documentation locally:
bun run docs
This code is licensed and distributed under the terms of the MIT license.