Data Size Parser: Convert Human-Readable Sizes into Bytes

Parse human-readable data sizes like 10MB, 2GB, and 1TB into bytes. A lightweight, reliable utility for handling file sizes, storage limits, and data processing workflows in JavaScript applications.
Data Size Parser: Convert Human-Readable Sizes into Bytes
Photo by Robynne O / Unsplash

Data Size Parser is a lightweight utility that converts human-readable data sizes into bytes.

It is designed for situations where size values are expressed in formats like “10MB”, “2GB”, or “1TB” and need to be converted into a consistent numeric representation for processing, validation, or comparison.

Explore the package:

View on GitHub:

GitHub - brandonhimpfen/data-size-parser
Contribute to brandonhimpfen/data-size-parser development by creating an account on GitHub.

The Problem

Applications frequently deal with data size inputs in multiple formats.

Users may enter file sizes as strings. Configuration values may use shorthand units. External systems may return sizes in different formats depending on context.

Without a consistent parsing approach, this leads to repetitive logic, edge cases, and inconsistencies across systems.

The Approach

Data Size Parser provides a simple and predictable way to convert size strings into bytes.

Instead of handling parsing logic manually in different parts of an application, this utility standardizes the process and ensures consistent results across workflows.

The goal is not to introduce complexity, but to remove it.

Supported Formats

The parser supports common data size units, including:

  • bytes (B)
  • kilobytes (KB)
  • megabytes (MB)
  • gigabytes (GB)
  • terabytes (TB)

Inputs can be provided in different cases, such as “mb”, “MB”, or “Gb”, and are normalized during parsing.

Why This Matters

Handling data sizes reliably is a small but important part of many systems.

When size values are normalized into bytes, it becomes easier to:

  • compare file sizes.
  • enforce upload or storage limits.
  • validate user input.
  • integrate with APIs and storage systems.

This reduces the likelihood of subtle bugs and improves consistency across applications.

Positioning

Data Size Parser is intentionally minimal.

It focuses on a single responsibility: converting human-readable size values into a consistent numeric format. It does not attempt to provide a full formatting or unit conversion framework.

This makes it easy to adopt, easy to understand, and easy to integrate into existing systems.

Example Use Cases

This utility is useful in any context where size values need to be interpreted programmatically.

Common scenarios include:

  • validating file upload limits.
  • processing configuration values.
  • handling storage or bandwidth constraints.
  • working with APIs that return size data in string format.

Looking Ahead

Data Size Parser is designed as a small, focused utility.

Future enhancements may include additional unit support, stricter validation options, and extended parsing flexibility, while maintaining the core principle of simplicity.