Skip to content

Commit

Permalink
Merge pull request #10467 from Snuffleupagus/less-indexOf
Browse files Browse the repository at this point in the history
Convert some usage of `indexOf` to `startsWith`/`includes` where applicable
  • Loading branch information
timvandermeij authored Jan 19, 2019
2 parents 1bb5ca0 + 24a688d commit 269168d
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 28 deletions.
8 changes: 4 additions & 4 deletions external/builder/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ function preprocess(inFilename, outFilename, defines) {
if (state === STATE_NONE) {
writeLine(line);
} else if ((state === STATE_IF_TRUE || state === STATE_ELSE_TRUE) &&
stack.indexOf(STATE_IF_FALSE) === -1 &&
stack.indexOf(STATE_ELSE_FALSE) === -1) {
!stack.includes(STATE_IF_FALSE) &&
!stack.includes(STATE_ELSE_FALSE)) {
writeLine(line.replace(/^\/\/|^<!--|-->$/g, ' '));
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ function preprocessCSS(mode, source, destination) {
if (checkBracket) {
if (checkBracket[1] === '{') {
bracketLevel++;
} else if (lines[j].indexOf('{') < 0) {
} else if (!lines[j].includes('{')) {
bracketLevel--;
}
}
Expand All @@ -238,7 +238,7 @@ function preprocessCSS(mode, source, destination) {
lines.splice(i, 1);
} while (i < lines.length &&
!/\}\s*$/.test(lines[i]) &&
lines[i].indexOf(':') < 0);
!lines[i].includes(':'));
if (i < lines.length && /\S\s*}\s*$/.test(lines[i])) {
lines[i] = lines[i].substring(lines[i].indexOf('}'));
}
Expand Down
7 changes: 3 additions & 4 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 4 additions & 7 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/fonts_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions test/downloadutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable object-shorthand, mozilla/use-includes-instead-of-indexOf */
/* eslint-disable object-shorthand */

'use strict';

Expand Down Expand Up @@ -52,7 +52,7 @@ function downloadFile(file, url, callback, redirects) {
downloadFile(file, redirectTo, callback, (redirects || 0) + 1);
return;
}
if (response.statusCode === 404 && url.indexOf('web.archive.org') < 0) {
if (response.statusCode === 404 && !url.includes('web.archive.org')) {
// trying waybackmachine
redirectTo = 'http://web.archive.org/web/' + url;
downloadFile(file, redirectTo, callback, (redirects || 0) + 1);
Expand Down Expand Up @@ -84,7 +84,7 @@ function downloadFile(file, url, callback, redirects) {
}).on('error', function (err) {
if (!completed) {
if (typeof err === 'object' && err.errno === 'ENOTFOUND' &&
url.indexOf('web.archive.org') < 0) {
!url.includes('web.archive.org')) {
// trying waybackmachine
var redirectTo = 'http://web.archive.org/web/' + url;
downloadFile(file, redirectTo, callback, (redirects || 0) + 1);
Expand Down
4 changes: 1 addition & 3 deletions test/stats/statcmp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable mozilla/use-includes-instead-of-indexOf */

'use strict';

var fs = require('fs');
Expand Down Expand Up @@ -67,7 +65,7 @@ function flatten(stats) {
});
});
// Use only overall results if not grouped by 'stat'
if (options.groupBy.indexOf('stat') < 0) {
if (!options.groupBy.includes('stat')) {
rows = rows.filter(function(s) {
return s.stat === 'Overall';
});
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function checkEq(task, results, browser, masterMode) {
continue;
}
var testSnapshot = pageResults[page].snapshot;
if (testSnapshot && testSnapshot.indexOf('data:image/png;base64,') === 0) {
if (testSnapshot && testSnapshot.startsWith('data:image/png;base64,')) {
testSnapshot = Buffer.from(testSnapshot.substring(22), 'base64');
} else {
console.error('Valid snapshot was not found.');
Expand Down
4 changes: 2 additions & 2 deletions test/unit/ui_utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('ui_utils', function() {
var typedArray = new Uint8Array([1, 2, 3, 4, 5]);
var blobUrl = createObjectURL(typedArray, 'application/pdf');
// Sanity check to ensure that a "blob:" URL was returned.
expect(blobUrl.indexOf('blob:') === 0).toEqual(true);
expect(blobUrl.startsWith('blob:')).toEqual(true);

expect(getPDFFileNameFromURL(blobUrl + '?file.pdf')).toEqual('file.pdf');
});
Expand All @@ -173,7 +173,7 @@ describe('ui_utils', function() {
var dataUrl = createObjectURL(typedArray, 'application/pdf',
/* forceDataSchema = */ true);
// Sanity check to ensure that a "data:" URL was returned.
expect(dataUrl.indexOf('data:') === 0).toEqual(true);
expect(dataUrl.startsWith('data:')).toEqual(true);

expect(getPDFFileNameFromURL(dataUrl + '?file1.pdf')).
toEqual('document.pdf');
Expand Down
4 changes: 2 additions & 2 deletions test/webbrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable object-shorthand, mozilla/use-includes-instead-of-indexOf */
/* eslint-disable object-shorthand */

'use strict';

Expand Down Expand Up @@ -157,7 +157,7 @@ WebBrowser.prototype = {
args: wmicPrefix.concat(['get', 'CommandLine']),
};
isAllKilled = function(exitCode, stdout) {
return stdout.indexOf(this.uniqStringId) === -1;
return !stdout.includes(this.uniqStringId);
}.bind(this);
} else {
cmdKillAll = { file: 'pkill', args: ['-f', this.uniqStringId], };
Expand Down

0 comments on commit 269168d

Please sign in to comment.