MCPcopy Create free account
hub / github.com/Snapchat/Valdi / parseMessageParams

Function parseMessageParams

compiler/companion/src/strings/utils.ts:13–48  ·  view source on GitHub ↗
(message: string)

Source from the content-addressed store, hash-verified

11}
12
13function parseMessageParams(message: string) {
14 const params: LocalizableParam[] = [];
15 const seenParams = new Set<string>();
16
17 const matches = message.matchAll(MESSAGE_PARAMS_REGEX);
18 for (const match of matches) {
19 if (match.length == 0) {
20 continue;
21 }
22 const placeholder = match[1];
23 const name = match[2];
24
25 // Handles the same key being used more than once in the format
26 if (seenParams.has(name)) {
27 continue;
28 }
29 seenParams.add(name);
30
31 const format = match[3];
32 let type: LocalizableParamType;
33 if (!format || format === '' || format === '%s') {
34 type = LocalizableParamType.STRING;
35 } else if (format === '%d') {
36 type = LocalizableParamType.INTEGER;
37 } else {
38 throw new Error(`Unsupported format type: ${format}`);
39 }
40 const param: LocalizableParam = {
41 placeholder,
42 name,
43 type,
44 };
45 params.push(param);
46 }
47 return params;
48}
49
50export function messageToFormat(baseEnglishMessage: string, localizedMessage: string) {
51 const baseEnglishParams = parseMessageParams(baseEnglishMessage);

Callers 1

messageToFormatFunction · 0.85

Calls 3

hasMethod · 0.65
addMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected