Travis build: 821 [ci skip]
This commit is contained in:
43
README.md
43
README.md
@ -278,6 +278,15 @@
|
|||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
### _Uncategorized_
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>View contents</summary>
|
||||||
|
|
||||||
|
* [`prettyBytes`](#prettybytes)
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
---
|
---
|
||||||
## 🔌 Adapter
|
## 🔌 Adapter
|
||||||
|
|
||||||
@ -4941,6 +4950,40 @@ yesNo('Foo', true); // true
|
|||||||
|
|
||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
---
|
||||||
|
## _Uncategorized_
|
||||||
|
|
||||||
|
### prettyBytes
|
||||||
|
|
||||||
|
Converts a number in bytes to a human-readable string.
|
||||||
|
|
||||||
|
Use an array dictionary of units to be accessed based on the exponent.
|
||||||
|
Use `Number.toPrecision()` to truncate the number to a certain number of digits.
|
||||||
|
Return the prettified string by building it up, taking into account the supplied options and whether it is negative or not.
|
||||||
|
Omit the second argument, `precision`, to use a default precision of `3` digits.
|
||||||
|
Omit the third argument, `addSpace`, to add space between the number and unit by default.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const prettyBytes = (num, precision = 3, addSpace = true) => {
|
||||||
|
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
|
if (Math.abs(num) < 1) return num + (addSpace ? ' ' : '') + UNITS[0];
|
||||||
|
const exponent = Math.min(Math.floor(Math.log10(num < 0 ? -num : num) / 3), UNITS.length - 1);
|
||||||
|
const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision));
|
||||||
|
return (num < 0 ? '-' : '') + n + (addSpace ? ' ' : '') + UNITS[exponent];
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
prettyBytes(1000); // 1 KB
|
||||||
|
prettyBytes(123456789); // 123 MB
|
||||||
|
prettyBytes(-50); // -50 B
|
||||||
|
prettyBytes(27145424323.5821); // 27.1 GB
|
||||||
|
prettyBytes(27145424323.5821, 5); // 27.145 GB
|
||||||
|
prettyBytes(5500, 3, false); // 5.5KB
|
||||||
|
```
|
||||||
|
|
||||||
|
<br>[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
## Collaborators
|
## Collaborators
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -9,12 +9,12 @@ Omit the second argument, `precision`, to use a default precision of `3` digits.
|
|||||||
Omit the third argument, `addSpace`, to add space between the number and unit by default.
|
Omit the third argument, `addSpace`, to add space between the number and unit by default.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const prettyBytes = (num, precision = 3, addSpace = true) => {
|
const prettyBytes = (num, precision = 3, addSpace = true) => {
|
||||||
const UNITS = ['B','KB','MB','GB','TB','PB','EB','ZB','YB'];
|
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
if (Math.abs(num) < 1) return num + (addSpace ? ' ' : '') + UNITS[0];
|
if (Math.abs(num) < 1) return num + (addSpace ? ' ' : '') + UNITS[0];
|
||||||
const exponent = Math.min(Math.floor(Math.log10(num < 0 ? -num : 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 < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision));
|
const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision));
|
||||||
return ((num < 0 ? '-' : '') + n + (addSpace ? ' ' : '') + UNITS[exponent]);
|
return (num < 0 ? '-' : '') + n + (addSpace ? ' ' : '') + UNITS[exponent];
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -23,6 +23,6 @@ prettyBytes(1000); // 1 KB
|
|||||||
prettyBytes(123456789); // 123 MB
|
prettyBytes(123456789); // 123 MB
|
||||||
prettyBytes(-50); // -50 B
|
prettyBytes(-50); // -50 B
|
||||||
prettyBytes(27145424323.5821); // 27.1 GB
|
prettyBytes(27145424323.5821); // 27.1 GB
|
||||||
prettyBytes(27145424323.5821, 5 ); // 27.145 GB
|
prettyBytes(27145424323.5821, 5); // 27.145 GB
|
||||||
prettyBytes(5500, 3, false); // 5.5KB
|
prettyBytes(5500, 3, false); // 5.5KB
|
||||||
```
|
```
|
||||||
|
|||||||
@ -108,6 +108,7 @@ percentile:math
|
|||||||
pick:array
|
pick:array
|
||||||
pipeFunctions:adapter
|
pipeFunctions:adapter
|
||||||
powerset:math
|
powerset:math
|
||||||
|
prettyBytes:uncategorized
|
||||||
primes:math
|
primes:math
|
||||||
promisify:adapter
|
promisify:adapter
|
||||||
pull:array
|
pull:array
|
||||||
|
|||||||
Reference in New Issue
Block a user