« get me outta code hell

remove a bunch of unnecessary guarding ?. - 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>2022-11-21 22:29:39 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-11-21 22:29:39 -0400
commit768927503b5948b846b9a6cddf4b788ca9792e8c (patch)
treec59259866e251d8177083212fe5571bcc7294895
parenta69bdd2c6c02286f63ce19812ed42c4576423c3d (diff)
remove a bunch of unnecessary guarding ?.
-rw-r--r--src/data/things.js90
-rw-r--r--src/listing-spec.js18
-rw-r--r--src/misc-templates.js13
-rw-r--r--src/page/artist.js31
4 files changed, 66 insertions, 86 deletions
diff --git a/src/data/things.js b/src/data/things.js
index 45cd7f2..4aa684d 100644
--- a/src/data/things.js
+++ b/src/data/things.js
@@ -359,9 +359,7 @@ Thing.common = {
         if (!artistData) return [];
         const refs =
           contribsByRef ??
-          findFn(thing, thingData, {mode: 'quiet'})?.[
-            parentContribsByRefProperty
-          ];
+          findFn(thing, thingData, {mode: 'quiet'})?.[parentContribsByRefProperty];
         if (!refs) return [];
         return refs
           .map(({who: ref, what}) => ({
@@ -378,32 +376,28 @@ Thing.common = {
   // you would use this to compute a corresponding "referenced *by* tracks"
   // property. Naturally, the passed ref list property is of the things in the
   // wiki data provided, not the requesting Thing itself.
-  reverseReferenceList: (wikiDataProperty, referencerRefListProperty) => ({
+  reverseReferenceList: (thingDataProperty, referencerRefListProperty) => ({
     flags: {expose: true},
 
     expose: {
-      dependencies: [wikiDataProperty],
+      dependencies: [thingDataProperty],
 
-      compute: ({[wikiDataProperty]: wikiData, [Thing.instance]: thing}) =>
-        wikiData
-          ? wikiData.filter((t) =>
-              t[referencerRefListProperty]?.includes(thing)
-            )
-          : [],
+      compute: ({[thingDataProperty]: thingData, [Thing.instance]: thing}) =>
+        thingData?.filter(t => t[referencerRefListProperty].includes(thing)) ?? [],
     },
   }),
 
   // Corresponding function for single references. Note that the return value
   // is still a list - this is for matching all the objects whose single
   // reference (in the given property) matches this Thing.
-  reverseSingleReference: (wikiDataProperty, referencerRefListProperty) => ({
+  reverseSingleReference: (thingDataProperty, referencerRefListProperty) => ({
     flags: {expose: true},
 
     expose: {
-      dependencies: [wikiDataProperty],
+      dependencies: [thingDataProperty],
 
-      compute: ({[wikiDataProperty]: wikiData, [Thing.instance]: thing}) =>
-        wikiData?.filter((t) => t[referencerRefListProperty] === thing),
+      compute: ({[thingDataProperty]: thingData, [Thing.instance]: thing}) =>
+        thingData?.filter((t) => t[referencerRefListProperty] === thing) ?? [],
     },
   }),
 
@@ -988,10 +982,13 @@ Artist.filterByContrib = (thingDataProperty, contribsProperty) => ({
   expose: {
     dependencies: [thingDataProperty],
 
-    compute: ({[thingDataProperty]: thingData, [Artist.instance]: artist}) =>
-      thingData?.filter(({[contribsProperty]: contribs}) =>
-        contribs?.some((contrib) => contrib.who === artist)
-      ),
+    compute: ({
+      [thingDataProperty]: thingData,
+      [Artist.instance]: artist
+    }) =>
+      thingData?.filter(thing =>
+        thing[contribsProperty]
+          .some(contrib => contrib.who === artist)) ?? [],
   },
 });
 
@@ -1037,15 +1034,12 @@ Artist.propertyDescriptors = {
     },
   },
 
-  tracksAsArtist: Artist.filterByContrib('trackData', 'artistContribs'),
-  tracksAsContributor: Artist.filterByContrib(
-    'trackData',
-    'contributorContribs'
-  ),
-  tracksAsCoverArtist: Artist.filterByContrib(
-    'trackData',
-    'coverArtistContribs'
-  ),
+  tracksAsArtist:
+    Artist.filterByContrib('trackData', 'artistContribs'),
+  tracksAsContributor:
+    Artist.filterByContrib('trackData', 'contributorContribs'),
+  tracksAsCoverArtist:
+    Artist.filterByContrib('trackData', 'coverArtistContribs'),
 
   tracksAsAny: {
     flags: {expose: true},
@@ -1059,8 +1053,7 @@ Artist.propertyDescriptors = {
             ...track.artistContribs,
             ...track.contributorContribs,
             ...track.coverArtistContribs,
-          ].some(({who}) => who === artist)
-        ),
+          ].some(({who}) => who === artist)) ?? [],
     },
   },
 
@@ -1072,24 +1065,18 @@ Artist.propertyDescriptors = {
 
       compute: ({trackData, [Artist.instance]: artist}) =>
         trackData?.filter(({commentatorArtists}) =>
-          commentatorArtists?.includes(artist)
-        ),
+          commentatorArtists.includes(artist)) ?? [],
     },
   },
 
-  albumsAsAlbumArtist: Artist.filterByContrib('albumData', 'artistContribs'),
-  albumsAsCoverArtist: Artist.filterByContrib(
-    'albumData',
-    'coverArtistContribs'
-  ),
-  albumsAsWallpaperArtist: Artist.filterByContrib(
-    'albumData',
-    'wallpaperArtistContribs'
-  ),
-  albumsAsBannerArtist: Artist.filterByContrib(
-    'albumData',
-    'bannerArtistContribs'
-  ),
+  albumsAsAlbumArtist:
+    Artist.filterByContrib('albumData', 'artistContribs'),
+  albumsAsCoverArtist:
+    Artist.filterByContrib('albumData', 'coverArtistContribs'),
+  albumsAsWallpaperArtist:
+    Artist.filterByContrib('albumData', 'wallpaperArtistContribs'),
+  albumsAsBannerArtist:
+    Artist.filterByContrib('albumData', 'bannerArtistContribs'),
 
   albumsAsCommentator: {
     flags: {expose: true},
@@ -1099,8 +1086,7 @@ Artist.propertyDescriptors = {
 
       compute: ({albumData, [Artist.instance]: artist}) =>
         albumData?.filter(({commentatorArtists}) =>
-          commentatorArtists?.includes(artist)
-        ),
+          commentatorArtists.includes(artist)) ?? [],
     },
   },
 
@@ -1181,7 +1167,7 @@ Group.propertyDescriptors = {
 
       compute: ({groupCategoryData, [Group.instance]: group}) =>
         groupCategoryData.find((category) => category.groups.includes(group))
-          ?.color ?? null,
+          ?.color,
     },
   },
 
