A lightweight, type-safe, and zero-dependency utility for formatting, parsing, converting, comparing, and analyzing byte sizes (supporting both decimal base-1000 and binary base-1024 units).
- 🚀 Zero dependencies & lightweight
- 📦 Dual package (CommonJS & ES Modules)
- 🔒 Fully type-safe with TypeScript types included
- ⚙️ Highly customizable (decimal places, binary vs. decimal units, spacing, custom forced units)
- 🌍 Global Configuration with customizable error handling (throwOnError, onError)
- 🧭 Parsing, Conversion & Manipulation (parse strings back to bytes, convert between units, compare sizes, get differences, calculate stats on collections)
- 🌍 Localization support using
Intl.NumberFormat
npm install bytes-kit
# or
yarn add bytes-kit
# or
pnpm add bytes-kitThis package supports both ES Modules (import) and CommonJS (require) styles:
// ES Modules (ESM)
import bytes, { formatBytes } from 'bytes-kit';
// CommonJS (CJS)
const bytes = require('bytes-kit').default;
const { formatBytes } = require('bytes-kit');You can configure global defaults once. Per-call options always take precedence.
import bytes from 'bytes-kit';
bytes.defaultConfig({
binary: true,
space: false,
throwOnError: false, // Don't crash on invalid input
onError(error) {
console.error('[App error handler]', error.message);
}
});bytes.getConfig(): Returns a read-only clone of the current configuration.bytes.resetConfig(): Restores config back to factory defaults.
Converts a raw number of bytes to a human-readable string:
import { formatBytes } from 'bytes-kit';
formatBytes(1000); // => '1 KB'
formatBytes(1337); // => '1.34 KB'
formatBytes(1024, { binary: true }); // => '1 KiB'
formatBytes(-1000); // => '-1 KB'Converts a human-readable string back to a number representing raw bytes:
import { parseBytes } from 'bytes-kit';
parseBytes('1.5 MB'); // => 1500000
parseBytes('20 KiB'); // => 20480
parseBytes('-5 KB'); // => -5000
parseBytes('100'); // => 100Finds the larger or smaller value between two inputs. Returns a formatted string.
import { getLargerByte, getSmallerByte } from 'bytes-kit';
getLargerByte('1.5 MB', '2000 KB'); // => '2 MB'
getSmallerByte('1.5 MB', 2000000); // => '1.5 MB'Checks equality or computes the difference between two byte sizes (a - b).
import { isEqualBytes, diffBytes } from 'bytes-kit';
isEqualBytes('1 MB', '1000 KB'); // => true
isEqualBytes('1 MiB', '1024 KiB'); // => true
diffBytes('1.5 MB', '500 KB'); // => '1 MB'
diffBytes('500 KB', '1.5 MB'); // => '-1 MB'Sums an array of byte sizes and returns the formatted sum.
import { sumBytes } from 'bytes-kit';
sumBytes(['1 MB', '500 KB']); // => '1.5 MB'Sorts an array of byte values while preserving their original formats (string or number). Returns a new array.
import { sortBytes } from 'bytes-kit';
sortBytes(['1 MB', '200 KB', '2 GB']); // => ['200 KB', '1 MB', '2 GB']
sortBytes(['1 MB', '200 KB', '2 GB'], 'desc'); // => ['2 GB', '1 MB', '200 KB']Converts a byte value (number or string) to a target unit (e.g. 'KB', 'MiB'). Returns a raw number by default, or a formatted string if { format: true } is provided:
import { convertBytes } from 'bytes-kit';
convertBytes('10 MB', 'KB'); // => 10000 (number)
convertBytes('1 GiB', 'MiB'); // => 1024 (number)
convertBytes('1 GiB', 'MB'); // => 1073.741824 (number)
convertBytes('1.5 MB', 'KB', { format: true }); // => '1500 KB' (string)Validate representations or detect units:
import { isValidByte, detectUnit } from 'bytes-kit';
isValidByte('1 MB'); // => true
isValidByte('invalid'); // => false
detectUnit('5 GiB'); // => 'GiB'
detectUnit('500'); // => 'B'Parses a list of mixed sizes and returns an analysis object:
import { analyzeBytes } from 'bytes-kit';
const stats = analyzeBytes(['500 KB', '1.2 MB', 3000000, '4.5 MiB']);
/*
Returns:
{
largest: '4.72 MB',
smallest: '500 KB',
average: '2.35 MB'
}
*/Formats a numeric number of bytes.
| Option | Type | Default | Description |
|---|---|---|---|
binary |
boolean |
false |
Use binary units (base-1024, e.g. KiB, MiB). |
decimalPlaces |
number |
2 |
The maximum number of decimal places to include. |
fixedDecimals |
boolean |
false |
If true, always displays trailing zeros in the decimal part. |
space |
boolean |
true |
Include a space between the value and the unit. |
unit |
UnitType |
undefined |
Forces conversion to a specific unit (e.g., 'MB', 'GiB'). |
locale |
string | boolean |
false |
Localizes the formatted string using Intl.NumberFormat. |
Parses a string size back to bytes. Supports standard decimal (B, KB, MB...) and binary (KiB, MiB, GiB...) units case-insensitively.
Compares a and b and returns the larger size formatted as a string.
Compares a and b and returns the smaller size formatted as a string.
Returns the difference a - b formatted as a string.
Returns true if a and b represent equivalent byte capacities.
Sums the array of byte sizes and returns the formatted sum.
Sorts the collection of byte sizes and returns a new sorted array.
Returns true if value is a valid byte representation.
Returns the detected UnitType symbol of the byte representation.
convertBytes(input: number | string, targetFormat: UnitType, options?: FormatOptions & { format?: boolean }): number | string
Converts a byte value (number or string) to a target unit. If options.format is true, returns a formatted string using the specified formatting options; otherwise, returns the raw converted number.
MIT © Cool Dev Master