MCPcopy
hub / github.com/PrismJS/prism / formatBytes

Function formatBytes

dangerfile.js:47–59  ·  view source on GitHub ↗
(bytes, decimals = 2)

Source from the content-addressed store, hash-verified

45
46// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
47const formatBytes = (bytes, decimals = 2) => {
48 if (bytes === 0) {
49 return '0 Bytes';
50 }
51
52 const k = 1000;
53 const dm = decimals < 0 ? 0 : decimals;
54 const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
55
56 const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k));
57
58 return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
59};
60
61const maybePlus = (from, to) => from < to ? '+' : '';
62

Callers 2

absDiffFunction · 0.85
runFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected