bytes-formatter 21.6.17 → 21.6.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +6 -6
  2. package/index.js +5 -2
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -21,10 +21,10 @@ import { formatBytes } from "bytes-formatter";
21
21
  * @return {string} The formatted bytes as string.
22
22
  */
23
23
  console.log(formatBytes(1000)); // "1000 bytes"
24
- console.log(formatBytes(1024)); // "1.00 KB"
25
- console.log(formatBytes(1000000)); // "976.56 KB"
26
- console.log(formatBytes(2000000)); // "1.91 MB"
27
- console.log(formatBytes(2000000000)); // "1.86 GB"
28
- console.log(formatBytes(2000000000000)); // "1.82 TB"
29
- console.log(formatBytes(Number.MAX_SAFE_INTEGER)); // "8.00 PB"
24
+ console.log(formatBytes(1024)); // "1.0 KB"
25
+ console.log(formatBytes(1000000)); // "976.6 KB"
26
+ console.log(formatBytes(2000000)); // "1.9 MB"
27
+ console.log(formatBytes(2000000000)); // "1.9 GB"
28
+ console.log(formatBytes(2000000000000)); // "1.8 TB"
29
+ console.log(formatBytes(Number.MAX_SAFE_INTEGER)); // "8.0 PB"
30
30
  ```
package/index.js CHANGED
@@ -20,7 +20,7 @@ const FORMATS = ["bytes", "KB", "MB", "GB", "TB", "PB"];
20
20
  * @example
21
21
  * import {formatBytes} from 'bytes-formatter';
22
22
  * formatBytes(1024);
23
- * //> 1.00 KB
23
+ * //> 1.0 KB
24
24
  */
25
25
  export const formatBytes = (bytes) => {
26
26
  // Handle non-number and non-finite values
@@ -36,5 +36,8 @@ export const formatBytes = (bytes) => {
36
36
  ++i;
37
37
  }
38
38
 
39
- return (i ? bytes.toFixed(2) : bytes) + " " + FORMATS[i];
39
+ // Use 1 decimal for all units except bytes.
40
+ const decimals = i > 0 ? 1 : 0;
41
+
42
+ return (decimals ? bytes.toFixed(decimals) : bytes) + " " + FORMATS[i];
40
43
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bytes-formatter",
3
3
  "type": "module",
4
- "version": "21.6.17",
4
+ "version": "21.6.19",
5
5
  "description": "Bytes formatter module provides a function to format byte values into human-readable strings with appropriate units (bytes, KB, MB, GB, TB, PB).",
6
6
  "homepage": "https://vpodk.com/bytes-formatter/",
7
7
  "repository": {