Skip to main content

data-size

$data-size

Parses data-size strings (e.g. '20KiB', '1.5 MB', '512') into byte counts. Bare numbers and numeric strings without a suffix are treated as bytes.

Parameters

  • standard (string, optional): Which unit standard to accept when parsing. 'iec' (binary: KiB, MiB, ...), 'si' (decimal: KB, MB, ...), or 'any' (both). Default: 'any'.
  • format (string, optional): When set, returns a formatted string instead of a byte count. Accepts 'iec' or 'si' to control the output standard.

Example

// Parse to bytes (normalizer)
new Schema().normalizer('$data-size')

// Strict IEC only
new Schema().normalizer({'$data-size': {standard: 'iec'}})

// Parse and format back as SI string
new Schema().normalizer({'$data-size': {format: 'si'}})

// Use as normalizer ahead of number validation
new Schema().normalizer(['$data-size']).validator('$is-number')