DEV: Extensively use `startsWith()` (#17540)

This commit is contained in:
Jarek Radosz 2022-07-17 20:16:39 +02:00 committed by GitHub
parent 6a4a7b1d88
commit 5f7163b5bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 34 additions and 35 deletions

View File

@ -194,7 +194,7 @@ export function buildResolver(baseName) {
findConnectorTemplate(parsedName) { findConnectorTemplate(parsedName) {
const full = parsedName.fullNameWithoutType.replace("components/", ""); const full = parsedName.fullNameWithoutType.replace("components/", "");
if (full.indexOf("connectors") === 0) { if (full.startsWith("connectors")) {
return Ember.TEMPLATES[`javascripts/${full}`]; return Ember.TEMPLATES[`javascripts/${full}`];
} }
}, },
@ -271,7 +271,7 @@ export function buildResolver(baseName) {
// (similar to how discourse lays out templates) // (similar to how discourse lays out templates)
findAdminTemplate(parsedName) { findAdminTemplate(parsedName) {
let decamelized = decamelize(parsedName.fullNameWithoutType); let decamelized = decamelize(parsedName.fullNameWithoutType);
if (decamelized.indexOf("components") === 0) { if (decamelized.startsWith("components")) {
let comPath = `admin/templates/${decamelized}`; let comPath = `admin/templates/${decamelized}`;
const compTemplate = const compTemplate =
Ember.TEMPLATES[`javascripts/${comPath}`] || Ember.TEMPLATES[comPath]; Ember.TEMPLATES[`javascripts/${comPath}`] || Ember.TEMPLATES[comPath];
@ -285,8 +285,8 @@ export function buildResolver(baseName) {
} }
if ( if (
decamelized.indexOf("admin") === 0 || decamelized.startsWith("admin") ||
decamelized.indexOf("javascripts/admin") === 0 decamelized.startsWith("javascripts/admin")
) { ) {
decamelized = decamelized.replace(/^admin\_/, "admin/templates/"); decamelized = decamelized.replace(/^admin\_/, "admin/templates/");
decamelized = decamelized.replace(/^admin\./, "admin/templates/"); decamelized = decamelized.replace(/^admin\./, "admin/templates/");

View File

@ -25,7 +25,7 @@ export function registerRawHelpers(hbs, handlebarsClass) {
let firstContext = options.contexts[0]; let firstContext = options.contexts[0];
let val = firstContext[context]; let val = firstContext[context];
if (context.toString().indexOf("controller.") === 0) { if (context.toString().startsWith("controller.")) {
context = context.slice(context.indexOf(".") + 1); context = context.slice(context.indexOf(".") + 1);
} }

View File

@ -611,8 +611,7 @@ export default Component.extend(TextareaTextManipulation, {
} }
const trimmedPre = sel.pre.trim(); const trimmedPre = sel.pre.trim();
const number = const number = sel.value.startsWith(hval)
sel.value.indexOf(hval) === 0
? sel.value.slice(hlen) ? sel.value.slice(hlen)
: `${hval}${sel.value}`; : `${hval}${sel.value}`;
const preLines = trimmedPre.length ? `${trimmedPre}\n\n` : ""; const preLines = trimmedPre.length ? `${trimmedPre}\n\n` : "";

View File

@ -38,7 +38,7 @@ export default Component.extend({
} }
// Relative urls // Relative urls
if (shareUrl.indexOf("/") === 0) { if (shareUrl.startsWith("/")) {
const location = window.location; const location = window.location;
shareUrl = `${location.protocol}//${location.host}${shareUrl}`; shareUrl = `${location.protocol}//${location.host}${shareUrl}`;
} }

View File

@ -32,7 +32,7 @@ export default Controller.extend(ModalFunctionality, GrantBadgeController, {
const protocolAndHost = const protocolAndHost =
window.location.protocol + "//" + window.location.host; window.location.protocol + "//" + window.location.host;
return url.indexOf("/") === 0 ? protocolAndHost + url : url; return url.startsWith("/") ? protocolAndHost + url : url;
}, },
@discourseComputed("saving", "selectedBadgeGrantable") @discourseComputed("saving", "selectedBadgeGrantable")

View File

@ -3,7 +3,7 @@ import deprecated from "discourse-common/lib/deprecated";
const pluses = /\+/g; const pluses = /\+/g;
function parseCookieValue(s) { function parseCookieValue(s) {
if (s.indexOf('"') === 0) { if (s.startsWith('"')) {
// This is a quoted cookie as according to RFC2068, unescape... // This is a quoted cookie as according to RFC2068, unescape...
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, "\\"); s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, "\\");
} }

View File

@ -207,7 +207,7 @@ const DiscourseLocation = EmberObject.extend({
if (url !== "") { if (url !== "") {
rootURL = rootURL.replace(/\/$/, ""); rootURL = rootURL.replace(/\/$/, "");
if (rootURL.length > 0 && url.indexOf(rootURL + "/") === 0) { if (rootURL.length > 0 && url.startsWith(rootURL + "/")) {
rootURL = ""; rootURL = "";
} }
} }

View File

@ -39,7 +39,7 @@ export default function interceptClick(e) {
!currentTarget.dataset.userCard && !currentTarget.dataset.userCard &&
currentTarget.classList.contains("ember-view")) || currentTarget.classList.contains("ember-view")) ||
currentTarget.classList.contains("lightbox") || currentTarget.classList.contains("lightbox") ||
href.indexOf("mailto:") === 0 || href.startsWith("mailto:") ||
(href.match(/^http[s]?:\/\//i) && (href.match(/^http[s]?:\/\//i) &&
!href.match(new RegExp("^https?:\\/\\/" + window.location.hostname, "i"))) !href.match(new RegExp("^https?:\\/\\/" + window.location.hostname, "i")))
) { ) {

View File

@ -44,7 +44,7 @@ export function rewritePath(path) {
let result = params[0]; let result = params[0];
rewrites.forEach((rw) => { rewrites.forEach((rw) => {
if ((rw.opts.exceptions || []).some((ex) => path.indexOf(ex) === 0)) { if ((rw.opts.exceptions || []).some((ex) => path.startsWith(ex))) {
return; return;
} }
result = result.replace(rw.regexp, rw.replacement); result = result.replace(rw.regexp, rw.replacement);
@ -248,7 +248,7 @@ const DiscourseURL = EmberObject.extend({
// Rewrite /my/* urls // Rewrite /my/* urls
let myPath = getURL("/my"); let myPath = getURL("/my");
const fullPath = getURL(path); const fullPath = getURL(path);
if (fullPath.indexOf(myPath) === 0) { if (fullPath.startsWith(myPath)) {
const currentUser = User.current(); const currentUser = User.current();
if (currentUser) { if (currentUser) {
path = fullPath.replace( path = fullPath.replace(
@ -261,7 +261,7 @@ const DiscourseURL = EmberObject.extend({
} }
// handle prefixes // handle prefixes
if (path.indexOf("/") === 0) { if (path.startsWith("/")) {
path = withoutPrefix(path); path = withoutPrefix(path);
} }
@ -318,22 +318,22 @@ const DiscourseURL = EmberObject.extend({
// Determines whether a URL is internal or not // Determines whether a URL is internal or not
isInternal(url) { isInternal(url) {
if (url && url.length) { if (url && url.length) {
if (url.indexOf("//") === 0) { if (url.startsWith("//")) {
url = "http:" + url; url = "http:" + url;
} }
if (url.indexOf("#") === 0) { if (url.startsWith("#")) {
return true; return true;
} }
if (url.indexOf("/") === 0) { if (url.startsWith("/")) {
return true; return true;
} }
if (url.indexOf(this.origin()) === 0) { if (url.startsWith(this.origin())) {
return true; return true;
} }
if (url.replace(/^http/, "https").indexOf(this.origin()) === 0) { if (url.replace(/^http/, "https").startsWith(this.origin())) {
return true; return true;
} }
if (url.replace(/^https/, "http").indexOf(this.origin()) === 0) { if (url.replace(/^https/, "http").startsWith(this.origin())) {
return true; return true;
} }
} }
@ -490,7 +490,7 @@ export function setURLContainer(container) {
} }
export function prefixProtocol(url) { export function prefixProtocol(url) {
return url.indexOf("://") === -1 && url.indexOf("mailto:") !== 0 return url.indexOf("://") === -1 && !url.startsWith("mailto:")
? "https://" + url ? "https://" + url
: url; : url;
} }

View File

@ -584,8 +584,8 @@ Category.reopenClass({
if ( if (
((emptyTerm && !category.get("parent_category_id")) || ((emptyTerm && !category.get("parent_category_id")) ||
(!emptyTerm && (!emptyTerm &&
(category.get("name").toLowerCase().indexOf(term) === 0 || (category.get("name").toLowerCase().startsWith(term) ||
category.get("slug").toLowerCase().indexOf(slugTerm) === 0))) && category.get("slug").toLowerCase().startsWith(slugTerm)))) &&
validCategoryParent(category) validCategoryParent(category)
) { ) {
data.push(category); data.push(category);

View File

@ -263,7 +263,7 @@ NavItem.reopenClass({
if ( if (
(category || !args.skipCategoriesNavItem) && (category || !args.skipCategoriesNavItem) &&
i.name.indexOf("categor") === 0 i.name.startsWith("categor")
) { ) {
return false; return false;
} }

View File

@ -209,7 +209,7 @@ export function emojiSearch(term, options) {
// if term matches from beginning // if term matches from beginning
for (const item of toSearch) { for (const item of toSearch) {
if (item.indexOf(term) === 0) { if (item.startsWith(term)) {
addResult(item); addResult(item);
} }
} }

View File

@ -121,7 +121,7 @@ export function sanitize(text, allowLister) {
(forAttr && (forAttr &&
(forAttr.indexOf("*") !== -1 || forAttr.indexOf(value) !== -1)) || (forAttr.indexOf("*") !== -1 || forAttr.indexOf(value) !== -1)) ||
(name.indexOf("data-html-") === -1 && (name.indexOf("data-html-") === -1 &&
name.indexOf("data-") === 0 && name.startsWith("data-") &&
(forTag["data-*"] || testDataAttribute(forTag, name, value))) || (forTag["data-*"] || testDataAttribute(forTag, name, value))) ||
(tag === "a" && (tag === "a" &&
name === "href" && name === "href" &&
@ -129,7 +129,7 @@ export function sanitize(text, allowLister) {
(tag === "iframe" && (tag === "iframe" &&
name === "src" && name === "src" &&
allowedIframes.some((i) => { allowedIframes.some((i) => {
return value.toLowerCase().indexOf((i || "").toLowerCase()) === 0; return value.toLowerCase().startsWith((i || "").toLowerCase());
})) }))
) { ) {
return attr(name, value); return attr(name, value);

View File

@ -22,12 +22,12 @@ const rule = {
let i; let i;
for (i = 1; i < split.length; 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); postNumber = parseInt(split[i].slice(5), 10);
continue; continue;
} }
if (split[i].indexOf("topic:") === 0) { if (split[i].startsWith("topic:")) {
topicId = parseInt(split[i].slice(6), 10); topicId = parseInt(split[i].slice(6), 10);
continue; continue;
} }
@ -39,7 +39,7 @@ const rule = {
// if we have the additional attribute of username: because we are prioritizing full name // if we have the additional attribute of username: because we are prioritizing full name
// then assign the name to be the displayName // 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 // 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 name // this protects us from when a user has a `,` in their name
displayName = split.slice(0, split.indexOf(`post:${postNumber}`)); displayName = split.slice(0, split.indexOf(`post:${postNumber}`));

View File

@ -1,5 +1,5 @@
function resolve(path) { function resolve(path) {
if (path.indexOf("settings") === 0 || path.indexOf("transformed") === 0) { if (path.startsWith("settings") || path.startsWith("transformed")) {
return `this.${path}`; return `this.${path}`;
} }
return path; return path;

View File

@ -17,7 +17,7 @@
var queryParams = {}; var queryParams = {};
if (DE.discourseEmbedUrl) { if (DE.discourseEmbedUrl) {
if (DE.discourseEmbedUrl.indexOf("/") === 0) { if (DE.discourseEmbedUrl.startsWith("/")) {
console.error( console.error(
"discourseEmbedUrl must be a full URL, not a relative path" "discourseEmbedUrl must be a full URL, not a relative path"
); );