« get me outta code hell

support albums and tracks without release dates - 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-05-05 19:36:59 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-05-05 19:36:59 -0300
commit27d366fd2de98d816ef0698a1a6d5b4d59b388ca (patch)
tree9f87d11391153ec7acd4f1c88eb46f39517e488f /src/util
parent9525ec4e143d17329c303e4ad09242a6dab9a9b9 (diff)
support albums and tracks without release dates
Diffstat (limited to 'src/util')
-rw-r--r--src/util/wiki-data.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js
index 6891a4c6..6d0db2c7 100644
--- a/src/util/wiki-data.js
+++ b/src/util/wiki-data.js
@@ -83,7 +83,24 @@ export function sortByDate(data, dateKey = 'date') {
     // Just to 8e clear: sort is a mutating function! I only return the array
     // 8ecause then you don't have to define it as a separate varia8le 8efore
     // passing it into this function.
-    return data.sort((a, b) => a[dateKey] - b[dateKey]);
+    return data.sort(({ [dateKey]: a }, { [dateKey]: b }) => {
+        // It's possible for objects with and without dates to be mixed
+        // together in the same array. If that's the case, we put all items
+        // without dates at the end.
+        if (a && b) {
+            return a - b;
+        } else if (a) {
+            return -1;
+        } else if (b) {
+            return 1;
+        } else {
+            // If neither of the items being compared have a date, don't move
+            // them relative to each other. This is basically the same as
+            // filtering out all non-date items and then pushing them at the
+            // end after sorting the rest.
+            return 0;
+        }
+    });
 }
 
 // Same details as the sortByDate, 8ut for covers~