From 9dfe9c552c372c44e3af27b8749d2ec4acccdbbd Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 21 Jan 2024 10:13:12 +0100 Subject: [PATCH] Use shorter arrow functions where possible For arrow functions that are both simple and short, we can avoid using explicit `return` to shorten them even further without hurting readability. For the `gulp mozcentral` build-target this reduces the overall size of the output by just under 1 kilo-byte (which isn't a lot but still can't hurt). --- src/core/annotation.js | 4 +- src/core/cmap.js | 6 +- src/core/fonts.js | 8 +-- src/core/writer.js | 7 +- src/display/api.js | 45 ++++--------- src/display/display_utils.js | 6 +- src/display/fetch_stream.js | 5 +- src/display/network.js | 5 +- src/display/node_stream.js | 10 +-- src/pdf.sandbox.js | 4 +- src/scripting_api/util.js | 80 ++++++----------------- test/integration/freetext_editor_spec.mjs | 7 +- test/unit/api_spec.js | 14 ++-- test/unit/cff_parser_spec.js | 4 +- test/unit/message_handler_spec.js | 4 +- test/unit/node_stream_spec.js | 4 +- test/unit/pdf_find_controller_spec.js | 4 +- test/unit/scripting_spec.js | 6 +- web/app.js | 4 +- web/pdf_link_service.js | 4 +- web/pdf_scripting_manager.component.js | 8 +-- web/ui_utils.js | 8 +-- 22 files changed, 78 insertions(+), 169 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index f7b5f92ad..e478a19b5 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -4824,9 +4824,7 @@ class StampAnnotation extends MarkupAnnotation { const jpegBufferPromise = canvas .convertToBlob({ type: "image/jpeg", quality: 1 }) - .then(blob => { - return blob.arrayBuffer(); - }); + .then(blob => blob.arrayBuffer()); const xobjectName = Name.get("XObject"); const imageName = Name.get("Image"); diff --git a/src/core/cmap.js b/src/core/cmap.js index f6d579bb6..7cf8567ef 100644 --- a/src/core/cmap.js +++ b/src/core/cmap.js @@ -691,9 +691,9 @@ async function createBuiltInCMap(name, fetchBuiltInCMap) { const cMap = new CMap(true); if (compressionType === CMapCompressionType.BINARY) { - return new BinaryCMapReader().process(cMapData, cMap, useCMap => { - return extendCMap(cMap, fetchBuiltInCMap, useCMap); - }); + return new BinaryCMapReader().process(cMapData, cMap, useCMap => + extendCMap(cMap, fetchBuiltInCMap, useCMap) + ); } if (compressionType === CMapCompressionType.NONE) { const lexer = new Lexer(new Stream(cMapData)); diff --git a/src/core/fonts.js b/src/core/fonts.js index 2ec70e41d..65e7d1fde 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -2093,9 +2093,7 @@ class Font { endOffset: 0, }); } - locaEntries.sort((a, b) => { - return a.offset - b.offset; - }); + locaEntries.sort((a, b) => a.offset - b.offset); // Now the offsets are sorted, calculate the end offset of each glyph. // The last loca entry's endOffset is not calculated since it's the end // of the data and will be stored on the previous entry's endOffset. @@ -2103,9 +2101,7 @@ class Font { locaEntries[i].endOffset = locaEntries[i + 1].offset; } // Re-sort so glyphs aren't out of order. - locaEntries.sort((a, b) => { - return a.index - b.index; - }); + locaEntries.sort((a, b) => a.index - b.index); // Calculate the endOffset of the "first" glyph correctly when there are // *multiple* empty ones at the start of the data (fixes issue14618.pdf). for (i = 0; i < numGlyphs; i++) { diff --git a/src/core/writer.js b/src/core/writer.js index a75c88418..409416596 100644 --- a/src/core/writer.js +++ b/src/core/writer.js @@ -344,10 +344,9 @@ async function incrementalUpdate({ // Add a ref for the new xref and sort them newRefs.push({ ref: refForXrefTable, data: "" }); - newRefs = newRefs.sort((a, b) => { - // compare the refs - return a.ref.num - b.ref.num; - }); + newRefs = newRefs.sort( + (a, b) => /* compare the refs */ a.ref.num - b.ref.num + ); const xrefTableData = [[0, 1, 0xffff]]; const indexes = [0, 1]; diff --git a/src/display/api.js b/src/display/api.js index 2c1ffaf40..f56dcc355 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -771,19 +771,13 @@ class PDFDocumentProxy { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { // For testing purposes. Object.defineProperty(this, "getXFADatasets", { - value: () => { - return this._transport.getXFADatasets(); - }, + value: () => this._transport.getXFADatasets(), }); Object.defineProperty(this, "getXRefPrevValue", { - value: () => { - return this._transport.getXRefPrevValue(); - }, + value: () => this._transport.getXRefPrevValue(), }); Object.defineProperty(this, "getAnnotArray", { - value: pageIndex => { - return this._transport.getAnnotArray(pageIndex); - }, + value: pageIndex => this._transport.getAnnotArray(pageIndex), }); } } @@ -1628,9 +1622,7 @@ class PDFPageProxy { if (this._transport._htmlForXfa) { // TODO: We need to revisit this once the XFA foreground patch lands and // only do this for non-foreground XFA. - return this.getXfa().then(xfa => { - return XfaText.textContent(xfa); - }); + return this.getXfa().then(xfa => XfaText.textContent(xfa)); } const readableStream = this.streamTextContent(params); @@ -2358,21 +2350,16 @@ class WorkerTransport { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { // For testing purposes. Object.defineProperty(this, "getXFADatasets", { - value: () => { - return this.messageHandler.sendWithPromise("GetXFADatasets", null); - }, + value: () => + this.messageHandler.sendWithPromise("GetXFADatasets", null), }); Object.defineProperty(this, "getXRefPrevValue", { - value: () => { - return this.messageHandler.sendWithPromise("GetXRefPrevValue", null); - }, + value: () => + this.messageHandler.sendWithPromise("GetXRefPrevValue", null), }); Object.defineProperty(this, "getAnnotArray", { - value: pageIndex => { - return this.messageHandler.sendWithPromise("GetAnnotArray", { - pageIndex, - }); - }, + value: pageIndex => + this.messageHandler.sendWithPromise("GetAnnotArray", { pageIndex }), }); } } @@ -2737,9 +2724,7 @@ class WorkerTransport { this.fontLoader .bind(font) - .catch(reason => { - return messageHandler.sendWithPromise("FontFallback", { id }); - }) + .catch(() => messageHandler.sendWithPromise("FontFallback", { id })) .finally(() => { if (!params.fontExtraProperties && font.data) { // Immediately release the `font.data` property once the font @@ -3013,9 +2998,7 @@ class WorkerTransport { getOptionalContentConfig() { return this.messageHandler .sendWithPromise("GetOptionalContentConfig", null) - .then(results => { - return new OptionalContentConfig(results); - }); + .then(results => new OptionalContentConfig(results)); } getPermissions() { @@ -3192,9 +3175,7 @@ class RenderTask { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { // For testing purposes. Object.defineProperty(this, "getOperatorList", { - value: () => { - return this.#internalRenderTask.operatorList; - }, + value: () => this.#internalRenderTask.operatorList, }); } } diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 2ff337ee7..e46b67295 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -483,9 +483,9 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory { * @ignore */ _fetchData(url) { - return fetchData(url, /* type = */ "arraybuffer").then(data => { - return new Uint8Array(data); - }); + return fetchData(url, /* type = */ "arraybuffer").then( + data => new Uint8Array(data) + ); } } diff --git a/src/display/fetch_stream.js b/src/display/fetch_stream.js index d68b1d77f..0504f1d9a 100644 --- a/src/display/fetch_stream.js +++ b/src/display/fetch_stream.js @@ -147,9 +147,8 @@ class PDFFetchStreamReader { this._reader = response.body.getReader(); this._headersCapability.resolve(); - const getResponseHeader = name => { - return response.headers.get(name); - }; + const getResponseHeader = name => response.headers.get(name); + const { allowRangeRequests, suggestedLength } = validateRangeRequestCapabilities({ getResponseHeader, diff --git a/src/display/network.js b/src/display/network.js index 998e58809..f9c4d3ff9 100644 --- a/src/display/network.js +++ b/src/display/network.js @@ -279,9 +279,8 @@ class PDFNetworkStreamFullRequestReader { const fullRequestXhrId = this._fullRequestId; const fullRequestXhr = this._manager.getRequestXhr(fullRequestXhrId); - const getResponseHeader = name => { - return fullRequestXhr.getResponseHeader(name); - }; + const getResponseHeader = name => fullRequestXhr.getResponseHeader(name); + const { allowRangeRequests, suggestedLength } = validateRangeRequestCapabilities({ getResponseHeader, diff --git a/src/display/node_stream.js b/src/display/node_stream.js index 185557282..df26d5c73 100644 --- a/src/display/node_stream.js +++ b/src/display/node_stream.js @@ -326,11 +326,11 @@ class PDFNodeStreamFullReader extends BaseFullReader { this._headersCapability.resolve(); this._setReadableStream(response); - const getResponseHeader = name => { - // Make sure that headers name are in lower case, as mentioned - // here: https://nodejs.org/api/http.html#http_message_headers. - return this._readableStream.headers[name.toLowerCase()]; - }; + // Make sure that headers name are in lower case, as mentioned + // here: https://nodejs.org/api/http.html#http_message_headers. + const getResponseHeader = name => + this._readableStream.headers[name.toLowerCase()]; + const { allowRangeRequests, suggestedLength } = validateRangeRequestCapabilities({ getResponseHeader, diff --git a/src/pdf.sandbox.js b/src/pdf.sandbox.js index f7c1d981c..adced961f 100644 --- a/src/pdf.sandbox.js +++ b/src/pdf.sandbox.js @@ -141,9 +141,7 @@ class Sandbox { } function QuickJSSandbox() { - return ModuleLoader().then(module => { - return new Sandbox(window, module); - }); + return ModuleLoader().then(module => new Sandbox(window, module)); } export { QuickJSSandbox }; diff --git a/src/scripting_api/util.js b/src/scripting_api/util.js index e807bb4a2..5220c5b64 100644 --- a/src/scripting_api/util.js +++ b/src/scripting_api/util.js @@ -212,66 +212,26 @@ class Util extends PDFObject { } const handlers = { - mmmm: data => { - return this._months[data.month]; - }, - mmm: data => { - return this._months[data.month].substring(0, 3); - }, - mm: data => { - return (data.month + 1).toString().padStart(2, "0"); - }, - m: data => { - return (data.month + 1).toString(); - }, - dddd: data => { - return this._days[data.dayOfWeek]; - }, - ddd: data => { - return this._days[data.dayOfWeek].substring(0, 3); - }, - dd: data => { - return data.day.toString().padStart(2, "0"); - }, - d: data => { - return data.day.toString(); - }, - yyyy: data => { - return data.year.toString(); - }, - yy: data => { - return (data.year % 100).toString().padStart(2, "0"); - }, - HH: data => { - return data.hours.toString().padStart(2, "0"); - }, - H: data => { - return data.hours.toString(); - }, - hh: data => { - return (1 + ((data.hours + 11) % 12)).toString().padStart(2, "0"); - }, - h: data => { - return (1 + ((data.hours + 11) % 12)).toString(); - }, - MM: data => { - return data.minutes.toString().padStart(2, "0"); - }, - M: data => { - return data.minutes.toString(); - }, - ss: data => { - return data.seconds.toString().padStart(2, "0"); - }, - s: data => { - return data.seconds.toString(); - }, - tt: data => { - return data.hours < 12 ? "am" : "pm"; - }, - t: data => { - return data.hours < 12 ? "a" : "p"; - }, + mmmm: data => this._months[data.month], + mmm: data => this._months[data.month].substring(0, 3), + mm: data => (data.month + 1).toString().padStart(2, "0"), + m: data => (data.month + 1).toString(), + dddd: data => this._days[data.dayOfWeek], + ddd: data => this._days[data.dayOfWeek].substring(0, 3), + dd: data => data.day.toString().padStart(2, "0"), + d: data => data.day.toString(), + yyyy: data => data.year.toString(), + yy: data => (data.year % 100).toString().padStart(2, "0"), + HH: data => data.hours.toString().padStart(2, "0"), + H: data => data.hours.toString(), + hh: data => (1 + ((data.hours + 11) % 12)).toString().padStart(2, "0"), + h: data => (1 + ((data.hours + 11) % 12)).toString(), + MM: data => data.minutes.toString().padStart(2, "0"), + M: data => data.minutes.toString(), + ss: data => data.seconds.toString().padStart(2, "0"), + s: data => data.seconds.toString(), + tt: data => (data.hours < 12 ? "am" : "pm"), + t: data => (data.hours < 12 ? "a" : "p"), }; const data = { diff --git a/test/integration/freetext_editor_spec.mjs b/test/integration/freetext_editor_spec.mjs index b2cd4f77d..ce068c3b4 100644 --- a/test/integration/freetext_editor_spec.mjs +++ b/test/integration/freetext_editor_spec.mjs @@ -237,9 +237,10 @@ describe("FreeText Editor", () => { await clearAll(page); for (const n of [0, 1, 2]) { - const hasEditor = await page.evaluate(sel => { - return !!document.querySelector(sel); - }, getEditorSelector(n)); + const hasEditor = await page.evaluate( + sel => !!document.querySelector(sel), + getEditorSelector(n) + ); expect(hasEditor).withContext(`In ${browserName}`).toEqual(false); } diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 341f406fd..333a73ef9 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -942,14 +942,10 @@ describe("api", function () { ); const loadingTask1 = getDocument(basicApiGetDocumentParams); - const promise1 = loadingTask1.promise.then(pdfDoc => { - return pdfDoc.numPages; - }); + const promise1 = loadingTask1.promise.then(pdfDoc => pdfDoc.numPages); const loadingTask2 = getDocument(tracemonkeyGetDocumentParams); - const promise2 = loadingTask2.promise.then(pdfDoc => { - return pdfDoc.numPages; - }); + const promise2 = loadingTask2.promise.then(pdfDoc => pdfDoc.numPages); const [numPages1, numPages2] = await Promise.all([promise1, promise2]); expect(numPages1).toEqual(3); @@ -3901,9 +3897,9 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`) true ); expect( - currentImgData.data.every((value, index) => { - return value === firstImgData.data[index]; - }) + currentImgData.data.every( + (value, index) => value === firstImgData.data[index] + ) ).toEqual(true); if (i === NUM_PAGES_THRESHOLD) { diff --git a/test/unit/cff_parser_spec.js b/test/unit/cff_parser_spec.js index cc0b3fcc3..e7b7410a5 100644 --- a/test/unit/cff_parser_spec.js +++ b/test/unit/cff_parser_spec.js @@ -377,9 +377,7 @@ describe("CFFCompiler", function () { bytes = new Uint8Array(bytes); return new CFFParser( { - getBytes: () => { - return bytes; - }, + getBytes: () => bytes, }, {}, SEAC_ANALYSIS_ENABLED diff --git a/test/unit/message_handler_spec.js b/test/unit/message_handler_spec.js index 09745a10d..37ecb3554 100644 --- a/test/unit/message_handler_spec.js +++ b/test/unit/message_handler_spec.js @@ -24,9 +24,7 @@ import { MessageHandler } from "../../src/shared/message_handler.js"; describe("message_handler", function () { // Sleep function to wait for sometime, similar to setTimeout but faster. function sleep(ticks) { - return Promise.resolve().then(() => { - return ticks && sleep(ticks - 1); - }); + return Promise.resolve().then(() => ticks && sleep(ticks - 1)); } describe("sendWithStream", function () { diff --git a/test/unit/node_stream_spec.js b/test/unit/node_stream_spec.js index 919fc63dd..6158aa9b7 100644 --- a/test/unit/node_stream_spec.js +++ b/test/unit/node_stream_spec.js @@ -62,9 +62,7 @@ describe("node_stream", function () { const [start, end] = request.headers.range .split("=")[1] .split("-") - .map(x => { - return Number(x); - }); + .map(x => Number(x)); const stream = fs.createReadStream(filePath, { start, end }); response.writeHead(206, { "Content-Type": "application/pdf", diff --git a/test/unit/pdf_find_controller_spec.js b/test/unit/pdf_find_controller_spec.js index db8b29548..b89316e5b 100644 --- a/test/unit/pdf_find_controller_spec.js +++ b/test/unit/pdf_find_controller_spec.js @@ -121,9 +121,7 @@ function testSearch({ } } - const totalMatches = matchesPerPage.reduce((a, b) => { - return a + b; - }); + const totalMatches = matchesPerPage.reduce((a, b) => a + b); if (updateFindControlState) { eventBus.on( diff --git a/test/unit/scripting_spec.js b/test/unit/scripting_spec.js index 57ec42809..89e45bced 100644 --- a/test/unit/scripting_spec.js +++ b/test/unit/scripting_spec.js @@ -52,9 +52,9 @@ describe("Scripting", function () { send_queue.set(command, { command, value }); }; // eslint-disable-next-line no-unsanitized/method - const promise = import(sandboxBundleSrc).then(pdfjsSandbox => { - return pdfjsSandbox.QuickJSSandbox(); - }); + const promise = import(sandboxBundleSrc).then(pdfjsSandbox => + pdfjsSandbox.QuickJSSandbox() + ); sandbox = { createSandbox(data) { promise.then(sbx => sbx.create(data)); diff --git a/web/app.js b/web/app.js index 2e8c7f4aa..e5e506f92 100644 --- a/web/app.js +++ b/web/app.js @@ -1764,9 +1764,7 @@ const PDFViewerApplication = { .catch(() => { /* Avoid breaking printing; ignoring errors. */ }) - .then(() => { - return this.pdfDocument?.annotationStorage.print; - }); + .then(() => this.pdfDocument?.annotationStorage.print); if (this.printService) { // There is no way to suppress beforePrint/afterPrint events, diff --git a/web/pdf_link_service.js b/web/pdf_link_service.js index 57372951b..455a00ff4 100644 --- a/web/pdf_link_service.js +++ b/web/pdf_link_service.js @@ -54,9 +54,7 @@ function addLinkAttributes(link, { url, target, rel, enabled = true } = {}) { } else { link.href = ""; link.title = `Disabled: ${url}`; - link.onclick = () => { - return false; - }; + link.onclick = () => false; } let targetStr = ""; // LinkTarget.NONE diff --git a/web/pdf_scripting_manager.component.js b/web/pdf_scripting_manager.component.js index 18ab3b687..6e1730186 100644 --- a/web/pdf_scripting_manager.component.js +++ b/web/pdf_scripting_manager.component.js @@ -30,13 +30,9 @@ class PDFScriptingManagerComponents extends PDFScriptingManager { } options.externalServices ||= { - createScripting: () => { - return new GenericScripting(options.sandboxBundleSrc); - }, - }; - options.docProperties ||= pdfDocument => { - return docProperties(pdfDocument); + createScripting: () => new GenericScripting(options.sandboxBundleSrc), }; + options.docProperties ||= pdfDocument => docProperties(pdfDocument); super(options); } } diff --git a/web/ui_utils.js b/web/ui_utils.js index a2d58993e..a6c0bc7f1 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -206,20 +206,18 @@ function parseQueryString(query) { return params; } -const InvisibleCharactersRegExp = /[\x00-\x1F]/g; +const InvisibleCharsRegExp = /[\x00-\x1F]/g; /** * @param {string} str * @param {boolean} [replaceInvisible] */ function removeNullCharacters(str, replaceInvisible = false) { - if (!InvisibleCharactersRegExp.test(str)) { + if (!InvisibleCharsRegExp.test(str)) { return str; } if (replaceInvisible) { - return str.replaceAll(InvisibleCharactersRegExp, m => { - return m === "\x00" ? "" : " "; - }); + return str.replaceAll(InvisibleCharsRegExp, m => (m === "\x00" ? "" : " ")); } return str.replaceAll("\x00", ""); }