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:
@@ -8,6 +8,7 @@ export class BookSorter {
|
|||||||
{label: 'Title + Series', field: 'titleSeries', direction: SortDirection.ASCENDING},
|
{label: 'Title + Series', field: 'titleSeries', direction: SortDirection.ASCENDING},
|
||||||
{label: 'File Name', field: 'fileName', direction: SortDirection.ASCENDING},
|
{label: 'File Name', field: 'fileName', direction: SortDirection.ASCENDING},
|
||||||
{label: 'Author', field: 'author', 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: 'Author + Series', field: 'authorSeries', direction: SortDirection.ASCENDING},
|
||||||
{label: 'Last Read', field: 'lastReadTime', direction: SortDirection.ASCENDING},
|
{label: 'Last Read', field: 'lastReadTime', direction: SortDirection.ASCENDING},
|
||||||
{label: 'Personal Rating', field: 'personalRating', 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']);
|
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', () => {
|
it('should sort by publishedDate descending', () => {
|
||||||
const sorted = service.applySort(books, {field: 'publishedDate', direction: SortDirection.DESCENDING, label: 'Published Date'});
|
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']);
|
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];
|
return [title, Number.MAX_SAFE_INTEGER];
|
||||||
},
|
},
|
||||||
author: (book) => book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null,
|
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) => {
|
authorSeries: (book) => {
|
||||||
const author = book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null;
|
const author = book.metadata?.authors?.map(a => a.toLowerCase()).join(", ") || null;
|
||||||
const series = book.metadata?.seriesName?.toLowerCase() || null;
|
const series = book.metadata?.seriesName?.toLowerCase() || null;
|
||||||
|
|||||||
Reference in New Issue
Block a user