format-byte-size
Version:
[DEPRECATED] A TypeScript library to format byte values into human-readable strings and parse them back.
45 lines (42 loc) • 1.67 kB
text/typescript
interface FormatBytesOptions {
/**
* Number of decimal places to include in the output.
* @default 2
*/
decimalPlaces?: number;
/**
* If true, uses binary prefixes (KiB, MiB, GiB - base 1024).
* If false, uses decimal prefixes (KB, MB, GB - base 1000).
* @default false (use decimal prefixes)
*/
useBinary?: boolean;
/**
* If true, always shows the specified number of decimal places, even if they are zero.
* e.g., true -> "1.00 MB", false -> "1 MB" if value is exactly 1MB.
* @default false
*/
fixedDecimals?: boolean;
/**
* If true, includes a space between the number and the unit.
* e.g., true -> "1.23 MB", false -> "1.23MB".
* @default true
*/
includeUnitSpace?: boolean;
}
/**
* Formats a number of bytes into a human-readable string.
* @param bytes The number of bytes to format.
* @param options Optional formatting configuration.
* @returns A human-readable string representation of the byte size.
* @throws {TypeError} If input `bytes` is not a number.
*/
declare function formatBytes(bytes: number, options?: FormatBytesOptions): string;
/**
* Parses a human-readable byte string (e.g., "512KB", "1.5 GiB") into a number of bytes.
* Supports both decimal (KB, MB) and binary (KiB, MiB) units case-insensitively.
* @param sizeString The human-readable byte string to parse.
* @returns The number of bytes, or null if the string is unparseable.
* @throws {TypeError} If input `sizeString` is not a string.
*/
declare function parseBytes(sizeString: string): number | null;
export { type FormatBytesOptions, formatBytes, parseBytes };