« get me outta code hell

detect what's an album without constructor refs - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-06-13 22:36:36 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-06-13 22:37:39 -0300
commit4136d9d53191be9e5bba72f70364fe0155c7593d (patch)
treee96ae79ebf78d93c7ec04a825aa4cb4f7cf939c7 /src/util
parentdacdf44c64688ea56bceb001a5ef431c1905061f (diff)
detect what's an album without constructor refs
This is kind of a hack but it totally works and gets client-side
code to run again.

http-music and mtui are filled to the brim with this kind of
check and it's one of many reasons they need some serious
reworking before i make a threequel lol
Diffstat (limited to 'src/util')
-rw-r--r--src/util/wiki-data.js15
1 files changed, 7 insertions, 8 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js
index aba508c5..5aef812d 100644
--- a/src/util/wiki-data.js
+++ b/src/util/wiki-data.js
@@ -1,7 +1,5 @@
 // Utility functions for interacting with wiki data.
 
-import { Album, Track } from '../data/things.js';
-
 // Generic value operations
 
 export function getKebabCase(name) {
@@ -216,12 +214,13 @@ export function sortByPositionInAlbum(data) {
     });
 }
 
-// Note that this function only checks constructor equality, not inheritence!
-// So it won't group subclasses together (as though they were the same type).
-export function sortByThingType(data, thingConstructors) {
+// Sorts data so that items are grouped together according to whichever of a
+// set of arbitrary given conditions is true first. If no conditions are met
+// for a given item, it's moved over to the end!
+export function sortByConditions(data, conditions) {
     data.sort((a, b) => {
-        const ai = thingConstructors.indexOf(a.constructor);
-        const bi = thingConstructors.indexOf(b.constructor);
+        const ai = conditions.findIndex(f => f(a));
+        const bi = conditions.findIndex(f => f(b));
 
         if (ai >= 0 && bi >= 0) {
             return ai - bi;
@@ -276,7 +275,7 @@ export function sortChronologically(data, {getDirectory, getName, getDate} = {})
 // This function also works for data lists which contain only tracks.
 export function sortAlbumsTracksChronologically(data, {getDate} = {}) {
     // Sort albums before tracks...
-    sortByThingType(data, [Album, Track]);
+    sortByConditions(data, [t => t.album === undefined]);
 
     // Group tracks by album...
     sortByDirectory(data, {