Skip to content

Commit 8fe26fb

Browse files
authored
Refactor all .length === 0 patterns in JS (#30045)
This pattern comes of often during review, so let's fix it once and for all. Did not test, but changes are trivial enough imho.
1 parent f73d891 commit 8fe26fb

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
@@ -153,11 +153,11 @@ export function initRepoTopicBar() {
153153

154154
$.fn.form.settings.rules.validateTopic = function (_values, regExp) {
155155
const $topics = $topicDropdown.children('a.ui.label');
156-
const status = $topics.length === 0 || $topics.last()[0].getAttribute('data-value').match(regExp);
156+
const status = !$topics.length || $topics.last()[0].getAttribute('data-value').match(regExp);
157157
if (!status) {
158158
$topics.last().removeClass('green').addClass('red');
159159
}
160-
return status && $topicDropdown.children('a.ui.label.red').length === 0;
160+
return status && !$topicDropdown.children('a.ui.label.red').length;
161161
};
162162

163163
$topicForm.form({

0 commit comments

Comments
 (0)