loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

(cherry picked from commit 8fe26fb314f1710139728d9118b455fc6a16cce2)

authored by

silverwind and committed by
Earl Warren
c55e30ff c5745f9d

+31 -40
+2 -2
web_src/js/components/DiffCommitSelector.vue
··· 103 103 this.menuVisible = !this.menuVisible; 104 104 // load our commits when the menu is not yet visible (it'll be toggled after loading) 105 105 // and we got no commits 106 - if (this.commits.length === 0 && this.menuVisible && !this.isLoading) { 106 + if (!this.commits.length && this.menuVisible && !this.isLoading) { 107 107 this.isLoading = true; 108 108 try { 109 109 await this.fetchCommits(); ··· 216 216 <div 217 217 v-if="lastReviewCommitSha != null" role="menuitem" 218 218 class="vertical item" 219 - :class="{disabled: commitsSinceLastReview === 0}" 219 + :class="{disabled: !commitsSinceLastReview}" 220 220 @keydown.enter="changesSinceLastReviewClick()" 221 221 @click="changesSinceLastReviewClick()" 222 222 >
+1 -1
web_src/js/components/RepoActionView.vue
··· 462 462 {{ locale.showFullScreen }} 463 463 </a> 464 464 <div class="divider"/> 465 - <a :class="['item', currentJob.steps.length === 0 ? 'disabled' : '']" :href="run.link+'/jobs/'+jobIndex+'/logs'" target="_blank"> 465 + <a :class="['item', !currentJob.steps.length ? 'disabled' : '']" :href="run.link+'/jobs/'+jobIndex+'/logs'" target="_blank"> 466 466 <i class="icon"><SvgIcon name="octicon-download"/></i> 467 467 {{ locale.downloadLogs }} 468 468 </a>
+5 -3
web_src/js/components/RepoBranchTagSelector.vue
··· 19 19 }); 20 20 21 21 // 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; 23 23 return items; 24 24 }, 25 25 showNoResults() { 26 - return this.filteredItems.length === 0 && !this.showCreateNewBranch; 26 + return !this.filteredItems.length && !this.showCreateNewBranch; 27 27 }, 28 28 showCreateNewBranch() { 29 29 if (this.disableCreateBranch || !this.searchTerm) { 30 30 return false; 31 31 } 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; 33 35 }, 34 36 formActionUrl() { 35 37 return `${this.repoLink}/branches/_new/${this.branchNameSubURL}`;
+1 -3
web_src/js/features/admin/common.js
··· 6 6 const {appSubUrl} = window.config; 7 7 8 8 export function initAdminCommon() { 9 - if ($('.page-content.admin').length === 0) { 10 - return; 11 - } 9 + if (!$('.page-content.admin').length) return; 12 10 13 11 // check whether appUrl(ROOT_URL) is correct, if not, show an error message 14 12 checkAppUrl();
+1 -1
web_src/js/features/common-global.js
··· 19 19 export function initGlobalFormDirtyLeaveConfirm() { 20 20 // Warn users that try to leave a page after entering data into a form. 21 21 // Except on sign-in pages, and for forms marked as 'ignore-dirty'. 22 - if ($('.user.signin').length === 0) { 22 + if (!$('.user.signin').length) { 23 23 $('form:not(.ignore-dirty)').areYouSure(); 24 24 } 25 25 }
+1 -1
web_src/js/features/comp/SearchUserBox.js
··· 34 34 } 35 35 }); 36 36 37 - if (allowEmailInput && items.length === 0 && looksLikeEmailAddressCheck.test(searchQuery)) { 37 + if (allowEmailInput && !items.length && looksLikeEmailAddressCheck.test(searchQuery)) { 38 38 const resultItem = { 39 39 title: searchQuery, 40 40 description: allowEmailDescription,
+1 -2
web_src/js/features/repo-diff.js
··· 214 214 215 215 export function initRepoDiffView() { 216 216 initRepoDiffConversationForm(); 217 - const $diffFileList = $('#diff-file-list'); 218 - if ($diffFileList.length === 0) return; 217 + if (!$('#diff-file-list').length) return; 219 218 initDiffFileTree(); 220 219 initDiffCommitSelect(); 221 220 initRepoDiffShowMore();
+4 -6
web_src/js/features/repo-editor.js
··· 39 39 } 40 40 41 41 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); 47 45 } 48 46 49 47 function getCursorPosition($e) { ··· 165 163 166 164 commitButton?.addEventListener('click', (e) => { 167 165 // A modal which asks if an empty file should be committed 168 - if ($editArea.val().length === 0) { 166 + if (!$editArea.val()) { 169 167 $('#edit-empty-content-modal').modal({ 170 168 onApprove() { 171 169 $('.edit.form').trigger('submit');
+1 -1
web_src/js/features/repo-findfile.js
··· 77 77 78 78 const filterResult = filterRepoFilesWeighted(files, filter); 79 79 80 - toggleElem(repoFindFileNoResult, filterResult.length === 0); 80 + toggleElem(repoFindFileNoResult, !filterResult.length); 81 81 for (const r of filterResult) { 82 82 const row = document.createElement('tr'); 83 83 const cell = document.createElement('td');
+2 -2
web_src/js/features/repo-home.js
··· 153 153 154 154 $.fn.form.settings.rules.validateTopic = function (_values, regExp) { 155 155 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); 157 157 if (!status) { 158 158 $topics.last().removeClass('green').addClass('red'); 159 159 } 160 - return status && $topicDropdown.children('a.ui.label.red').length === 0; 160 + return status && !$topicDropdown.children('a.ui.label.red').length; 161 161 }; 162 162 163 163 $topicForm.form({
+6 -8
web_src/js/features/repo-issue.js
··· 362 362 } 363 363 364 364 export function initRepoIssueComments() { 365 - if ($('.repository.view.issue .timeline').length === 0) return; 365 + if (!$('.repository.view.issue .timeline').length) return; 366 366 367 367 $('.re-request-review').on('click', async function (e) { 368 368 e.preventDefault(); ··· 377 377 378 378 $(document).on('click', (event) => { 379 379 const $urlTarget = $(':target'); 380 - if ($urlTarget.length === 0) return; 380 + if (!$urlTarget.length) return; 381 381 382 382 const urlTargetId = $urlTarget.attr('id'); 383 383 if (!urlTargetId) return; ··· 385 385 386 386 const $target = $(event.target); 387 387 388 - if ($target.closest(`#${urlTargetId}`).length === 0) { 388 + if (!$target.closest(`#${urlTargetId}`).length) { 389 389 const scrollPosition = $(window).scrollTop(); 390 390 window.location.hash = ''; 391 391 $(window).scrollTop(scrollPosition); ··· 478 478 } 479 479 480 480 // 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; 484 482 485 483 const $reviewBtn = $('.js-btn-review'); 486 484 const $panel = $reviewBtn.parent().find('.review-box-panel'); ··· 529 527 530 528 const $td = $ntr.find(`.add-comment-${side}`); 531 529 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) { 533 531 try { 534 532 const response = await GET($(this).closest('[data-new-comment-url]').attr('data-new-comment-url')); 535 533 const html = await response.text(); ··· 626 624 }; 627 625 628 626 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()) { 630 628 $editInput.val($issueTitle.text()); 631 629 await pullrequest_targetbranch_change(pullrequest_target_update_url); 632 630 } else {
+4 -8
web_src/js/features/repo-legacy.js
··· 50 50 51 51 export function initRepoCommentForm() { 52 52 const $commentForm = $('.comment.form'); 53 - if ($commentForm.length === 0) { 54 - return; 55 - } 53 + if (!$commentForm.length) return; 56 54 57 55 if ($commentForm.find('.field.combo-editor-dropzone').length) { 58 56 // at the moment, if a form has multiple combo-markdown-editors, it must be an issue template form ··· 202 200 $($(this).data('id-selector')).addClass('tw-hidden'); 203 201 } 204 202 }); 205 - if (listIds.length === 0) { 203 + if (!listIds.length) { 206 204 $noSelect.removeClass('tw-hidden'); 207 205 } else { 208 206 $noSelect.addClass('tw-hidden'); ··· 329 327 let comboMarkdownEditor; 330 328 331 329 const setupDropzone = async ($dropzone) => { 332 - if ($dropzone.length === 0) return null; 330 + if (!$dropzone.length) return null; 333 331 334 332 let disableRemovedfileEvent = false; // when resetting the dropzone (removeAllFiles), disable the "removedfile" event 335 333 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 ··· 485 483 } 486 484 487 485 export function initRepository() { 488 - if ($('.page-content.repository').length === 0) { 489 - return; 490 - } 486 + if (!$('.page-content.repository').length) return; 491 487 492 488 initRepoBranchTagSelector('.js-branch-tag-selector'); 493 489
+1 -1
web_src/js/features/repo-settings.js
··· 71 71 } 72 72 73 73 export function initRepoSettingGitHook() { 74 - if ($('.edit.githook').length === 0) return; 74 + if (!$('.edit.githook').length) return; 75 75 const filename = document.querySelector('.hook-filename').textContent; 76 76 const _promise = createMonaco($('#content')[0], filename, {language: 'shell'}); 77 77 }
+1 -1
web_src/js/features/user-settings.js
··· 1 1 import {hideElem, showElem} from '../utils/dom.js'; 2 2 3 3 export function initUserSettings() { 4 - if (document.querySelectorAll('.user.settings.profile').length === 0) return; 4 + if (!document.querySelectorAll('.user.settings.profile').length) return; 5 5 6 6 const usernameInput = document.getElementById('username'); 7 7 if (!usernameInput) return;