(polyfill) replace (?<) lookbehind group with custom split function

This commit is contained in:
Valentyne Stigloher 2024-03-12 15:04:28 +01:00
parent 1a12dce4a8
commit eb9151112a

View File

@ -66,6 +66,29 @@ const buildPronounFromTemplate = (key: string, template: PronounTemplate): Prono
);
};
const splitSlashes = (path: string): string[] => {
const chunks = [];
let escape = false;
let currentChunk = '';
for (const character of path) {
if (escape) {
currentChunk += `\`${character}`;
escape = false;
} else {
if (character === '`') {
escape = true;
} else if (character === '/') {
chunks.push(currentChunk);
currentChunk = '';
} else {
currentChunk += character;
}
}
}
chunks.push(currentChunk);
return chunks;
};
const isModifier = (chunk: string, key: string, translator: Translator): boolean => {
// use both locale and base translations to ensure backwards compatibility if key gets translated
return chunk === `:${translator.translate(key)}` || chunk === `:${translator.get(key, false, true)}`;
@ -83,7 +106,7 @@ const extractModifierValue = (chunk: string, key: string, translator: Translator
};
const buildPronounFromSlashes = (config: Config, path: string, translator: Translator): Pronoun | null => {
const chunks = path.split(/(?<!`)\//);
const chunks = splitSlashes(path);
let plural = false;
let pluralHonorific = false;
let description = '';