Skip to content

Commit

Permalink
fix: fallback to other only when undefined (#1665)
Browse files Browse the repository at this point in the history
  • Loading branch information
taozhou-glean authored May 18, 2023
1 parent 0cec936 commit a79de75
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/core/src/interpolate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ describe("interpolate", () => {
expect(offset({ value: 3 })).toEqual("2 Books")
})

it("should compile plurals with falsy value choice", () => {
const plural = prepare("{value, plural, one {} other {# Books}}")
expect(plural({ value: 1 })).toEqual("")
expect(plural({ value: 2 })).toEqual("2 Books")
})

it("when a value is defined (even when empty) plural will return it. Conversely, if a value is not defined, plural defaults to 'other'", () => {
const plural = prepare("{value, plural, =0 {} other {#% discount}}")
expect(plural({ value: 0 })).toEqual("")
Expand Down Expand Up @@ -119,6 +125,15 @@ describe("interpolate", () => {
expect(cache({ value: "n/a" })).toEqual("They")
})

it("should support select with empty string choice", () => {
const cache = prepare("{value, select, female {} other {They}}")
expect(cache({ value: "female" })).toEqual("")
expect(cache({ value: "n/a" })).toEqual("They")
const cache2 = prepare("{value, select, female {0} other {They}}")
expect(cache2({ value: "female" })).toEqual("0")
expect(cache2({ value: "n/a" })).toEqual("They")
})

describe("Custom format", () => {
const testVector = [
["en", null, "0.1", "10%", "20%", "3/4/2017", "€0.10", "€1.00"],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/interpolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const getDefaultFormats = (
return replaceOctothorpe(value - offset, message)
},

select: (value: string, rules) => rules[value] || rules.other,
select: (value: string, rules) => rules[value] ?? rules.other,

number: (
value: number,
Expand Down

1 comment on commit a79de75

@vercel
Copy link

@vercel vercel bot commented on a79de75 May 18, 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.