Skip to content

Commit

Permalink
fix(utils): Locale-aware long formatting (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Sep 30, 2024
1 parent 84e77f5 commit aad01c3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion benchmarks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export function formatTime(ms: number): string {
return `${formatLong(ms)} ms`;
}

const _longFormatter = new Intl.NumberFormat(undefined, { maximumFractionDigits: 0 });
export function formatLong(x: number): string {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return _longFormatter.format(x);
}

export function underline(str: string): string {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ export async function _waitExit(process: ChildProcess): Promise<[unknown, string

// Formatting.

const _longFormatter = new Intl.NumberFormat(undefined, { maximumFractionDigits: 0 });
export function formatLong(x: number): string {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return _longFormatter.format(x);
}

export function formatBytes(bytes: number, decimals = 2): string {
Expand Down
4 changes: 3 additions & 1 deletion packages/functions/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ export function formatBytes(bytes: number, decimals = 2): string {
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

const _longFormatter = new Intl.NumberFormat(undefined, { maximumFractionDigits: 0 });

/** @hidden */
export function formatLong(x: number): string {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return _longFormatter.format(x);
}

/** @hidden */
Expand Down

0 comments on commit aad01c3

Please sign in to comment.