From 05edd91bdb3690b253fb598f2eb716c7a66e154b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 22 Feb 2022 11:55:34 +0100 Subject: [PATCH] Remove the `isNum` helper function The call-sites are replaced by direct `typeof`-checks instead, which removes unnecessary function calls. Note that in the `src/`-folder we already had more `typeof`-cases than `isNum`-calls. These changes were *mostly* done using regular expression search-and-replace, with two exceptions: - In `Font._charToGlyph` we no longer unconditionally update the `width`, since that seems completely unnecessary. - In `PDFDocument.documentInfo`, when parsing custom entries, we now do the `typeof`-check once. --- src/core/catalog.js | 5 ++--- src/core/document.js | 13 ++++++++----- src/core/evaluator.js | 20 +++++++++++--------- src/core/fonts.js | 5 +++-- src/core/parser.js | 3 +-- src/display/canvas.js | 5 ++--- src/display/svg.js | 3 +-- src/shared/fonts_utils.js | 4 ++-- src/shared/util.js | 5 ----- test/unit/util_spec.js | 18 ------------------ 10 files changed, 30 insertions(+), 51 deletions(-) diff --git a/src/core/catalog.js b/src/core/catalog.js index edaec0547..d4e925809 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -26,7 +26,6 @@ import { FormatError, info, isBool, - isNum, isString, objectSize, PermissionFlag, @@ -381,7 +380,7 @@ class Catalog { } let flags = encrypt.get("P"); - if (!isNum(flags)) { + if (typeof flags !== "number") { return null; } @@ -1475,7 +1474,7 @@ class Catalog { switch (actionName) { case "ResetForm": const flags = action.get("Flags"); - const include = ((isNum(flags) ? flags : 0) & 1) === 0; + const include = ((typeof flags === "number" ? flags : 0) & 1) === 0; const fields = []; const refs = []; for (const obj of action.get("Fields") || []) { diff --git a/src/core/document.js b/src/core/document.js index bcef7d875..0c205a21a 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -20,8 +20,6 @@ import { InvalidPDFException, isArrayBuffer, isArrayEqual, - isBool, - isNum, isString, OPS, PageActionEventType, @@ -177,7 +175,7 @@ class Page { get userUnit() { let obj = this.pageDict.get("UserUnit"); - if (!isNum(obj) || obj <= 0) { + if (typeof obj !== "number" || obj <= 0) { obj = DEFAULT_USER_UNIT; } return shadow(this, "userUnit", obj); @@ -1193,10 +1191,15 @@ class PDFDocument { // For custom values, only accept white-listed types to prevent // errors that would occur when trying to send non-serializable // objects to the main-thread (for example `Dict` or `Stream`). + const customType = typeof value; let customValue; - if (isString(value)) { + if (customType === "string") { customValue = stringToPDFString(value); - } else if (value instanceof Name || isNum(value) || isBool(value)) { + } else if ( + value instanceof Name || + customType === "number" || + customType === "boolean" + ) { customValue = value; } else { info(`Unsupported value in document info for (custom) "${key}".`); diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 6b8b907b5..d7f5e140b 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -24,7 +24,6 @@ import { IDENTITY_MATRIX, info, isArrayEqual, - isNum, isString, OPS, shadow, @@ -572,7 +571,7 @@ class PartialEvaluator { const w = dict.get("W", "Width"); const h = dict.get("H", "Height"); - if (!(w && isNum(w)) || !(h && isNum(h))) { + if (!(w && typeof w === "number") || !(h && typeof h === "number")) { warn("Image dimensions are missing, or not numbers."); return; } @@ -1790,7 +1789,7 @@ class PartialEvaluator { combinedGlyphs, self.handleText(arrItem, state) ); - } else if (isNum(arrItem)) { + } else if (typeof arrItem === "number") { combinedGlyphs.push(arrItem); } } @@ -3224,7 +3223,7 @@ class PartialEvaluator { let index = 0; for (let j = 0, jj = diffEncoding.length; j < jj; j++) { const data = xref.fetchIfRef(diffEncoding[j]); - if (isNum(data)) { + if (typeof data === "number") { index = data; } else if (data instanceof Name) { differences[index++] = data.name; @@ -3703,7 +3702,7 @@ class PartialEvaluator { } const glyphWidths = Metrics[lookupName]; - if (isNum(glyphWidths)) { + if (typeof glyphWidths === "number") { defaultWidth = glyphWidths; monospace = true; } else { @@ -3790,7 +3789,10 @@ class PartialEvaluator { const diffEntry = entry[j]; if (diffEntry instanceof Name) { diffBuf[j] = diffEntry.name; - } else if (isNum(diffEntry) || diffEntry instanceof Ref) { + } else if ( + typeof diffEntry === "number" || + diffEntry instanceof Ref + ) { diffBuf[j] = diffEntry.toString(); } } @@ -3820,7 +3822,7 @@ class PartialEvaluator { if (Array.isArray(widths)) { const widthsBuf = []; for (const entry of widths) { - if (isNum(entry) || entry instanceof Ref) { + if (typeof entry === "number" || entry instanceof Ref) { widthsBuf.push(entry.toString()); } } @@ -3834,12 +3836,12 @@ class PartialEvaluator { if (Array.isArray(compositeWidths)) { const widthsBuf = []; for (const entry of compositeWidths) { - if (isNum(entry) || entry instanceof Ref) { + if (typeof entry === "number" || entry instanceof Ref) { widthsBuf.push(entry.toString()); } else if (Array.isArray(entry)) { const subWidthsBuf = []; for (const element of entry) { - if (isNum(element) || element instanceof Ref) { + if (typeof element === "number" || element instanceof Ref) { subWidthsBuf.push(element.toString()); } } diff --git a/src/core/fonts.js b/src/core/fonts.js index 555a052ed..e6055ae8e 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -20,7 +20,6 @@ import { FontType, FormatError, info, - isNum, shadow, string32, warn, @@ -3184,7 +3183,9 @@ class Font { } } width = this.widths[widthCode]; - width = isNum(width) ? width : this.defaultWidth; + if (typeof width !== "number") { + width = this.defaultWidth; + } const vmetric = this.vmetrics && this.vmetrics[widthCode]; let unicode = this.toUnicode.get(charcode) || charcode; diff --git a/src/core/parser.js b/src/core/parser.js index e3267783d..6da0f1dbe 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -18,7 +18,6 @@ import { bytesToString, FormatError, info, - isNum, StreamType, warn, } from "../shared/util.js"; @@ -1393,7 +1392,7 @@ class Linearization { Number.isInteger(obj2) && isCmd(obj3, "obj") && linDict instanceof Dict && - isNum((obj = linDict.get("Linearized"))) && + typeof (obj = linDict.get("Linearized")) === "number" && obj > 0 ) ) { diff --git a/src/display/canvas.js b/src/display/canvas.js index 03a19114b..f54b7bff4 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -19,7 +19,6 @@ import { ImageKind, info, IsLittleEndianCached, - isNum, OPS, shadow, TextRenderingMode, @@ -2210,7 +2209,7 @@ class CanvasGraphics { i; for (i = 0; i < glyphsLength; ++i) { const glyph = glyphs[i]; - if (isNum(glyph)) { + if (typeof glyph === "number") { x += (spacingDir * glyph * fontSize) / 1000; continue; } @@ -2336,7 +2335,7 @@ class CanvasGraphics { for (i = 0; i < glyphsLength; ++i) { glyph = glyphs[i]; - if (isNum(glyph)) { + if (typeof glyph === "number") { spacingLength = (spacingDir * glyph * fontSize) / 1000; this.ctx.translate(spacingLength, 0); current.x += spacingLength * textHScale; diff --git a/src/display/svg.js b/src/display/svg.js index 52fe55189..c2f9a794e 100644 --- a/src/display/svg.js +++ b/src/display/svg.js @@ -18,7 +18,6 @@ import { FONT_IDENTITY_MATRIX, IDENTITY_MATRIX, ImageKind, - isNum, OPS, TextRenderingMode, unreachable, @@ -837,7 +836,7 @@ if ( // Word break x += fontDirection * wordSpacing; continue; - } else if (isNum(glyph)) { + } else if (typeof glyph === "number") { x += (spacingDir * glyph * fontSize) / 1000; continue; } diff --git a/src/shared/fonts_utils.js b/src/shared/fonts_utils.js index 553cc8ea0..a909b6405 100644 --- a/src/shared/fonts_utils.js +++ b/src/shared/fonts_utils.js @@ -13,7 +13,7 @@ * limitations under the License. */ /* globals CFFDictDataMap, CFFDictPrivateDataMap, CFFEncodingMap, CFFStrings, - Components, Dict, dump, FormatError, isNum, netscape, Stream */ + Components, Dict, dump, FormatError, netscape, Stream */ 'use strict'; @@ -273,7 +273,7 @@ var Type2Parser = function type2Parser(aFilePath) { var count = decoded.length; for (var i = 0; i < count; i++) { var token = decoded[i]; - if (isNum(token)) { + if (typeof token === "number") { stack.push(token); } else { switch (token.operand) { diff --git a/src/shared/util.js b/src/shared/util.js index 802860133..4e84ea58d 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -1034,10 +1034,6 @@ function isBool(v) { return typeof v === "boolean"; } -function isNum(v) { - return typeof v === "number"; -} - function isString(v) { return typeof v === "string"; } @@ -1146,7 +1142,6 @@ export { isBool, IsEvalSupportedCached, IsLittleEndianCached, - isNum, isSameOrigin, isString, MissingPDFException, diff --git a/test/unit/util_spec.js b/test/unit/util_spec.js index 8f766a644..8f70e7054 100644 --- a/test/unit/util_spec.js +++ b/test/unit/util_spec.js @@ -22,7 +22,6 @@ import { isArrayBuffer, isAscii, isBool, - isNum, isSameOrigin, isString, string32, @@ -91,23 +90,6 @@ describe("util", function () { }); }); - describe("isNum", function () { - it("handles numeric values", function () { - expect(isNum(1)).toEqual(true); - expect(isNum(0)).toEqual(true); - expect(isNum(-1)).toEqual(true); - expect(isNum(1000000000000000000)).toEqual(true); - expect(isNum(12.34)).toEqual(true); - }); - - it("handles non-numeric values", function () { - expect(isNum("true")).toEqual(false); - expect(isNum(true)).toEqual(false); - expect(isNum(null)).toEqual(false); - expect(isNum(undefined)).toEqual(false); - }); - }); - describe("isString", function () { it("handles string values", function () { expect(isString("foo")).toEqual(true);