feat(sort): Add sort by author surname and first name (#2292)

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2026-01-18 18:31:45 -07:00
committed by GitHub
parent 3b56294d1c
commit d4cd6f52f4
3 changed files with 19 additions and 0 deletions
@@ -8,6 +8,7 @@ export class BookSorter {
{label: 'Title + Series', field: 'titleSeries', direction: SortDirection.ASCENDING},
{label: 'File Name', field: 'fileName', direction: SortDirection.ASCENDING},
{label: 'Author', field: 'author', direction: SortDirection.ASCENDING},
{label: 'Author (Surname)', field: 'authorSurnameVorname', direction: SortDirection.ASCENDING},
{label: 'Author + Series', field: 'authorSeries', direction: SortDirection.ASCENDING},
{label: 'Last Read', field: 'lastReadTime', direction: SortDirection.ASCENDING},
{label: 'Personal Rating', field: 'personalRating', direction: SortDirection.ASCENDING},
@@ -85,6 +85,17 @@ describe('SortService', () => {
expect(sorted.map(b => b.metadata?.authors?.[0])).toEqual(['Alice', 'Alice', 'Bob']);
});
it('should sort by author surname', () => {
const testBooks = [
makeBook({ id: 1, metadata: { bookId: 1, title: 'T1', authors: ['Terry Pratchett'] } }),
makeBook({ id: 2, metadata: { bookId: 2, title: 'T2', authors: ['Neil Gaiman'] } }),
makeBook({ id: 3, metadata: { bookId: 3, title: 'T3', authors: ['Stephen King'] } }),
];
const sorted = service.applySort(testBooks, { field: 'authorSurnameVorname', direction: SortDirection.ASCENDING, label: 'Author (Surname)' });
expect(sorted.map(b => b.metadata?.authors?.[0])).toEqual(['Neil Gaiman', 'Stephen King', 'Terry Pratchett']);
});
it('should sort by publishedDate descending', () => {
const sorted = service.applySort(books, {field: 'publishedDate', direction: SortDirection.DESCENDING, label: 'Published Date'});
expect(sorted.map(b => b.metadata?.title)).toEqual(['Book 1', 'Book 10', 'Book 2']);
@@ -61,6 +61,13 @@ export class SortService {
return [title, Number.MAX_SAFE_INTEGER];
},
author: (book) => book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null,
authorSurnameVorname: (book) => book.metadata?.authors?.map(a => {
const parts = a.trim().split(/\s+/);
if (parts.length < 2) return a.toLowerCase();
const surname = parts.pop();
const firstname = parts.join(" ");
return `${surname}, ${firstname}`.toLowerCase();
}).join(", ") || null,
authorSeries: (book) => {
const author = book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null;
const series = book.metadata?.seriesName?.toLowerCase() || null;