Skip to content

Commit

Permalink
fix(message-utils): correctly handle multi-digit complex argument cas…
Browse files Browse the repository at this point in the history
…es (#1937)

* test: include failing tests for complex argument cases

* fix: correctly handle multi digit complex argument cases

* perf: replace regular expression with plain js
  • Loading branch information
Bertg authored May 16, 2024
1 parent 8f1ddd7 commit 47a0dde
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/interpolate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ describe("interpolate", () => {

it("should interpolate plurals", () => {
const plural = prepare(
"{value, plural, one {{value} Book} other {# Books}}"
"{value, plural, one {{value} Book} =4 {Four books} =99 { Books with problems } other {# Books}}"
)
expect(plural({ value: 1 })).toEqual("1 Book")
expect(plural({ value: 2 })).toEqual("2 Books")
expect(plural({ value: 4 })).toEqual("Four books")
expect(plural({ value: 99 })).toEqual("Books with problems")

const offset = prepare(
"{value, plural, offset:1 =0 {No Books} one {# Book} other {# Books}}"
Expand Down
4 changes: 3 additions & 1 deletion packages/message-utils/src/compileMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("compileMessage", () => {

it("should compile plurals", () => {
const tokens = compileMessage(
"{value, plural, offset:1 =0 {No Books} one {# Book} other {# Books}}"
"{value, plural, offset:1 =0 {No Books} one {# Book} other {# Books} =42 {FourtyTwo books} =99 {Books with problems}}"
)
expect(tokens).toMatchInlineSnapshot(`
[
Expand All @@ -63,6 +63,8 @@ describe("compileMessage", () => {
plural,
{
0: No Books,
42: FourtyTwo books,
99: Books with problems,
offset: 1,
one: [
#,
Expand Down
8 changes: 3 additions & 5 deletions packages/message-utils/src/compileMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ function processTokens(tokens: Token[], mapText?: MapTextFn): CompiledMessage {

// complex argument with cases
const formatProps: Record<string, CompiledMessage> = {}
token.cases.forEach((item) => {
formatProps[item.key.replace(/^=(.)+/, "$1")] = processTokens(
item.tokens,
mapText
)
token.cases.forEach(({ key, tokens }) => {
const prop = key[0] === "=" ? key.slice(1) : key
formatProps[prop] = processTokens(tokens, mapText)
})

return [
Expand Down

0 comments on commit 47a0dde

Please sign in to comment.