2022-05-19 18:25:01 +00:00
|
|
|
/**
|
|
|
|
* Properties of a schema which are to be translated,
|
|
|
|
* irrespective of nesting.
|
|
|
|
*/
|
2022-05-19 09:05:19 +00:00
|
|
|
export const schemaTranslateables = ['label', 'description', 'placeholder'];
|
2022-05-19 18:25:01 +00:00
|
|
|
|
2022-04-17 05:49:18 +00:00
|
|
|
export function getIndexFormat(inp: string | string[]) {
|
2022-05-19 18:25:01 +00:00
|
|
|
/**
|
|
|
|
* converts:
|
|
|
|
* ['This is an ', ,' interpolated ',' string.'] and
|
|
|
|
* 'This is an ${variableA} interpolated ${variableB} string.'
|
|
|
|
* to 'This is an ${0} interpolated ${1} string.'
|
|
|
|
*/
|
|
|
|
|
2022-04-17 05:49:18 +00:00
|
|
|
let string: string | undefined = undefined;
|
|
|
|
let snippets: string[] | undefined = undefined;
|
|
|
|
|
2022-02-14 11:14:21 +00:00
|
|
|
if (typeof inp === 'string') {
|
|
|
|
string = inp;
|
|
|
|
} else if (inp instanceof Array) {
|
|
|
|
snippets = inp;
|
|
|
|
} else {
|
|
|
|
throw new Error(`invalid input ${inp} of type ${typeof inp}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (snippets === undefined) {
|
2022-04-17 05:49:18 +00:00
|
|
|
snippets = getSnippets(string as string);
|
2022-02-14 11:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (snippets.length === 1) {
|
|
|
|
return snippets[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
let str = '';
|
|
|
|
snippets.forEach((s, i) => {
|
2022-04-17 05:49:18 +00:00
|
|
|
if (i === snippets!.length - 1) {
|
2022-02-14 11:14:21 +00:00
|
|
|
str += s;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
str += s + '${' + i + '}';
|
|
|
|
});
|
2022-04-17 05:49:18 +00:00
|
|
|
|
2022-02-14 11:14:21 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2022-04-17 05:49:18 +00:00
|
|
|
export function getSnippets(str: string) {
|
2022-02-14 11:14:21 +00:00
|
|
|
let start = 0;
|
2022-04-17 05:49:18 +00:00
|
|
|
const snippets = [...str.matchAll(/\${[^}]+}/g)].map((m) => {
|
|
|
|
const end = m.index;
|
|
|
|
if (end === undefined) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
const snip = str.slice(start, end);
|
2022-02-14 11:14:21 +00:00
|
|
|
start = end + m[0].length;
|
|
|
|
return snip;
|
|
|
|
});
|
|
|
|
|
2022-04-17 05:49:18 +00:00
|
|
|
snippets.push(str.slice(start));
|
2022-02-14 11:14:21 +00:00
|
|
|
return snippets;
|
|
|
|
}
|
|
|
|
|
2022-04-17 05:49:18 +00:00
|
|
|
export function getWhitespaceSanitized(str: string) {
|
|
|
|
return str.replace(/\s+/g, ' ').trim();
|
2022-02-14 11:14:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 05:49:18 +00:00
|
|
|
export function getIndexList(str: string) {
|
|
|
|
return [...str.matchAll(/\${([^}]+)}/g)].map(([_, i]) => parseInt(i));
|
2022-02-14 11:14:21 +00:00
|
|
|
}
|