ByteSize
Represents exact, non-negative, integral byte counts.
Decimal units use powers of 1,000 and binary units use powers of 1,024.
Constants
Constructors
Creates a byte size from a non-negative byte count.
Signature
declare function bytes(value: number | bigint): ByteSizeCreates a decimal exabyte value.
Signature
declare const exabytes: (value: number | bigint) => ByteSizeCreates a binary exbibyte value.
Signature
declare const exbibytes: (value: number | bigint) => ByteSizeDecodes an input into a byte size, returning None for invalid input.
Signature
declare const fromInput: (input: Input) => Option.Option<ByteSize>fromInputUnsafe
Decodes a trusted input into a byte size and throws for invalid input.
Signature
declare function fromInputUnsafe(input: Input): ByteSizefromString
Parses a byte-size string, returning None for invalid syntax, unsupported
units, or quantities that do not represent non-negative integral bytes.
Details
Accepts external strings and fractional quantities using the same syntax
as fromStringUnsafe.
See
- fromStringUnsafe for throwing validation
Signature
declare const fromString: (input: string) => Option.Option<ByteSize>fromStringUnsafe
Parses a byte-size string and throws for invalid syntax, unsupported units, or quantities that do not represent non-negative integral bytes.
Details
Accepts decimal fractions, canonical SI and IEC symbols, lowercase unit names, and whitespace around the quantity and unit. Parsing uses exact bigint arithmetic, including above the safe integer range.
See
- fromString for non-throwing validation
Signature
declare function fromStringUnsafe(input: string): ByteSizeCreates a binary gibibyte value.
Signature
declare const gibibytes: (value: number | bigint) => ByteSizeCreates a decimal gigabyte value.
Signature
declare const gigabytes: (value: number | bigint) => ByteSizeCreates a binary kibibyte value.
Signature
declare const kibibytes: (value: number | bigint) => ByteSizeCreates a decimal kilobyte value.
Signature
declare const kilobytes: (value: number | bigint) => ByteSizeCreates a binary mebibyte value.
Signature
declare const mebibytes: (value: number | bigint) => ByteSizeCreates a decimal megabyte value.
Signature
declare const megabytes: (value: number | bigint) => ByteSizeCreates a binary pebibyte value.
Signature
declare const pebibytes: (value: number | bigint) => ByteSizeCreates a decimal petabyte value.
Signature
declare const petabytes: (value: number | bigint) => ByteSizequettabytes
Creates a decimal quettabyte value.
Signature
declare const quettabytes: (value: number | bigint) => ByteSizeronnabytes
Creates a decimal ronnabyte value.
Signature
declare const ronnabytes: (value: number | bigint) => ByteSizeCreates a binary tebibyte value.
Signature
declare const tebibytes: (value: number | bigint) => ByteSizeCreates a decimal terabyte value.
Signature
declare const terabytes: (value: number | bigint) => ByteSizeCreates a binary yobibyte value.
Signature
declare const yobibytes: (value: number | bigint) => ByteSizeyottabytes
Creates a decimal yottabyte value.
Signature
declare const yottabytes: (value: number | bigint) => ByteSizeCreates a binary zebibyte value.
Signature
declare const zebibytes: (value: number | bigint) => ByteSizezettabytes
Creates a decimal zettabyte value.
Signature
declare const zettabytes: (value: number | bigint) => ByteSizeConverting
Formats a byte size with canonical decimal or binary unit symbols.
Signature
declare function format(self: ByteSize, options: FormatOptions): stringConverts a byte size to a safe integer, returning None when it is too large.
Signature
declare const toNumber: (self: ByteSize) => Option.Option<number>Converts a byte size to an approximate number of the specified unit.
Signature
declare const toUnit: { (unit: Unit): (self: ByteSize) => number; (self: ByteSize, unit: Unit): number;}Getters
Guards
isByteSize
Checks whether a value is a byte size.
Signature
declare function isByteSize(input: unknown): input is ByteSizeInstances
Equivalence
Provides an equivalence for byte sizes.
Signature
declare const Equivalence: Equ.Equivalence<ByteSize>Provides an order for byte sizes.
Signature
declare const Order: order.Order<ByteSize>Math
CombinerMax
Combiner that keeps the largest byte size.
Signature
declare const CombinerMax: Combiner.Combiner<ByteSize>CombinerMin
Combiner that keeps the smallest byte size.
Signature
declare const CombinerMin: Combiner.Combiner<ByteSize>Divides a byte size by a positive integer, discarding any remainder.
Signature
declare const divide: { (divisor: number | bigint): (self: ByteSize) => Option<ByteSize>; (self: ByteSize, divisor: number | bigint): Option<ByteSize>;}ReducerSum
Reducer that sums byte sizes from zero.
Signature
declare const ReducerSum: Reducer.Reducer<ByteSize>Subtracts byte sizes, returning None on underflow.
Signature
declare const subtract: { (that: ByteSize): (self: ByteSize) => Option<ByteSize>; (self: ByteSize, that: ByteSize): Option<ByteSize>;}Adds two byte sizes exactly.
Signature
declare const sum: { (that: ByteSize): (self: ByteSize) => ByteSize; (self: ByteSize, that: ByteSize): ByteSize;}Multiplies a byte size by a non-negative integer scalar.
Signature
declare const times: { (multiplier: number | bigint): (self: ByteSize) => Option<ByteSize>; (self: ByteSize, multiplier: number | bigint): Option<ByteSize>;}Models
BinaryUnit type
Canonical binary byte unit symbols.
Signature
type BinaryUnit = "B" | "KiB" | "MiB" | "GiB" | "TiB" | "PiB" | "EiB" | "ZiB" | "YiB"Represents an exact, non-negative number of bytes.
Signature
type ByteSize = Brand.Branded<bigint, typeof TypeId>DecimalUnit type
Canonical decimal byte unit symbols.
Signature
type DecimalUnit = "B" | "kB" | "MB" | "GB" | "TB" | "PB" | "EB" | "ZB" | "YB" | "RB" | "QB"FormatOptions type
Options controlling compact byte-size formatting.
Signature
type FormatOptions = { readonly precision?: number; readonly trailingZeros?: boolean;} & { readonly system?: "decimal"; readonly unit?: DecimalUnit;} | { readonly system?: "binary"; readonly unit?: BinaryUnit;}Values accepted by byte-size decoding operations.
Details
String inputs require a non-negative decimal integer without leading zeros, followed by a canonical unit symbol or lowercase unit name, optionally separated by one space. Numeric inputs are validated at runtime.
See
- fromString for external strings and fractional quantities
- fromStringUnsafe for throwing string validation
Signature
type Input = ByteSize | bigint | number | `${IntegerQuantity}${"" | " "}${InputUnit}`Canonical decimal and binary byte unit symbols.
Signature
type Unit = DecimalUnit | BinaryUnitOrdering
Constrains a byte size to an inclusive range.
Signature
declare const clamp: { (options: { maximum: ByteSize; minimum: ByteSize; }): (self: ByteSize) => ByteSize; (self: ByteSize, options: { maximum: ByteSize; minimum: ByteSize; }): ByteSize;}Returns the larger byte size.
Signature
declare const max: { (that: ByteSize): (self: ByteSize) => ByteSize; (self: ByteSize, that: ByteSize): ByteSize;}Returns the smaller byte size.
Signature
declare const min: { (that: ByteSize): (self: ByteSize) => ByteSize; (self: ByteSize, that: ByteSize): ByteSize;}Predicates
Returns whether a byte size is in an inclusive range.
Signature
declare const between: { (options: { maximum: ByteSize; minimum: ByteSize; }): (self: ByteSize) => boolean; (self: ByteSize, options: { maximum: ByteSize; minimum: ByteSize; }): boolean;}Checks whether two byte sizes contain the same count.
Signature
declare const equals: { (that: ByteSize): (self: ByteSize) => boolean; (self: ByteSize, that: ByteSize): boolean;}isGreaterThan
Checks whether the first byte size is greater than the second.
Signature
declare const isGreaterThan: { (that: ByteSize): (self: ByteSize) => boolean; (self: ByteSize, that: ByteSize): boolean;}isGreaterThanOrEqualTo
Checks whether the first byte size is at least the second.
Signature
declare const isGreaterThanOrEqualTo: { (that: ByteSize): (self: ByteSize) => boolean; (self: ByteSize, that: ByteSize): boolean;}isLessThan
Checks whether the first byte size is less than the second.
Signature
declare const isLessThan: { (that: ByteSize): (self: ByteSize) => boolean; (self: ByteSize, that: ByteSize): boolean;}isLessThanOrEqualTo
Checks whether the first byte size is at most the second.
Signature
declare const isLessThanOrEqualTo: { (that: ByteSize): (self: ByteSize) => boolean; (self: ByteSize, that: ByteSize): boolean;}Checks whether a byte size is zero.
Signature
declare function isZero(self: ByteSize): booleanUnsafe
subtractUnsafe
Subtracts byte sizes and throws on underflow.
Signature
declare const subtractUnsafe: { (that: ByteSize): (self: ByteSize) => ByteSize; (self: ByteSize, that: ByteSize): ByteSize;}toNumberUnsafe
Converts a byte size to a safe integer and throws when it is too large.
Signature
declare function toNumberUnsafe(self: ByteSize): number