this repo has no description
1import { i18n } from '@lingui/core';
2
3// https://tc39.es/ecma402/#table-sanctioned-single-unit-identifiers
4const BYTES_UNITS = [
5 'byte',
6 'kilobyte',
7 'megabyte',
8 'gigabyte',
9 'terabyte',
10 'petabyte',
11];
12export default function prettyBytes(bytes) {
13 const unitIndex = Math.min(
14 Math.floor(Math.log2(bytes) / 10),
15 BYTES_UNITS.length - 1,
16 );
17 const value = bytes / 1024 ** unitIndex;
18 return i18n.number(value, {
19 style: 'unit',
20 unit: BYTES_UNITS[unitIndex],
21 unitDisplay: 'narrow',
22 maximumFractionDigits: 0,
23 });
24}