-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6d29d1
commit da71061
Showing
4 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
class MacrosManager { | ||
/** | ||
* @type {Map<string, import("..").MacroOptions>} | ||
*/ | ||
cache = new Map(); | ||
|
||
/** | ||
* Adds a macro to the manager. | ||
* @param {import("..").MacroOptions} macro - The macro to be added. | ||
* @returns {this} | ||
*/ | ||
add(macro) { | ||
macro.name = macro.name.startsWith("#") | ||
? macro.name.slice(1) | ||
: macro.name; | ||
|
||
this.cache.set(macro.name, macro); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Adds many macros to the manager. | ||
* @param {import("..").MacroOptions[]} macros - The macros to be added. | ||
* @returns {this} | ||
*/ | ||
addMany(macros) { | ||
for (const macro of macros) { | ||
this.add(macro); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Get a macro. | ||
* @param {string | ((macro: import("..").MacroOptions) => boolean)} name - The name of the macro. | ||
* @returns {?import("..").MacroOptions} | ||
*/ | ||
get(name) { | ||
if (typeof name === "string") return this.cache.get(name) || null; | ||
|
||
return this.toArray().find(name) || null; | ||
} | ||
|
||
/** | ||
* Return the list of cached macro names. | ||
* @returns {string[]} | ||
*/ | ||
list() { | ||
return [...this.cache.keys()]; | ||
} | ||
|
||
/** | ||
* Return an array of cached macros. | ||
* @returns {import("..").MacroOptions[]} | ||
*/ | ||
toArray() { | ||
return [...this.cache.values()]; | ||
} | ||
}; | ||
|
||
/** | ||
* Creates a pattern to match the cached macros. | ||
* @param {string[]} names - The names of the macros. | ||
* @returns {RegExp} | ||
*/ | ||
function createMacrosPattern(names) { | ||
return new RegExp(`#(${names.join("|")})`, "g"); | ||
}; | ||
|
||
/** | ||
* Check whether the given code has macros inside. | ||
* @param {string[]} names - The names of the cached macros. | ||
* @param {string} code - The code to be tested. | ||
* @returns {boolean} | ||
*/ | ||
function hasMacros(names, code) { | ||
return Array.isArray(code.match(createMacrosPattern(names))) | ||
}; | ||
|
||
/** | ||
* Resolve the macros in the command code with actual code. | ||
* @param {import("..").MacroOptions[]} macros - The cached macros. | ||
* @param {string} code - The code to be resolved. | ||
* @returns {string} | ||
*/ | ||
function resolveMacros(macros, code) { | ||
const matchedMacros = [...new Set(code.match(createMacrosPattern(macros.map(m => m.name))) ?? [])]; | ||
let newCode = code; | ||
|
||
for (const matchedMacro of matchedMacros) { | ||
newCode = newCode.replace( | ||
createMacrosPattern( | ||
[matchedMacro.slice(1)] | ||
), | ||
macros.find( | ||
macro => macro.name === matchedMacro.slice(1) | ||
)?.code || "" | ||
); | ||
} | ||
|
||
return newCode; | ||
}; | ||
|
||
module.exports = { | ||
hasMacros, | ||
MacrosManager, | ||
resolveMacros | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters