Skip to content
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

Merged
merged 16 commits into from Apr 17, 2017

Conversation

NandoSangenetto
Copy link
Contributor

@NandoSangenetto NandoSangenetto commented Apr 13, 2017

  • Changed .indexOf(var) > -1 to .includes(var) === true
  • Changed .indexOf(var) >= 0 to .includes(var) === true
  • Changed (!(array.indexOf(var) == -1) to .includes(var) === true
  • Changed .indexOf(var) == -1 to .includes(var) === false
  • Changed .indexOf(var) === -1 to .includes(var) === false
  • Changed .indexOf(var) < 0 to .includes(var) === false

Closes #8450

@googlebot
Copy link

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!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@NandoSangenetto
Copy link
Contributor Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

@dreamofabear
Copy link

/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) {

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!

Copy link

@dreamofabear dreamofabear left a 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:

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.

@NandoSangenetto
Copy link
Contributor Author

Done @choumx and @jridgewell

Copy link
Contributor

@kmh287 kmh287 left a 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'))) {
Copy link
Contributor

@kmh287 kmh287 Apr 17, 2017

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')) { ... }

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -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('#')) {
Copy link
Contributor

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.

@@ -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.')) {
Copy link
Contributor

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'))) {
Copy link
Contributor

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]) {
Copy link
Contributor

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.

@@ -179,7 +179,7 @@ export class VariableSource {
* @return {!VariableSource}
*/
setAsync(varName, asyncResolver) {
dev().assert(varName.indexOf('RETURN') == -1);
dev().assert(!varName.includes('RETURN'));
Copy link
Contributor

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);
Copy link
Contributor

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.

@@ -386,7 +386,7 @@
var context = this, names, index, lookupHit = false;

while (context) {
if (name.indexOf('.') > 0) {
if (name.includes('.')) {
Copy link
Contributor

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))) {
Copy link
Contributor

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.

@@ -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)) {
Copy link
Contributor

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.

@NandoSangenetto
Copy link
Contributor Author

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']]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't change this.

Copy link
Contributor Author

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@jridgewell jridgewell merged commit ec2a479 into ampproject:master Apr 17, 2017
DarXector pushed a commit to DarXector/amphtml that referenced this pull request Apr 25, 2017
* 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
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants