-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(message-utils): make generateMessageId to be working in browser (#…
…1776) * feat(message-utils): make generateMessageId to be working in browser / lambda / etc * refactor(message-utils): extract tests for compileMessage fn
- Loading branch information
1 parent
efcd405
commit f879ddb
Showing
5 changed files
with
240 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
import { mockConsole } from "@lingui/jest-mocks" | ||
import { compileMessage } from "./compileMessage" | ||
|
||
describe("compileMessage", () => { | ||
it("should handle an error if message has syntax errors", () => { | ||
mockConsole((console) => { | ||
expect(compileMessage("Invalid {message")).toEqual("Invalid {message") | ||
expect(console.error).toBeCalledWith( | ||
expect.stringMatching("Unexpected message end at line") | ||
) | ||
}) | ||
}) | ||
|
||
it("should process string chunks with provided map fn", () => { | ||
const tokens = compileMessage( | ||
"Message {value, plural, one {{value} Book} other {# Books}}", | ||
(text) => `<${text}>` | ||
) | ||
expect(tokens).toEqual([ | ||
"<Message >", | ||
[ | ||
"value", | ||
"plural", | ||
{ | ||
one: [["value"], "< Book>"], | ||
other: ["#", "< Books>"], | ||
}, | ||
], | ||
]) | ||
}) | ||
|
||
it("should compileMessage static message", () => { | ||
const tokens = compileMessage("Static message") | ||
expect(tokens).toEqual("Static message") | ||
}) | ||
|
||
it("should compileMessage message with variable", () => { | ||
const tokens = compileMessage("Hey {name}!") | ||
expect(tokens).toMatchInlineSnapshot(` | ||
[ | ||
Hey , | ||
[ | ||
name, | ||
], | ||
!, | ||
] | ||
`) | ||
}) | ||
|
||
it("should not interpolate escaped placeholder", () => { | ||
const tokens = compileMessage("Hey '{name}'!") | ||
expect(tokens).toMatchInlineSnapshot(`Hey {name}!`) | ||
}) | ||
|
||
it("should compile plurals", () => { | ||
const tokens = compileMessage( | ||
"{value, plural, offset:1 =0 {No Books} one {# Book} other {# Books}}" | ||
) | ||
expect(tokens).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
value, | ||
plural, | ||
{ | ||
0: No Books, | ||
offset: 1, | ||
one: [ | ||
#, | ||
Book, | ||
], | ||
other: [ | ||
#, | ||
Books, | ||
], | ||
}, | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
it("should compile selectordinal", () => { | ||
const tokens = compileMessage( | ||
"{value, selectordinal, one {#st Book} two {#nd Book}}" | ||
) | ||
expect(tokens).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
value, | ||
selectordinal, | ||
{ | ||
offset: undefined, | ||
one: [ | ||
#, | ||
st Book, | ||
], | ||
two: [ | ||
#, | ||
nd Book, | ||
], | ||
}, | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
it("should compile nested choice components", () => { | ||
const tokens = compileMessage( | ||
`{ | ||
gender, select, | ||
male {{numOfGuests, plural, one {He invites one guest} other {He invites # guests}}} | ||
female {{numOfGuests, plural, one {She invites one guest} other {She invites # guests}}} | ||
other {They is {gender}}}` | ||
) | ||
expect(tokens).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
gender, | ||
select, | ||
{ | ||
female: [ | ||
[ | ||
numOfGuests, | ||
plural, | ||
{ | ||
offset: undefined, | ||
one: She invites one guest, | ||
other: [ | ||
She invites , | ||
#, | ||
guests, | ||
], | ||
}, | ||
], | ||
], | ||
male: [ | ||
[ | ||
numOfGuests, | ||
plural, | ||
{ | ||
offset: undefined, | ||
one: He invites one guest, | ||
other: [ | ||
He invites , | ||
#, | ||
guests, | ||
], | ||
}, | ||
], | ||
], | ||
offset: undefined, | ||
other: [ | ||
They is , | ||
[ | ||
gender, | ||
], | ||
], | ||
}, | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
it("should compile select", () => { | ||
const tokens = compileMessage("{value, select, female {She} other {They}}") | ||
expect(tokens).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
value, | ||
select, | ||
{ | ||
female: She, | ||
offset: undefined, | ||
other: They, | ||
}, | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
it("should compile date", () => { | ||
const tokens = compileMessage("{value, date}") | ||
expect(tokens).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
value, | ||
date, | ||
], | ||
] | ||
`) | ||
}) | ||
|
||
it("should compile number", () => { | ||
expect(compileMessage("{value, number, percent}")).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
value, | ||
number, | ||
percent, | ||
], | ||
] | ||
`) | ||
expect(compileMessage("{value, number}")).toMatchInlineSnapshot(` | ||
[ | ||
[ | ||
value, | ||
number, | ||
], | ||
] | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import crypto from "crypto" | ||
import { sha256 } from "js-sha256" | ||
|
||
const UNIT_SEPARATOR = "\u001F" | ||
|
||
export function generateMessageId(msg: string, context = "") { | ||
return crypto | ||
.createHash("sha256") | ||
.update(msg + UNIT_SEPARATOR + (context || "")) | ||
.digest("base64") | ||
.slice(0, 6) | ||
return hexToBase64(sha256(msg + UNIT_SEPARATOR + (context || ""))).slice(0, 6) | ||
} | ||
|
||
function hexToBase64(hexStr: string) { | ||
let base64 = "" | ||
for (let i = 0; i < hexStr.length; i++) { | ||
base64 += !((i - 1) & 1) | ||
? String.fromCharCode(parseInt(hexStr.substring(i - 1, i + 1), 16)) | ||
: "" | ||
} | ||
return btoa(base64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f879ddb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
js-lingui – ./
js-lingui.vercel.app
js-lingui-git-main-lingui.vercel.app
js-lingui-lingui.vercel.app
lingui.dev
lingui.js.org