Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: clarify the order by messageId in v4 and support order by message #1515

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about renaming or adding linguiId as an option, but feel like it may be misleading itself, as we do use custom message id as messageId if exists. And support purely order by linguiId seems not that helpful at the moment. So only adding the message here.


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