Skip to content

Commit

Permalink
refactor: deduplicate repeated code into seperate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverth committed Sep 29, 2024
1 parent e0a72df commit 521c5a3
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/renderer/helpers/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export function getSortedPlaylistItems(playlistItems, sortOrder, locale, reverse
})
}

function getDurationForSort(a) {
return (isNaN(a.lengthSeconds) || a.lengthSeconds === 0) ? 0 : a.lengthSeconds
}

function compareTwoPlaylistItems(a, b, sortOrder, collator) {
switch (sortOrder) {
case SORT_BY_VALUES.DateAddedNewest:
Expand All @@ -50,14 +54,10 @@ function compareTwoPlaylistItems(a, b, sortOrder, collator) {
case SORT_BY_VALUES.AuthorDescending:
return collator.compare(b.author, a.author)
case SORT_BY_VALUES.VideoDurationAscending: {
const aLengthForSort = (isNaN(a.lengthSeconds) || a.lengthSeconds === 0) ? 0 : a.lengthSeconds
const bLengthForSort = (isNaN(b.lengthSeconds) || b.lengthSeconds === 0) ? 0 : b.lengthSeconds
return aLengthForSort - bLengthForSort
return getDurationForSort(a) - getDurationForSort(b)
}
case SORT_BY_VALUES.VideoDurationDescending: {
const aLengthForSort = (isNaN(a.lengthSeconds) || a.lengthSeconds === 0) ? 0 : a.lengthSeconds
const bLengthForSort = (isNaN(b.lengthSeconds) || b.lengthSeconds === 0) ? 0 : b.lengthSeconds
return bLengthForSort - aLengthForSort
return getDurationForSort(b) - getDurationForSort(a)
}
default:
console.error(`Unknown sortOrder: ${sortOrder}`)
Expand Down

0 comments on commit 521c5a3

Please sign in to comment.