Skip to content

Commit

Permalink
Refactor: filter out DATs earlier (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Feb 11, 2024
1 parent 64b027f commit b7c4147
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions src/modules/datScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class DATScanner extends Scanner {
this.progressBar.logDebug(`parsing ${datFiles.length.toLocaleString()} DAT file${datFiles.length !== 1 ? 's' : ''}`);
await this.progressBar.setSymbol(ProgressBarSymbol.PARSING_CONTENTS);

const results = (await new DriveSemaphore(this.options.getReaderThreads()).map(
return (await new DriveSemaphore(this.options.getReaderThreads()).map(
datFiles,
async (datFile) => {
await this.progressBar.incrementProgress();
Expand All @@ -120,41 +120,15 @@ export default class DATScanner extends Scanner {

await this.progressBar.incrementDone();
this.progressBar.removeWaitingMessage(waitingMessage);

if (dat && this.shouldFilterOut(dat)) {
return undefined;
}
return dat;
},
))
.filter(ArrayPoly.filterNotNullish)
.map((dat) => this.sanitizeDat(dat));

return results
.filter((dat) => {
const datNameRegex = this.options.getDatNameRegex();
return !datNameRegex
|| datNameRegex.some((regex) => regex.test(dat.getName()));
})
.filter((dat) => {
const datNameRegexExclude = this.options.getDatNameRegexExclude();
return !datNameRegexExclude
|| !datNameRegexExclude.some((regex) => regex.test(dat.getName()));
})
.filter((dat) => {
const datDescription = dat.getDescription();
if (!datDescription) {
return true;
}
const datDescriptionRegex = this.options.getDatDescriptionRegex();
return !datDescriptionRegex
|| datDescriptionRegex.some((regex) => regex.test(datDescription));
})
.filter((dat) => {
const datDescription = dat.getDescription();
if (!datDescription) {
return true;
}
const datDescriptionRegexExclude = this.options.getDatDescriptionRegexExclude();
return !datDescriptionRegexExclude
|| !datDescriptionRegexExclude.some((regex) => regex.test(datDescription));
})
.map((dat) => this.sanitizeDat(dat))
.sort((a, b) => a.getNameShort().localeCompare(b.getNameShort()));
}

Expand Down Expand Up @@ -445,6 +419,38 @@ export default class DATScanner extends Scanner {
});
}

private shouldFilterOut(dat: DAT): boolean {
const datNameRegex = this.options.getDatNameRegex();
if (datNameRegex && !datNameRegex.some((regex) => regex.test(dat.getName()))) {
return true;
}

const datNameRegexExclude = this.options.getDatNameRegexExclude();
if (datNameRegexExclude && datNameRegexExclude.some((regex) => regex.test(dat.getName()))) {
return true;
}

const datDescription = dat.getDescription();

const datDescriptionRegex = this.options.getDatDescriptionRegex();
if (datDescription
&& datDescriptionRegex
&& !datDescriptionRegex.some((regex) => regex.test(datDescription))
) {
return true;
}

const datDescriptionRegexExclude = this.options.getDatDescriptionRegexExclude();
if (datDescription
&& datDescriptionRegexExclude
&& datDescriptionRegexExclude.some((regex) => regex.test(datDescription))
) {
return true;
}

return false;
}

private sanitizeDat(dat: DAT): DAT {
const games = dat.getGames()
.map((game) => {
Expand Down

0 comments on commit b7c4147

Please sign in to comment.