1 const dictionary: Array<{ max: number; type: string }> = [
2 { max: 1024, type: 'B' },
3 { max: 1048576, type: 'KB' },
4 { max: 1073741824, type: 'MB' },
5 { max: 1.0995116e12, type: 'GB' }
8 function getBytes (value: number, precision?: number | undefined): string | number {
9 const format = dictionary.find(d => value < d.max) || dictionary[dictionary.length - 1]
10 const calc = value / (format.max / 1024)
12 const num = precision === undefined
14 : applyPrecision(calc, precision)
16 return `${num} ${format.type}`
19 function applyPrecision (num: number, precision: number) {
21 return Math.round(num)
24 const tho = 10 ** precision
26 return Math.round(num * tho) / tho