Skip to content

Commit

Permalink
fix: format should follow locale order (#1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak authored Apr 27, 2023
1 parent 9989963 commit 4d53b7b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
39 changes: 21 additions & 18 deletions packages/core/src/formats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,53 @@ import { date, number } from "./formats"
describe("@lingui/core/formats", () => {
describe("date", () => {
it("should support Date as input", () => {
expect(date(["en"], new Date(2023, 2, 5))).toMatchInlineSnapshot(
`"3/5/2023"`
)
expect(date(["en"], new Date(2023, 2, 5))).toBe("3/5/2023")
})
it("should support iso string as input", () => {
expect(
date(["en"], new Date(2023, 2, 5).toISOString())
).toMatchInlineSnapshot(`"3/5/2023"`)
expect(date(["en"], new Date(2023, 2, 5).toISOString())).toBe("3/5/2023")
})

it("should pass format options", () => {
expect(
date(["en"], new Date(2023, 2, 5).toISOString(), { dateStyle: "full" })
).toMatchInlineSnapshot(`"Sunday, March 5, 2023"`)
).toBe("Sunday, March 5, 2023")

expect(
date(["en"], new Date(2023, 2, 5).toISOString(), {
dateStyle: "medium",
})
).toMatchInlineSnapshot(`"Mar 5, 2023"`)
).toBe("Mar 5, 2023")
})

it("should respect passed locale", () => {
expect(
date(["pl"], new Date(2023, 2, 5).toISOString(), { dateStyle: "full" })
).toMatchInlineSnapshot(`"niedziela, 5 marca 2023"`)
).toBe("niedziela, 5 marca 2023")
})
})

describe("number", () => {
it("should pass format options", () => {
expect(
number(["en"], 1000, { style: "currency", currency: "EUR" })
).toMatchInlineSnapshot(`"€1,000.00"`)
expect(number(["en"], 1000, { style: "currency", currency: "EUR" })).toBe(
"€1,000.00"
)

expect(
number(["en"], 1000, { maximumSignificantDigits: 3 })
).toMatchInlineSnapshot(`"1,000"`)
expect(number(["en"], 1000, { maximumSignificantDigits: 3 })).toBe(
"1,000"
)
})

it("should respect passed locale", () => {
it("should respect passed locale(s)", () => {
expect(number(["pl"], 1000, { style: "currency", currency: "EUR" })).toBe(
"1000,00 €"
)

expect(
number(["pl", "en-US"], 1000, { style: "currency", currency: "EUR" })
).toBe("1000,00 €")
expect(
number(["pl"], 1000, { style: "currency", currency: "EUR" })
).toMatchInlineSnapshot(`"1000,00 €"`)
number(["en-US", "pl"], 1000, { style: "currency", currency: "EUR" })
).toBe("€1,000.00")
})
})
})
14 changes: 7 additions & 7 deletions packages/core/src/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function normalizeLocales(locales: Locales): string[] {
export function date(
locales: Locales,
value: string | Date,
format: Intl.DateTimeFormatOptions = {}
format?: Intl.DateTimeFormatOptions
): string {
const _locales = normalizeLocales(locales)

Expand All @@ -27,7 +27,7 @@ export function date(
export function number(
locales: Locales,
value: number,
format: Intl.NumberFormatOptions = {}
format?: Intl.NumberFormatOptions
): string {
const _locales = normalizeLocales(locales)

Expand All @@ -49,11 +49,11 @@ export function plural(

const plurals = ordinal
? getMemoized(
() => cacheKey("plural-ordinal", _locales, {}),
() => cacheKey("plural-ordinal", _locales),
() => new Intl.PluralRules(_locales, { type: "ordinal" })
)
: getMemoized(
() => cacheKey("plural-cardinal", _locales, {}),
() => cacheKey("plural-cardinal", _locales),
() => new Intl.PluralRules(_locales, { type: "cardinal" })
)

Expand All @@ -75,9 +75,9 @@ function getMemoized<T>(getKey: () => string, construct: () => T) {

function cacheKey(
type: string,
locales?: readonly string[],
options: unknown = {}
locales: readonly string[],
options?: Intl.DateTimeFormatOptions | Intl.NumberFormatOptions
) {
const localeKey = [...locales].sort().join("-")
const localeKey = locales.join("-")
return `${type}-${localeKey}-${JSON.stringify(options)}`
}

1 comment on commit 4d53b7b

@vercel
Copy link

@vercel vercel bot commented on 4d53b7b Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.