New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Array#includes instead Array#indexOf when needed #8743
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
I signed it! |
CLAs look good, thanks! |
/to @jridgewell |
3p/3p.js
Outdated
@@ -154,7 +154,7 @@ export function validateSrcPrefix(prefix, src) { | |||
* @param {string} src | |||
*/ | |||
export function validateSrcContains(string, src) { | |||
if (src.indexOf(string) === -1) { | |||
if (src.includes(string) === false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of if (condition === false)
, please use if (!condition)
. Likewise for the true
case. It's more concise and easier to read!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking on this issue! 👍
yarn.lock
Outdated
@@ -345,7 +345,7 @@ asynckit@^0.4.0: | |||
version "0.4.0" | |||
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" | |||
|
|||
autoprefixer@6.7.0: | |||
autoprefixer@6.7.0, autoprefixer@^6.0.3: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this file to keep the PR strictly relevant to the attached issue.
Done @choumx and @jridgewell |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! Just one comment.
ads/holder.js
Outdated
@@ -25,7 +25,7 @@ export function holder(global, data) { | |||
const wcl = global.context.location; | |||
const n = navigator.userAgent; | |||
let l = '&r' + Math.round((Math.random() * 10000000)) + '&h' + wcl.href; | |||
if (!(n.indexOf('Safari') != -1 && n.indexOf('Chrome') == -1)) { | |||
if (!(n.includes('Safari') && !n.includes('Chrome'))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: This may be more readable as
if (!n.includes('Safari') || n.includes('Chrome')) { ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I agree. I'll commit this change.
3p/3p.js
Outdated
@@ -154,7 +154,7 @@ export function validateSrcPrefix(prefix, src) { | |||
* @param {string} src |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So everything in this file cannot be updated, because the 3p directory is run in foreign iframes. This means they will not have the Array#includes
polyfill installed in them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Ok, I'll revert this file.
ads/alp/handler.js
Outdated
@@ -73,7 +73,7 @@ export function handleClick(e, opt_viewerNavigate) { | |||
const fragment = 'click=' + encodeURIComponent( | |||
addParamToUrl(link.a.href, 'amp', '1', /* opt_addToFront */ true)); | |||
let destination = link.eventualUrl; | |||
if (link.eventualUrl.indexOf('#') == -1) { | |||
if (!link.eventualUrl.includes('#')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String#includes
is not supported in IE.
ads/alp/handler.js
Outdated
@@ -224,7 +224,7 @@ export function getA2AAncestor(win) { | |||
// Not a security property. We just check whether the | |||
// viewer might support A2A. More domains can be added to whitelist | |||
// as needed. | |||
if (top.indexOf('.google.') == -1) { | |||
if (!top.includes('.google.')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String#includes
is not supported in IE.
ads/holder.js
Outdated
@@ -25,7 +25,7 @@ export function holder(global, data) { | |||
const wcl = global.context.location; | |||
const n = navigator.userAgent; | |||
let l = '&r' + Math.round((Math.random() * 10000000)) + '&h' + wcl.href; | |||
if (!(n.indexOf('Safari') != -1 && n.indexOf('Chrome') == -1)) { | |||
if (!(n.includes('Safari') && !n.includes('Chrome'))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String#includes
is not supported in IE.
ads/medianet.js
Outdated
@@ -132,7 +132,7 @@ function loadHBTag(global, data, publisherUrl, referrerUrl) { | |||
let currentParam = ''; | |||
for (let i = 0; i < allParams.length; i++) { | |||
currentParam = allParams[i]; | |||
if (dfpParams.indexOf(currentParam) === -1 && data[currentParam]) { | |||
if (!dfpParams.includes(currentParam) && data[currentParam]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So everything in this file cannot be updated, because the ads directory is run in foreign iframes. This means they will not have the Array#includes polyfill installed in them.
src/service/variable-source.js
Outdated
@@ -179,7 +179,7 @@ export class VariableSource { | |||
* @return {!VariableSource} | |||
*/ | |||
setAsync(varName, asyncResolver) { | |||
dev().assert(varName.indexOf('RETURN') == -1); | |||
dev().assert(!varName.includes('RETURN')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String#includes
is not supported in IE.
src/url.js
Outdated
@@ -371,7 +371,7 @@ export function getSourceUrl(url) { | |||
? 'https://' + decodeURIComponent(path[3]) | |||
: 'http://' + decodeURIComponent(domainOrHttpsSignal); | |||
// Sanity test that what we found looks like a domain. | |||
user().assert(origin.indexOf('.') > 0, 'Expected a . in origin %s', origin); | |||
user().assert(origin.includes('.'), 'Expected a . in origin %s', origin); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String#includes
is not supported in IE.
third_party/mustache/mustache.js
Outdated
@@ -386,7 +386,7 @@ | |||
var context = this, names, index, lookupHit = false; | |||
|
|||
while (context) { | |||
if (name.indexOf('.') > 0) { | |||
if (name.includes('.')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String#includes
is not supported in IE.
@@ -236,7 +236,7 @@ export const ShadowCSS = { | |||
scoped = parts.map(function(p) { | |||
// remove :host since it should be unnecessary | |||
var t = p.trim().replace(polyfillHostRe, ''); | |||
if (t && (splits.indexOf(t) < 0) && (t.indexOf(attrName) < 0)) { | |||
if (t && (!splits.includes(t)) && (!t.includes(attrName))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The t.includes
is String#includes
. String#includes
is not supported in IE.
validator/engine/validator.js
Outdated
@@ -837,7 +837,7 @@ class ChildTagMatcher { | |||
this.numChildTagsSeen_++; // Increment this first to allow early exit. | |||
if (childTags.childTagNameOneof.length > 0) { | |||
const names = childTags.childTagNameOneof; | |||
if (names.indexOf(tagName) === -1) { | |||
if (!names.includes(tagName)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another special file that can't be updated.
Done @jridgewell, everything should be ok now :) |
ads/taboola.js
Outdated
@@ -28,7 +28,7 @@ export function taboola(global, data) { | |||
// ensure we have vlid publisher, placement and mode | |||
// and exactly one page-type | |||
validateData(data, ['publisher', 'placement', 'mode', | |||
['article', 'video', 'photo', 'search', 'category', 'homepage', 'others']]); | |||
['article', 'video', 'photo', 'search', 'category', 'homepage', 'other']]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't change this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted manually.
@@ -184,7 +184,7 @@ describes.sandboxed('amp-ad-network-adsense-impl', {}, () => { | |||
ampStickyAd.appendChild(element); | |||
fixture.doc.body.appendChild(ampStickyAd); | |||
return impl.getAdUrl().then(adUrl => { | |||
expect(adUrl.indexOf('act=sa') >= 0).to.be.true; | |||
expect(adUrl.includes('act=sa')).to.be.true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
* All indexOf > -1 rmeoved * Removed all indexOf >= 0 * Replacing indexOf != -1 and == -1 * All indexOf replaced to includes * Fixing lint errors * Only arrays should use includes * Conditions more concise and easier to read * Revert yarn.lock * Last char * Condition more readable * Removing all includes from strings * Removing the last string include
* All indexOf > -1 rmeoved * Removed all indexOf >= 0 * Replacing indexOf != -1 and == -1 * All indexOf replaced to includes * Fixing lint errors * Only arrays should use includes * Conditions more concise and easier to read * Revert yarn.lock * Last char * Condition more readable * Removing all includes from strings * Removing the last string include
.indexOf(var) > -1
to.includes(var) === true
.indexOf(var) >= 0
to.includes(var) === true
(!(array.indexOf(var) == -1)
to.includes(var) === true
.indexOf(var) == -1
to.includes(var) === false
.indexOf(var) === -1
to.includes(var) === false
.indexOf(var) < 0
to.includes(var) === false
Closes #8450