Add definePatch helper function

main
peshomir 2025-11-03 16:37:31 +02:00
parent 5dfe34c2d0
commit 46d925daa1
1 changed files with 9 additions and 3 deletions

View File

@ -106,6 +106,7 @@ class ModUtils {
} }
return Object.fromEntries(Object.entries(groups).map(([identifier, groupNumber]) => [identifier, expressionMatchResult[groupNumber]])); return Object.fromEntries(Object.entries(groups).map(([identifier, groupNumber]) => [identifier, expressionMatchResult[groupNumber]]));
} }
/** @param {{ [x: string]: string; }} [nameMappings] */
matchRawCode(/** @type {string} */ raw, nameMappings) { matchRawCode(/** @type {string} */ raw, nameMappings) {
const { expression, groups } = this.generateRegularExpression(raw, false, nameMappings); const { expression, groups } = this.generateRegularExpression(raw, false, nameMappings);
try { try {
@ -148,8 +149,8 @@ class ModUtils {
* @typedef {{ dictionary?: { [x: string]: string } }} BaseOptions * @typedef {{ dictionary?: { [x: string]: string } }} BaseOptions
* @typedef {BaseOptions & { addToDictionary?: string[] }} MatchCodeOptions * @typedef {BaseOptions & { addToDictionary?: string[] }} MatchCodeOptions
*/ */
matchCode(code, /** @type {MatchCodeOptions=} */ options) { matchCode(/** @type {string} */ code, /** @type {MatchCodeOptions=} */ options) {
const result = this.matchRawCode(minifyCode(code)); const result = this.matchRawCode(minifyCode(code), options?.dictionary);
if (options?.addToDictionary !== undefined) { if (options?.addToDictionary !== undefined) {
options.addToDictionary.forEach(varName => { options.addToDictionary.forEach(varName => {
if (result[varName] === undefined) if (result[varName] === undefined)
@ -186,3 +187,8 @@ class ModUtils {
} }
export default ModUtils; export default ModUtils;
/** @param {(modUtils: ModUtils) => any} callback */
export function definePatch(callback) {
return (/** @type {ModUtils} */ modUtils) => callback(modUtils)
}