@oh-my-pi/pi-utils 14.6.2 → 14.6.3
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.
- package/package.json +2 -2
- package/src/format.ts +11 -5
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@oh-my-pi/pi-utils",
|
|
4
|
-
"version": "14.6.
|
|
4
|
+
"version": "14.6.3",
|
|
5
5
|
"description": "Shared utilities for pi packages",
|
|
6
6
|
"homepage": "https://github.com/can1357/oh-my-pi",
|
|
7
7
|
"author": "Can Boluk",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/bun": "^1.3",
|
|
41
|
-
"@oh-my-pi/pi-natives": "14.6.
|
|
41
|
+
"@oh-my-pi/pi-natives": "14.6.3"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
44
|
"bun": ">=1.3.7"
|
package/src/format.ts
CHANGED
|
@@ -27,19 +27,25 @@ export function formatDuration(ms: number): string {
|
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Format a number with K/M/B suffix for compact display.
|
|
30
|
-
* Uses 1 decimal for small leading digits, rounded otherwise.
|
|
31
|
-
* Examples: "999", "1.5K", "25K", "1.5M", "25M", "1.5B"
|
|
30
|
+
* Uses 1 decimal for small leading digits when non-zero, rounded otherwise.
|
|
31
|
+
* Examples: "999", "1K", "1.5K", "25K", "1M", "1.5M", "25M", "1.5B"
|
|
32
32
|
*/
|
|
33
33
|
export function formatNumber(n: number): string {
|
|
34
34
|
if (n < 1_000) return n.toString();
|
|
35
|
-
if (n < 10_000) return `${(n / 1_000)
|
|
35
|
+
if (n < 10_000) return `${trim1(n / 1_000)}K`;
|
|
36
36
|
if (n < 1_000_000) return `${Math.round(n / 1_000)}K`;
|
|
37
|
-
if (n < 10_000_000) return `${(n / 1_000_000)
|
|
37
|
+
if (n < 10_000_000) return `${trim1(n / 1_000_000)}M`;
|
|
38
38
|
if (n < 1_000_000_000) return `${Math.round(n / 1_000_000)}M`;
|
|
39
|
-
if (n < 10_000_000_000) return `${(n / 1_000_000_000)
|
|
39
|
+
if (n < 10_000_000_000) return `${trim1(n / 1_000_000_000)}B`;
|
|
40
40
|
return `${Math.round(n / 1_000_000_000)}B`;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
/** Format with up to 1 decimal place, dropping trailing `.0`. */
|
|
44
|
+
function trim1(n: number): string {
|
|
45
|
+
const s = n.toFixed(1);
|
|
46
|
+
return s.endsWith(".0") ? s.slice(0, -2) : s;
|
|
47
|
+
}
|
|
48
|
+
|
|
43
49
|
/**
|
|
44
50
|
* Format a byte count to a human-readable string.
|
|
45
51
|
* Examples: "512B", "1.5KB", "2.3MB", "1.2GB"
|