61a1f008d0
- i18next 23->26 + react-i18next 15->17 - prettier 2->3, reformat all files - replace @fontsource/inter with @fontsource-variable/inter 5, update import path - domhandler 5->6 (aligns with transitive deps) - lint-staged 16->17
29 lines
980 B
TypeScript
29 lines
980 B
TypeScript
export type ReplaceCallback<R> = (
|
|
match: RegExpExecArray | RegExpMatchArray,
|
|
pushIndex: number,
|
|
) => R;
|
|
export type ConvertPartCallback<R> = (text: string, pushIndex: number) => R;
|
|
|
|
export const findAndReplace = <ReplaceReturnType, ConvertReturnType>(
|
|
text: string,
|
|
regex: RegExp,
|
|
replace: ReplaceCallback<ReplaceReturnType>,
|
|
convertPart: ConvertPartCallback<ConvertReturnType>,
|
|
): Array<ReplaceReturnType | ConvertReturnType> => {
|
|
const result: Array<ReplaceReturnType | ConvertReturnType> = [];
|
|
let lastEnd = 0;
|
|
|
|
let match: RegExpExecArray | RegExpMatchArray | null = regex.exec(text);
|
|
while (match !== null && typeof match.index === 'number') {
|
|
result.push(convertPart(text.slice(lastEnd, match.index), result.length));
|
|
result.push(replace(match, result.length));
|
|
|
|
lastEnd = match.index + match[0].length;
|
|
if (regex.global) match = regex.exec(text);
|
|
}
|
|
|
|
result.push(convertPart(text.slice(lastEnd), result.length));
|
|
|
|
return result;
|
|
};
|