-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (30 loc) · 750 Bytes
/
index.js
File metadata and controls
34 lines (30 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @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];
};