Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fmt): handle missing group separator for 1000.1 in some locales #6117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

IgorM867
Copy link

In some locales there is no group separator in parts which cause error during tests.

const parts = new Intl.NumberFormat().formatToParts(1000.1);
console.log(parts);
const group = parts.find(({ type }) => type === "group")!.value;
[
  { type: "integer", value: "1000" },
  { type: "decimal", value: "," },   
  { type: "fraction", value: "1" }   
]
error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'value')
const group = parts.find(({ type }) => type === "group")!.value;

Change to 10000.1 fix this issue.

const parts = new Intl.NumberFormat().formatToParts(10000.1);
console.log(parts);
const group = parts.find(({ type }) => type === "group")!.value;
[
  { type: "integer", value: "10" },
  { type: "group", value: " " },
  { type: "integer", value: "000" },
  { type: "decimal", value: "," },
  { type: "fraction", value: "1" }
]

@IgorM867 IgorM867 requested a review from kt3k as a code owner October 14, 2024 07:53
@CLAassistant
Copy link

CLAassistant commented Oct 14, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the fmt label Oct 14, 2024
Copy link

codecov bot commented Oct 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.58%. Comparing base (ae7048f) to head (4ad5309).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6117   +/-   ##
=======================================
  Coverage   96.58%   96.58%           
=======================================
  Files         536      536           
  Lines       40679    40679           
  Branches     6110     6110           
=======================================
  Hits        39291    39291           
  Misses       1344     1344           
  Partials       44       44           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lionel-rowe
Copy link
Contributor

lionel-rowe commented Oct 15, 2024

This still won't make the tests fully locale independent — at a minimum, it'll fail for locales like hi-IN that use lakhs and crores.

There's currently only one test that uses group, so something like this could work to replace it:

import { format } from "./bytes.ts";
import { assertEquals } from "@std/assert";
import { stub } from "@std/testing/mock";

Deno.test("format() handles group separators", async (t) => {
  const tests: { locale: string; expected: string }[] = [
    { locale: "en-US", expected: "1,000,000 YB" },
    // narrow non-breaking space
    { locale: "fr-FR", expected: "1\u202f000\u202f000 YB" },
    { locale: "es-ES", expected: "1.000.000 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 (l) {
          return toLocaleString.call(this, l ?? locale);
        },
      );

      // explicitly supplying the locale
      assertEquals(format(1e30, { locale }), expected);
      // using the default locale (mocked with stub)
      assertEquals(format(1e30, { locale: true }), expected);
    });
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants