@philiprehberger/filesize
v0.2.0
Published
Human-readable file size formatting and parsing — bytes to KB, MB, GB and back
Readme
@philiprehberger/filesize
Human-readable file size formatting and parsing — bytes to KB, MB, GB and back
Installation
npm install @philiprehberger/filesizeUsage
Format bytes
import { formatBytes } from '@philiprehberger/filesize';
formatBytes(0); // "0 B"
formatBytes(1500); // "1.5 KB"
formatBytes(1000000); // "1 MB"
formatBytes(1000000000); // "1 GB"Binary mode
import { formatBytes } from '@philiprehberger/filesize';
formatBytes(1024, { binary: true }); // "1 KiB"
formatBytes(1048576, { binary: true }); // "1 MiB"Parse file size strings
import { parseBytes } from '@philiprehberger/filesize';
parseBytes('1.5 MB'); // 1500000
parseBytes('1 GiB'); // 1073741824
parseBytes('500 B'); // 500Format bits
import { formatBits } from '@philiprehberger/filesize';
formatBits(1000); // "1 Kb"
formatBits(1000000); // "1 Mb"Format bitrate
import { formatBitrate } from '@philiprehberger/filesize';
formatBitrate(1_000_000); // "1 Mb/s"
formatBitrate(125_000_000); // "125 Mb/s"API
| Function | Description |
|----------|-------------|
| formatBytes(bytes, options?) | Format bytes as human-readable SI (KB, MB) or IEC (KiB, MiB) string |
| parseBytes(str) | Parse a human-readable file size string back to bytes |
| formatBits(bits, options?) | Format bits as Kb, Mb, Gb |
| formatBitrate(bitsPerSecond, options?) | Format bits-per-second as Kb/s, Mb/s, Gb/s |
FormatOptions
| Property | Type | Default | Description |
| ----------- | --------- | ------- | ---------------------------------------- |
| binary | boolean | false | Use binary units (KiB, MiB) instead of SI (KB, MB) |
| precision | number | 1 | Number of decimal places |
| locale | string | — | Locale for number formatting |
| space | boolean | true | Include space between value and unit |
Development
npm install
npm run build
npm testSupport
If you find this project useful:
