Skip to content

Commit

Permalink
Remove the isString helper function
Browse files Browse the repository at this point in the history
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 `isString`-calls.
  • Loading branch information
Snuffleupagus authored and pull[bot] committed Jul 1, 2023
1 parent 6279068 commit 84ca2de
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 75 deletions.
21 changes: 9 additions & 12 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
escapeString,
getModificationDate,
isAscii,
isString,
OPS,
RenderingIntentFlag,
shadow,
Expand Down Expand Up @@ -542,9 +541,8 @@ class Annotation {
* annotation was last modified
*/
setModificationDate(modificationDate) {
this.modificationDate = isString(modificationDate)
? modificationDate
: null;
this.modificationDate =
typeof modificationDate === "string" ? modificationDate : null;
}

/**
Expand Down Expand Up @@ -1121,7 +1119,7 @@ class MarkupAnnotation extends Annotation {
* annotation was originally created
*/
setCreationDate(creationDate) {
this.creationDate = isString(creationDate) ? creationDate : null;
this.creationDate = typeof creationDate === "string" ? creationDate : null;
}

_setDefaultAppearance({
Expand Down Expand Up @@ -1258,9 +1256,8 @@ class WidgetAnnotation extends Annotation {

const defaultAppearance =
getInheritableProperty({ dict, key: "DA" }) || params.acroForm.get("DA");
this._defaultAppearance = isString(defaultAppearance)
? defaultAppearance
: "";
this._defaultAppearance =
typeof defaultAppearance === "string" ? defaultAppearance : "";
data.defaultAppearanceData = parseDefaultAppearance(
this._defaultAppearance
);
Expand Down Expand Up @@ -1305,11 +1302,11 @@ class WidgetAnnotation extends Annotation {
_decodeFormValue(formValue) {
if (Array.isArray(formValue)) {
return formValue
.filter(item => isString(item))
.filter(item => typeof item === "string")
.map(item => stringToPDFString(item));
} else if (formValue instanceof Name) {
return stringToPDFString(formValue.name);
} else if (isString(formValue)) {
} else if (typeof formValue === "string") {
return stringToPDFString(formValue);
}
return null;
Expand Down Expand Up @@ -1788,7 +1785,7 @@ class TextWidgetAnnotation extends WidgetAnnotation {
const dict = params.dict;

// The field value is always a string.
if (!isString(this.data.fieldValue)) {
if (typeof this.data.fieldValue !== "string") {
this.data.fieldValue = "";
}

Expand Down Expand Up @@ -2452,7 +2449,7 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
// item is selected or an array of strings if multiple items are selected.
// For consistency in the API and convenience in the display layer, we
// always make the field value an array with zero, one or multiple items.
if (isString(this.data.fieldValue)) {
if (typeof this.data.fieldValue === "string") {
this.data.fieldValue = [this.data.fieldValue];
} else if (!this.data.fieldValue) {
this.data.fieldValue = [];
Expand Down
45 changes: 24 additions & 21 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
DocumentActionEventType,
FormatError,
info,
isString,
objectSize,
PermissionFlag,
shadow,
Expand Down Expand Up @@ -424,12 +423,14 @@ class Catalog {
const group = this.xref.fetchIfRef(groupRef);
groups.push({
id: groupRef.toString(),
name: isString(group.get("Name"))
? stringToPDFString(group.get("Name"))
: null,
intent: isString(group.get("Intent"))
? stringToPDFString(group.get("Intent"))
: null,
name:
typeof group.get("Name") === "string"
? stringToPDFString(group.get("Name"))
: null,
intent:
typeof group.get("Intent") === "string"
? stringToPDFString(group.get("Intent"))
: null,
});
}
config = this._readOptionalContentConfig(defaultConfig, groupRefs);
Expand Down Expand Up @@ -521,12 +522,14 @@ class Catalog {
MAX_NESTED_LEVELS = 10;

return {
name: isString(config.get("Name"))
? stringToPDFString(config.get("Name"))
: null,
creator: isString(config.get("Creator"))
? stringToPDFString(config.get("Creator"))
: null,
name:
typeof config.get("Name") === "string"
? stringToPDFString(config.get("Name"))
: null,
creator:
typeof config.get("Creator") === "string"
? stringToPDFString(config.get("Creator"))
: null,
baseState:
config.get("BaseState") instanceof Name
? config.get("BaseState").name
Expand Down Expand Up @@ -676,7 +679,7 @@ class Catalog {

if (labelDict.has("P")) {
const p = labelDict.get("P");
if (!isString(p)) {
if (typeof p !== "string") {
throw new FormatError("Invalid prefix in PageLabel dictionary.");
}
prefix = stringToPDFString(p);
Expand Down Expand Up @@ -1467,7 +1470,7 @@ class Catalog {
for (const obj of action.get("Fields") || []) {
if (obj instanceof Ref) {
refs.push(obj.toString());
} else if (isString(obj)) {
} else if (typeof obj === "string") {
fields.push(stringToPDFString(obj));
}
}
Expand Down Expand Up @@ -1499,7 +1502,7 @@ class Catalog {
// We assume that we found a FileSpec dictionary
// and fetch the URL without checking any further.
url = urlDict.get("F") || null;
} else if (isString(urlDict)) {
} else if (typeof urlDict === "string") {
url = urlDict;
}

Expand All @@ -1509,9 +1512,9 @@ class Catalog {
if (remoteDest instanceof Name) {
remoteDest = remoteDest.name;
}
if (isString(url)) {
if (typeof url === "string") {
const baseUrl = url.split("#")[0];
if (isString(remoteDest)) {
if (typeof remoteDest === "string") {
url = baseUrl + "#" + remoteDest;
} else if (Array.isArray(remoteDest)) {
url = baseUrl + "#" + JSON.stringify(remoteDest);
Expand All @@ -1538,7 +1541,7 @@ class Catalog {

if (jsAction instanceof BaseStream) {
js = jsAction.getString();
} else if (isString(jsAction)) {
} else if (typeof jsAction === "string") {
js = jsAction;
}

Expand All @@ -1563,7 +1566,7 @@ class Catalog {
dest = destDict.get("Dest");
}

if (isString(url)) {
if (typeof url === "string") {
const absoluteUrl = createValidAbsoluteUrl(url, docBaseUrl, {
addDefaultProtocol: true,
tryConvertEncoding: true,
Expand All @@ -1577,7 +1580,7 @@ class Catalog {
if (dest instanceof Name) {
dest = dest.name;
}
if (isString(dest) || Array.isArray(dest)) {
if (typeof dest === "string" || Array.isArray(dest)) {
resultObj.dest = dest;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/core/cmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import {
CMapCompressionType,
FormatError,
isString,
unreachable,
warn,
} from "../shared/util.js";
Expand Down Expand Up @@ -767,7 +766,7 @@ const CMapFactory = (function CMapFactoryClosure() {
}

function expectString(obj) {
if (!isString(obj)) {
if (typeof obj !== "string") {
throw new FormatError("Malformed CMap: expected string.");
}
}
Expand Down Expand Up @@ -812,7 +811,7 @@ const CMapFactory = (function CMapFactoryClosure() {
expectString(obj);
const high = strToInt(obj);
obj = lexer.getObj();
if (Number.isInteger(obj) || isString(obj)) {
if (Number.isInteger(obj) || typeof obj === "string") {
const dstLow = Number.isInteger(obj) ? String.fromCharCode(obj) : obj;
cMap.mapBfRange(low, high, dstLow);
} else if (isCmd(obj, "[")) {
Expand Down Expand Up @@ -878,12 +877,12 @@ const CMapFactory = (function CMapFactoryClosure() {
if (isCmd(obj, "endcodespacerange")) {
return;
}
if (!isString(obj)) {
if (typeof obj !== "string") {
break;
}
const low = strToInt(obj);
obj = lexer.getObj();
if (!isString(obj)) {
if (typeof obj !== "string") {
break;
}
const high = strToInt(obj);
Expand Down
7 changes: 3 additions & 4 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
IDENTITY_MATRIX,
info,
isArrayEqual,
isString,
OPS,
shadow,
stringToPDFString,
Expand Down Expand Up @@ -1784,7 +1783,7 @@ class PartialEvaluator {
var state = stateManager.state;
for (i = 0; i < arrLength; ++i) {
const arrItem = arr[i];
if (isString(arrItem)) {
if (typeof arrItem === "string") {
Array.prototype.push.apply(
combinedGlyphs,
self.handleText(arrItem, state)
Expand Down Expand Up @@ -3974,10 +3973,10 @@ class PartialEvaluator {
let fontName = descriptor.get("FontName");
let baseFont = dict.get("BaseFont");
// Some bad PDFs have a string as the font name.
if (isString(fontName)) {
if (typeof fontName === "string") {
fontName = Name.get(fontName);
}
if (isString(baseFont)) {
if (typeof baseFont === "string") {
baseFont = Name.get(baseFont);
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/murmurhash3.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Hashes roughly 100 KB per millisecond on i7 3.4 GHz.
*/

import { isArrayBuffer, isString } from "../shared/util.js";
import { isArrayBuffer } from "../shared/util.js";

const SEED = 0xc3d2e1f0;
// Workaround for missing math precision in JS.
Expand All @@ -32,7 +32,7 @@ class MurmurHash3_64 {

update(input) {
let data, length;
if (isString(input)) {
if (typeof input === "string") {
data = new Uint8Array(input.length * 2);
length = 0;
for (let i = 0, ii = input.length; i < ii; i++) {
Expand Down
6 changes: 3 additions & 3 deletions src/core/struct_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import { Dict, isName, Name, Ref } from "./primitives.js";
import { isString, stringToPDFString, warn } from "../shared/util.js";
import { stringToPDFString, warn } from "../shared/util.js";
import { NumberTree } from "./name_number_tree.js";

const MAX_DEPTH = 40;
Expand Down Expand Up @@ -295,11 +295,11 @@ class StructTreePage {
obj.children = [];
parent.children.push(obj);
const alt = node.dict.get("Alt");
if (isString(alt)) {
if (typeof alt === "string") {
obj.alt = stringToPDFString(alt);
}
const lang = node.dict.get("Lang");
if (isString(lang)) {
if (typeof lang === "string") {
obj.lang = stringToPDFString(lang);
}

Expand Down
10 changes: 2 additions & 8 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ import {
BaseStandardFontDataFactory,
BaseSVGFactory,
} from "./base_factory.js";
import {
BaseException,
isString,
stringToBytes,
Util,
warn,
} from "../shared/util.js";
import { BaseException, stringToBytes, Util, warn } from "../shared/util.js";

const SVG_NS = "http://www.w3.org/2000/svg";

Expand Down Expand Up @@ -482,7 +476,7 @@ class PDFDateString {
* @returns {Date|null}
*/
static toDateObject(input) {
if (!input || !isString(input)) {
if (!input || typeof input !== "string") {
return null;
}

Expand Down
5 changes: 0 additions & 5 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,6 @@ function utf8StringToString(str) {
return unescape(encodeURIComponent(str));
}

function isString(v) {
return typeof v === "string";
}

function isArrayBuffer(v) {
return typeof v === "object" && v !== null && v.byteLength !== undefined;
}
Expand Down Expand Up @@ -1138,7 +1134,6 @@ export {
IsEvalSupportedCached,
IsLittleEndianCached,
isSameOrigin,
isString,
MissingPDFException,
objectFromMap,
objectSize,
Expand Down
15 changes: 0 additions & 15 deletions test/unit/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
isArrayBuffer,
isAscii,
isSameOrigin,
isString,
string32,
stringToBytes,
stringToPDFString,
Expand Down Expand Up @@ -73,20 +72,6 @@ describe("util", function () {
});
});

describe("isString", function () {
it("handles string values", function () {
expect(isString("foo")).toEqual(true);
expect(isString("")).toEqual(true);
});

it("handles non-string values", function () {
expect(isString(true)).toEqual(false);
expect(isString(1)).toEqual(false);
expect(isString(null)).toEqual(false);
expect(isString(undefined)).toEqual(false);
});
});

describe("string32", function () {
it("converts unsigned 32-bit integers to strings", function () {
expect(string32(0x74727565)).toEqual("true");
Expand Down

0 comments on commit 84ca2de

Please sign in to comment.