@@ -1243,11 +1229,9 @@ ArtTag.propertyDescriptors = {
       dependencies: ['albumData', 'trackData'],
       compute: ({albumData, trackData, [ArtTag.instance]: artTag}) =>
         sortAlbumsTracksChronologically(
-          [...albumData, ...trackData].filter((thing) =>
-            thing.artTags?.includes(artTag)
-          ),
-          {getDate: (o) => o.coverArtDate}
-        ),
+          [...albumData, ...trackData]
+            .filter(({artTags}) => artTags.includes(artTag)),
+          {getDate: o => o.coverArtDate}),
     },
   },
 };
diff --git a/src/listing-spec.js b/src/listing-spec.js
index 9924474..e6cd7a0 100644
--- a/src/listing-spec.js
+++ b/src/listing-spec.js
@@ -141,8 +141,8 @@ const listingSpec = [
         .map(artist => ({
           artist,
           contributions:
-            (artist.tracksAsContributor?.length ?? 0) +
-            (artist.tracksAsArtist?.length ?? 0),
+            artist.tracksAsContributor.length +
+            artist.tracksAsArtist.length,
         }))
         .sort((a, b) => b.contributions - a.contributions)
         .filter(({contributions}) => contributions),
@@ -151,12 +151,12 @@ const listingSpec = [
         .map(artist => ({
           artist,
           contributions:
-            (artist.tracksAsCoverArtist?.length ?? 0) +
-            (artist.albumsAsCoverArtist?.length ?? 0) +
-            (artist.albumsAsWallpaperArtist?.length ?? 0) +
-            (artist.albumsAsBannerArtist?.length ?? 0) +
+            artist.tracksAsCoverArtist.length +
+            artist.albumsAsCoverArtist.length +
+            artist.albumsAsWallpaperArtist.length +
+            artist.albumsAsBannerArtist.length +
             (wikiInfo.enableFlashesAndGames
-              ? artist.flashesAsContributor?.length ?? 0
+              ? artist.flashesAsContributor.length
               : 0),
         }))
         .sort((a, b) => b.contributions - a.contributions)
@@ -217,8 +217,8 @@ const listingSpec = [
         .map(artist => ({
           artist,
           entries:
-            (artist.tracksAsCommentator?.length ?? 0) +
-            (artist.albumsAsCommentator?.length ?? 0),
+            artist.tracksAsCommentator.length +
+            artist.albumsAsCommentator.length,
         }))
         .filter(({entries}) => entries)
         .sort((a, b) => b.entries - a.entries),
diff --git a/src/misc-templates.js b/src/misc-templates.js
index 7b4ef57..c627533 100644
--- a/src/misc-templates.js
+++ b/src/misc-templates.js
@@ -8,7 +8,10 @@ import {Track, Album} from './data/things.js';
 
 import {getColors} from './util/colors.js';
 
-import {unique} from './util/sugar.js';
+import {
+  empty,
+  unique,
+} from './util/sugar.js';
 
 import {
   getTotalDuration,
@@ -26,7 +29,7 @@ function unbound_generateAdditionalFilesShortcut(additionalFiles, {
   html,
   language,
 }) {
-  if (!additionalFiles?.length) return '';
+  if (empty(additionalFiles)) return '';
 
   return language.$('releaseInfo.additionalFiles.shortcut', {
     anchorLink:
@@ -45,7 +48,7 @@ function unbound_generateAdditionalFilesList(additionalFiles, {
   getFileSize,
   linkFile,
 }) {
-  if (!additionalFiles?.length) return [];
+  if (empty(additionalFiles)) return [];
 
   const fileCount = additionalFiles.flatMap((g) => g.files).length;
 
@@ -103,7 +106,7 @@ function unbound_getArtistString(artists, {
       const {urls} = who;
 
       const hasContribPart = !!(showContrib && what);
-      const hasExternalPart = !!(showIcons && urls?.length);
+      const hasExternalPart = !!(showIcons && !empty(urls));
 
       const artistLink = link.artist(who);
 
@@ -377,7 +380,7 @@ function unbound_generateTrackListDividedByGroups(tracks, {
 }) {
   const {divideTrackListsByGroups: groups} = wikiData.wikiInfo;
 
-  if (!groups?.length) {
+  if (empty(groups)) {
     return html.tag('ul',
       tracks.map(t => getTrackItem(t)));
   }
diff --git a/src/page/artist.js b/src/page/artist.js
index efd4e1f..f6a81f6 100644
--- a/src/page/artist.js
+++ b/src/page/artist.js
@@ -128,7 +128,7 @@ export function write(artist, {wikiData}) {
 
   let flashes, flashListChunks;
   if (wikiInfo.enableFlashesAndGames) {
-    flashes = sortChronologically(artist.flashesAsContributor?.slice() ?? []);
+    flashes = sortChronologically(artist.flashesAsContributor.slice());
     flashListChunks = chunkByProperties(
       flashes.map((flash) => ({
         act: flash.act,
@@ -299,22 +299,17 @@ export function write(artist, {wikiData}) {
 
       return {
         albums: {
-          asCoverArtist: artist.albumsAsCoverArtist?.map(
-            serializeArtistsAndContrib('coverArtistContribs')
-          ),
-          asWallpaperArtist: artist.albumsAsWallpaperArtist?.map(
-            serializeArtistsAndContrib('wallpaperArtistContribs')
-          ),
-          asBannerArtist: artist.albumsAsBannerArtist?.map(
-            serializeArtistsAndContrib('bannerArtistContribs')
-          ),
+          asCoverArtist: artist.albumsAsCoverArtist
+            .map(serializeArtistsAndContrib('coverArtistContribs')),
+          asWallpaperArtist: artist.albumsAsWallpaperArtist
+            .map(serializeArtistsAndContrib('wallpaperArtistContribs')),
+          asBannerArtist: artist.albumsAsBannerArtis
+            .map(serializeArtistsAndContrib('bannerArtistContribs')),
         },
         flashes: wikiInfo.enableFlashesAndGames
           ? {
               asContributor: artist.flashesAsContributor
-                ?.map((flash) =>
-                  getArtistsAndContrib(flash, 'contributorContribs')
-                )
+                .map(flash => getArtistsAndContrib(flash, 'contributorContribs'))
                 .map(({contrib, thing: flash}) => ({
                   link: serializeLink(flash),
                   contribution: contrib.what,
@@ -322,12 +317,10 @@ export function write(artist, {wikiData}) {
             }
           : null,
         tracks: {
-          asArtist: artist.tracksAsArtist.map(
-            serializeArtistsAndContrib('artistContribs')
-          ),
-          asContributor: artist.tracksAsContributor.map(
-            serializeArtistsAndContrib('contributorContribs')
-          ),
+          asArtist: artist.tracksAsArtist
+            .map(serializeArtistsAndContrib('artistContribs')),
+          asContributor: artist.tracksAsContributo
+            .map(serializeArtistsAndContrib('contributorContribs')),
           chunked: serializeTrackListChunks(trackListChunks),
         },
       };