Skip to content

Commit

Permalink
DEV: Extensively use includes() (discourse#17541)
Browse files Browse the repository at this point in the history
Also, the change in insert-hyperlink (from `this.linkUrl.indexOf("http") === -1` to `!this.linkUrl.startsWith("http")`) was intentional fix: we don't want to prevent users from looking up topics with http in their titles.
  • Loading branch information
CvX authored Jul 17, 2022
1 parent 5f7163b commit 057d6b4
Show file tree
Hide file tree
Showing 55 changed files with 95 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default Component.extend({
if (["color_definitions"].includes(fieldName)) {
return "scss";
}
return fieldName && fieldName.indexOf("scss") > -1 ? "scss" : "html";
return fieldName && fieldName.includes("scss") ? "scss" : "html";
},

@discourseComputed("currentTargetName", "fieldName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Component from "@ember/component";

function RGBToHex(rgb) {
// Choose correct separator
let sep = rgb.indexOf(",") > -1 ? "," : " ";
let sep = rgb.includes(",") ? "," : " ";
// Turn "rgb(r,g,b)" into [r,g,b]
rgb = rgb.slice(4).split(")")[0].split(sep);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default Component.extend({

@discourseComputed("choices.[]", "collection.[]")
filteredChoices(choices, collection) {
return makeArray(choices).filter((i) => collection.indexOf(i) < 0);
return makeArray(choices).filter((i) => !collection.includes(i));
},

keyDown(event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default Controller.extend({
if (available) {
const themes = !childThemes
? available
: available.filter((theme) => childThemes.indexOf(theme) === -1);
: available.filter((theme) => !childThemes.includes(theme));
return themes.length === 0 ? null : themes;
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export default Controller.extend({
filter = filter.toLowerCase();
reports = reports.filter((report) => {
return (
(get(report, "title") || "").toLowerCase().indexOf(filter) > -1 ||
(get(report, "description") || "").toLowerCase().indexOf(filter) > -1
(get(report, "title") || "").toLowerCase().includes(filter) ||
(get(report, "description") || "").toLowerCase().includes(filter)
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default Controller.extend({

this.allWatchedWords.forEach((wordsForAction) => {
const wordRecords = wordsForAction.words.filter((wordRecord) => {
return wordRecord.word.indexOf(filter) > -1;
return wordRecord.word.includes(filter);
});

model.pushObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default Controller.extend({
_addIncoming(eventId) {
const incomingEventIds = this.incomingEventIds;

if (incomingEventIds.indexOf(eventId) === -1) {
if (!incomingEventIds.includes(eventId)) {
incomingEventIds.pushObject(eventId);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default Controller.extend({
@discourseComputed("model.secret")
secretValidation(secret) {
if (!isEmpty(secret)) {
if (secret.indexOf(" ") !== -1) {
if (secret.includes(" ")) {
return EmberObject.create({
failed: true,
reason: I18n.t("admin.web_hooks.secret_invalid"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default Mixin.create({

@discourseComputed("type")
componentType(type) {
return CUSTOM_TYPES.indexOf(type) !== -1 ? type : "string";
return CUSTOM_TYPES.includes(type) ? type : "string";
},

@discourseComputed("setting")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function registerIconRenderer(renderer) {
function iconClasses(icon, params) {
// "notification." is invalid syntax for classes, use replacement instead
const dClass =
icon.replacementId && icon.id.indexOf("notification.") > -1
icon.replacementId && icon.id.includes("notification.")
? icon.replacementId
: icon.id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ function niceAttr(attr) {
let i;

for (i = 0; i < parts.length; i++) {
if (
parts[i] === "@each" ||
parts[i] === "[]" ||
parts[i].indexOf("{") !== -1
) {
if (parts[i] === "@each" || parts[i] === "[]" || parts[i].includes("{")) {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/adapters/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default EmberObject.extend({
},

basePath(store, type) {
if (ADMIN_MODELS.indexOf(type.replace("_", "-")) !== -1) {
if (ADMIN_MODELS.includes(type.replace("_", "-"))) {
return "/admin/";
}
return "/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ export default Component.extend(ComposerUploadUppy, {
}

let name = mention.dataset.name;
if (found.indexOf(name) === -1) {
if (!found.includes(name)) {
this.groupsMentioned([
{
name,
Expand Down Expand Up @@ -517,7 +517,7 @@ export default Component.extend(ComposerUploadUppy, {
preview?.querySelectorAll(".mention.cannot-see")?.forEach((mention) => {
let name = mention.dataset.name;

if (found.indexOf(name) === -1) {
if (!found.includes(name)) {
// add a delay to allow for typing, so you don't open the warning right away
// previously we would warn after @bob even if you were about to mention @bob2
discourseLater(
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/discourse/app/components/d-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ export default Component.extend(TextareaTextManipulation, {
},

_applyList(sel, head, exampleKey, opts) {
if (sel.value.indexOf("\n") !== -1) {
if (sel.value.includes("\n")) {
this.applySurround(sel, head, "", exampleKey, opts);
} else {
const [hval, hlen] = getHead(head);
Expand Down Expand Up @@ -739,7 +739,7 @@ export default Component.extend(TextareaTextManipulation, {

const sel = this.getSelected("", { lineVal: true });
const selValue = sel.value;
const hasNewLine = selValue.indexOf("\n") !== -1;
const hasNewLine = selValue.includes("\n");
const isBlankLine = sel.lineVal.trim().length === 0;
const isFourSpacesIndent =
this.siteSettings.code_formatting_style === FOUR_SPACES_INDENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default TextField.extend({
// https://bugs.chromium.org/p/chromium/issues/detail?id=987293
// work around issue while leaving a semi useable honeypot for
// bots that are running full Chrome
if (navigator.userAgent.indexOf("Chrome") > -1) {
if (navigator.userAgent.includes("Chrome")) {
this.set("type", "text");
} else {
this.set("type", "password");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default MountWidget.extend({

delete prev[postNumber];

if (onscreen.indexOf(idx) !== -1) {
if (onscreen.includes(idx)) {
onscreenPostNumbers.push(postNumber);
if (post.read) {
readPostNumbers.push(postNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ export default TextField.extend({
return v.username || v.name;
} else {
const excludes = allExcludedUsernames();
return v.usernames.filter((item) => excludes.indexOf(item) === -1);
return v.usernames.filter((item) => !excludes.includes(item));
}
},

onChangeItems(items) {
let hasGroups = false;
items = items.map((i) => {
if (groups.indexOf(i) > -1) {
if (groups.includes(i)) {
hasGroups = true;
}
return i.username ? i.username : i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default Controller.extend(ModalFunctionality, {
}

if (this.siteSettings.hide_email_address_taken) {
return (accountEmailOrUsername || "").indexOf("@") === -1;
return !(accountEmailOrUsername || "").includes("@");
} else {
return isEmpty((accountEmailOrUsername || "").trim());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default Controller.extend({

@discourseComputed("q")
showLikeCount(q) {
return q && q.indexOf("order:likes") > -1;
return q?.includes("order:likes");
},

@observes("q")
Expand All @@ -211,11 +211,11 @@ export default Controller.extend({
return (
q &&
this.currentUser &&
(q.indexOf("in:messages") > -1 ||
q.indexOf("in:personal") > -1 ||
q.indexOf(
(q.includes("in:messages") ||
q.includes("in:personal") ||
q.includes(
`personal_messages:${this.currentUser.get("username_lower")}`
) > -1)
))
);
},

Expand Down Expand Up @@ -394,7 +394,7 @@ export default Controller.extend({
actions: {
createTopic(searchTerm) {
let topicCategory;
if (searchTerm.indexOf("category:") !== -1) {
if (searchTerm.includes("category:")) {
const match = searchTerm.match(/category:(\S*)/);
if (match && match[1]) {
topicCategory = match[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export default Controller.extend(ModalFunctionality, {
},

triggerSearch() {
if (this.linkUrl.length > 3 && this.linkUrl.indexOf("http") === -1) {
if (this.linkUrl.length > 3 && !this.linkUrl.startsWith("http")) {
this.set("searchLoading", true);
this._activeSearch = searchForTerm(this.linkUrl, {
typeFilter: "topic",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default Controller.extend({
}

let newList = this.reviewables.reject((reviewable) => {
return ids.indexOf(reviewable.id) !== -1;
return ids.includes(reviewable.id);
});

if (newList.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
initialize() {
let queryStrings = window.location.search;

if (queryStrings.indexOf("user_api_public_key") !== -1) {
if (queryStrings.includes("user_api_public_key")) {
let params = queryStrings.startsWith("?")
? queryStrings.slice(1).split("&")
: [];
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/lib/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ export default function (options) {
}

function performAutocomplete(e) {
if ([keys.esc, keys.enter].indexOf(e.which) !== -1) {
if ([keys.esc, keys.enter].includes(e.which)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/lib/autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const set =

return {
has(key) {
return Boolean(list.indexOf(key) > -1);
return Boolean(list.includes(key));
},
add(key) {
list.push(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const DefaultConnectorClass = {

function findOutlets(collection, callback) {
Object.keys(collection).forEach(function (res) {
if (res.indexOf("/connectors/") !== -1) {
if (res.includes("/connectors/")) {
const segments = res.split("/");
let outletName = segments[segments.length - 2];
const uniqueName = segments[segments.length - 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function isTrackedTopic(topic) {
const tags = User.current().trackedTags;

for (const tag of tags) {
if (topic.tags.indexOf(tag) > -1) {
if (topic.tags.includes(tag)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/lib/transform-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default function transformPost(
}

const showTopicMap =
_additionalAttributes.indexOf("topicMap") !== -1 ||
_additionalAttributes.includes("topicMap") ||
showPMMap ||
(post.post_number === 1 &&
topic.archetype === "regular" &&
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/lib/uploads.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function extensionsToArray(exts) {
.toLowerCase()
.replace(/[\s\.]+/g, "")
.split("|")
.filter((ext) => ext.indexOf("*") === -1);
.filter((ext) => !ext.includes("*"));
}

function extensions(siteSettings) {
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export function setURLContainer(container) {
}

export function prefixProtocol(url) {
return url.indexOf("://") === -1 && !url.startsWith("mailto:")
return !url.includes("://") && !url.startsWith("mailto:")
? "https://" + url
: url;
}
Expand Down
6 changes: 3 additions & 3 deletions app/assets/javascripts/discourse/app/lib/user-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function organizeResults(r, options) {

if (r.users) {
r.users.every(function (u) {
if (exclude.indexOf(u.username) === -1) {
if (!exclude.includes(u.username)) {
users.push(u);
results.push(u);
}
Expand All @@ -179,7 +179,7 @@ function organizeResults(r, options) {
options.term.toLowerCase() === g.name.toLowerCase() ||
results.length < limit
) {
if (exclude.indexOf(g.name) === -1) {
if (!exclude.includes(g.name)) {
groups.push(g);
results.push(g);
}
Expand Down Expand Up @@ -207,7 +207,7 @@ export function skipSearch(term, allowEmails, lastSeenUsers = false) {
if (lastSeenUsers) {
return false;
}
if (term.indexOf("@") > -1 && !allowEmails) {
if (term.includes("@") && !allowEmails) {
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/discourse/app/lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function hostnameValid(hostname) {
}

export function extractDomainFromUrl(url) {
if (url.indexOf("://") > -1) {
if (url.includes("://")) {
url = url.split("/")[2];
} else {
url = url.split("/")[0];
Expand Down Expand Up @@ -441,7 +441,7 @@ export function areCookiesEnabled() {
// see: https://github.com/Modernizr/Modernizr/blob/400db4043c22af98d46e1d2b9cbc5cb062791192/feature-detects/cookies.js
try {
document.cookie = "cookietest=1";
let ret = document.cookie.indexOf("cookietest=") !== -1;
let ret = document.cookie.includes("cookietest=");
document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
return ret;
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/models/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const Bookmark = RestModel.extend({
const newTags = [];

tags.forEach(function (tag) {
if (title.toLowerCase().indexOf(tag) === -1) {
if (!title.toLowerCase().includes(tag)) {
newTags.push(tag);
}
});
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/app/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ Category.reopenClass({
category.get("slug").toLowerCase().indexOf(slugTerm) > 0) &&
validCategoryParent(category)
) {
if (data.indexOf(category) === -1) {
if (!data.includes(category)) {
data.push(category);
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/discourse/app/models/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ const Composer = RestModel.extend({
if (
!categoryId &&
categoryIds &&
(categoryIds.indexOf(this.site.uncategorized_category_id) !== -1 ||
(categoryIds.includes(this.site.uncategorized_category_id) ||
!this.siteSettings.allow_uncategorized_topics)
) {
return true;
}
return (
categoryIds === undefined ||
!categoryIds.length ||
categoryIds.indexOf(categoryId) !== -1
categoryIds.includes(categoryId)
);
},

Expand Down
Loading

0 comments on commit 057d6b4

Please sign in to comment.