« get me outta code hell

new "empty" sugar.js util - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/page
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-11-21 21:48:31 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-11-21 21:48:31 -0400
commitba2751eae00c4bcf10403edbd549ca70dc316cab (patch)
tree492537e39b268dd1991bc85ffb2e4c666ff03536 /src/page
parent860b9fbf12315362d2c33864521c3f081fd66545 (diff)
new "empty" sugar.js util
Diffstat (limited to 'src/page')
-rw-r--r--src/page/album.js94
-rw-r--r--src/page/artist.js36
-rw-r--r--src/page/flash.js7
-rw-r--r--src/page/group.js11
-rw-r--r--src/page/homepage.js11
-rw-r--r--src/page/listing.js4
-rw-r--r--src/page/track.js44
7 files changed, 112 insertions, 95 deletions
diff --git a/src/page/album.js b/src/page/album.js
index 1313bb3b..14d4a9d2 100644
--- a/src/page/album.js
+++ b/src/page/album.js
@@ -2,7 +2,11 @@
 
 // Album page specification.
 
-import {bindOpts, compareArrays} from '../util/sugar.js';
+import {
+  bindOpts,
+  compareArrays,
+  empty,
+} from '../util/sugar.js';
 
 import {
   getAlbumCover,
@@ -45,8 +49,8 @@ export function write(album, {wikiData}) {
   };
 
   const hasCommentaryEntries =
-    [album, ...album.tracks].filter((x) => x.commentary).length > 0;
-  const hasAdditionalFiles = album.additionalFiles?.length > 0;
+    !empty([album, ...album.tracks].filter((x) => x.commentary));
+  const hasAdditionalFiles = !empty(album.additionalFiles);
   const albumDuration = getTotalDuration(album.tracks);
 
   const displayTrackGroups =
@@ -132,7 +136,7 @@ export function write(album, {wikiData}) {
           `--album-directory: ${album.directory}`,
         ]),
 
