-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
102 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# deverything | ||
|
||
## 0.34.0 | ||
|
||
### Minor Changes | ||
|
||
- formatters | ||
|
||
## 0.33.0 | ||
|
||
### Minor Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { describe, expect, test } from "@jest/globals"; | ||
import { formatNumber } from "./formatNumber"; | ||
import { randomInt } from "../random/randomInt"; | ||
|
||
describe("formatNumber", () => { | ||
test("should return the same number if test is under a thousand", () => { | ||
const value = randomInt(0, 999); | ||
expect(formatNumber(value, { compact: true })).toBe(`${value}`); | ||
}); | ||
|
||
test("should return a string in compact K notation if value is one thousand or above", () => { | ||
const value = randomInt(1000, 9999); | ||
expect(formatNumber(value, { compact: true })).toContain("K"); | ||
}); | ||
|
||
test("should return a string in compact M notation if value is one million or above", () => { | ||
const value = randomInt(1000000, 9999999); | ||
expect(formatNumber(value, { compact: true })).toContain("M"); | ||
}); | ||
|
||
test("should return a string with thousand separator but no compact notation", () => { | ||
const formattedValue = formatNumber(randomInt(1000000, 9999999)); | ||
expect(formattedValue).not.toContain("M"); | ||
expect(formattedValue).toContain(","); | ||
}); | ||
|
||
test("Retains floating point up to 3", () => { | ||
const formattedValue = formatNumber(123456.123456); | ||
expect(formattedValue).toEqual("123,456.123"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* | ||
* @example formatNumber(1000, { compact: true }) // 1K | ||
* @example formatNumber(1111, { maxDigits: 2 }) // 1,100 | ||
* @example formatNumber(111111.123123123) // 111,111.123 | ||
*/ | ||
export const formatNumber = ( | ||
value: number, | ||
{ | ||
compact, | ||
maxDigits, | ||
}: { | ||
compact?: boolean; | ||
maxDigits?: number; | ||
} = {} | ||
): string => { | ||
const formatter = Intl.NumberFormat("en", { | ||
notation: compact ? "compact" : "standard", | ||
maximumSignificantDigits: maxDigits, | ||
}); | ||
return formatter.format(value); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./formatNumber"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters