[api-minor] Convert getViewerPreferences to return data in a Map

Compared to regular Objects there's a number of advantages to using Maps:
 - They support proper iteration.
 - They have a simple way to check for the existence of data.
 - They have a simple/efficient way to check the number of elements.

If this functionality was added today, I cannot imagine that we'd choose an Object for this data.
This commit is contained in:
Jonas Jenwald 2026-07-20 16:48:31 +02:00
parent cd03276d60
commit de3eecca11
3 changed files with 12 additions and 12 deletions

View File

@ -1076,17 +1076,18 @@ class Catalog {
break; break;
case "PrintPageRange": case "PrintPageRange":
// The number of elements must be even. // The number of elements must be even.
if (Array.isArray(value) && value.length % 2 === 0) { if (
const isValid = value.every( Array.isArray(value) &&
value.length % 2 === 0 &&
value.every(
(page, i, arr) => (page, i, arr) =>
Number.isInteger(page) && Number.isInteger(page) &&
page > 0 && page > 0 &&
(i === 0 || page >= arr[i - 1]) && (i === 0 || page >= arr[i - 1]) &&
page <= this.numPages page <= this.numPages
); )
if (isValid) { ) {
prefValue = value; prefValue = value;
}
} }
break; break;
case "NumCopies": case "NumCopies":
@ -1103,8 +1104,7 @@ class Catalog {
warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`); warn(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`);
continue; continue;
} }
prefs ??= Object.create(null); (prefs ??= new Map()).set(key, prefValue);
prefs[key] = prefValue;
} }
return shadow(this, "viewerPreferences", prefs); return shadow(this, "viewerPreferences", prefs);
} }

View File

@ -820,9 +820,9 @@ class PDFDocumentProxy {
} }
/** /**
* @returns {Promise<Object | null>} A promise that is resolved with an * @returns {Promise<Map | null>} A promise that is resolved with a {Map}
* {Object} containing the viewer preferences, or `null` when no viewer * containing the viewer preferences, or `null` when no viewer preferences
* preferences are present in the PDF file. * are present in the PDF file.
*/ */
getViewerPreferences() { getViewerPreferences() {
return this._transport.getViewerPreferences(); return this._transport.getViewerPreferences();

View File

@ -1760,7 +1760,7 @@ describe("api", function () {
it("gets non-default viewer preferences", async function () { it("gets non-default viewer preferences", async function () {
const prefs = await pdfDocument.getViewerPreferences(); const prefs = await pdfDocument.getViewerPreferences();
expect(prefs).toEqual({ Direction: "L2R" }); expect(prefs).toEqual(new Map([["Direction", "L2R"]]));
}); });
it("gets default open action", async function () { it("gets default open action", async function () {