human-readable

Presenting values in human-readable form.

Usage no npm install needed!

<script type="module">
  import humanReadable from 'https://cdn.skypack.dev/human-readable';
</script>

README

human-readable

Presenting values in human-readable form.

npm no dependencies


Installation

npm add human-readable

Usage

Size formatter

import { sizeFormatter } from 'human-readable'

const format = sizeFormatter({
    std: 'JEDEC', // 'SI' (default) | 'IEC' | 'JEDEC'
    decimalPlaces: 2,
    keepTrailingZeroes: false,
    render: (literal, symbol) => `${literal} ${symbol}B`,
})

format(4096) // '4 KB'

const defaults = sizeFormatter()

defaults(4096) // '4.1 kB'

Duration formatter

import { durationFormatter } from 'human-readable'

const span = Date.now() - Date.parse('2019-01-01T09:00:00.000+0200')

const format = durationFormatter({
    // 'y' | 'mo' | 'w' | 'd' | 'h' | 'm' | 's' | 'ms'
    allowMultiples: ['y', 'mo', 'd'],
    keepNonLeadingZeroes: false, // E.g. '1y 0mo 0d'
})

format(span) // '1y 1mo 19d'

const defaults = durationFormatter()

defaults(span) // '1y 1mo 19d 19h 41m 48s'

Disk size units

Decimal SI Binary IEC JEDEC
(103)1 kB (210)1 KiB KB
(103)2 MB (210)2 MiB MB
(103)3 GB (210)3 GiB GB
(103)4 TB (210)4 TiB TB
(103)5 PB (210)5 PiB PB

License

MIT