From 878a96cb223783ef1b100fdb3bb9d795404c44f5 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 27 Apr 2023 15:39:55 -0300 Subject: sort art galleries reverse-chronologically (again) Fixes #170. --- src/util/wiki-data.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/util') diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 0eb86a1e..5a0e241a 100644 --- a/src/util/wiki-data.js +++ b/src/util/wiki-data.js @@ -316,6 +316,7 @@ export function sortChronologically(data, { // // This function also works for data lists which contain only tracks. export function sortAlbumsTracksChronologically(data, { + latestFirst = false, getDate, } = {}) { // Sort albums before tracks... @@ -333,7 +334,18 @@ 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). - sortByDate(data, {getDate}); + + 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}); + } return data; } -- cgit 1.3.0-6-gf8a5 From 3a322d96666b8da2b615ffd1c245f3a2f3d0cd90 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 27 Apr 2023 16:16:53 -0300 Subject: 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. --- src/util/wiki-data.js | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) (limited to 'src/util') diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 5a0e241a..7a3f4144 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; } -- cgit 1.3.0-6-gf8a5 From 6128ba16c1b5c4c6095e0ddba0977817cce4bc6e Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 27 Apr 2023 16:55:06 -0300 Subject: data: new sortByPositionInFlash act function Fixes #168. This refactors the duplicated logic with sortByPositionInAlbum into a new template, sortByPositionInParent. --- src/util/wiki-data.js | 83 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 17 deletions(-) (limited to 'src/util') diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 7a3f4144..2a8f12ca 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) { -- cgit 1.3.0-6-gf8a5 From 7830c73e6047ab8ec1322c4a56ed6a450bfb11e6 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 27 Apr 2023 16:57:09 -0300 Subject: data: make sortByConditions return sorted array This wasn't causing any bugs within the codebase but it should be consistent with the rest of the sort functions anyway. --- src/util/wiki-data.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/util') diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 2a8f12ca..89c621c5 100644 --- a/src/util/wiki-data.js +++ b/src/util/wiki-data.js @@ -260,7 +260,7 @@ export function sortByPositionInFlashAct(data) { // 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) => { + return data.sort((a, b) => { const ai = conditions.findIndex((f) => f(a)); const bi = conditions.findIndex((f) => f(b)); -- cgit 1.3.0-6-gf8a5