Skip to content

Commit

Permalink
feat: clarify the order by messageId in v4 and support order by mes…
Browse files Browse the repository at this point in the history
…sage (#1515)
  • Loading branch information
taozhou-glean authored Mar 14, 2023
1 parent 2b6f563 commit 6a5a5f0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
40 changes: 40 additions & 0 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,46 @@ 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 is optional.
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)

// Jest snapshot order the keys automatically, so test that the key order explicitly
expect(Object.keys(orderedCatalogs)).toMatchInlineSnapshot(`
[
msg2,
msg1,
msg4,
msg3,
]
`)
})
})

describe("writeCompiled", () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ export function order<T extends ExtractedCatalogType>(
): (catalog: T) => T {
return {
messageId: orderByMessageId,
message: orderByMessage,
origin: orderByOrigin,
}[by]
}
Expand Down Expand Up @@ -337,3 +338,16 @@ export function orderByOrigin<T extends ExtractedCatalogType>(messages: T): T {
return acc
}, {} as T)
}

export function orderByMessage<T extends ExtractedCatalogType>(messages: T): T {
return Object.keys(messages)
.sort((a, b) => {
const aMsg = messages[a].message || ""
const bMsg = messages[b].message || ""
return aMsg.localeCompare(bMsg)
})
.reduce((acc, key) => {
;(acc as any)[key] = messages[key]
return acc
}, {} as T)
}
2 changes: 1 addition & 1 deletion packages/conf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion website/docs/ref/conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 source message.

#### origin

Expand Down

0 comments on commit 6a5a5f0

Please sign in to comment.