« get me outta code hell

util: new sortEntryThingPairs util - 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>2023-06-26 08:44:44 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-06-26 08:44:44 -0300
commit160c6c4c92f0c574981bab9fabd5f2d06cb0bf10 (patch)
treeef773399ff6ab47be788913bbcde2f7e8ac45cad
parent1deb589b2e22d92f6488d259ce6196706f1515b1 (diff)
util: new sortEntryThingPairs util
-rw-r--r--src/content/dependencies/generateArtistInfoPageTracksChunkedList.js57
-rw-r--r--src/util/wiki-data.js28
2 files changed, 52 insertions, 33 deletions
diff --git a/src/content/dependencies/generateArtistInfoPageTracksChunkedList.js b/src/content/dependencies/generateArtistInfoPageTracksChunkedList.js
index 27d8a56a..0132d7ba 100644
--- a/src/content/dependencies/generateArtistInfoPageTracksChunkedList.js
+++ b/src/content/dependencies/generateArtistInfoPageTracksChunkedList.js
@@ -1,26 +1,10 @@
-import {accumulateSum, stitchArrays, unique} from '../../util/sugar.js';
-import {chunkByProperties, sortAlbumsTracksChronologically} from '../../util/wiki-data.js';
+import {accumulateSum, stitchArrays} from '../../util/sugar.js';
 
-// TODO: This obviously needs to be more generalized.
-function sortContributionEntries(entries, sortFunction) {
-  const things = unique(entries.map(({thing}) => thing));
-  sortFunction(things);
-
-  const outputArrays = [];
-  const thingToOutputArray = new Map();
-
-  for (const thing of things) {
-    const array = [];
-    thingToOutputArray.set(thing, array);
-    outputArrays.push(array);
-  }
-
-  for (const entry of entries) {
-    thingToOutputArray.get(entry.thing).push(entry);
-  }
-
-  entries.splice(0, entries.length, ...outputArrays.flat());
-}
+import {
+  chunkByProperties,
+  sortAlbumsTracksChronologically,
+  sortEntryThingPairs,
+} from '../../util/wiki-data.js';
 
 export default {
   contentDependencies: [
@@ -36,27 +20,34 @@ export default {
   query(artist) {
     const entries = [
       ...artist.tracksAsArtist.map(track => ({
-        track,
-        date: track.date,
         thing: track,
-        album: track.album,
-        contribs: track.artistContribs,
+        entry: {
+          track,
+          album: track.album,
+          date: track.date,
+          contribs: track.artistContribs,
+        },
       })),
 
       ...artist.tracksAsContributor.map(track => ({
-        track,
-        date: track.date,
         thing: track,
-        album: track.album,
-        contribs: track.contributorContribs,
+        entry: {
+          track,
+          date: track.date,
+          album: track.album,
+          contribs: track.contributorContribs,
+        },
       })),
     ];
 
-    sortContributionEntries(entries, sortAlbumsTracksChronologically);
+    sortEntryThingPairs(entries, sortAlbumsTracksChronologically);
 
-    const chunks = chunkByProperties(entries, ['album', 'date']);
+    const chunks =
+      chunkByProperties(
+        entries.map(({entry}) => entry),
+        ['album', 'date']);
 
-    return {entries, chunks};
+    return {chunks};
   },
 
   relations(relation, query, artist) {
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js
index 8a2897d2..382f1620 100644
--- a/src/util/wiki-data.js
+++ b/src/util/wiki-data.js
@@ -3,6 +3,7 @@
 import {
   accumulateSum,
   empty,
+  unique,
 } from './sugar.js';
 
 // Generic value operations
@@ -315,6 +316,33 @@ export function sortChronologically(data, {
   return data;
 }
 
+// This one's a little odd! Sorts an array of {entry, thing} pairs using
+// the provided sortFunction, which will operate on each item's `thing`, not
+// its entry (or the item as a whole). If multiple entries are associated
+// with the same thing, they'll end up bunched together in the output,
+// retaining their original relative positioning.
+export function sortEntryThingPairs(data, sortFunction) {
+  const things = unique(data.map(item => item.thing));
+  sortFunction(things);
+
+  const outputArrays = [];
+  const thingToOutputArray = new Map();
+
+  for (const thing of things) {
+    const array = [];
+    thingToOutputArray.set(thing, array);
+    outputArrays.push(array);
+  }
+
+  for (const item of data) {
+    thingToOutputArray.get(item.thing).push(item);
+  }
+
+  data.splice(0, data.length, ...outputArrays.flat());
+
+  return data;
+}
+
 // Highly contextual sort functions - these are only for very specific types
 // of Things, and have appropriately hard-coded behavior.