diff options
| -rw-r--r-- | src/common-util/sort.js | 74 | ||||
| -rw-r--r-- | src/data/things/sorting-rule/ThingSortingRule.js | 16 | ||||
| -rw-r--r-- | src/reverse.js | 8 |
3 files changed, 68 insertions, 30 deletions
diff --git a/src/common-util/sort.js b/src/common-util/sort.js index d89fa4cc..26363ac3 100644 --- a/src/common-util/sort.js +++ b/src/common-util/sort.js @@ -90,9 +90,10 @@ export function normalizeName(s) { // sortByDirectory will handle the rest, given all directories are unique // except when album and track directories overlap with each other. export function sortByDirectory(data, { - getDirectory = object => object.directory, + getDirectory = nativeGetDirectory, } = {}) { - const directories = data.map(getDirectory); + const directories = + data.map(thing => getDirectory(thing, nativeGetDirectory)); sortMultipleArrays(data, directories, (a, b, directoryA, directoryB) => @@ -101,11 +102,18 @@ export function sortByDirectory(data, { return data; } +export function nativeGetDirectory(thing, _nativeGetDirectory) { + return thing.directory; +} + export function sortByName(data, { - getName = object => object.name, + getName = nativeGetName, } = {}) { - const names = data.map(getName); - const normalizedNames = names.map(normalizeName); + const names = + data.map(thing => getName(thing, nativeGetName)); + + const normalizedNames = + names.map(normalizeName); sortMultipleArrays(data, normalizedNames, names, ( @@ -121,6 +129,13 @@ export function sortByName(data, { return data; } +export function nativeGetName(thing, _nativeGetName) { + return ( + thing.nameForSorting ?? + thing.name + ); +} + export function compareNormalizedNames( normalizedA, normalizedB, nonNormalizedA, nonNormalizedB, @@ -133,10 +148,11 @@ export function compareNormalizedNames( } export function sortByDate(data, { - getDate = object => object.date, + getDate = nativeGetDate, latestFirst = false, } = {}) { - const dates = data.map(getDate); + const dates = + data.map(thing => getDate(thing, nativeGetDate)); sortMultipleArrays(data, dates, (a, b, dateA, dateB) => @@ -145,6 +161,10 @@ export function sortByDate(data, { return data; } +export function nativeGetDate(thing) { + return thing.date; +} + export function compareDates(a, b, { latestFirst = false, } = {}) { @@ -287,8 +307,8 @@ export function sortByConditions(data, conditions) { // * directory (or override getDirectory) // * name (or override getName) export function sortAlphabetically(data, { - getDirectory, - getName, + getDirectory = undefined, + getName = undefined, } = {}) { sortByDirectory(data, {getDirectory}); sortByName(data, {getName}); @@ -301,9 +321,9 @@ export function sortAlphabetically(data, { // * date (or override getDate) export function sortChronologically(data, { latestFirst = false, - getDirectory, - getName, - getDate, + getDirectory = undefined, + getName = undefined, + getDate = undefined, } = {}) { sortAlphabetically(data, {getDirectory, getName}); sortByDate(data, {latestFirst, getDate}); @@ -373,16 +393,23 @@ export function prepareAndSort(sources, prepareForSort, sortFunction) { // // This function also works for data lists which contain only tracks. export function sortAlbumsTracksChronologically(data, { + getDate = undefined, latestFirst = false, - getDate, } = {}) { // Sort albums before tracks... sortByConditions(data, [t => t.isAlbum]); // Put albums alphabetically, and group with them... sortAlphabetically(data, { - getDirectory: t => t.isTrack ? t.album.directory : t.directory, - getName: t => t.isTrack ? t.album.name : t.name, + getDirectory: (thing, nativeGetDirectory) => + (thing.isTrack + ? nativeGetDirectory(thing.album) + : nativeGetDirectory(thing)), + + getName: (thing, nativeGetName) => + (thing.isTrack + ? nativeGetName(thing.album) + : nativeGetName(thing)), }); // Sort tracks by position in album... @@ -414,13 +441,16 @@ export function sortArtworksChronologically(data, { } export function sortFlashesChronologically(data, { + getDate = undefined, latestFirst = false, - getDate, } = {}) { // Group flashes by act... sortAlphabetically(data, { - getName: flash => flash.act.name, - getDirectory: flash => flash.act.directory, + getName: (flash, nativeGetName) => + nativeGetName(flash.act), + + getDirectory: (flash, nativeGetDirectory) => + nativeGetDirectory(flash.act), }); // Sort flashes by position in act... @@ -436,8 +466,8 @@ export function sortFlashesChronologically(data, { } export function sortContributionsChronologically(data, sortThings, { + getThing = nativeGetContributionThing, latestFirst = false, - getThing = contrib => contrib.thing, } = {}) { // Contributions only have one date property (which is provided when // the contribution is created). They're sorted by this most primarily, @@ -446,7 +476,7 @@ export function sortContributionsChronologically(data, sortThings, { const entries = data.map(contrib => ({ entry: contrib, - thing: getThing(contrib), + thing: getThing(contrib, nativeGetContributionThing), })); sortEntryThingPairs( @@ -467,3 +497,7 @@ export function sortContributionsChronologically(data, sortThings, { return data; } + +export function nativeGetContributionThing(contrib, _nativeGetContributionThing) { + return contrib.thing; +} diff --git a/src/data/things/sorting-rule/ThingSortingRule.js b/src/data/things/sorting-rule/ThingSortingRule.js index 6dbaccf7..363146a3 100644 --- a/src/data/things/sorting-rule/ThingSortingRule.js +++ b/src/data/things/sorting-rule/ThingSortingRule.js @@ -41,22 +41,24 @@ export class ThingSortingRule extends SortingRule { const get = thing => thing[property]; const lc = property.toLowerCase(); - if (lc.endsWith('date')) { + if (lc === 'date') { + sortByDate(sortable); + continue; + } else if (lc.endsWith('date')) { sortByDate(sortable, {getDate: get}); continue; } - if (lc.endsWith('directory')) { + if (lc === 'directory') { + sortByDirectory(sortable); + continue; + } else if (lc.endsWith('directory')) { sortByDirectory(sortable, {getDirectory: get}); continue; } if (lc === 'name') { - sortByName(sortable, { - getName: thing => - thing.nameForSorting ?? - thing.name, - }); + sortByName(sortable); continue; } else if (lc.endsWith('name')) { sortByName(sortable, {getName: get}); diff --git a/src/reverse.js b/src/reverse.js index 7d7e3672..ab493bca 100644 --- a/src/reverse.js +++ b/src/reverse.js @@ -84,9 +84,11 @@ function reverseHelper(spec) { for (const referencedThing of allReferencedThings) { if (cacheRecord.has(referencedThing)) { const referencingThings = cacheRecord.get(referencedThing); - sortByDate(referencingThings, { - getDate: spec.date ?? (thing => thing.date), - }); + if (spec.date) { + sortByDate(referencingThings, {getDate: spec.date}); + } else { + sortByDate(referencingThings); + } } } |