From 12ea0f4da034847c01494ad0b119dcaf385e2f56 Mon Sep 17 00:00:00 2001 From: Braunson Yager Date: Fri, 8 Oct 2021 00:53:28 -0400 Subject: [PATCH 1/6] Added ES6 local support for formatNumber helper as per #2951 https://github.com/flarum/core/issues/2951 --- js/dist-typings/common/utils/formatNumber.d.ts | 6 +++--- js/src/common/utils/formatNumber.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/js/dist-typings/common/utils/formatNumber.d.ts b/js/dist-typings/common/utils/formatNumber.d.ts index 759a234a0d..00dae3b4cc 100644 --- a/js/dist-typings/common/utils/formatNumber.d.ts +++ b/js/dist-typings/common/utils/formatNumber.d.ts @@ -1,9 +1,9 @@ /** * The `formatNumber` utility localizes a number into a string with the - * appropriate punctuation. + * appropriate punctuation based on the provided locale. * * @example - * formatNumber(1234); + * formatNumber(1234, 'en-US'); * // 1,234 */ -export default function formatNumber(number: number): string; +export default function formatNumber(number: number, locale: string): string; diff --git a/js/src/common/utils/formatNumber.ts b/js/src/common/utils/formatNumber.ts index 436a8131c1..32deb74d23 100644 --- a/js/src/common/utils/formatNumber.ts +++ b/js/src/common/utils/formatNumber.ts @@ -1,11 +1,13 @@ /** * The `formatNumber` utility localizes a number into a string with the - * appropriate punctuation. + * appropriate punctuation based on the provided locale. * * @example - * formatNumber(1234); + * formatNumber(1234, 'en-US'); * // 1,234 */ -export default function formatNumber(number: number): string { - return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); +export default function formatNumber(number: number, locale: string = 'en-US'): string { + return new Intl + .NumberFormat(locale) + .format(number); } From 17b7154b6dbffdd4ac31e30a6ab53e6520b9ebe3 Mon Sep 17 00:00:00 2001 From: Braunson Yager Date: Fri, 8 Oct 2021 10:10:57 -0400 Subject: [PATCH 2/6] Made locale an optional parameter, default to user locale --- js/dist-typings/common/utils/formatNumber.d.ts | 4 ++-- js/src/common/utils/formatNumber.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/js/dist-typings/common/utils/formatNumber.d.ts b/js/dist-typings/common/utils/formatNumber.d.ts index 00dae3b4cc..829ec50aee 100644 --- a/js/dist-typings/common/utils/formatNumber.d.ts +++ b/js/dist-typings/common/utils/formatNumber.d.ts @@ -1,9 +1,9 @@ /** * The `formatNumber` utility localizes a number into a string with the - * appropriate punctuation based on the provided locale. + * appropriate punctuation based on the provided locale otherwise will default to the users locale. * * @example * formatNumber(1234, 'en-US'); * // 1,234 */ -export default function formatNumber(number: number, locale: string): string; +export default function formatNumber(number: number, locale?: string): string; diff --git a/js/src/common/utils/formatNumber.ts b/js/src/common/utils/formatNumber.ts index 32deb74d23..72a2fd3e25 100644 --- a/js/src/common/utils/formatNumber.ts +++ b/js/src/common/utils/formatNumber.ts @@ -1,12 +1,16 @@ /** * The `formatNumber` utility localizes a number into a string with the - * appropriate punctuation based on the provided locale. + * appropriate punctuation based on the provided locale otherwise will default to the users locale. * * @example * formatNumber(1234, 'en-US'); * // 1,234 */ -export default function formatNumber(number: number, locale: string = 'en-US'): string { +export default function formatNumber(number: number, locale?: string): string { + if (typeof locale === 'undefined') { + const locale: string = app.data.locale; + } + return new Intl .NumberFormat(locale) .format(number); From 063b24cb05e6d18f7ebcab33e9b8f9a4b43abbe7 Mon Sep 17 00:00:00 2001 From: Braunson Yager Date: Fri, 8 Oct 2021 10:34:53 -0400 Subject: [PATCH 3/6] Updated to default to app locale --- js/dist-typings/common/utils/formatNumber.d.ts | 6 +++--- js/src/common/utils/formatNumber.ts | 10 ++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/js/dist-typings/common/utils/formatNumber.d.ts b/js/dist-typings/common/utils/formatNumber.d.ts index 829ec50aee..dce399de74 100644 --- a/js/dist-typings/common/utils/formatNumber.d.ts +++ b/js/dist-typings/common/utils/formatNumber.d.ts @@ -1,9 +1,9 @@ /** * The `formatNumber` utility localizes a number into a string with the - * appropriate punctuation based on the provided locale otherwise will default to the users locale. + * appropriate punctuation. * * @example - * formatNumber(1234, 'en-US'); + * formatNumber(1234); * // 1,234 */ -export default function formatNumber(number: number, locale?: string): string; + export default function formatNumber(number: number): string; diff --git a/js/src/common/utils/formatNumber.ts b/js/src/common/utils/formatNumber.ts index 72a2fd3e25..2ed1fd7fe7 100644 --- a/js/src/common/utils/formatNumber.ts +++ b/js/src/common/utils/formatNumber.ts @@ -1,16 +1,14 @@ +import app from '../../forum/app'; + /** * The `formatNumber` utility localizes a number into a string with the * appropriate punctuation based on the provided locale otherwise will default to the users locale. * * @example - * formatNumber(1234, 'en-US'); + * formatNumber(1234); * // 1,234 */ -export default function formatNumber(number: number, locale?: string): string { - if (typeof locale === 'undefined') { - const locale: string = app.data.locale; - } - +export default function formatNumber(number: number, locale?: string = app.data.locale): string { return new Intl .NumberFormat(locale) .format(number); From c4a3ae1b9b56d6d5d3e9cba57f97ac2419c5f43b Mon Sep 17 00:00:00 2001 From: Braunson Yager Date: Fri, 8 Oct 2021 10:35:09 -0400 Subject: [PATCH 4/6] Update formatNumber.d.ts --- js/dist-typings/common/utils/formatNumber.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/dist-typings/common/utils/formatNumber.d.ts b/js/dist-typings/common/utils/formatNumber.d.ts index dce399de74..759a234a0d 100644 --- a/js/dist-typings/common/utils/formatNumber.d.ts +++ b/js/dist-typings/common/utils/formatNumber.d.ts @@ -6,4 +6,4 @@ * formatNumber(1234); * // 1,234 */ - export default function formatNumber(number: number): string; +export default function formatNumber(number: number): string; From 3222a9884a8401cae568fb0df01f0d6a3dbc1cb6 Mon Sep 17 00:00:00 2001 From: Braunson Yager Date: Fri, 8 Oct 2021 10:35:38 -0400 Subject: [PATCH 5/6] Update formatNumber.ts --- js/src/common/utils/formatNumber.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/src/common/utils/formatNumber.ts b/js/src/common/utils/formatNumber.ts index 2ed1fd7fe7..69b96acaf2 100644 --- a/js/src/common/utils/formatNumber.ts +++ b/js/src/common/utils/formatNumber.ts @@ -8,7 +8,7 @@ import app from '../../forum/app'; * formatNumber(1234); * // 1,234 */ -export default function formatNumber(number: number, locale?: string = app.data.locale): string { +export default function formatNumber(number: number, locale: string = app.data.locale): string { return new Intl .NumberFormat(locale) .format(number); From 6de79293b498b4a34180fe70d6702851cd672e2a Mon Sep 17 00:00:00 2001 From: Braunson Yager Date: Fri, 8 Oct 2021 10:44:52 -0400 Subject: [PATCH 6/6] Updated formatting --- js/src/common/utils/formatNumber.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/js/src/common/utils/formatNumber.ts b/js/src/common/utils/formatNumber.ts index 69b96acaf2..0e1f1284bc 100644 --- a/js/src/common/utils/formatNumber.ts +++ b/js/src/common/utils/formatNumber.ts @@ -9,7 +9,5 @@ import app from '../../forum/app'; * // 1,234 */ export default function formatNumber(number: number, locale: string = app.data.locale): string { - return new Intl - .NumberFormat(locale) - .format(number); + return new Intl.NumberFormat(locale).format(number); }