-        banner: album.bannerArtistContribs.length && {
+        banner: !empty(album.bannerArtistContribs) && {
           dimensions: album.bannerDimensions,
           path: [
             'media.albumBanner',
@@ -161,7 +165,7 @@ export function write(album, {wikiData}) {
                 [html.joinChildren]: '<br>',
               },
               [
-                album.artistContribs.length &&
+                !empty(album.artistContribs) &&
                   language.$('releaseInfo.by', {
                     artists: getArtistString(album.artistContribs, {
                       showContrib: true,
@@ -169,7 +173,7 @@ export function write(album, {wikiData}) {
                     }),
                   }),
 
-                album.coverArtistContribs.length &&
+                !empty(album.coverArtistContribs) &&
                   language.$('releaseInfo.coverArtBy', {
                     artists: getArtistString(album.coverArtistContribs, {
                       showContrib: true,
@@ -177,7 +181,7 @@ export function write(album, {wikiData}) {
                     }),
                   }),
 
-                album.wallpaperArtistContribs.length &&
+                !empty(album.wallpaperArtistContribs) &&
                   language.$('releaseInfo.wallpaperArtBy', {
                     artists: getArtistString(album.wallpaperArtistContribs, {
                       showContrib: true,
@@ -185,7 +189,7 @@ export function write(album, {wikiData}) {
                     }),
                   }),
 
-                album.bannerArtistContribs.length &&
+                !empty(album.bannerArtistContribs) &&
                   language.$('releaseInfo.bannerArtBy', {
                     artists: getArtistString(album.bannerArtistContribs, {
                       showContrib: true,
@@ -204,7 +208,7 @@ export function write(album, {wikiData}) {
                     date: language.formatDate(album.coverArtDate),
                   }),
 
-                album.duration &&
+                album.duration > 0 &&
                   language.$('releaseInfo.duration', {
                     duration: language.formatDuration(albumDuration, {
                       approximate: album.tracks.length > 1,
@@ -229,7 +233,7 @@ export function write(album, {wikiData}) {
                   }),
               ]),
 
-            album.urls?.length &&
+            !empty(album.urls) &&
               html.tag('p',
                 language.$('releaseInfo.listenOn', {
                   links: language.formatDisjunctionList(
@@ -434,7 +438,7 @@ export function generateAlbumSidebar(album, currentTrack, {
       isAlbumPage &&
         transformMultiline(group.descriptionShort),
 
-      group.urls?.length &&
+      !empty(group.urls) &&
         html.tag('p', language.$('releaseInfo.visitOn', {
           links: language.formatDisjunctionList(
             group.urls.map((url) => fancifyURL(url))
@@ -459,24 +463,16 @@ export function generateAlbumSidebar(album, currentTrack, {
         ]),
     ]);
 
-  if (groupParts.length) {
-    if (isTrackPage) {
-      const combinedGroupPart =
-        groupParts
-          .map(groupPart => groupPart.filter(Boolean).join('\n'))
-          .join('\n<hr>\n');
-      return {
-        multiple: [trackListPart, combinedGroupPart],
-      };
-    } else {
-      return {
-        multiple: [...groupParts, trackListPart],
-      };
-    }
+  if (empty(groupParts)) {
+    return {content: trackListPart};
+  } else if (isTrackPage) {
+    const combinedGroupPart =
+      groupParts
+        .map(groupPart => groupPart.filter(Boolean).join('\n'))
+        .join('\n<hr>\n');
+    return {multiple: [trackListPart, combinedGroupPart]};
   } else {
-    return {
-      content: trackListPart,
-    };
+    return {multiple: [...groupParts, trackListPart]};
   }
 }
 
@@ -490,7 +486,7 @@ export function generateAlbumSecondaryNav(album, currentTrack, {
 
   const {groups} = album;
 
-  if (!groups.length) {
+  if (empty(groups)) {
     return null;
   }
 
@@ -503,27 +499,29 @@ export function generateAlbumSecondaryNav(album, currentTrack, {
       return {group, next, previous};
     })
     .map(({group, next, previous}) => {
-      const previousNext =
+      const previousLink =
         isAlbumPage &&
-          [
-            previous &&
-              link.album(previous, {
-                color: false,
-                text: language.$('misc.nav.previous'),
-              }),
-            next &&
-              link.album(next, {
-                color: false,
-                text: language.$('misc.nav.next'),
-              }),
-          ].filter(Boolean);
+        previous &&
+          link.album(previous, {
+            color: false,
+            text: language.$('misc.nav.previous'),
+          });
+      const nextLink =
+        isAlbumPage &&
+        next &&
+          link.album(next, {
+            color: false,
+            text: language.$('misc.nav.next'),
+          });
+      const links = [previousLink, nextLink].filter(Boolean);
       return html.tag('span',
-        {style: getLinkThemeString(group.color)}, [
-        language.$('albumSidebar.groupBox.title', {
-          group: link.groupInfo(group),
-        }),
-        previousNext?.length && `(${previousNext.join(',\n')})`,
-      ]);
+        {style: getLinkThemeString(group.color)},
+        [
+          language.$('albumSidebar.groupBox.title', {
+            group: link.groupInfo(group),
+          }),
+          !empty(links) && `(${language.formatUnitList(links)})`,
+        ]);
     });
 
   return {
diff --git a/src/page/artist.js b/src/page/artist.js
index 8606a7ae..efd4e1f3 100644
--- a/src/page/artist.js
+++ b/src/page/artist.js
@@ -4,7 +4,11 @@
 //
 // NB: See artist-alias.js for artist alias redirect pages.
 
-import {bindOpts, unique} from '../util/sugar.js';
+import {
+  bindOpts,
+  empty,
+  unique,
+} from '../util/sugar.js';
 
 import {
   chunkByProperties,
@@ -43,7 +47,7 @@ export function write(artist, {wikiData}) {
     ...(artist.tracksAsCommentator ?? []),
   ]);
 
-  const hasGallery = artThingsGallery.length > 0;
+  const hasGallery = !empty(artThingsGallery);
 
   const getArtistsAndContrib = (thing, key) => ({
     artists: thing[key]?.filter(({who}) => who !== artist),
@@ -155,7 +159,7 @@ export function write(artist, {wikiData}) {
   }) =>
     original
       ? language.$('artistPage.creditList.entry.rerelease', {entry})
-      : artists.length
+      : !empty(artists)
       ? contrib.what || contrib.whatArray?.length
         ? language.$(
             'artistPage.creditList.entry.withArtists.withContribution',
@@ -250,7 +254,7 @@ export function write(artist, {wikiData}) {
       const ret = {};
       ret.link = serializeLink(thing);
       if (contrib.what) ret.contribution = contrib.what;
-      if (artists.length) ret.otherArtists = serializeContribs(artists);
+      if (!empty(artists)) ret.otherArtists = serializeContribs(artists);
       return ret;
     };
 
@@ -266,10 +270,10 @@ export function write(artist, {wikiData}) {
     }));
 
   const jumpTo = {
-    tracks: allTracks.length > 0,
-    art: artThingsAll.length > 0,
-    flashes: wikiInfo.enableFlashesAndGames && flashes.length > 0,
-    commentary: commentaryThings.length > 0,
+    tracks: !empty(allTracks),
+    art: !empty(artThingsAll),
+    flashes: wikiInfo.enableFlashesAndGames && !empty(flashes),
+    commentary: !empty(commentaryThings),
   };
 
   const showJumpTo = Object.values(jumpTo).includes(true);
@@ -378,7 +382,7 @@ export function write(artist, {wikiData}) {
                 html.tag('hr'),
               ]),
 
-            urls?.length &&
+            !empty(urls) &&
               html.tag('p',
                 language.$('releaseInfo.visitOn', {
                   links: language.formatDisjunctionList(
@@ -422,12 +426,12 @@ export function write(artist, {wikiData}) {
                 })),
 
             ...html.fragment(
-              allTracks.length && [
+              !empty(allTracks) && [
                 html.tag('h2',
                   {id: 'tracks'},
                   language.$('artistPage.trackList.title')),
 
-                totalDuration &&
+                totalDuration > 0 &&
                   html.tag('p',
                     language.$('artistPage.contributedDurationLine', {
                       artist: artist.name,
@@ -440,7 +444,7 @@ export function write(artist, {wikiData}) {
                       ),
                     })),
 
-                musicGroups.length &&
+                !empty(musicGroups) &&
                   html.tag('p',
                     language.$('artistPage.musicGroupsLine', {
                       groups: language.formatUnitList(
@@ -460,7 +464,7 @@ export function write(artist, {wikiData}) {
               ]),
 
             ...html.fragment(
-              artThingsAll.length && [
+              !empty(artThingsAll) && [
                 html.tag('h2',
                   {id: 'art'},
                   language.$('artistPage.artList.title')),
@@ -473,7 +477,7 @@ export function write(artist, {wikiData}) {
                       })
                     })),
 
-                artGroups.length &&
+                !empty(artGroups) &&
                   html.tag('p',
                     language.$('artistPage.artGroupsLine', {
                     groups: language.formatUnitList(
@@ -527,7 +531,7 @@ export function write(artist, {wikiData}) {
 
             ...html.fragment(
               wikiInfo.enableFlashesAndGames &&
-              flashes.length && [
+              !empty(flashes) && [
                 html.tag('h2',
                   {id: 'flashes'},
                   language.$('artistPage.flashList.title')),
@@ -569,7 +573,7 @@ export function write(artist, {wikiData}) {
               ]),
 
             ...html.fragment(
-              commentaryThings.length && [
+              !empty(commentaryThings) && [
                 html.tag('h2',
                   {id: 'commentary'},
                   language.$('artistPage.commentaryList.title')),
diff --git a/src/page/flash.js b/src/page/flash.js
index ad619973..237dd47f 100644
--- a/src/page/flash.js
+++ b/src/page/flash.js
@@ -2,6 +2,7 @@
 
 // Flash page and index specifications.
 
+import {empty} from '../util/sugar.js';
 import {getFlashLink} from '../util/wiki-data.js';
 
 export function condition({wikiData}) {
@@ -50,7 +51,7 @@ export function write(flash, {wikiData}) {
               date: language.formatDate(flash.date),
             })),
 
-          (flash.page || flash.urls?.length) &&
+          (flash.page || !empty(flash.urls)) &&
             html.tag('p',
               language.$('releaseInfo.playOn', {
                 links: language.formatDisjunctionList(
@@ -62,7 +63,7 @@ export function write(flash, {wikiData}) {
               })),
 
           ...html.fragment(
-            flash.featuredTracks?.length && [
+            !empty(flash.featuredTracks) && [
               html.tag('p',
                 `Tracks featured in <i>${
                   flash.name.replace(/\.$/, '')
@@ -81,7 +82,7 @@ export function write(flash, {wikiData}) {
             ]),
 
           ...html.fragment(
-            flash.contributorContribs.length && [
+            !empty(flash.contributorContribs) && [
               html.tag('p',
                 language.$('releaseInfo.contributors')),
 
diff --git a/src/page/group.js b/src/page/group.js
index 0ff04ae5..2bd6da96 100644
--- a/src/page/group.js
+++ b/src/page/group.js
@@ -2,7 +2,14 @@
 
 // Group page specifications.
 
-import {getTotalDuration, sortChronologically} from '../util/wiki-data.js';
+import {
+  empty,
+} from '../util/sugar.js';
+
+import {
+  getTotalDuration,
+  sortChronologically,
+} from '../util/wiki-data.js';
 
 export function targets({wikiData}) {
   return wikiData.groupData;
@@ -44,7 +51,7 @@ export function write(group, {wikiData}) {
               group: group.name
             })),
 
-          group.urls?.length &&
+          !empty(group.urls) &&
             html.tag('p',
               language.$('releaseInfo.visitOn', {
                 links: language.formatDisjunctionList(
diff --git a/src/page/homepage.js b/src/page/homepage.js
index 2219593e..c15c969a 100644
--- a/src/page/homepage.js
+++ b/src/page/homepage.js
@@ -1,6 +1,11 @@
 // Homepage specification.
 
-import {getNewAdditions, getNewReleases} from '../util/wiki-data.js';
+import {empty} from '../util/sugar.js';
+
+import {
+  getNewAdditions,
+  getNewReleases,
+} from '../util/wiki-data.js';
 
 export function writeTargetless({wikiData}) {
   const {newsData, staticPageData, homepageLayout, wikiInfo} = wikiData;
@@ -29,7 +34,7 @@ export function writeTargetless({wikiData}) {
               : [];
         }
 
-        if (row.sourceAlbums.length) {
+        if (!empty(row.sourceAlbums)) {
           entry.gridEntries.push(...row.sourceAlbums.map(album => ({item: album})));
         }
 
@@ -86,7 +91,7 @@ export function writeTargetless({wikiData}) {
                           lazy: i > 0,
                         })),
 
-                      entry.actionLinks.length &&
+                      !empty(entry.actionLinks) &&
                         html.tag('div', {class: 'grid-actions'},
                           entry.actionLinks.map(action =>
                             transformInline(action)
diff --git a/src/page/listing.js b/src/page/listing.js
index 5a2b6d2a..65982f83 100644
--- a/src/page/listing.js
+++ b/src/page/listing.js
@@ -10,6 +10,8 @@
 // Individual listing specs are described in src/listing-spec.js, but are
 // provided via wikiData like other (normal) data objects.
 
+import {empty} from '../util/sugar.js';
+
 import {getTotalDuration} from '../util/wiki-data.js';
 
 export function condition({wikiData}) {
@@ -201,7 +203,7 @@ function generateLinkIndexForListings(currentListing, forSidebar, {
       ...rest,
       listings: listings.filter(({condition: c}) => !c || c({wikiData})),
     }))
-    .filter(({listings}) => listings.length > 0);
+    .filter(({listings}) => !empty(listings));
 
   const genUL = (listings) =>
     html.tag('ul',
diff --git a/src/page/track.js b/src/page/track.js
index bdbe60cf..cf937248 100644
--- a/src/page/track.js
+++ b/src/page/track.js
@@ -9,7 +9,10 @@ import {
   generateAlbumSidebar,
 } from './album.js';
 
-import {bindOpts} from '../util/sugar.js';
+import {
+  bindOpts,
+  empty,
+} from '../util/sugar.js';
 
 import {
   getTrackCover,
@@ -115,8 +118,8 @@ export function write(track, {wikiData}) {
     getArtistString: _getArtistString,
     language,
   }) => {
-    const hasArtists = track.artistContribs?.length > 0;
-    const hasCoverArtists = track.coverArtistContribs?.length > 0;
+    const hasArtists = !empty(track.artistContribs);
+    const hasCoverArtists = !empty(track.coverArtistContribs);
     const getArtistString = (contribs) =>
       _getArtistString(contribs, {
         // We don't want to put actual HTML tags in social embeds (sadly
@@ -197,7 +200,7 @@ export function write(track, {wikiData}) {
 
         // disabled for now! shifting banner position per height of page is disorienting
         /*
-        banner: album.bannerArtistContribs.length && {
+        banner: !empty(album.bannerArtistContribs) && {
           classes: ['dim'],
           dimensions: album.bannerDimensions,
           path: ['media.albumBanner', album.directory, album.bannerFileExtension],
@@ -222,7 +225,7 @@ export function write(track, {wikiData}) {
                 [html.joinChildren]: '<br>',
               },
               [
-                track.artistContribs.length &&
+                !empty(track.artistContribs) &&
                   language.$('releaseInfo.by', {
                     artists: getArtistString(track.artistContribs, {
                       showContrib: true,
@@ -230,15 +233,12 @@ export function write(track, {wikiData}) {
                     }),
                   }),
 
-                track.coverArtistContribs.length &&
+                !empty(track.coverArtistContribs) &&
                   language.$('releaseInfo.coverArtBy', {
-                    artists: getArtistString(
-                      track.coverArtistContribs,
-                      {
-                        showContrib: true,
-                        showIcons: true,
-                      }
-                    ),
+                    artists: getArtistString(track.coverArtistContribs, {
+                      showContrib: true,
+                      showIcons: true,
+                    }),
                   }),
 
                 track.date &&
@@ -261,15 +261,15 @@ export function write(track, {wikiData}) {
               ]),
 
             html.tag('p',
-              (track.urls?.length
-                ? language.$('releaseInfo.listenOn', {
+              (empty(track.urls)
+                ? language.$('releaseInfo.listenOn.noLinks')
+                : language.$('releaseInfo.listenOn', {
                     links: language.formatDisjunctionList(
                       track.urls.map(url => fancifyURL(url, {language}))),
-                  })
-                : language.$('releaseInfo.listenOn.noLinks'))),
+                  }))),
 
             ...html.fragment(
-              otherReleases.length && [
+              !empty(otherReleases) && [
                 html.tag('p', language.$('releaseInfo.alsoReleasedAs')),
                 html.tag('ul', otherReleases.map(track =>
                   html.tag('li', language.$('releaseInfo.alsoReleasedAs.item', {
@@ -279,7 +279,7 @@ export function write(track, {wikiData}) {
               ]),
 
             ...html.fragment(
-              track.contributorContribs.length && [
+              !empty(track.contributorContribs) && [
                 html.tag('p', language.$('releaseInfo.contributors')),
                 html.tag('ul', track.contributorContribs.map(contrib =>
                   html.tag('li', getArtistString([contrib], {
@@ -289,7 +289,7 @@ export function write(track, {wikiData}) {
               ]),
 
             ...html.fragment(
-              referencedTracks.length && [
+              !empty(referencedTracks) && [
                 html.tag('p', language.$('releaseInfo.tracksReferenced', {
                   track: html.tag('i', track.name),
                 })),
@@ -297,7 +297,7 @@ export function write(track, {wikiData}) {
               ]),
 
             ...html.fragment(
-              referencedByTracks.length && [
+              !empty(referencedByTracks) && [
                 html.tag('p', language.$('releaseInfo.tracksThatReference', {
                   track: html.tag('i', track.name),
                 })),
@@ -309,7 +309,7 @@ export function write(track, {wikiData}) {
 
             ...html.fragment(
               wikiInfo.enableFlashesAndGames &&
-              flashesThatFeature.length && [
+              !empty(flashesThatFeature) && [
                 html.tag('p', language.$('releaseInfo.flashesThatFeature', {
                   track: `<i>${track.name}</i>`,
                 })),