Skip to content

Commit f59b762

Browse files
authored
feat(stacks-utils): formatCount support for negative values (#2153)
1 parent 721511f commit f59b762

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

.changeset/strong-stamps-occur.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@stackoverflow/stacks-utils": patch
3+
---
4+
5+
formatCount support for negative values

packages/stacks-utils/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ formatCount(9999); // "9,999"
2727
formatCount(10000); // "10k"
2828
formatCount(12345); // "12.3k"
2929
formatCount(1234567); // "1.2m"
30+
31+
// Negative numbers
32+
formatCount(-1234); // "-1,234"
33+
formatCount(-12345); // "-12.3k"
3034
```
3135

3236
You can also import from the class:

packages/stacks-utils/src/NumberFormatter.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ describe("NumberFormatter.formatCount", () => {
4545
});
4646
});
4747

48+
describe("negative numbers", () => {
49+
it("should format negative numbers in standard format", () => {
50+
expect(NumberFormatter.formatCount(-42)).toBe("-42");
51+
expect(NumberFormatter.formatCount(-999)).toBe("-999");
52+
expect(NumberFormatter.formatCount(-1234)).toBe("-1,234");
53+
expect(NumberFormatter.formatCount(-9999)).toBe("-9,999");
54+
});
55+
56+
it("should format negative numbers in compact format", () => {
57+
expect(NumberFormatter.formatCount(-10000)).toBe("-10k");
58+
expect(NumberFormatter.formatCount(-12345)).toBe("-12.3k");
59+
expect(NumberFormatter.formatCount(-1234567)).toBe("-1.2m");
60+
});
61+
});
62+
4863
describe("convenience function export", () => {
4964
it("should work with the exported formatCount function", () => {
5065
expect(formatCount(1234)).toBe("1,234");

packages/stacks-utils/src/NumberFormatter.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ export class NumberFormatter {
1818
* @param count - The number to format
1919
* @param compactThreshold - (Optional) The number at which to switch from standard to compact format
2020
* @returns A formatted string:
21-
* - Less than 10,000: standard format with commas (e.g., "1,234", "999")
22-
* - 10,000 or more: compact format with lowercase suffixes (e.g., "10k", "1.2m")
21+
* - Less than 10,000: standard format with commas (e.g., "1,234", "999", "-1,234")
22+
* - 10,000 or more: compact format with lowercase suffixes (e.g., "10k", "1.2m", "-10k")
2323
*/
2424
public static formatCount(
2525
count: number,
2626
compactThreshold: number = 10000
2727
): string {
28+
const absoluteValue = Math.abs(count);
29+
const sign = count < 0 ? "-" : "";
30+
2831
for (const formatter of NumberFormatter.getFormatters(
2932
compactThreshold
3033
)) {
31-
if (formatter.condition(count)) {
32-
return formatter.format(count);
34+
if (formatter.condition(absoluteValue)) {
35+
return sign + formatter.format(absoluteValue);
3336
}
3437
}
3538
return count.toString(); // should never reach here

0 commit comments

Comments
 (0)