1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00

Trim extensions when sorting filenames

This commit is contained in:
sarayourfriend 2024-03-29 13:20:40 +11:00
parent fafab0db18
commit c44110d4a6
No known key found for this signature in database
3 changed files with 46 additions and 3 deletions

View File

@ -389,6 +389,19 @@ export class Utils {
const sign = (parts[3] === "N" || parts[3] === "E") ? 1 : -1; const sign = (parts[3] === "N" || parts[3] === "E") ? 1 : -1;
return sign * (degrees + (minutes / 60.0)) return sign * (degrees + (minutes / 60.0))
} }
public static sortableFilename(filename: string): string {
const lastDot = filename.lastIndexOf(".");
// Avoid 0 as well as -1 to prevent empty names for extensionless dot-files
if (lastDot > 0) {
return filename.substring(0, lastDot);
}
// Fallback to the full name
return filename;
}
} }
export class LRU<V> { export class LRU<V> {

View File

@ -132,8 +132,20 @@ export class GallerySortingService {
} }
switch (sorting.method) { switch (sorting.method) {
case SortByTypes.Name: case SortByTypes.Name:
media.sort((a: PhotoDTO, b: PhotoDTO) => media.sort((a: PhotoDTO, b: PhotoDTO) => {
this.collator.compare(a.name, b.name) const aSortable = Utils.sortableFilename(a.name)
const bSortable = Utils.sortableFilename(b.name)
if (aSortable === bSortable) {
// If the trimmed filenames match, use the full name as tie breaker
// This preserves a consistent final position for files named e.g.,
// 10.jpg and 10.png, even if their starting position in the list
// changes based on any previous sorting that's happened under different heuristics
return this.collator.compare(a.name, b.name)
}
return this.collator.compare(aSortable, bSortable)
}
); );
break; break;
case SortByTypes.Date: case SortByTypes.Date:

View File

@ -58,4 +58,22 @@ describe('Utils', () => {
expect(Utils.equalsFilter({a: 0}, {b: 0})).to.be.equal(false); expect(Utils.equalsFilter({a: 0}, {b: 0})).to.be.equal(false);
expect(Utils.equalsFilter({a: 0}, {a: 0})).to.be.equal(true); expect(Utils.equalsFilter({a: 0}, {a: 0})).to.be.equal(true);
}); });
describe('sortableFilename', () => {
it('should trim extensions', () => {
expect(Utils.sortableFilename("10.jpg")).to.be.equal("10")
})
it('should not trim dotfiles to empty strings', () => {
expect(Utils.sortableFilename(".file")).to.be.equal(".file")
})
it('should trim dotfiles with extensions', () => {
expect(Utils.sortableFilename(".favourite.jpg")).to.be.equal(".favourite")
})
it('should not trim without dots', () => {
expect(Utils.sortableFilename("hello_world")).to.be.equal("hello_world")
})
})
}); });