This commit is contained in:
Chris Kruining 2025-02-11 16:55:12 +11:00
parent f3069b12af
commit 487e41c2d7
No known key found for this signature in database
GPG key ID: EB894A3560CCCAD2
10 changed files with 163 additions and 69 deletions

View file

@ -9,6 +9,23 @@ export const splitAt = (subject: string, index: number): readonly [string, strin
return [subject.slice(0, index), subject.slice(index + 1)];
};
export function* gen__split_by_filter(subject: string, filter: string): Generator<readonly [boolean, string], void, unknown> {
let lastIndex = 0;
for (const { 0: match, index, ...rest } of subject.matchAll(new RegExp(filter, 'gmi'))) {
const end = index + match.length;
yield [false, subject.slice(lastIndex, index)];
yield [true, subject.slice(index, end)];
lastIndex = end;
}
yield [false, subject.slice(lastIndex, subject.length)];
}
export function split_by_filter(subject: string, filter: string): (readonly [boolean, string])[] {
return Array.from<readonly [boolean, string]>(gen__split_by_filter(subject, filter));
}
const decodeRegex = /(?<!\\)\\(t|b|n|r|f|'|"|u[0-9a-f]{1,4})/gi;
const decodeReplacer = (_: any, char: string) => ({