From 295804ffd1d705875f87c2bba797c79e919a0dd1 Mon Sep 17 00:00:00 2001 From: IgorM867 Date: Mon, 14 Oct 2024 09:39:49 +0200 Subject: [PATCH 1/2] fix(fmt): handle missing group separator for 1000.1 in some locales --- fmt/bytes_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fmt/bytes_test.ts b/fmt/bytes_test.ts index 765951829040..6f7d7021136d 100644 --- a/fmt/bytes_test.ts +++ b/fmt/bytes_test.ts @@ -5,7 +5,7 @@ import { format } from "./bytes.ts"; import { assertEquals, assertThrows } from "@std/assert"; -const parts = new Intl.NumberFormat().formatToParts(1000.1); +const parts = new Intl.NumberFormat().formatToParts(10000.1); const decimal = parts.find(({ type }) => type === "decimal")!.value; const group = parts.find(({ type }) => type === "group")!.value; From 2b06d090982b1c89718e25db6b3e8e3446948a1b Mon Sep 17 00:00:00 2001 From: IgorM867 Date: Wed, 16 Oct 2024 09:02:27 +0200 Subject: [PATCH 2/2] add test for group separators --- fmt/bytes_test.ts | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/fmt/bytes_test.ts b/fmt/bytes_test.ts index 6f7d7021136d..7fc57ece05ed 100644 --- a/fmt/bytes_test.ts +++ b/fmt/bytes_test.ts @@ -4,10 +4,10 @@ import { format } from "./bytes.ts"; import { assertEquals, assertThrows } from "@std/assert"; +import { stub } from "@std/testing/mock"; const parts = new Intl.NumberFormat().formatToParts(10000.1); const decimal = parts.find(({ type }) => type === "decimal")!.value; -const group = parts.find(({ type }) => type === "group")!.value; Deno.test("format() throws on invalid input", () => { // deno-lint-ignore no-explicit-any @@ -76,10 +76,6 @@ Deno.test("format() handles locale option", () => { assertEquals(format(0.4, { locale: true }), `0${decimal}4 B`); assertEquals(format(1001, { locale: true }), "1 kB"); assertEquals(format(10.1, { locale: true }), `10${decimal}1 B`); - assertEquals( - format(1e30, { locale: true }), - `1${group}000${group}000 YB`, - ); assertEquals(format(-0.4, { locale: false }), "-0.4 B"); assertEquals(format(0.4, { locale: false }), "0.4 B"); @@ -105,6 +101,41 @@ Deno.test("format() handles locale option", () => { assertEquals(format(1e30, {}), "1000000 YB"); }); +Deno.test("format() handles group separators", async (t) => { + const tests: { locale: string; expected: string }[] = [ + { locale: "en-US", expected: "1,000,000 YB" }, + { locale: "es-ES", expected: "1.000.000 YB" }, + + // narrow non-breaking space + { locale: "fr-FR", expected: "1\u202f000\u202f000 YB" }, + + // non-breaking space + { locale: "pl-PL", expected: "1\u00A0000\u00A0000 YB" }, + + // https://en.wikipedia.org/wiki/Indian_numbering_system + { locale: "hi-IN", expected: "10,00,000 YB" }, + { locale: "gu-IN", expected: "10,00,000 YB" }, + ]; + + for (const { locale, expected } of tests) { + await t.step(locale, () => { + const toLocaleString = Number.prototype.toLocaleString; + using _ = stub( + Number.prototype, + "toLocaleString", + function (loc) { + return toLocaleString.call(this, loc ?? locale); + }, + ); + + // explicitly supplying the locale + assertEquals(format(1e30, { locale }), expected); + // using the default locale (mocked with stub) + assertEquals(format(1e30, { locale: true }), expected); + }); + } +}); + Deno.test("format() handles signed option", () => { assertEquals(format(42, { signed: true }), "+42 B"); assertEquals(format(-13, { signed: true }), "-13 B");