vael:fmt/bytes
Human-readable byte and bit size formatting.
Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes.
Formats numeric byte counts into decimal (kB, MB, …) or binary IEC
(kiB, MiB, …) and bit-rate strings.
import ... from "vael:fmt/bytes"
Since v0.0.0-alpha.2 vjs (V8) vjs-lite (QuickJS)
Cargo feature:
vael-fmt (enabled by default in vaeljs-core and vaeljs-lite-core)Example
Section titled “Example”import { format } from "vael:fmt/bytes";
console.log(format(1337)); // "1.34 kB"console.log(format(1024, { binary: true })); // "1 kiB"API Symbols
Functions
Section titled “Functions”format
Section titled “format”export function format(num: number, options?: FormatOptions): string;
Format a finite byte count num into a human-readable string.
Parameters
| Name | Type | Description |
|---|---|---|
num | number | Finite number of bytes (may be negative or fractional). |
options optional | FormatOptions | Optional formatting configuration. |
Returns
string — Human-readable size string (for example "1.34 kB" or "1 kiB").Exceptions
TypeError— whennumis not a finite number oroptionsis invalid.
import { format } from "vael:fmt/bytes";
console.log(format(42, { signed: true })); // "+42 B"console.log(format(1001, { bits: true })); // "1 kbit"Interfaces
Section titled “Interfaces”FormatOptions
Section titled “FormatOptions”export interface FormatOptions {
bits?: boolean;
binary?: boolean;
signed?: boolean;
locale?: boolean | string | string[];
minimumFractionDigits?: number;
maximumFractionDigits?: number;
}
Formatting options accepted by format in "vael:fmt/bytes".
Properties
| Property | Type | Description | Since |
|---|---|---|---|
bits optional | boolean | Format using bit units (b, kbit, Mbit, ...) instead of byte units. Defaults to false. | v0.0.0-alpha.2 |
binary optional | boolean | Use binary IEC base-1024 prefixes (kiB, MiB, ...) instead of SI base-1000 prefixes (kB, MB, ...). Defaults to false. | v0.0.0-alpha.2 |
signed optional | boolean | Include an explicit + sign for positive numbers and leading space for 0. Defaults to false. | v0.0.0-alpha.2 |
locale optional | boolean | string | string[] | Use localized number formatting (true for default locale, or a BCP 47 locale tag / tag list). Defaults to false. | v0.0.0-alpha.2 |
minimumFractionDigits optional | number | Minimum number of fraction digits to display. | v0.0.0-alpha.2 |
maximumFractionDigits optional | number | Maximum number of fraction digits to display. | v0.0.0-alpha.2 |
