1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-23 08:38:06 +02:00

Convert some usage of indexOf to startsWith/includes where applicable

In many cases in the code you don't actually care about the index itself, but rather just want to know if something exists in a String/Array or if a String starts in a particular way. With modern JavaScript functionality, it's thus possible to remove a number of existing `indexOf` cases.
This commit is contained in:
Jonas Jenwald 2019-01-18 15:05:23 +01:00
parent cdbc33ba06
commit 24a688d6c6
10 changed files with 22 additions and 28 deletions

View file

@ -2533,13 +2533,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var fontNameStr = fontName && fontName.name;
var baseFontStr = baseFont && baseFont.name;
if (fontNameStr !== baseFontStr) {
info('The FontDescriptor\'s FontName is "' + fontNameStr +
'" but should be the same as the Font\'s BaseFont "' +
baseFontStr + '"');
info(`The FontDescriptor\'s FontName is "${fontNameStr}" but ` +
`should be the same as the Font\'s BaseFont "${baseFontStr}".`);
// Workaround for cases where e.g. fontNameStr = 'Arial' and
// baseFontStr = 'Arial,Bold' (needed when no font file is embedded).
if (fontNameStr && baseFontStr &&
baseFontStr.indexOf(fontNameStr) === 0) {
baseFontStr.startsWith(fontNameStr)) {
fontName = baseFont;
}
}

View file

@ -1200,7 +1200,7 @@ var Font = (function FontClosure() {
// if at least one width is present, remeasure all chars when exists
this.remeasure = Object.keys(this.widths).length > 0;
if (isStandardFont && type === 'CIDFontType2' &&
this.cidEncoding.indexOf('Identity-') === 0) {
this.cidEncoding.startsWith('Identity-')) {
var GlyphMapForStandardFonts = getGlyphMapForStandardFonts();
// Standard fonts might be embedded as CID font without glyph mapping.
// Building one based on GlyphMapForStandardFonts.

View file

@ -701,10 +701,7 @@ class Catalog {
static parseDestDictionary(params) {
// Lets URLs beginning with 'www.' default to using the 'http://' protocol.
function addDefaultProtocolToUrl(url) {
if (url.indexOf('www.') === 0) {
return `http://${url}`;
}
return url;
return (url.startsWith('www.') ? `http://${url}` : url);
}
// According to ISO 32000-1:2008, section 12.6.4.7, URIs should be encoded
@ -1234,7 +1231,7 @@ var XRef = (function XRefClosure() {
}
var token = readToken(buffer, position);
var m;
if (token.indexOf('xref') === 0 &&
if (token.startsWith('xref') &&
(token.length === 4 || /\s/.test(token[4]))) {
position += skipUntil(buffer, position, trailerBytes);
trailers.push(position);
@ -1288,7 +1285,7 @@ var XRef = (function XRefClosure() {
}
position += contentLength;
} else if (token.indexOf('trailer') === 0 &&
} else if (token.startsWith('trailer') &&
(token.length === 7 || /\s/.test(token[7]))) {
trailers.push(position);
position += skipUntil(buffer, position, startxrefBytes);
@ -1507,7 +1504,7 @@ var XRef = (function XRefClosure() {
}
if (obj3.cmd !== 'obj') {
// some bad PDFs use "obj1234" and really mean 1234
if (obj3.cmd.indexOf('obj') === 0) {
if (obj3.cmd.startsWith('obj')) {
num = parseInt(obj3.cmd.substring(3), 10);
if (!Number.isNaN(num)) {
return num;

View file

@ -253,7 +253,7 @@ var Type2Parser = function type2Parser(aFilePath) {
var xhr = new XMLHttpRequest();
xhr.open('GET', aFilePath, false);
xhr.responseType = 'arraybuffer';
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
xhr.expected = document.URL.startsWith('file:') ? 0 : 200;
xhr.send(null);
this.data = new Stream(xhr.response);