Skip to content

Commit 34f2718

Browse files
committed
Reduce duplication when parsing searchParams in the getPdfFilenameFromUrl function
Currently we essentially "duplicate" the same code for parsing the `values` and `keys` of the `searchParams`, which seems a little unnecessary. To be able to parse the `searchParams` from the end, we currently create an Array (from the Iterator) and then reverse it before finally looping through it. Here the latter two steps can be replaced with the `Array.prototype.findLast()` method instead. *Please note:* I completely understand if this patch is rejected, on account of being less readable than the current code.
1 parent 74ab1a9 commit 34f2718

1 file changed

Lines changed: 8 additions & 13 deletions

File tree

src/display/display_utils.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,14 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
395395
}
396396

397397
if (newURL.searchParams.size > 0) {
398-
const values = Array.from(newURL.searchParams.values()).reverse();
399-
for (const value of values) {
400-
if (pdfRegex.test(value)) {
401-
// If any of the search parameters ends with ".pdf", return it.
402-
return decode(value);
403-
}
404-
}
405-
const keys = Array.from(newURL.searchParams.keys()).reverse();
406-
for (const key of keys) {
407-
if (pdfRegex.test(key)) {
408-
// If any of the search parameter keys ends with ".pdf", return it.
409-
return decode(key);
410-
}
398+
const getLast = iterator => [...iterator].findLast(v => pdfRegex.test(v));
399+
400+
// If any of the search parameters ends with ".pdf", return it.
401+
const name =
402+
getLast(newURL.searchParams.values()) ??
403+
getLast(newURL.searchParams.keys());
404+
if (name) {
405+
return decode(name);
411406
}
412407
}
413408

0 commit comments

Comments
 (0)