mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-13 18:50:29 +03:00
Timeline Perf Improvement (#1521)
* emojify msg txt find&replace instead of recursion * move findAndReplace func in its own file * improve find and replace * move markdown file to plugins * make find and replace work without g flag regex * fix pagination stop on msg arrive * render blurhash in small size
This commit is contained in:
parent
3713125f57
commit
c854c7f9d2
7 changed files with 65 additions and 30 deletions
28
src/app/utils/findAndReplace.ts
Normal file
28
src/app/utils/findAndReplace.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
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;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue