@batou.dev/bytes
v0.6.0
Published
Work with byte sizes in JavaScript with native Intl support
Maintainers
Readme
Bytes
Bytes is a JavaScript library for parsing, formatting, converting, and calculating byte sizes with native Intl support.
Install
With Bun:
bun add @batou.dev/bytesWith npm:
npm install @batou.dev/bytesBytes 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");Usage
Format byte values:
Bytes.fromBytes(1024).toString(); // 1 KiB
Bytes.fromString("1234.56kB").toFormat("%.3m"); // 1.235 MBParse localized values:
Bytes.fromString("1,234.56 GiB", { locale: "en" }).toDecimal({ locale: "fr" }); // 1,33 TBConvert to numeric values:
Bytes.fromString("1234.56 GiB").valueOf(); // 1325598706237
Bytes.fromString("12.345 MiB").as("kilobytes"); // 12944.67072Calculate with byte sizes:
Bytes.fromString("123.45 MB").add("1.23 MiB").toBinary(); // 118.96 MiBDevelopment
This project uses Bun.
bun installRun the full verification suite with:
bun run lint
bun run typecheck
bun run build
bun run test
bun run test:packageGenerate documentation locally:
bun run docsLinks
License
This code is licensed and distributed under the terms of the MIT license.
