From 6a3447cdb102d97d37b34a3ad852db863312be13 Mon Sep 17 00:00:00 2001 From: Tao Zhou Date: Mon, 13 Mar 2023 20:23:21 -0700 Subject: [PATCH 1/3] feat: clarify the order by `messageId` in v4 and support order by message --- .../api/__snapshots__/catalog.test.ts.snap | 62 +++++++++++++++++++ packages/cli/src/api/catalog.test.ts | 36 +++++++++++ packages/cli/src/api/catalog.ts | 18 ++++++ packages/conf/src/types.ts | 2 +- website/docs/ref/conf.md | 6 +- 5 files changed, 122 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/api/__snapshots__/catalog.test.ts.snap b/packages/cli/src/api/__snapshots__/catalog.test.ts.snap index fa062c2d6..6680fc86a 100644 --- a/packages/cli/src/api/__snapshots__/catalog.test.ts.snap +++ b/packages/cli/src/api/__snapshots__/catalog.test.ts.snap @@ -943,6 +943,68 @@ exports[`order should order messages alphabetically 2`] = ` ] `; +exports[`order should order messages by message 1`] = ` +{ + msg1: { + message: B, + obsolete: false, + origin: [ + [ + file2.js, + 2, + ], + [ + file1.js, + 2, + ], + ], + translation: B, + }, + msg2: { + message: A, + obsolete: false, + origin: [ + [ + file2.js, + 3, + ], + ], + translation: A, + }, + msg3: { + message: D, + obsolete: false, + origin: [ + [ + file2.js, + 100, + ], + ], + translation: D, + }, + msg4: { + message: C, + obsolete: false, + origin: [ + [ + file1.js, + 1, + ], + ], + translation: C, + }, +} +`; + +exports[`order should order messages by message 2`] = ` +[ + msg2, + msg1, + msg4, + msg3, +] +`; + exports[`order should order messages by origin 1`] = ` { LabelA: { diff --git a/packages/cli/src/api/catalog.test.ts b/packages/cli/src/api/catalog.test.ts index 76c8aad25..0c1be6a77 100644 --- a/packages/cli/src/api/catalog.test.ts +++ b/packages/cli/src/api/catalog.test.ts @@ -553,6 +553,42 @@ describe("order", () => { // Jest snapshot order the keys automatically, so test that the key order explicitly expect(Object.keys(orderedCatalogs)).toMatchSnapshot() }) + + it("should order messages by message", () => { + const catalog = { + msg1: makeNextMessage({ + message: "B", + translation: "B", + origin: [ + ["file2.js", 2], + ["file1.js", 2], + ], + }), + msg2: makeNextMessage({ + message: "A", + translation: "A", + origin: [["file2.js", 3]], + }), + msg3: makeNextMessage({ + message: "D", + translation: "D", + origin: [["file2.js", 100]], + }), + msg4: makeNextMessage({ + message: "C", + translation: "C", + origin: [["file1.js", 1]], + }), + } + + const orderedCatalogs = order("message")(catalog) + + // Test that the message content is the same as before + expect(orderedCatalogs).toMatchSnapshot() + + // Jest snapshot order the keys automatically, so test that the key order explicitly + expect(Object.keys(orderedCatalogs)).toMatchSnapshot() + }) }) describe("writeCompiled", () => { diff --git a/packages/cli/src/api/catalog.ts b/packages/cli/src/api/catalog.ts index 7a638e801..00833bd01 100644 --- a/packages/cli/src/api/catalog.ts +++ b/packages/cli/src/api/catalog.ts @@ -290,6 +290,7 @@ export function order( ): (catalog: T) => T { return { messageId: orderByMessageId, + message: orderByMessage, origin: orderByOrigin, }[by] } @@ -337,3 +338,20 @@ export function orderByOrigin(messages: T): T { return acc }, {} as T) } + +export function orderByMessage(messages: T): T { + return Object.keys(messages) + .sort((a, b) => { + const aMsg = messages[a].message + const bMsg = messages[b].message + + if (aMsg < bMsg) return -1 + if (aMsg > bMsg) return 1 + + return 0 + }) + .reduce((acc, key) => { + ;(acc as any)[key] = messages[key] + return acc + }, {} as T) +} diff --git a/packages/conf/src/types.ts b/packages/conf/src/types.ts index 456a144b4..b5033f19e 100644 --- a/packages/conf/src/types.ts +++ b/packages/conf/src/types.ts @@ -37,7 +37,7 @@ export type CatalogFormatOptions = { disableSelectWarning?: boolean } -export type OrderBy = "messageId" | "origin" +export type OrderBy = "messageId" | "message" | "origin" export type CatalogConfig = { name?: string diff --git a/website/docs/ref/conf.md b/website/docs/ref/conf.md index 0b3fd125c..ccad400b1 100644 --- a/website/docs/ref/conf.md +++ b/website/docs/ref/conf.md @@ -369,7 +369,11 @@ Order of messages in catalog: #### messageId -Sort by the message ID. +Sort by the message ID, `js-lingui-id` will be used if no custom id provided. + +#### message + +Sort by message. #### origin From 9309cc4adf76cdd722d3ded1e2ff37c8f739019e Mon Sep 17 00:00:00 2001 From: Tao Zhou Date: Tue, 14 Mar 2023 08:02:20 -0700 Subject: [PATCH 2/3] adddress comments --- .../api/__snapshots__/catalog.test.ts.snap | 61 ------------------- packages/cli/src/api/catalog.test.ts | 14 +++-- packages/cli/src/api/catalog.ts | 10 +-- website/docs/ref/conf.md | 2 +- 4 files changed, 13 insertions(+), 74 deletions(-) diff --git a/packages/cli/src/api/__snapshots__/catalog.test.ts.snap b/packages/cli/src/api/__snapshots__/catalog.test.ts.snap index 6680fc86a..ebad66113 100644 --- a/packages/cli/src/api/__snapshots__/catalog.test.ts.snap +++ b/packages/cli/src/api/__snapshots__/catalog.test.ts.snap @@ -943,67 +943,6 @@ exports[`order should order messages alphabetically 2`] = ` ] `; -exports[`order should order messages by message 1`] = ` -{ - msg1: { - message: B, - obsolete: false, - origin: [ - [ - file2.js, - 2, - ], - [ - file1.js, - 2, - ], - ], - translation: B, - }, - msg2: { - message: A, - obsolete: false, - origin: [ - [ - file2.js, - 3, - ], - ], - translation: A, - }, - msg3: { - message: D, - obsolete: false, - origin: [ - [ - file2.js, - 100, - ], - ], - translation: D, - }, - msg4: { - message: C, - obsolete: false, - origin: [ - [ - file1.js, - 1, - ], - ], - translation: C, - }, -} -`; - -exports[`order should order messages by message 2`] = ` -[ - msg2, - msg1, - msg4, - msg3, -] -`; exports[`order should order messages by origin 1`] = ` { diff --git a/packages/cli/src/api/catalog.test.ts b/packages/cli/src/api/catalog.test.ts index 0c1be6a77..c7da5ffd9 100644 --- a/packages/cli/src/api/catalog.test.ts +++ b/packages/cli/src/api/catalog.test.ts @@ -565,7 +565,7 @@ describe("order", () => { ], }), msg2: makeNextMessage({ - message: "A", + // message is optional. translation: "A", origin: [["file2.js", 3]], }), @@ -583,11 +583,15 @@ describe("order", () => { const orderedCatalogs = order("message")(catalog) - // Test that the message content is the same as before - expect(orderedCatalogs).toMatchSnapshot() - // Jest snapshot order the keys automatically, so test that the key order explicitly - expect(Object.keys(orderedCatalogs)).toMatchSnapshot() + expect(Object.keys(orderedCatalogs)).toMatchInlineSnapshot(` + [ + msg2, + msg1, + msg4, + msg3, + ] + `) }) }) diff --git a/packages/cli/src/api/catalog.ts b/packages/cli/src/api/catalog.ts index 00833bd01..1c3f85623 100644 --- a/packages/cli/src/api/catalog.ts +++ b/packages/cli/src/api/catalog.ts @@ -342,13 +342,9 @@ export function orderByOrigin(messages: T): T { export function orderByMessage(messages: T): T { return Object.keys(messages) .sort((a, b) => { - const aMsg = messages[a].message - const bMsg = messages[b].message - - if (aMsg < bMsg) return -1 - if (aMsg > bMsg) return 1 - - return 0 + const aMsg = messages[a].message || "" + const bMsg = messages[b].message || "" + return aMsg.localeCompare(bMsg) }) .reduce((acc, key) => { ;(acc as any)[key] = messages[key] diff --git a/website/docs/ref/conf.md b/website/docs/ref/conf.md index ccad400b1..781057214 100644 --- a/website/docs/ref/conf.md +++ b/website/docs/ref/conf.md @@ -373,7 +373,7 @@ Sort by the message ID, `js-lingui-id` will be used if no custom id provided. #### message -Sort by message. +Sort by source message. #### origin From 8a90b3ee1451ab98eafe2fca7085dfc3300f38a9 Mon Sep 17 00:00:00 2001 From: Tao Zhou Date: Tue, 14 Mar 2023 08:04:01 -0700 Subject: [PATCH 3/3] remove extra new line --- packages/cli/src/api/__snapshots__/catalog.test.ts.snap | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/cli/src/api/__snapshots__/catalog.test.ts.snap b/packages/cli/src/api/__snapshots__/catalog.test.ts.snap index ebad66113..fa062c2d6 100644 --- a/packages/cli/src/api/__snapshots__/catalog.test.ts.snap +++ b/packages/cli/src/api/__snapshots__/catalog.test.ts.snap @@ -943,7 +943,6 @@ exports[`order should order messages alphabetically 2`] = ` ] `; - exports[`order should order messages by origin 1`] = ` { LabelA: {