Skip to content

Commit

Permalink
feat: allow filtering per model by a specific series (#595)
Browse files Browse the repository at this point in the history
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
  • Loading branch information
SesioN and jef committed Oct 25, 2020
1 parent 699de57 commit a42418f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -124,7 +124,7 @@ Here is a list of variables that you can use to customize your newly copied `.en
| `PUSHOVER_PRIORITY` | Pushover message priority |
| `SCREENSHOT` | Capture screenshot of page if a card is found | Default: `true` |
| `SHOW_ONLY_BRANDS` | Filter to show specified brands | Comma separated, e.g.: `evga,zotac` |
| `SHOW_ONLY_MODELS` | Filter to show specified models | Comma separated, e.g.: `founders edition,rog strix` |
| `SHOW_ONLY_MODELS` | Filter to show specified models | Both supported formats are comma separated <br/><br/>1. Standard E.g.: `founders edition,rog strix` <br/><br/> 2. Advanced E.g: `MODEL:SERIES`, E.g: `founders edition:3090,rog strix` |
| `SHOW_ONLY_SERIES` | Filter to show specified series | Comma separated, e.g.: `3080` |
| `SLACK_CHANNEL` | Slack channel for posting | E.g.: `update`, no need for `#` |
| `SLACK_TOKEN` | Slack API token | |
Expand Down
8 changes: 7 additions & 1 deletion src/config.ts
Expand Up @@ -243,7 +243,13 @@ const store = {
},
microCenterLocation: envOrArray(process.env.MICROCENTER_LOCATION, ['web']),
showOnlyBrands: envOrArray(process.env.SHOW_ONLY_BRANDS),
showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS),
showOnlyModels: envOrArray(process.env.SHOW_ONLY_MODELS).map(entry => {
const [name, series] = entry.match(/[^:]+/g) ?? [];
return {
name: envOrString(name),
series: envOrString(series)
};
}),
showOnlySeries: envOrArray(process.env.SHOW_ONLY_SERIES, ['3070', '3080', '3090']),
stores: envOrArray(process.env.STORES, ['nvidia']).map(entry => {
const [name, minPageSleep, maxPageSleep] = entry.match(/[^:]+/g) ?? [];
Expand Down
16 changes: 11 additions & 5 deletions src/store/filter.ts
Expand Up @@ -18,16 +18,22 @@ function filterBrand(brand: Link['brand']): boolean {
* Returns true if the model should be checked for stock
*
* @param model The model of the GPU
* @param series The series of the GPU
*/
function filterModel(model: Link['model']): boolean {
function filterModel(model: Link['model'], series: Link['series']): boolean {
if (config.store.showOnlyModels.length === 0) {
return true;
}

const sanitizedModel = model.replace(/\s/g, '');
for (const configModel of config.store.showOnlyModels) {
const sanitizedConfigModel = configModel.replace(/\s/g, '');
if (sanitizedModel === sanitizedConfigModel) {
const sanitizedSeries = series.replace(/\s/g, '');
for (const configModelEntry of config.store.showOnlyModels) {
const sanitizedConfigModel = configModelEntry.name.replace(/\s/g, '');
const sanitizedConfigSeries = configModelEntry.series.replace(/\s/g, '');
if (sanitizedConfigSeries ?
sanitizedSeries === sanitizedConfigSeries && sanitizedModel === sanitizedConfigModel :
sanitizedModel === sanitizedConfigModel
) {
return true;
}
}
Expand Down Expand Up @@ -56,7 +62,7 @@ export function filterSeries(series: Link['series']): boolean {
export function filterStoreLink(link: Link): boolean {
return (
filterBrand(link.brand) &&
filterModel(link.model) &&
filterModel(link.model, link.series) &&
filterSeries(link.series)
);
}
4 changes: 3 additions & 1 deletion src/store/model/index.ts
Expand Up @@ -115,7 +115,9 @@ if (config.store.showOnlyBrands.length > 0) {
}

if (config.store.showOnlyModels.length > 0) {
logger.info(`ℹ selected models: ${config.store.showOnlyModels.join(', ')}`);
logger.info(`ℹ selected models: ${config.store.showOnlyModels.map(entry => {
return entry.series ? entry.name + ' (' + entry.series + ')' : entry.name;
}).join(', ')}`);
}

if (config.store.showOnlySeries.length > 0) {
Expand Down

0 comments on commit a42418f

Please sign in to comment.