Skip to content

Commit

Permalink
home: Cleanup a long if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
ThrRip committed Dec 17, 2024
1 parent 4b19451 commit d4a731c
Showing 1 changed file with 76 additions and 77 deletions.
153 changes: 76 additions & 77 deletions packages/home/app/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -359,92 +359,91 @@ const viewPlaylistData = useState<Playlist>('viewPlaylistData')
let viewPlaylistDataShuffled: Playlist = []
function viewPlaylistDataUpdate (tasks: Array<'shuffle' | 'sort'>) {
// @ts-expect-error
if (backendPlaylist.value.length !== 0) {
function shuffle () {
// @ts-expect-error
const playlist = backendPlaylist.value.slice()
let currentSong = playlist.length
while (currentSong !== 0) {
const targetSong = Math.floor(Math.random() * currentSong)
currentSong--
const targetSongBackup = { ...playlist[targetSong] }
playlist[targetSong] = playlist[currentSong]
playlist[currentSong] = targetSongBackup
}
viewPlaylistDataShuffled = playlist
return playlist
if (backendPlaylist.value.length === 0) return
function shuffle () {
// @ts-expect-error
const playlist = backendPlaylist.value.slice()
let currentSong = playlist.length
while (currentSong !== 0) {
const targetSong = Math.floor(Math.random() * currentSong)
currentSong--
const targetSongBackup = { ...playlist[targetSong] }
playlist[targetSong] = playlist[currentSong]
playlist[currentSong] = targetSongBackup
}
viewPlaylistDataShuffled = playlist
return playlist
}

async function sort () {
// @ts-expect-error
const playlist: Array<SortingSong> = backendPlaylist.value.slice()
const orderModifier = viewPlaylistSortingOrder.value === 'ascending' ? 1 : -1
async function sort () {
// @ts-expect-error
const playlist: Array<SortingSong> = backendPlaylist.value.slice()
const orderModifier = viewPlaylistSortingOrder.value === 'ascending' ? 1 : -1

// Sort by song name or artist in alphabetical order
if (viewPlaylistSortingColumn.value === 'name' || viewPlaylistSortingColumn.value === 'artist') {
const pinyinPro = await import('pinyin-pro')
playlist.forEach((song, index) => {
// @ts-expect-error
playlist[index][`${viewPlaylistSortingColumn.value}Pinyin`] =
// @ts-expect-error
pinyinPro.pinyin(song[viewPlaylistSortingColumn.value], { toneType: 'none', nonZh: 'consecutive' })
.replaceAll(' ', '')
})
playlist.sort((a, b) => {
// Sort by song name or artist in alphabetical order
if (viewPlaylistSortingColumn.value === 'name' || viewPlaylistSortingColumn.value === 'artist') {
const pinyinPro = await import('pinyin-pro')
playlist.forEach((song, index) => {
// @ts-expect-error
playlist[index][`${viewPlaylistSortingColumn.value}Pinyin`] =
// @ts-expect-error
if (a[`${viewPlaylistSortingColumn.value}Pinyin`] < b[`${viewPlaylistSortingColumn.value}Pinyin`]) {
return -1 * orderModifier
}
else
// @ts-expect-error
if (a[`${viewPlaylistSortingColumn.value}Pinyin`] > b[`${viewPlaylistSortingColumn.value}Pinyin`]) {
return 1 * orderModifier
}
else {
return 0
}
})
}
else

// Sort by payment requirement
if (viewPlaylistSortingColumn.value === 'payment_amount') {
playlist.sort((a, b) => {
if (a.payment_required === false || b.payment_required === false) {
return (Number(a.payment_required ?? 0) - Number(b.payment_required ?? 0)) * orderModifier
}
return ((a.payment_amount ?? 0) - (b.payment_amount ?? 0)) * orderModifier
})
}
else
pinyinPro.pinyin(song[viewPlaylistSortingColumn.value], { toneType: 'none', nonZh: 'consecutive' })
.replaceAll(' ', '')
})
playlist.sort((a, b) => {
// @ts-expect-error
if (a[`${viewPlaylistSortingColumn.value}Pinyin`] < b[`${viewPlaylistSortingColumn.value}Pinyin`]) {
return -1 * orderModifier
}
else
// @ts-expect-error
if (a[`${viewPlaylistSortingColumn.value}Pinyin`] > b[`${viewPlaylistSortingColumn.value}Pinyin`]) {
return 1 * orderModifier
}
else {
return 0
}
})
}
else

// Sort by language
if (viewPlaylistSortingColumn.value === 'language') {
const languageOrder = ['国语', '粤语', '日语']
playlist.sort((a, b) => {
return (languageOrder.findIndex(language => a.language === language) -
languageOrder.findIndex(language => b.language === language)) * orderModifier
})
}
// Sort by payment requirement
if (viewPlaylistSortingColumn.value === 'payment_amount') {
playlist.sort((a, b) => {
if (a.payment_required === false || b.payment_required === false) {
return (Number(a.payment_required ?? 0) - Number(b.payment_required ?? 0)) * orderModifier
}
return ((a.payment_amount ?? 0) - (b.payment_amount ?? 0)) * orderModifier
})
}
else

return playlist
// Sort by language
if (viewPlaylistSortingColumn.value === 'language') {
const languageOrder = ['国语', '粤语', '日语']
playlist.sort((a, b) => {
return (languageOrder.findIndex(language => a.language === language) -
languageOrder.findIndex(language => b.language === language)) * orderModifier
})
}

tasks.forEach(async (task, index) => {
if (task === 'shuffle') {
shuffle()
if (index !== tasks.length - 1) { return }
viewPlaylistSortingColumn.value = null
viewPlaylistDataUpdate(['sort'])
viewPlaylistData.value = viewPlaylistDataShuffled
}
else
if (task === 'sort') {
if (index !== tasks.length - 1) { return }
viewPlaylistData.value = viewPlaylistSortingColumn.value === null ? viewPlaylistDataShuffled : await sort()
}
})
return playlist
}

tasks.forEach(async (task, index) => {
if (task === 'shuffle') {
shuffle()
if (index !== tasks.length - 1) { return }
viewPlaylistSortingColumn.value = null
viewPlaylistDataUpdate(['sort'])
viewPlaylistData.value = viewPlaylistDataShuffled
}
else
if (task === 'sort') {
if (index !== tasks.length - 1) { return }
viewPlaylistData.value = viewPlaylistSortingColumn.value === null ? viewPlaylistDataShuffled : await sort()
}
})
}
callOnce(() => viewPlaylistDataUpdate(['shuffle']))
if (import.meta.client && useNuxtApp().isHydrating && useNuxtApp().payload.serverRendered) {
Expand Down

0 comments on commit d4a731c

Please sign in to comment.