-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: format numbers using the local users format
- Loading branch information
1 parent
43fb25c
commit 101a29c
Showing
11 changed files
with
98 additions
and
43 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
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
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,20 @@ | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import { format } from './format.js' | ||
|
||
const { formatFloat, formatInt } = format('en-US') | ||
|
||
void describe('formatInt()', () => { | ||
void it('should format an integer', () => { | ||
assert.equal(formatInt(1234), '1,234') | ||
}) | ||
}) | ||
|
||
void describe('formatFloat()', () => { | ||
void it('should format a float', () => { | ||
assert.equal(formatFloat(1.234), '1.2') | ||
}) | ||
void it('should format a float with custom number of digits', () => { | ||
assert.equal(formatFloat(1.234, 2), '1.23') | ||
}) | ||
}) |
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,39 @@ | ||
export const format = ( | ||
locales?: Intl.LocalesArgument, | ||
): { | ||
formatInt: (value: number) => string | ||
formatFloat: (value: number, maximumFractionDigits?: number) => string | ||
} => { | ||
const intFormatter = new Intl.NumberFormat(locales, { | ||
maximumFractionDigits: 0, | ||
}) | ||
const formatInt = (value: number) => intFormatter.format(value) | ||
|
||
const formatters = new Map<number, Intl.NumberFormat>([ | ||
[ | ||
1, | ||
new Intl.NumberFormat(locales, { | ||
maximumFractionDigits: 1, | ||
}), | ||
], | ||
]) | ||
|
||
const formatFloat = (value: number, maximumFractionDigits = 1) => { | ||
if (!formatters.has(maximumFractionDigits)) { | ||
const f = new Intl.NumberFormat(undefined, { | ||
maximumFractionDigits, | ||
}) | ||
formatters.set(maximumFractionDigits, f) | ||
} | ||
return formatters.get(maximumFractionDigits)!.format(value) | ||
} | ||
|
||
return { | ||
formatInt, | ||
formatFloat, | ||
} | ||
} | ||
|
||
const { formatFloat, formatInt } = format() | ||
|
||
export { formatFloat, formatInt } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.