Skip to content

Commit

Permalink
fix: clean up getFormattedNumber.ts
Browse files Browse the repository at this point in the history
- update types and initialize class fields
  • Loading branch information
mohitb35 committed Dec 15, 2022
1 parent 1cff0ea commit e942990
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/Utils/getFormattedNumber.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
class NumberParser {
constructor(locale) {
private _group: RegExp;
private _decimal: RegExp;
private _numeral: RegExp;
private _index: (d: string) => number | undefined;
constructor(locale: string) {
const format = new Intl.NumberFormat(locale);
const parts = format.formatToParts(12345.6);
const numerals = Array.from({ length: 10 }).map((_, i) => format.format(i));
const index = new Map(numerals.map((d, i) => [d, i]));
this._group = new RegExp(
`[${parts.find((d) => d.type === "group").value}]`,
`[${parts.find((d) => d.type === "group")?.value}]`,
"g"
);
this._decimal = new RegExp(
`[${parts.find((d) => d.type === "decimal").value}]`
`[${parts.find((d) => d.type === "decimal")?.value}]`
);
this._numeral = new RegExp(`[${numerals.join("")}]`, "g");
this._index = (d) => index.get(d);
this._index = (d: string) => index.get(d);
}
parse(string) {
parse(string: string) {
string = string
.trim()
.replace(this._group, "")
.replace(this._decimal, ".")
.replace(this._numeral, this._index);
.replace(this._numeral, this._index.toString());
return string ? +string : NaN;
}
}

const localizedAbbr = {
const localizedAbbr: Record<string, Record<string, string>> = {
en: {
b: "b",
m: "m",
Expand All @@ -37,7 +41,7 @@ const localizedAbbr = {
},
};

function getLocalizedAbbreviation(langCode: string, abbr: string) {
function getLocalizedAbbreviation(langCode: string, abbr: string): string {
return localizedAbbr[langCode]
? localizedAbbr[langCode][abbr]
: localizedAbbr["en"][abbr];
Expand All @@ -47,7 +51,7 @@ export function localizedAbbreviatedNumber(
langCode: string,
number: number,
fractionDigits: number
) {
): string {
if (number >= 1000000000)
return (
getFormattedRoundedNumber(langCode, number / 1000000000, fractionDigits) +
Expand All @@ -68,7 +72,7 @@ export function getFormattedRoundedNumber(
langCode: string,
number: number,
fractionDigits: number
) {
): string {
if (
Math.round(number) ===
Math.round(number * fractionDigits * 10) / (fractionDigits * 10)
Expand All @@ -82,12 +86,12 @@ export function getFormattedRoundedNumber(
return formatter.format(number);
}

export function getFormattedNumber(langCode: string, number: number) {
export function getFormattedNumber(langCode: string, number: number): string {
const formatter = new Intl.NumberFormat(langCode);
return formatter.format(number);
}

export function parseNumber(langCode: string, number: number) {
export function parseNumber(langCode: string, number: string): number {
const parser = new NumberParser(langCode);
return parser.parse(number);
}

0 comments on commit e942990

Please sign in to comment.