Skip to content

Commit 72f394e

Browse files
committed
Refactor all .length === 0 patterns in JS
1 parent 4734d43 commit 72f394e

14 files changed

+31
-40
lines changed

web_src/js/components/DiffCommitSelector.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default {
103103
this.menuVisible = !this.menuVisible;
104104
// load our commits when the menu is not yet visible (it'll be toggled after loading)
105105
// and we got no commits
106-
if (this.commits.length === 0 && this.menuVisible && !this.isLoading) {
106+
if (!this.commits.length && this.menuVisible && !this.isLoading) {
107107
this.isLoading = true;
108108
try {
109109
await this.fetchCommits();
@@ -216,7 +216,7 @@ export default {
216216
<div
217217
v-if="lastReviewCommitSha != null" role="menuitem"
218218
class="vertical item"
219-
:class="{disabled: commitsSinceLastReview === 0}"
219+
:class="{disabled: !commitsSinceLastReview}"
220220
@keydown.enter="changesSinceLastReviewClick()"
221221
@click="changesSinceLastReviewClick()"
222222
>

web_src/js/components/RepoActionView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ export function initRepositoryActionView() {
453453
{{ locale.showFullScreen }}
454454
</a>
455455
<div class="divider"/>
456-
<a :class="['item', currentJob.steps.length === 0 ? 'disabled' : '']" :href="run.link+'/jobs/'+jobIndex+'/logs'" target="_blank">
456+
<a :class="['item', !currentJob.steps.length ? 'disabled' : '']" :href="run.link+'/jobs/'+jobIndex+'/logs'" target="_blank">
457457
<i class="icon"><SvgIcon name="octicon-download"/></i>
458458
{{ locale.downloadLogs }}
459459
</a>

web_src/js/components/RepoBranchTagSelector.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ const sfc = {
1919
});
2020
2121
// TODO: fix this anti-pattern: side-effects-in-computed-properties
22-
this.active = (items.length === 0 && this.showCreateNewBranch ? 0 : -1);
22+
this.active = !items.length && this.showCreateNewBranch ? 0 : -1;
2323
return items;
2424
},
2525
showNoResults() {
26-
return this.filteredItems.length === 0 && !this.showCreateNewBranch;
26+
return !this.filteredItems.length && !this.showCreateNewBranch;
2727
},
2828
showCreateNewBranch() {
2929
if (this.disableCreateBranch || !this.searchTerm) {
3030
return false;
3131
}
32-
return this.items.filter((item) => item.name.toLowerCase() === this.searchTerm.toLowerCase()).length === 0;
32+
return !this.items.filter((item) => {
33+
return item.name.toLowerCase() === this.searchTerm.toLowerCase();
34+
}).length;
3335
},
3436
formActionUrl() {
3537
return `${this.repoLink}/branches/_new/${this.branchNameSubURL}`;

web_src/js/features/admin/common.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import {POST} from '../../modules/fetch.js';
66
const {appSubUrl} = window.config;
77

88
export function initAdminCommon() {
9-
if ($('.page-content.admin').length === 0) {
10-
return;
11-
}
9+
if (!$('.page-content.admin').length) return;
1210

1311
// check whether appUrl(ROOT_URL) is correct, if not, show an error message
1412
checkAppUrl();

web_src/js/features/common-global.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {appUrl, appSubUrl, csrfToken, i18n} = window.config;
1919
export function initGlobalFormDirtyLeaveConfirm() {
2020
// Warn users that try to leave a page after entering data into a form.
2121
// Except on sign-in pages, and for forms marked as 'ignore-dirty'.
22-
if ($('.user.signin').length === 0) {
22+
if (!$('.user.signin').length) {
2323
$('form:not(.ignore-dirty)').areYouSure();
2424
}
2525
}

web_src/js/features/comp/SearchUserBox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function initCompSearchUserBox() {
3434
}
3535
});
3636

37-
if (allowEmailInput && items.length === 0 && looksLikeEmailAddressCheck.test(searchQuery)) {
37+
if (allowEmailInput && !items.length && looksLikeEmailAddressCheck.test(searchQuery)) {
3838
const resultItem = {
3939
title: searchQuery,
4040
description: allowEmailDescription,

web_src/js/features/repo-diff.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ function initRepoDiffShowMore() {
212212

213213
export function initRepoDiffView() {
214214
initRepoDiffConversationForm();
215-
const $diffFileList = $('#diff-file-list');
216-
if ($diffFileList.length === 0) return;
215+
if (!$('#diff-file-list').length) return;
217216
initDiffFileTree();
218217
initDiffCommitSelect();
219218
initRepoDiffShowMore();

web_src/js/features/repo-editor.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@ function initEditPreviewTab($form) {
3939
}
4040

4141
function initEditorForm() {
42-
if ($('.repository .edit.form').length === 0) {
43-
return;
44-
}
45-
46-
initEditPreviewTab($('.repository .edit.form'));
42+
const $form = $('.repository .edit.form');
43+
if (!$form) return;
44+
initEditPreviewTab($form);
4745
}
4846

4947
function getCursorPosition($e) {
@@ -165,7 +163,7 @@ export function initRepoEditor() {
165163

166164
commitButton?.addEventListener('click', (e) => {
167165
// A modal which asks if an empty file should be committed
168-
if ($editArea.val().length === 0) {
166+
if (!$editArea.val()) {
169167
$('#edit-empty-content-modal').modal({
170168
onApprove() {
171169
$('.edit.form').trigger('submit');

web_src/js/features/repo-findfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function filterRepoFiles(filter) {
7777

7878
const filterResult = filterRepoFilesWeighted(files, filter);
7979

80-
toggleElem(repoFindFileNoResult, filterResult.length === 0);
80+
toggleElem(repoFindFileNoResult, !filterResult.length);
8181
for (const r of filterResult) {
8282
const row = document.createElement('tr');
8383
const cell = document.createElement('td');

web_src/js/features/repo-home.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ export function initRepoTopicBar() {
151151

152152
$.fn.form.settings.rules.validateTopic = function (_values, regExp) {
153153
const $topics = $topicDropdown.children('a.ui.label');
154-
const status = $topics.length === 0 || $topics.last().attr('data-value').match(regExp);
154+
const status = !$topics.length || $topics.last().attr('data-value').match(regExp);
155155
if (!status) {
156156
$topics.last().removeClass('green').addClass('red');
157157
}
158-
return status && $topicDropdown.children('a.ui.label.red').length === 0;
158+
return status && !$topicDropdown.children('a.ui.label.red').length;
159159
};
160160

161161
$topicForm.form({

web_src/js/features/repo-issue.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export async function updateIssuesMeta(url, action, issue_ids, id) {
362362
}
363363

364364
export function initRepoIssueComments() {
365-
if ($('.repository.view.issue .timeline').length === 0) return;
365+
if (!$('.repository.view.issue .timeline').length) return;
366366

367367
$('.re-request-review').on('click', async function (e) {
368368
e.preventDefault();
@@ -377,15 +377,15 @@ export function initRepoIssueComments() {
377377

378378
$(document).on('click', (event) => {
379379
const $urlTarget = $(':target');
380-
if ($urlTarget.length === 0) return;
380+
if (!$urlTarget.length) return;
381381

382382
const urlTargetId = $urlTarget.attr('id');
383383
if (!urlTargetId) return;
384384
if (!/^(issue|pull)(comment)?-\d+$/.test(urlTargetId)) return;
385385

386386
const $target = $(event.target);
387387

388-
if ($target.closest(`#${urlTargetId}`).length === 0) {
388+
if (!$target.closest(`#${urlTargetId}`).length) {
389389
const scrollPosition = $(window).scrollTop();
390390
window.location.hash = '';
391391
$(window).scrollTop(scrollPosition);
@@ -478,9 +478,7 @@ export function initRepoPullRequestReview() {
478478
}
479479

480480
// The following part is only for diff views
481-
if ($('.repository.pull.diff').length === 0) {
482-
return;
483-
}
481+
if (!$('.repository.pull.diff').length) return;
484482

485483
const $reviewBtn = $('.js-btn-review');
486484
const $panel = $reviewBtn.parent().find('.review-box-panel');
@@ -529,7 +527,7 @@ export function initRepoPullRequestReview() {
529527

530528
const $td = $ntr.find(`.add-comment-${side}`);
531529
const $commentCloud = $td.find('.comment-code-cloud');
532-
if ($commentCloud.length === 0 && !$ntr.find('button[name="pending_review"]').length) {
530+
if (!$commentCloud.length && !$ntr.find('button[name="pending_review"]').length) {
533531
try {
534532
const response = await GET($(this).closest('[data-new-comment-url]').attr('data-new-comment-url'));
535533
const html = await response.text();
@@ -626,7 +624,7 @@ export function initRepoIssueTitleEdit() {
626624
};
627625

628626
const pullrequest_target_update_url = $(this).attr('data-target-update-url');
629-
if ($editInput.val().length === 0 || $editInput.val() === $issueTitle.text()) {
627+
if (!$editInput.val().length || $editInput.val() === $issueTitle.text()) {
630628
$editInput.val($issueTitle.text());
631629
await pullrequest_targetbranch_change(pullrequest_target_update_url);
632630
} else {

web_src/js/features/repo-legacy.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ function reloadConfirmDraftComment() {
5050

5151
export function initRepoCommentForm() {
5252
const $commentForm = $('.comment.form');
53-
if ($commentForm.length === 0) {
54-
return;
55-
}
53+
if (!$commentForm.length) return;
5654

5755
if ($commentForm.find('.field.combo-editor-dropzone').length) {
5856
// at the moment, if a form has multiple combo-markdown-editors, it must be an issue template form
@@ -202,7 +200,7 @@ export function initRepoCommentForm() {
202200
$($(this).data('id-selector')).addClass('gt-hidden');
203201
}
204202
});
205-
if (listIds.length === 0) {
203+
if (!listIds.length) {
206204
$noSelect.removeClass('gt-hidden');
207205
} else {
208206
$noSelect.addClass('gt-hidden');
@@ -329,7 +327,7 @@ async function onEditContent(event) {
329327
let comboMarkdownEditor;
330328

331329
const setupDropzone = async ($dropzone) => {
332-
if ($dropzone.length === 0) return null;
330+
if (!$dropzone.length) return null;
333331

334332
let disableRemovedfileEvent = false; // when resetting the dropzone (removeAllFiles), disable the "removedfile" event
335333
let fileUuidDict = {}; // to record: if a comment has been saved, then the uploaded files won't be deleted from server when clicking the Remove in the dropzone
@@ -482,9 +480,7 @@ async function onEditContent(event) {
482480
}
483481

484482
export function initRepository() {
485-
if ($('.page-content.repository').length === 0) {
486-
return;
487-
}
483+
if (!$('.page-content.repository').length) return;
488484

489485
initRepoBranchTagSelector('.js-branch-tag-selector');
490486

web_src/js/features/repo-settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function initRepoSettingSearchTeamBox() {
7171
}
7272

7373
export function initRepoSettingGitHook() {
74-
if ($('.edit.githook').length === 0) return;
74+
if (!$('.edit.githook').length) return;
7575
const filename = document.querySelector('.hook-filename').textContent;
7676
const _promise = createMonaco($('#content')[0], filename, {language: 'shell'});
7777
}

web_src/js/features/user-settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {hideElem, showElem} from '../utils/dom.js';
22

33
export function initUserSettings() {
4-
if (document.querySelectorAll('.user.settings.profile').length === 0) return;
4+
if (!document.querySelectorAll('.user.settings.profile').length) return;
55

66
const usernameInput = document.getElementById('username');
77
if (!usernameInput) return;

0 commit comments

Comments
 (0)