From aad01c370884ee1241cc969fb35948b39f4caa52 Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Mon, 30 Sep 2024 08:56:49 -0400 Subject: [PATCH] fix(utils): Locale-aware long formatting (#1512) --- benchmarks/utils.ts | 3 ++- packages/cli/src/util.ts | 3 ++- packages/functions/src/utils.ts | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/benchmarks/utils.ts b/benchmarks/utils.ts index e9b746335..314973d86 100644 --- a/benchmarks/utils.ts +++ b/benchmarks/utils.ts @@ -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 { diff --git a/packages/cli/src/util.ts b/packages/cli/src/util.ts index 5e0685d88..6326904c4 100644 --- a/packages/cli/src/util.ts +++ b/packages/cli/src/util.ts @@ -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 { diff --git a/packages/functions/src/utils.ts b/packages/functions/src/utils.ts index 86e3414e4..90c5a2d29 100644 --- a/packages/functions/src/utils.ts +++ b/packages/functions/src/utils.ts @@ -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 */