forked from lingodotdev/lingo.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
91 lines (84 loc) · 2.89 KB
/
index.ts
File metadata and controls
91 lines (84 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import _ from "lodash";
import { ILoader } from "../_types";
import { composeLoaders, createLoader } from "../_utils";
export type VariableLoaderParams = {
type: "ieee" | "python";
};
export default function createVariableLoader(
params: VariableLoaderParams,
): ILoader<Record<string, any>, Record<string, string>> {
return composeLoaders(variableExtractLoader(params), variableContentLoader());
}
type VariableExtractionPayload = {
variables: string[];
value: string;
};
function variableExtractLoader(
params: VariableLoaderParams,
): ILoader<Record<string, string>, Record<string, VariableExtractionPayload>> {
const specifierPattern = getFormatSpecifierPattern(params.type);
return createLoader({
pull: async (locale, input) => {
const result: Record<string, VariableExtractionPayload> = {};
for (const [key, value] of Object.entries(input)) {
if (typeof value !== "string") {
continue;
}
const matches = value.match(specifierPattern) || [];
result[key] = result[key] || {
value,
variables: [],
};
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const currentValue = result[key].value;
const newValue = currentValue?.replace(match, `{variable:${i}}`);
result[key].value = newValue;
result[key].variables[i] = match;
}
}
return result;
},
push: async (locale, data) => {
const result: Record<string, string> = {};
for (const [key, valueObj] of Object.entries(data)) {
result[key] = valueObj.value;
for (let i = 0; i < valueObj.variables.length; i++) {
const variable = valueObj.variables[i];
const currentValue = result[key];
const newValue = currentValue?.replace(`{variable:${i}}`, variable);
result[key] = newValue;
}
}
return result;
},
});
}
function variableContentLoader(): ILoader<Record<string, VariableExtractionPayload>, Record<string, string>> {
return createLoader({
pull: async (locale, input) => {
const result = _.mapValues(input, (payload) => payload.value);
return result;
},
push: async (locale, data, originalInput) => {
const result: Record<string, VariableExtractionPayload> = _.cloneDeep(originalInput || {});
for (const [key, originalValueObj] of Object.entries(result)) {
result[key] = {
...originalValueObj,
value: data[key],
};
}
return result;
},
});
}
function getFormatSpecifierPattern(type: VariableLoaderParams["type"]): RegExp {
switch (type) {
case "ieee":
return /%(?:\d+\$)?[+-]?(?:[ 0]|'.)?-?\d*(?:\.\d+)?(?:[hljztL]|ll|hh)?[@diuoxXfFeEgGaAcspn%]/g;
case "python":
return /%\([^)]+\)[diouxXeEfFgGcrs%]/g;
default:
throw new Error(`Unsupported variable format type: ${type}`);
}
}