Skip to content

Commit 63e3fcc

Browse files
committed
1.0.4
1 parent b14997d commit 63e3fcc

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 moonlight contributors
3+
Copyright (c) 2025 moonlight contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@moonlight-mod/moonmap",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"license": "MIT",
55
"repository": {
66
"type": "git",

src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type WebpackModule = (
4141
export type ModuleExport =
4242
| {
4343
type: ModuleExportType.Function;
44-
find: string | RegExp;
44+
find: string | RegExp | (string | RegExp)[];
45+
recursive?: boolean;
4546
}
4647
| {
4748
type: ModuleExportType.Key;
@@ -202,8 +203,13 @@ export default class Moonmap {
202203
) {
203204
switch (moduleExport.type) {
204205
case ModuleExportType.Function: {
205-
const match = findFunctionByStrings(original, moduleExport.find);
206-
return match?.[0]?.[0];
206+
return findFunctionByStrings(
207+
original,
208+
Array.isArray(moduleExport.find)
209+
? moduleExport.find
210+
: [moduleExport.find],
211+
moduleExport.recursive ?? false
212+
);
207213
}
208214

209215
case ModuleExportType.Key: {

src/utils.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
export function findFunctionByStrings(
22
exports: Record<string, any>,
3-
...strings: (string | RegExp)[]
3+
strings: (string | RegExp)[],
4+
recursive = false,
5+
state = new WeakSet()
46
) {
5-
return (
6-
Object.entries(exports).filter(
7-
([index, func]) =>
8-
typeof func === "function" &&
9-
!strings.some(
10-
(query) =>
11-
!(query instanceof RegExp
12-
? func.toString().match(query)
13-
: func.toString().includes(query))
7+
for (const [key, value] of Object.entries(exports)) {
8+
if (typeof value === "function") {
9+
if (
10+
strings.every((query) =>
11+
query instanceof RegExp
12+
? value.toString().match(query)
13+
: value.toString().includes(query)
1414
)
15-
) ?? null
16-
);
15+
) {
16+
return key;
17+
}
18+
} else if (recursive && typeof value === "object" && value !== null) {
19+
if (state.has(value)) continue;
20+
state.add(value);
21+
const result = findFunctionByStrings(value, strings, recursive, state);
22+
if (result != null) return key;
23+
}
24+
}
25+
26+
return null;
1727
}
1828

1929
export function findObjectFromKey(exports: Record<string, any>, key: string) {

0 commit comments

Comments
 (0)