« get me outta code hell

data: new sortByPositionInFlash act function - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/wiki-data.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-04-27 16:55:06 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-04-27 16:56:00 -0300
commit6128ba16c1b5c4c6095e0ddba0977817cce4bc6e (patch)
tree2f22c42534fdfb4e8dfdd3bb1108dd7eaeaeca23 /src/util/wiki-data.js
parent3a322d96666b8da2b615ffd1c245f3a2f3d0cd90 (diff)
data: new sortByPositionInFlash act function
Fixes #168.

This refactors the duplicated logic with sortByPositionInAlbum
into a new template, sortByPositionInParent.
Diffstat (limited to 'src/util/wiki-data.js')
-rw-r--r--src/util/wiki-data.js83
1 files changed, 66 insertions, 17 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js
index 7a3f414..2a8f12c 100644
--- a/src/util/wiki-data.js
+++ b/src/util/wiki-data.js
@@ -207,35 +207,52 @@ export function sortByDate(data, {
   });
 }
 
-export function sortByPositionInAlbum(data) {
+export function sortByPositionInParent(data, {
+  getParent,
+  getChildren,
+}) {
   return data.sort((a, b) => {
-    const aa = a.album;
-    const ba = b.album;
+    const parentA = getParent(a);
+    const parentB = getParent(b);
 
-    // Don't change the sort when the two tracks are from separate albums.
-    // This function doesn't change the order of albums or try to "merge"
-    // two separated chunks of tracks from the same album together.
-    if (aa !== ba) {
+    // Don't change the sort when the two items are from separate parents.
+    // This function doesn't change the order of parents or try to "merge"
+    // two separated chunks of items from the same parent together.
+    if (parentA !== parentB) {
       return 0;
     }
 
-    // Don't change the sort when only one (or neither) item is actually
-    // a track (i.e. has an album).
-    if (!aa || !ba) {
+    // Don't change the sort when either (or both) of the items doesn't
+    // even have a parent (e.g. it's the passed data is a mixed array of
+    // children and parents).
+    if (!parentA || !parentB) {
       return 0;
     }
 
-    const ai = aa.tracks.indexOf(a);
-    const bi = ba.tracks.indexOf(b);
+    const indexA = getChildren(parentA).indexOf(a);
+    const indexB = getChildren(parentB).indexOf(b);
 
-    // There's no reason this two-way reference (a track's album and the
-    // album's track list) should be broken, but if for any reason it is,
-    // don't change the sort.
-    if (ai === -1 || bi === -1) {
+    // If the getParent/getChildren relationship doesn't go both ways for
+    // some reason, don't change the sort.
+    if (indexA === -1 || indexB === -1) {
       return 0;
     }
 
-    return ai - bi;
+    return indexA - indexB;
+  });
+}
+
+export function sortByPositionInAlbum(data) {
+  return sortByPositionInParent(data, {
+    getParent: track => track.album,
+    getChildren: album => album.tracks,
+  });
+}
+
+export function sortByPositionInFlashAct(data) {
+  return sortByPositionInParent(data, {
+    getParent: flash => flash.act,
+    getChildren: act => act.flashes,
   });
 }
 
@@ -330,6 +347,38 @@ export function sortAlbumsTracksChronologically(data, {
   return data;
 }
 
+export function sortFlashesChronologically(data, {
+  latestFirst = false,
+  getDate,
+} = {}) {
+  // Flash acts don't actually have any identifying properties because they
+  // don't have dedicated pages (yet), so don't have a directory. Make up a
+  // fake key identifying them so flashes can be grouped together.
+  const flashActs = new Set(data.map(flash => flash.act));
+  const flashActIdentifiers = new Map();
+
+  let counter = 0;
+  for (const act of flashActs) {
+    flashActIdentifiers.set(act, ++counter);
+  }
+
+  // Group flashes by act...
+  data.sort((a, b) => {
+    return flashActIdentifiers.get(a.act) - flashActIdentifiers.get(b.act);
+  });
+
+  // Sort flashes by position in act...
+  sortByPositionInFlashAct(data);
+
+  // ...and finally sort by date. If flashes from more than one act were
+  // released on the same date, they'll still be grouped together by act,
+  // and flashes within an act will retain their relative positioning (i.e.
+  // stay in the same order as the act's flash listing).
+  sortByDate(data, {latestFirst, getDate});
+
+  return data;
+}
+
 // Specific data utilities
 
 export function filterAlbumsByCommentary(albums) {