Skip to content

Commit f255b18

Browse files
authored
Prefer .includes() over .indexOf() (#13945)
Following on from #13908. See https://caniuse.com/?search=includes
1 parent 663cdfa commit f255b18

20 files changed

+74
-75
lines changed

src/cpuprofiler.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ var emscriptenCpuProfiler = {
351351
document.getElementById('trace_limit').onkeydown = function(e) { if (e.which == 13 || e.keycode == 13) emscriptenCpuProfiler.enableTraceWebGL(); else emscriptenCpuProfiler.disableTraceWebGL(); };
352352
cpuprofiler = document.getElementById('cpuprofiler');
353353

354-
if (location.search.indexOf('expandhelp') != -1) this.toggleHelpTextVisible();
354+
if (location.search.includes('expandhelp')) this.toggleHelpTextVisible();
355355
}
356356

357357
this.canvas = document.getElementById('cpuprofiler_canvas');
@@ -470,9 +470,9 @@ var emscriptenCpuProfiler = {
470470

471471
// Also poll to autodetect if there is an Emscripten GL canvas available that we could hook into. This is a bit clumsy, but there's no good location to get an event after GL context has been created, so
472472
// need to resort to polling.
473-
if (location.search.indexOf('webglprofiler') != -1 && !this.automaticallyHookedWebGLProfiler) {
473+
if (location.search.includes('webglprofiler') && !this.automaticallyHookedWebGLProfiler) {
474474
this.hookWebGL();
475-
if (location.search.indexOf('tracegl') != -1) {
475+
if (location.search.includes('tracegl')) {
476476
var res = location.search.match(/tracegl=(\d+)/);
477477
var traceGl = res[1];
478478
document.getElementById('trace_limit').value = traceGl;
@@ -513,18 +513,18 @@ var emscriptenCpuProfiler = {
513513
var l9 = ['copyTexSubImage3D'];
514514
var l10 = ['blitFramebuffer', 'texImage3D', 'compressedTexSubImage3D'];
515515
var l11 = ['texSubImage3D'];
516-
if (l0.indexOf(f) != -1) return 0;
517-
if (l1.indexOf(f) != -1) return 1;
518-
if (l2.indexOf(f) != -1) return 2;
519-
if (l3.indexOf(f) != -1) return 3;
520-
if (l4.indexOf(f) != -1) return 4;
521-
if (l5.indexOf(f) != -1) return 5;
522-
if (l6.indexOf(f) != -1) return 6;
523-
if (l7.indexOf(f) != -1) return 7;
524-
if (l8.indexOf(f) != -1) return 8;
525-
if (l9.indexOf(f) != -1) return 9;
526-
if (l10.indexOf(f) != -1) return 10;
527-
if (l11.indexOf(f) != -1) return 11;
516+
if (l0.includes(f)) return 0;
517+
if (l1.includes(f)) return 1;
518+
if (l2.includes(f)) return 2;
519+
if (l3.includes(f)) return 3;
520+
if (l4.includes(f)) return 4;
521+
if (l5.includes(f)) return 5;
522+
if (l6.includes(f)) return 6;
523+
if (l7.includes(f)) return 7;
524+
if (l8.includes(f)) return 8;
525+
if (l9.includes(f)) return 9;
526+
if (l10.includes(f)) return 10;
527+
if (l11.includes(f)) return 11;
528528
console.warn('Unexpected WebGL function ' + f);
529529
},
530530

@@ -537,7 +537,7 @@ var emscriptenCpuProfiler = {
537537

538538
toggleHookWebGL: function(glCtx) {
539539
if (!glCtx) glCtx = this.detectWebGLContext();
540-
if (this.hookedWebGLContexts.indexOf(glCtx) != -1) this.unhookWebGL(glCtx);
540+
if (this.hookedWebGLContexts.includes(glCtx)) this.unhookWebGL(glCtx);
541541
else this.hookWebGL(glCtx);
542542
},
543543

@@ -567,7 +567,7 @@ var emscriptenCpuProfiler = {
567567
document.getElementById("toggle_webgl_profile").style.background = '#E1E1E1';
568568

569569
for (var f in glCtx) {
570-
if (typeof glCtx[f] !== 'function' || f.indexOf('real_') == 0) continue;
570+
if (typeof glCtx[f] !== 'function' || f.startsWith('real_')) continue;
571571
var realf = 'real_' + f;
572572
glCtx[f] = glCtx[realf];
573573
delete glCtx[realf];
@@ -576,7 +576,7 @@ var emscriptenCpuProfiler = {
576576

577577
hookWebGLFunction: function(f, glCtx) {
578578
var this_ = this;
579-
var section = (this_.hotGLFunctions.indexOf(f) != -1 || f.indexOf('uniform') == 0 || f.indexOf('vertexAttrib') == 0) ? 0 : 1;
579+
var section = (this_.hotGLFunctions.incudes(f) || f.startsWith('uniform') || f.startsWith('vertexAttrib')) ? 0 : 1;
580580
var realf = 'real_' + f;
581581
glCtx[realf] = glCtx[f];
582582
var numArgs = this_.webGLFunctionLength(f); // On Firefox & Chrome, could do "glCtx[realf].length", but that doesn't work on Edge, which always reports 0.
@@ -617,7 +617,7 @@ var emscriptenCpuProfiler = {
617617
this.createSection(0, 'Hot GL', this.colorHotGLFunction, /*traceable=*/true);
618618
this.createSection(1, 'Cold GL', this.colorColdGLFunction, /*traceable=*/true);
619619
for (var f in glCtx) {
620-
if (typeof glCtx[f] !== 'function' || f.indexOf('real_') == 0) continue;
620+
if (typeof glCtx[f] !== 'function' || f.startsWith('real_')) continue;
621621
this.hookWebGLFunction(f, glCtx);
622622
}
623623
var this_ = this;

src/embind/embind.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ var LibraryEmbind = {
562562
};
563563
}
564564

565-
var isUnsignedType = (name.indexOf('unsigned') != -1);
565+
var isUnsignedType = (name.includes('unsigned'));
566566

567567
registerType(primitiveType, {
568568
name: name,
@@ -1075,7 +1075,7 @@ var LibraryEmbind = {
10751075
return getDynCaller(signature, rawFunction);
10761076
#else
10771077
#if !WASM_BIGINT
1078-
if (signature.indexOf('j') != -1) {
1078+
if (signature.includes('j')) {
10791079
return getDynCaller(signature, rawFunction);
10801080
}
10811081
#endif

src/jsifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function isJsOnlyIdentifier(ident) {
3535

3636
function escapeJSONKey(x) {
3737
if (/^[\d\w_]+$/.exec(x) || x[0] === '"' || x[0] === "'") return x;
38-
assert(x.indexOf("'") < 0, 'cannot have internal single quotes in keys: ' + x);
38+
assert(!x.includes("'"), 'cannot have internal single quotes in keys: ' + x);
3939
return "'" + x + "'";
4040
}
4141

src/library.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ LibraryManager.library = {
11391139
}
11401140
};
11411141
for (var rule in EXPANSION_RULES_2) {
1142-
if (pattern.indexOf(rule) >= 0) {
1142+
if (pattern.includes(rule)) {
11431143
pattern = pattern.replace(new RegExp(rule, 'g'), EXPANSION_RULES_2[rule](date));
11441144
}
11451145
}
@@ -1867,7 +1867,7 @@ LibraryManager.library = {
18671867
return [0, 0, 0, 0, 0, 0, 0, 0];
18681868
}
18691869
// Z placeholder to keep track of zeros when splitting the string on ":"
1870-
if (str.indexOf("::") === 0) {
1870+
if (str.startsWith("::")) {
18711871
str = str.replace("::", "Z:"); // leading zeros case
18721872
} else {
18731873
str = str.replace("::", ":Z:");
@@ -2805,7 +2805,7 @@ LibraryManager.library = {
28052805
if (flags & 128 /*EM_LOG_FUNC_PARAMS*/) {
28062806
// To get the actual parameters to the functions, traverse the stack via the unfortunately deprecated 'arguments.callee' method, if it works:
28072807
stack_args = traverseStack(arguments);
2808-
while (stack_args[1].indexOf('_emscripten_') >= 0)
2808+
while (stack_args[1].includes('_emscripten_'))
28092809
stack_args = traverseStack(stack_args[0]);
28102810
}
28112811

@@ -3495,7 +3495,7 @@ LibraryManager.library = {
34953495
$getDynCaller__deps: ['$dynCall'],
34963496
$getDynCaller: function(sig, ptr) {
34973497
#if ASSERTIONS && !DYNCALLS
3498-
assert(sig.indexOf('j') >= 0, 'getDynCaller should only be called with i64 sigs')
3498+
assert(sig.includes('j'), 'getDynCaller should only be called with i64 sigs')
34993499
#endif
35003500
var argCache = [];
35013501
return function() {
@@ -3516,7 +3516,7 @@ LibraryManager.library = {
35163516
// Without WASM_BIGINT support we cannot directly call function with i64 as
35173517
// part of thier signature, so we rely the dynCall functions generated by
35183518
// wasm-emscripten-finalize
3519-
if (sig.indexOf('j') != -1) {
3519+
if (sig.includes('j')) {
35203520
return dynCallLegacy(sig, ptr, args);
35213521
}
35223522
#endif
@@ -3681,7 +3681,7 @@ LibraryManager.library = {
36813681

36823682
$asmjsMangle: function(x) {
36833683
var unmangledSymbols = {{{ buildStringArray(WASM_SYSTEM_EXPORTS) }}};
3684-
return x.indexOf('dynCall_') == 0 || unmangledSymbols.indexOf(x) != -1 ? x : '_' + x;
3684+
return x.indexOf('dynCall_') == 0 || unmangledSymbols.includes(x) ? x : '_' + x;
36853685
},
36863686

36873687
#if RELOCATABLE

src/library_c_preprocessor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mergeInto(LibraryManager.library, {
9595
} else if (kind == 1) {
9696
// Lookahead for two-character operators.
9797
var op2 = exprString.substr(i, 2);
98-
if (['<=', '>=', '==', '!=', '&&', '||'].indexOf(op2) >= 0) {
98+
if (['<=', '>=', '==', '!=', '&&', '||'].includes(op2)) {
9999
out.push(op2);
100100
++i;
101101
} else {

src/library_dylink.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var LibraryDylink = {
3232
sym = Module[asmjsMangle(symName)];
3333
}
3434

35-
if (!sym && symName.indexOf('invoke_') == 0) {
35+
if (!sym && symName.startsWith('invoke_')) {
3636
sym = createInvokeFunction(symName.split('_')[1]);
3737
}
3838

@@ -63,7 +63,7 @@ var LibraryDylink = {
6363
'__wasm_apply_data_relocs',
6464
'__dso_handle',
6565
'__set_stack_limits'
66-
].indexOf(symName) != -1
66+
].includes(symName)
6767
#if SPLIT_MODULE
6868
// Exports synthesized by wasm-split should be prefixed with '%'
6969
|| symName[0] == '%'
@@ -84,7 +84,7 @@ var LibraryDylink = {
8484
var replace = false;
8585
var value = exports[symName];
8686
#if !WASM_BIGINT
87-
if (symName.indexOf('orig$') == 0) {
87+
if (symName.startsWith('orig$')) {
8888
symName = symName.split('$')[1];
8989
replace = true;
9090
}

src/library_formatString.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ mergeInto(LibraryManager.library, {
358358
var parts = argText.split('e');
359359
if (isGeneral && !flagAlternative) {
360360
// Discard trailing zeros and periods.
361-
while (parts[0].length > 1 && parts[0].indexOf('.') != -1 &&
361+
while (parts[0].length > 1 && parts[0].includes('.') &&
362362
(parts[0].slice(-1) == '0' || parts[0].slice(-1) == '.')) {
363363
parts[0] = parts[0].slice(0, -1);
364364
}

src/library_fs.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ FS.staticInit();` +
326326
return 0;
327327
}
328328
// return 0 if any user, group or owner bits are set.
329-
if (perms.indexOf('r') !== -1 && !(node.mode & {{{ cDefine('S_IRUGO') }}})) {
329+
if (perms.includes('r') && !(node.mode & {{{ cDefine('S_IRUGO') }}})) {
330330
return {{{ cDefine('EACCES') }}};
331-
} else if (perms.indexOf('w') !== -1 && !(node.mode & {{{ cDefine('S_IWUGO') }}})) {
331+
} else if (perms.includes('w') && !(node.mode & {{{ cDefine('S_IWUGO') }}})) {
332332
return {{{ cDefine('EACCES') }}};
333-
} else if (perms.indexOf('x') !== -1 && !(node.mode & {{{ cDefine('S_IXUGO') }}})) {
333+
} else if (perms.includes('x') && !(node.mode & {{{ cDefine('S_IXUGO') }}})) {
334334
return {{{ cDefine('EACCES') }}};
335335
}
336336
return 0;
@@ -616,7 +616,7 @@ FS.staticInit();` +
616616
while (current) {
617617
var next = current.name_next;
618618

619-
if (mounts.indexOf(current.mount) !== -1) {
619+
if (mounts.includes(current.mount)) {
620620
FS.destroyNode(current);
621621
}
622622

src/library_glemu.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,20 +483,20 @@ var LibraryGLEmulation = {
483483
source = 'uniform mat4 u_textureMatrix' + i + '; \n' + source;
484484
}
485485
}
486-
if (source.indexOf('gl_FrontColor') >= 0) {
486+
if (source.includes('gl_FrontColor')) {
487487
source = 'varying vec4 v_color; \n' +
488488
source.replace(/gl_FrontColor/g, 'v_color');
489489
}
490-
if (source.indexOf('gl_Color') >= 0) {
490+
if (source.includes('gl_Color')) {
491491
source = 'attribute vec4 a_color; \n' +
492492
source.replace(/gl_Color/g, 'a_color');
493493
}
494-
if (source.indexOf('gl_Normal') >= 0) {
494+
if (source.includes('gl_Normal')) {
495495
source = 'attribute vec3 a_normal; \n' +
496496
source.replace(/gl_Normal/g, 'a_normal');
497497
}
498498
// fog
499-
if (source.indexOf('gl_FogFragCoord') >= 0) {
499+
if (source.includes('gl_FogFragCoord')) {
500500
source = 'varying float v_fogFragCoord; \n' +
501501
source.replace(/gl_FogFragCoord/g, 'v_fogFragCoord');
502502
}
@@ -508,26 +508,26 @@ var LibraryGLEmulation = {
508508
source = 'varying vec4 v_texCoord' + i + '; \n' + source;
509509
}
510510
}
511-
if (source.indexOf('gl_Color') >= 0) {
511+
if (source.includes('gl_Color')) {
512512
source = 'varying vec4 v_color; \n' + source.replace(/gl_Color/g, 'v_color');
513513
}
514-
if (source.indexOf('gl_Fog.color') >= 0) {
514+
if (source.includes('gl_Fog.color')) {
515515
source = 'uniform vec4 u_fogColor; \n' +
516516
source.replace(/gl_Fog.color/g, 'u_fogColor');
517517
}
518-
if (source.indexOf('gl_Fog.end') >= 0) {
518+
if (source.includes('gl_Fog.end')) {
519519
source = 'uniform float u_fogEnd; \n' +
520520
source.replace(/gl_Fog.end/g, 'u_fogEnd');
521521
}
522-
if (source.indexOf('gl_Fog.scale') >= 0) {
522+
if (source.includes('gl_Fog.scale')) {
523523
source = 'uniform float u_fogScale; \n' +
524524
source.replace(/gl_Fog.scale/g, 'u_fogScale');
525525
}
526-
if (source.indexOf('gl_Fog.density') >= 0) {
526+
if (source.includes('gl_Fog.density')) {
527527
source = 'uniform float u_fogDensity; \n' +
528528
source.replace(/gl_Fog.density/g, 'u_fogDensity');
529529
}
530-
if (source.indexOf('gl_FogFragCoord') >= 0) {
530+
if (source.includes('gl_FogFragCoord')) {
531531
source = 'varying float v_fogFragCoord; \n' +
532532
source.replace(/gl_FogFragCoord/g, 'v_fogFragCoord');
533533
}

src/library_glew.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ var LibraryGLEW = {
103103
GLEW.extensions = UTF8ToString(_glGetString(0x1F03)).split(' ');
104104
}
105105

106-
if (GLEW.extensions.indexOf(name) != -1)
106+
if (GLEW.extensions.includes(name))
107107
return 1;
108108

109109
// extensions from GLEmulations do not come unprefixed
110110
// so, try with prefix
111-
return (GLEW.extensions.indexOf("GL_" + name) != -1);
111+
return (GLEW.extensions.includes("GL_" + name));
112112
},
113113
},
114114

0 commit comments

Comments
 (0)