« get me outta code hell

content: generateAlbumSocialEmbedDescription: code cleanup - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-09-11 17:11:41 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-09-11 17:11:51 -0300
commitf198626619b9dba45156632d1d81eced1192bd38 (patch)
tree2726147f3ef92a203a863fdd46ff26b15e30988a
parentb0444ca365a7f1165a3aef95a85ad1f380ffc591 (diff)
content: generateAlbumSocialEmbedDescription: code cleanup
Also fixes #555 by scrapping hasAny altogether.
-rw-r--r--src/content/dependencies/generateAlbumSocialEmbedDescription.js77
1 files 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);
+    }),
 };