Update prettyBytes.md
This commit is contained in:
@ -9,29 +9,15 @@ negative or not.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const prettyBytes = (num, options) => {
|
const prettyBytes = (num, options) => {
|
||||||
options = { precision: 3, addSpace: true, wholeWord: false, ...options };
|
options = { precision: 3, addSpace: true, ...options };
|
||||||
const UNITS = [
|
const UNITS = ['B','KB','MB','GB','TB','PB','EB','ZB','YB'];
|
||||||
['B', 'Byte'],
|
|
||||||
['KB', 'Kilo'],
|
|
||||||
['MB', 'Mega'],
|
|
||||||
['GB', 'Giga'],
|
|
||||||
['TB', 'Tera'],
|
|
||||||
['PB', 'Peta'],
|
|
||||||
['EB', 'Exa'],
|
|
||||||
['ZB', 'Zetta'],
|
|
||||||
['YB', 'Yotta']
|
|
||||||
];
|
|
||||||
if (num < 0) num = -num;
|
|
||||||
if (num < 1) return (num < 0 ? '-' : '') + num + ' B';
|
if (num < 1) return (num < 0 ? '-' : '') + num + ' B';
|
||||||
const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1);
|
const exponent = Math.min(Math.floor(Math.log10(num < 0 ? -num : num) / 3), UNITS.length - 1);
|
||||||
const n = Number((num / 1000 ** exponent).toPrecision(options.precision));
|
const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(options.precision));
|
||||||
return (
|
return (
|
||||||
(num < 0 ? '-' : '') +
|
(num < 0 ? '-' : '') +
|
||||||
n +
|
n +
|
||||||
(options.addSpace ? ' ' : '') +
|
(options.addSpace ? ' ' : '') + UNITS[exponent]
|
||||||
UNITS[exponent][options.wholeWord ? 1 : 0] +
|
|
||||||
(options.wholeWord && exponent > 0 ? 'byte' : '') +
|
|
||||||
(options.wholeWord && n !== 1 ? 's' : '')
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user