30 lines
984 B
1
import { PatchReplace, PatchReplaceType } from "@moonlight-mod/types";
2
3
type SingleFind = string | RegExp;
4
type Find = SingleFind | SingleFind[];
5
6
export function processFind<T extends Find>(find: T): T {
7
if (Array.isArray(find)) {
8
return find.map(processFind) as T;
9
} else if (find instanceof RegExp) {
10
// Add support for \i to match rspack's minified names
11
return new RegExp(find.source.replace(/\\i/g, "[A-Za-z_$][\\w$]*"), find.flags) as T;
12
} else {
13
return find;
14
}
15
}
16
17
export function processReplace(replace: PatchReplace | PatchReplace[]) {
18
if (Array.isArray(replace)) {
19
replace.forEach(processReplace);
20
} else {
21
if (replace.type === undefined || replace.type === PatchReplaceType.Normal) {
22
replace.match = processFind(replace.match);
23
}
24
}
25
}
26
27
export function testFind(src: string, find: SingleFind) {
28
// indexOf is faster than includes by 0.25% lmao
29
return typeof find === "string" ? src.indexOf(find) !== -1 : find.test(src);
30
}
31