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

bytes-format-lite-ts

v1.0.0

Published

Ultra-fast (<1 KB), zero-dependency bytes formatter and string parser supporting binary and decimal units with precision control.

Readme

📊 bytes-format-lite-ts

Ultra-fast (< 1 KB), zero-dependency file size and byte formatter and string parser supporting binary (KiB/MiB) and decimal (KB/MB) standards with precision control.

npm version License: MIT Sponsor TypeScript Size


💡 Why bytes-format-lite-ts?

Displaying upload progress, bundle sizes, disk usage, or parsing configuration limits like "50MB" is a daily frontend and backend requirement. Standard packages like bytes or pretty-bytes often have outdated commonJS builds or separate formatting vs parsing packages.

bytes-format-lite-ts provides:

  • ⚡ Zero Dependencies — Minified to under 1 KB, pure native TypeScript.
  • 🔄 Bidirectional Formatting & Parsing — formatBytes(1048576) ➔ "1 MiB" and parseBytes("1 MiB") ➔ 1048576.
  • 📐 Binary & Decimal Standards — Supports both IEC binary units (1024 base: KiB, MiB, GiB) and SI decimal units (1000 base: KB, MB, GB).
  • 🎯 Fine Precision Control — Customize decimal points (precision: 1) and spacing (space: false).
  • ➖ Negative Numbers Safe — Accurately formats deltas (e.g. "-4.2 MiB" reduction in bundle size).
  • 📦 Dual ESM & CommonJS — Native compatibility in Node.js, Next.js, Vite, React Native, and edge workers.

📥 Installation

# npm
npm install bytes-format-lite-ts

# pnpm
pnpm add bytes-format-lite-ts

# yarn
yarn add bytes-format-lite-ts

# bun
bun add bytes-format-lite-ts

🚀 Usage & Real-World Examples

1. Basic File Size Formatting (Binary IEC by default)

import { formatBytes } from 'bytes-format-lite-ts';

console.log(formatBytes(1024));          // "1 KiB"
console.log(formatBytes(1048576));       // "1 MiB"
console.log(formatBytes(5368709120));    // "5 GiB"
console.log(formatBytes(0));             // "0 B"

2. Decimal (SI) Units & Precision Tuning

Use decimal mode (1000 base) for network bandwidth, marketing metrics, or disk drive capacities:

import { formatBytes } from 'bytes-format-lite-ts';

// Decimal standard (1000 base)
console.log(formatBytes(1500000, { mode: 'decimal' }));
// "1.5 MB"

// Custom precision (e.g. 1 decimal place)
console.log(formatBytes(1572864, { precision: 1 }));
// "1.5 MiB"

// No space between number and unit:
console.log(formatBytes(1024, { space: false }));
// "1KiB"

// Negative byte differences:
console.log(formatBytes(-2097152));
// "-2 MiB"

3. Parsing Human Strings to Bytes (parseBytes)

Ideal for validating file upload limits, configuration files, and CLI parameters:

import { parseBytes } from 'bytes-format-lite-ts';

// Parse binary units (1024 base):
console.log(parseBytes('1 KiB')); // 1024
console.log(parseBytes('2.5 MiB')); // 2621440

// Parse decimal units (1000 base):
console.log(parseBytes('10 MB')); // 10000000
console.log(parseBytes('1 GB'));  // 1000000000

// Express middleware upload check:
function validateFileSize(fileSizeBytes: number, maxAllowedStr = '10MB') {
  const maxBytes = parseBytes(maxAllowedStr);
  if (fileSizeBytes > maxBytes) {
    throw new Error(`File exceeds maximum allowed size of ${maxAllowedStr}`);
  }
}

📖 API Reference

formatBytes(bytes, options?)

Formats raw bytes into a formatted human-readable string.

  • Parameters:
    • bytes (number): The number of bytes to format.
    • options (FormatBytesOptions, optional):
      • mode ('binary' | 'decimal', default: 'binary'): Binary (1024) or decimal (1000).
      • precision (number, default: 2): Number of decimal digits to retain.
      • space (boolean, default: true): Whether to include a space before unit.
  • Returns: string (e.g. "1.25 MiB", "500 KB")

parseBytes(input)

Parses human-readable byte strings back into integer bytes.

  • Parameters:
    • input (string): String such as "100 B", "5 KiB", "2.5 MB", "1 GiB".
  • Returns: number (integer bytes, or 0 if invalid).

💖 Support the Project

If bytes-format-lite-ts made handling file sizes simpler in your apps, please consider sponsoring:

👉 Sponsor @Piratha97 on GitHub Sponsors


📄 License

MIT © 2026 Pirapaakaran Pirathapan