UNPKG

bytes-formatter

Version:

Bytes formatter module provides a function to format byte values into human-readable strings with appropriate units (bytes, KB, MB, GB, TB, PB).

35 lines (30 loc) 750 B
/** * @fileoverview Bytes formatter module * * @see https://google.github.io/styleguide/jsguide.html * @see https://google.github.io/styleguide/tsguide.html * @module bytes-formatter */ /** * @type {!Array<string>} * @inner */ const FORMATS = ["bytes", "KB", "MB", "GB", "TB", "PB"]; /** * Formats given <code>bytes</code> to human friendly format. * @param {number} bytes The bytes to be formatted. * @return {string} The formatted bytes as string. * @method * @example * import {formatBytes} from 'bytes-formatter'; * formatBytes(1024); * //> 1.0 Kb */ export const formatBytes = (bytes) => { let i = 0; while (1023 < bytes) { bytes /= 1024; ++i; } return (i ? bytes.toFixed(2) : bytes) + " " + FORMATS[i]; };