A platform for community discussion. Free, open, simple.
DEV: Extensively use `startsWith()` (#17540)
Jarek Radosz5f7163b5bb85
diff --git a/app/assets/javascripts/discourse-common/addon/lib/legacy-resolver.js b/app/assets/javascripts/discourse-common/addon/lib/legacy-resolver.jsindex b7d66de8a27..66cc058216e 100644--- a/app/assets/javascripts/discourse-common/addon/lib/legacy-resolver.js+++ b/app/assets/javascripts/discourse-common/addon/lib/legacy-resolver.js@@ -194,7 +194,7 @@ export function buildResolver(baseName) {findConnectorTemplate(parsedName) {const full = parsedName.fullNameWithoutType.replace("components/", "");- if (full.indexOf("connectors") === 0) {+ if (full.startsWith("connectors")) {return Ember.TEMPLATES[`javascripts/${full}`];}},@@ -271,7 +271,7 @@ export function buildResolver(baseName) {// (similar to how discourse lays out templates)findAdminTemplate(parsedName) {let decamelized = decamelize(parsedName.fullNameWithoutType);- if (decamelized.indexOf("components") === 0) {+ if (decamelized.startsWith("components")) {let comPath = `admin/templates/${decamelized}`;const compTemplate =Ember.TEMPLATES[`javascripts/${comPath}`] || Ember.TEMPLATES[comPath];@@ -285,8 +285,8 @@ export function buildResolver(baseName) {}if (- decamelized.indexOf("admin") === 0 ||- decamelized.indexOf("javascripts/admin") === 0+ decamelized.startsWith("admin") ||+ decamelized.startsWith("javascripts/admin")) {decamelized = decamelized.replace(/^admin\_/, "admin/templates/");decamelized = decamelized.replace(/^admin\./, "admin/templates/");diff --git a/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars-helpers.js b/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars-helpers.jsindex c992c51b51f..2e1e8ca89b8 100644--- a/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars-helpers.js+++ b/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars-helpers.js@@ -25,7 +25,7 @@ export function registerRawHelpers(hbs, handlebarsClass) {let firstContext = options.contexts[0];let val = firstContext[context];- if (context.toString().indexOf("controller.") === 0) {+ if (context.toString().startsWith("controller.")) {context = context.slice(context.indexOf(".") + 1);}diff --git a/app/assets/javascripts/discourse/app/components/d-editor.js b/app/assets/javascripts/discourse/app/components/d-editor.jsindex 305cc6baa4f..5515b0e83ee 100644--- a/app/assets/javascripts/discourse/app/components/d-editor.js+++ b/app/assets/javascripts/discourse/app/components/d-editor.js@@ -611,10 +611,9 @@ export default Component.extend(TextareaTextManipulation, {}const trimmedPre = sel.pre.trim();- const number =- sel.value.indexOf(hval) === 0- ? sel.value.slice(hlen)- : `${hval}${sel.value}`;+ const number = sel.value.startsWith(hval)+ ? sel.value.slice(hlen)+ : `${hval}${sel.value}`;const preLines = trimmedPre.length ? `${trimmedPre}\n\n` : "";const trimmedPost = sel.post.trim();diff --git a/app/assets/javascripts/discourse/app/components/share-panel.js b/app/assets/javascripts/discourse/app/components/share-panel.jsindex a2e111fa2e5..e742360ee52 100644--- a/app/assets/javascripts/discourse/app/components/share-panel.js+++ b/app/assets/javascripts/discourse/app/components/share-panel.js@@ -38,7 +38,7 @@ export default Component.extend({}// Relative urls- if (shareUrl.indexOf("/") === 0) {+ if (shareUrl.startsWith("/")) {const location = window.location;shareUrl = `${location.protocol}//${location.host}${shareUrl}`;}diff --git a/app/assets/javascripts/discourse/app/controllers/grant-badge.js b/app/assets/javascripts/discourse/app/controllers/grant-badge.jsindex 5d86380bc7e..0b09e326616 100644--- a/app/assets/javascripts/discourse/app/controllers/grant-badge.js+++ b/app/assets/javascripts/discourse/app/controllers/grant-badge.js@@ -32,7 +32,7 @@ export default Controller.extend(ModalFunctionality, GrantBadgeController, {const protocolAndHost =window.location.protocol + "//" + window.location.host;- return url.indexOf("/") === 0 ? protocolAndHost + url : url;+ return url.startsWith("/") ? protocolAndHost + url : url;},@discourseComputed("saving", "selectedBadgeGrantable")diff --git a/app/assets/javascripts/discourse/app/lib/cookie.js b/app/assets/javascripts/discourse/app/lib/cookie.jsindex 956eda24db3..8409200930e 100644--- a/app/assets/javascripts/discourse/app/lib/cookie.js+++ b/app/assets/javascripts/discourse/app/lib/cookie.js@@ -3,7 +3,7 @@ import deprecated from "discourse-common/lib/deprecated";const pluses = /\+/g;function parseCookieValue(s) {- if (s.indexOf('"') === 0) {+ if (s.startsWith('"')) {// This is a quoted cookie as according to RFC2068, unescape...s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, "\\");}diff --git a/app/assets/javascripts/discourse/app/lib/discourse-location.js b/app/assets/javascripts/discourse/app/lib/discourse-location.jsindex fdbe91410a4..d672978f269 100644--- a/app/assets/javascripts/discourse/app/lib/discourse-location.js+++ b/app/assets/javascripts/discourse/app/lib/discourse-location.js@@ -207,7 +207,7 @@ const DiscourseLocation = EmberObject.extend({if (url !== "") {rootURL = rootURL.replace(/\/$/, "");- if (rootURL.length > 0 && url.indexOf(rootURL + "/") === 0) {+ if (rootURL.length > 0 && url.startsWith(rootURL + "/")) {rootURL = "";}}diff --git a/app/assets/javascripts/discourse/app/lib/intercept-click.js b/app/assets/javascripts/discourse/app/lib/intercept-click.jsindex 46b6da45c8c..62d91484ed9 100644--- a/app/assets/javascripts/discourse/app/lib/intercept-click.js+++ b/app/assets/javascripts/discourse/app/lib/intercept-click.js@@ -39,7 +39,7 @@ export default function interceptClick(e) {!currentTarget.dataset.userCard &¤tTarget.classList.contains("ember-view")) ||currentTarget.classList.contains("lightbox") ||- href.indexOf("mailto:") === 0 ||+ href.startsWith("mailto:") ||(href.match(/^http[s]?:\/\//i) &&!href.match(new RegExp("^https?:\\/\\/" + window.location.hostname, "i")))) {diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.jsindex 9d168964c59..854647583cf 100644--- a/app/assets/javascripts/discourse/app/lib/url.js+++ b/app/assets/javascripts/discourse/app/lib/url.js@@ -44,7 +44,7 @@ export function rewritePath(path) {let result = params[0];rewrites.forEach((rw) => {- if ((rw.opts.exceptions || []).some((ex) => path.indexOf(ex) === 0)) {+ if ((rw.opts.exceptions || []).some((ex) => path.startsWith(ex))) {return;}result = result.replace(rw.regexp, rw.replacement);@@ -248,7 +248,7 @@ const DiscourseURL = EmberObject.extend({// Rewrite /my/* urlslet myPath = getURL("/my");const fullPath = getURL(path);- if (fullPath.indexOf(myPath) === 0) {+ if (fullPath.startsWith(myPath)) {const currentUser = User.current();if (currentUser) {path = fullPath.replace(@@ -261,7 +261,7 @@ const DiscourseURL = EmberObject.extend({}// handle prefixes- if (path.indexOf("/") === 0) {+ if (path.startsWith("/")) {path = withoutPrefix(path);}@@ -318,22 +318,22 @@ const DiscourseURL = EmberObject.extend({// Determines whether a URL is internal or notisInternal(url) {if (url && url.length) {- if (url.indexOf("//") === 0) {+ if (url.startsWith("//")) {url = "http:" + url;}- if (url.indexOf("#") === 0) {+ if (url.startsWith("#")) {return true;}- if (url.indexOf("/") === 0) {+ if (url.startsWith("/")) {return true;}- if (url.indexOf(this.origin()) === 0) {+ if (url.startsWith(this.origin())) {return true;}- if (url.replace(/^http/, "https").indexOf(this.origin()) === 0) {+ if (url.replace(/^http/, "https").startsWith(this.origin())) {return true;}- if (url.replace(/^https/, "http").indexOf(this.origin()) === 0) {+ if (url.replace(/^https/, "http").startsWith(this.origin())) {return true;}}@@ -490,7 +490,7 @@ export function setURLContainer(container) {}export function prefixProtocol(url) {- return url.indexOf("://") === -1 && url.indexOf("mailto:") !== 0+ return url.indexOf("://") === -1 && !url.startsWith("mailto:")? "https://" + url: url;}diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.jsindex 1c72cec4cfe..9abf1372391 100644--- a/app/assets/javascripts/discourse/app/models/category.js+++ b/app/assets/javascripts/discourse/app/models/category.js@@ -584,8 +584,8 @@ Category.reopenClass({if (((emptyTerm && !category.get("parent_category_id")) ||(!emptyTerm &&- (category.get("name").toLowerCase().indexOf(term) === 0 ||- category.get("slug").toLowerCase().indexOf(slugTerm) === 0))) &&+ (category.get("name").toLowerCase().startsWith(term) ||+ category.get("slug").toLowerCase().startsWith(slugTerm)))) &&validCategoryParent(category)) {data.push(category);diff --git a/app/assets/javascripts/discourse/app/models/nav-item.js b/app/assets/javascripts/discourse/app/models/nav-item.jsindex 56d1f9566c1..a1869dbffd9 100644--- a/app/assets/javascripts/discourse/app/models/nav-item.js+++ b/app/assets/javascripts/discourse/app/models/nav-item.js@@ -263,7 +263,7 @@ NavItem.reopenClass({if ((category || !args.skipCategoriesNavItem) &&- i.name.indexOf("categor") === 0+ i.name.startsWith("categor")) {return false;}diff --git a/app/assets/javascripts/pretty-text/addon/emoji.js b/app/assets/javascripts/pretty-text/addon/emoji.jsindex 597b1cf627c..e8f2a29f067 100644--- a/app/assets/javascripts/pretty-text/addon/emoji.js+++ b/app/assets/javascripts/pretty-text/addon/emoji.js@@ -209,7 +209,7 @@ export function emojiSearch(term, options) {// if term matches from beginningfor (const item of toSearch) {- if (item.indexOf(term) === 0) {+ if (item.startsWith(term)) {addResult(item);}}diff --git a/app/assets/javascripts/pretty-text/addon/sanitizer.js b/app/assets/javascripts/pretty-text/addon/sanitizer.jsindex 6592232bef2..69b24d0ac3a 100644--- a/app/assets/javascripts/pretty-text/addon/sanitizer.js+++ b/app/assets/javascripts/pretty-text/addon/sanitizer.js@@ -121,7 +121,7 @@ export function sanitize(text, allowLister) {(forAttr &&(forAttr.indexOf("*") !== -1 || forAttr.indexOf(value) !== -1)) ||(name.indexOf("data-html-") === -1 &&- name.indexOf("data-") === 0 &&+ name.startsWith("data-") &&(forTag["data-*"] || testDataAttribute(forTag, name, value))) ||(tag === "a" &&name === "href" &&@@ -129,7 +129,7 @@ export function sanitize(text, allowLister) {(tag === "iframe" &&name === "src" &&allowedIframes.some((i) => {- return value.toLowerCase().indexOf((i || "").toLowerCase()) === 0;+ return value.toLowerCase().startsWith((i || "").toLowerCase());}))) {return attr(name, value);diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.jsindex 5bd51dfb905..11bd3a77797 100644--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/quotes.js@@ -22,12 +22,12 @@ const rule = {let i;for (i = 1; i < split.length; i++) {- if (split[i].indexOf("post:") === 0) {+ if (split[i].startsWith("post:")) {postNumber = parseInt(split[i].slice(5), 10);continue;}- if (split[i].indexOf("topic:") === 0) {+ if (split[i].startsWith("topic:")) {topicId = parseInt(split[i].slice(6), 10);continue;}@@ -39,7 +39,7 @@ const rule = {// if we have the additional attribute of username: because we are prioritizing full name// then assign the name to be the displayName- if (split[i].indexOf("username:") === 0) {+ if (split[i].startsWith("username:")) {// return users name by selecting all values from the first index to the post// this protects us from when a user has a `,` in their namedisplayName = split.slice(0, split.indexOf(`post:${postNumber}`));diff --git a/lib/javascripts/widget-hbs-compiler.js b/lib/javascripts/widget-hbs-compiler.jsindex 3f23ef53133..0b70f166950 100644--- a/lib/javascripts/widget-hbs-compiler.js+++ b/lib/javascripts/widget-hbs-compiler.js@@ -1,5 +1,5 @@function resolve(path) {- if (path.indexOf("settings") === 0 || path.indexOf("transformed") === 0) {+ if (path.startsWith("settings") || path.startsWith("transformed")) {return `this.${path}`;}return path;diff --git a/public/javascripts/embed.js b/public/javascripts/embed.jsindex 2cb671c93df..5cd27a925c7 100644--- a/public/javascripts/embed.js+++ b/public/javascripts/embed.js@@ -17,7 +17,7 @@var queryParams = {};if (DE.discourseEmbedUrl) {- if (DE.discourseEmbedUrl.indexOf("/") === 0) {+ if (DE.discourseEmbedUrl.startsWith("/")) {console.error("discourseEmbedUrl must be a full URL, not a relative path");