From f198626619b9dba45156632d1d81eced1192bd38 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 11 Sep 2024 17:11:41 -0300 Subject: content: generateAlbumSocialEmbedDescription: code cleanup Also fixes #555 by scrapping hasAny altogether. --- .../generateAlbumSocialEmbedDescription.js | 77 ++++++++++------------ 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/src/content/dependencies/generateAlbumSocialEmbedDescription.js b/src/content/dependencies/generateAlbumSocialEmbedDescription.js index 7099616a..69c39c3a 100644 --- a/src/content/dependencies/generateAlbumSocialEmbedDescription.js +++ b/src/content/dependencies/generateAlbumSocialEmbedDescription.js @@ -3,46 +3,39 @@ import {accumulateSum} from '#sugar'; export default { extraDependencies: ['language'], - data(album) { - const data = {}; - - const duration = accumulateSum(album.tracks, track => track.duration); - - data.hasDuration = duration > 0; - data.hasTracks = album.tracks.length > 0; - data.hasDate = !!album.date; - data.hasAny = (data.hasDuration || data.hasTracks || data.hasDuration); - - if (!data.hasAny) - return data; - - if (data.hasDuration) - data.duration = duration; - - if (data.hasTracks) - data.tracks = album.tracks.length; - - if (data.hasDate) - data.date = album.date; - - return data; - }, - - generate(data, {language}) { - return language.formatString( - 'albumPage.socialEmbed.body' + [ - data.hasDuration && '.withDuration', - data.hasTracks && '.withTracks', - data.hasDate && '.withReleaseDate', - ].filter(Boolean).join(''), - - Object.fromEntries([ - data.hasDuration && - ['duration', language.formatDuration(data.duration)], - data.hasTracks && - ['tracks', language.countTracks(data.tracks, {unit: true})], - data.hasDate && - ['date', language.formatDate(data.date)], - ].filter(Boolean))); - }, + data: (album) => ({ + duration: + accumulateSum(album.tracks, track => track.duration), + + tracks: + album.tracks.length, + + date: + album.date, + }), + + generate: (data, {language}) => + language.encapsulate('albumPage.socialEmbed.body', workingCapsule => { + const workingOptions = {}; + + if (data.duration > 0) { + workingCapsule += '.withDuration'; + workingOptions.duration = + language.formatDuration(data.duration); + } + + if (data.tracks > 0) { + workingCapsule += '.withTracks'; + workingOptions.tracks = + language.countTracks(data.tracks, {unit: true}); + } + + if (data.date) { + workingCapsule += '.withReleaseDate'; + workingOptions.date = + language.formatDate(data.date); + } + + return language.$(workingCapsule, workingOptions); + }), }; -- cgit 1.3.0-6-gf8a5