this repo has no description
1import { PatchReplace, PatchReplaceType } from "@moonlight-mod/types";
2
3type SingleFind = string | RegExp;
4type Find = SingleFind | SingleFind[];
5
6export 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
17export 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
27export 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}