« get me outta code hell

data: refactor sortByDate to handle latestFirst directly - 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:16:53 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-04-27 16:16:53 -0300
commit3a322d96666b8da2b615ffd1c245f3a2f3d0cd90 (patch)
tree7c3607682293ed1491d848fea7b427e6e183a433 /src/util/wiki-data.js
parent7e0dfb0f797b6dabad4a4491ef8e7333cc199926 (diff)
data: refactor sortByDate to handle latestFirst directly
Fixes #180.

This enables sortByDate to keep dateless items at the end even
when sorting with latest first, and conveniently reduces the ops
since there's no need for .reverse() before and after the sort
anymore. It also cleans logic by deduplicating latest-first code
in compositional sort functions using sortByDate.
Diffstat (limited to 'src/util/wiki-data.js')
-rw-r--r--src/util/wiki-data.js30
1 files changed, 5 insertions, 25 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js
index 5a0e241..7a3f414 100644
--- a/src/util/wiki-data.js
+++ b/src/util/wiki-data.js
@@ -181,6 +181,7 @@ export function sortByName(data, {
 }
 
 export function sortByDate(data, {
+  latestFirst = false,
   getDate = (o) => o.date,
 } = {}) {
   return data.sort((a, b) => {
@@ -191,7 +192,7 @@ export function sortByDate(data, {
     // together in the same array. If that's the case, we put all items
     // without dates at the end.
     if (ad && bd) {
-      return ad - bd;
+      return (latestFirst ? bd - ad : ad - bd);
     } else if (ad) {
       return -1;
     } else if (bd) {
@@ -292,18 +293,8 @@ export function sortChronologically(data, {
   getName,
   getDate,
 } = {}) {
-  if (latestFirst) {
-    // Double reverse: Since we reverse after sorting by date, also reverse
-    // after sorting A-Z, so the second reverse restores A-Z relative
-    // positioning (for entries with the same date).
-    sortAlphabetically(data, {getDirectory, getName});
-    data.reverse();
-    sortByDate(data, {getDate});
-    data.reverse();
-  } else {
-    sortAlphabetically(data, {getDirectory, getName});
-    sortByDate(data, {getDate});
-  }
+  sortAlphabetically(data, {getDirectory, getName});
+  sortByDate(data, {latestFirst, getDate});
   return data;
 }
 
@@ -334,18 +325,7 @@ export function sortAlbumsTracksChronologically(data, {
   // released on the same date, they'll still be grouped together by album,
   // and tracks within an album will retain their relative positioning (i.e.
   // stay in the same order as part of the album's track listing).
-
-  if (latestFirst) {
-    // Like in sortChronologically, double reverse: Since we reverse after
-    // sorting by date, also reverse before, so that items with the same date
-    // are flipped relative to each other twice - that maintains the original
-    // relative ordering!
-    data.reverse();
-    sortByDate(data, {getDate});
-    data.reverse();
-  } else {
-    sortByDate(data, {getDate});
-  }
+  sortByDate(data, {latestFirst, getDate});
 
   return data;
 }