Cleanup & Improvements

- Rename `getByProps()` to `getByKeys()` and deprecated *byProps methods & filters.
- Fix a couple string errors
- Rename `getByPrototypes()` to `getByPrototypeFields()`
- Rename `getMangled()` to `getWithKey()`
This commit is contained in:
Strencher 2023-05-24 23:00:42 +02:00
parent 68c6b81ed7
commit 83c0c81079
2 changed files with 31 additions and 21 deletions

View File

@ -32,12 +32,17 @@ const Webpack = {
* @memberof Webpack * @memberof Webpack
*/ */
Filters: { Filters: {
/**
* @deprecated
*/
byProps(...props) {return Filters.byKeys(props);},
/** /**
* Generates a function that filters by a set of properties. * Generates a function that filters by a set of properties.
* @param {...string} props List of property names * @param {...string} keys List of property names
* @returns {function} A filter that checks for a set of properties * @returns {function} A filter that checks for a set of properties
*/ */
byProps(...props) {return Filters.byProps(props);}, byKeys(...keys) {return Filters.byKeys(keys);},
/** /**
* Generates a function that filters by a set of properties on the object's prototype. * Generates a function that filters by a set of properties on the object's prototype.
@ -85,11 +90,11 @@ const Webpack = {
* @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack export getters. * @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack export getters.
* @return {[Any, string]} * @return {[Any, string]}
*/ */
getMangled(filter, options = {}) { getWithKey(filter, options = {}) {
if (("first" in options)) return Logger.error("BdApi.Webpack~getModule", "Unsupported option first."); if (("first" in options)) return Logger.error("BdApi.Webpack~getWithKey", "Unsupported option first.");
if (("defaultExport" in options) && typeof(options.defaultExport) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.defaultExport", options.defaultExport, "boolean expected."); if (("defaultExport" in options) && typeof(options.defaultExport) !== "boolean") return Logger.error("BdApi.Webpack~getWithKey", "Unsupported type used for options.defaultExport", options.defaultExport, "boolean expected.");
if (("searchExports" in options) && typeof(options.searchExports) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.searchExports", options.searchExports, "boolean expected."); if (("searchExports" in options) && typeof(options.searchExports) !== "boolean") return Logger.error("BdApi.Webpack~getWithKey", "Unsupported type used for options.searchExports", options.searchExports, "boolean expected.");
return WebpackModules.getMangled(filter, options); return WebpackModules.getWithKey(filter, options);
}, },
/** /**
@ -102,13 +107,18 @@ const Webpack = {
* @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack exports * @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack exports
* @return {any} * @return {any}
*/ */
getModule(filter, options = {}) { get(filter, options = {}) {
if (("first" in options) && typeof(options.first) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.first", options.first, "boolean expected."); if (("first" in options) && typeof(options.first) !== "boolean") return Logger.error("BdApi.Webpack~get", "Unsupported type used for options.first", options.first, "boolean expected.");
if (("defaultExport" in options) && typeof(options.defaultExport) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.defaultExport", options.defaultExport, "boolean expected."); if (("defaultExport" in options) && typeof(options.defaultExport) !== "boolean") return Logger.error("BdApi.Webpack~get", "Unsupported type used for options.defaultExport", options.defaultExport, "boolean expected.");
if (("searchExports" in options) && typeof(options.searchExports) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.searchExports", options.searchExports, "boolean expected."); if (("searchExports" in options) && typeof(options.searchExports) !== "boolean") return Logger.error("BdApi.Webpack~get", "Unsupported type used for options.searchExports", options.searchExports, "boolean expected.");
return WebpackModules.getModule(filter, options); return WebpackModules.getModule(filter, options);
}, },
/**
* @deprecated
*/
getModule() {return this.get.apply(this, arguments);},
/** /**
* Finds multiple modules using multiple filters. * Finds multiple modules using multiple filters.
* @memberof Webpack * @memberof Webpack
@ -134,7 +144,7 @@ const Webpack = {
waitForModule(filter, options = {}) { waitForModule(filter, options = {}) {
if (("defaultExport" in options) && typeof(options.defaultExport) !== "boolean") return Logger.error("BdApi.Webpack~waitForModule", "Unsupported type used for options.defaultExport", options.defaultExport, "boolean expected."); if (("defaultExport" in options) && typeof(options.defaultExport) !== "boolean") return Logger.error("BdApi.Webpack~waitForModule", "Unsupported type used for options.defaultExport", options.defaultExport, "boolean expected.");
if (("signal" in options) && !(options.signal instanceof AbortSignal)) return Logger.error("BdApi.Webpack~waitForModule", "Unsupported type used for options.signal", options.signal, "AbortSignal expected."); if (("signal" in options) && !(options.signal instanceof AbortSignal)) return Logger.error("BdApi.Webpack~waitForModule", "Unsupported type used for options.signal", options.signal, "AbortSignal expected.");
if (("searchExports" in options) && typeof(options.searchExports) !== "boolean") return Logger.error("BdApi.Webpack~getModule", "Unsupported type used for options.searchExports", options.searchExports, "boolean expected."); if (("searchExports" in options) && typeof(options.searchExports) !== "boolean") return Logger.error("BdApi.Webpack~waitForModule", "Unsupported type used for options.searchExports", options.searchExports, "boolean expected.");
return WebpackModules.getLazy(filter, options); return WebpackModules.getLazy(filter, options);
}, },
@ -184,7 +194,7 @@ const Webpack = {
* @param {...string} prototypes Properties to use to filter modules * @param {...string} prototypes Properties to use to filter modules
* @return {Any[]} * @return {Any[]}
*/ */
getAllByPrototypes(...prototypes) { getAllByPrototypeFields(...prototypes) {
const options = getOptions(prototypes, {first: false}); const options = getOptions(prototypes, {first: false});
return WebpackModules.getModule(Filters.byPrototypeFields(prototypes), options); return WebpackModules.getModule(Filters.byPrototypeFields(prototypes), options);
@ -195,10 +205,10 @@ const Webpack = {
* @param {...string} props Properties to use to filter modules * @param {...string} props Properties to use to filter modules
* @return {Any} * @return {Any}
*/ */
getByProps(...props) { getByKeys(...props) {
const options = getOptions(props); const options = getOptions(props);
return this.getModule(Filters.byProps(props), options); return WebpackModules.getModule(Filters.byKeys(props), options);
}, },
/** /**
@ -206,10 +216,10 @@ const Webpack = {
* @param {...string} props Properties to use to filter modules * @param {...string} props Properties to use to filter modules
* @return {Any[]} * @return {Any[]}
*/ */
getAllByProps(...props) { getAllByKeys(...props) {
const options = getOptions(props, {first: false}); const options = getOptions(props, {first: false});
return WebpackModules.getModule(Filters.byProps(props), options); return WebpackModules.getModule(Filters.byKeys(props), options);
}, },
/** /**

View File

@ -22,7 +22,7 @@ export class Filters {
* @param {module:WebpackModules.Filters~filter} filter - Additional filter * @param {module:WebpackModules.Filters~filter} filter - Additional filter
* @returns {module:WebpackModules.Filters~filter} - A filter that checks for a set of properties * @returns {module:WebpackModules.Filters~filter} - A filter that checks for a set of properties
*/ */
static byProps(props, filter = m => m) { static byKeys(props, filter = m => m) {
return module => { return module => {
if (!module) return false; if (!module) return false;
if (typeof(module) !== "object" && typeof(module) !== "function") return false; if (typeof(module) !== "object" && typeof(module) !== "function") return false;
@ -275,7 +275,7 @@ export default class WebpackModules {
* @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack export getters. * @param {Boolean} [options.searchExports=false] Whether to execute the filter on webpack export getters.
* @return {[Any, string]} * @return {[Any, string]}
*/ */
static *getMangled(filter, {target = null, ...rest} = {}) { static *getWithKey(filter, {target = null, ...rest} = {}) {
yield target ??= this.getModule(exports => yield target ??= this.getModule(exports =>
Object.values(exports).some(filter), Object.values(exports).some(filter),
rest rest
@ -333,7 +333,7 @@ export default class WebpackModules {
* @return {Any} * @return {Any}
*/ */
static getByProps(...props) { static getByProps(...props) {
return this.getModule(Filters.byProps(props)); return this.getModule(Filters.byKeys(props));
} }
/** /**
@ -342,7 +342,7 @@ export default class WebpackModules {
* @return {Any} * @return {Any}
*/ */
static getAllByProps(...props) { static getAllByProps(...props) {
return this.getModule(Filters.byProps(props), {first: false}); return this.getModule(Filters.byKeys(props), {first: false});
} }
/** /**