aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/services
diff options
context:
space:
mode:
Diffstat (limited to 'viewer/src/services')
-rw-r--r--viewer/src/services/itemComparators.ts33
1 files changed, 28 insertions, 5 deletions
diff --git a/viewer/src/services/itemComparators.ts b/viewer/src/services/itemComparators.ts
index e131b8b..ab1036d 100644
--- a/viewer/src/services/itemComparators.ts
+++ b/viewer/src/services/itemComparators.ts
@@ -23,18 +23,34 @@ export type ItemComparator = (left: Gallery.Item, right: Gallery.Item) => number
23export type ItemSort = { name: Gallery.ItemSortStr; text: TranslateResult; fn: ItemComparator }; 23export type ItemSort = { name: Gallery.ItemSortStr; text: TranslateResult; fn: ItemComparator };
24 24
25export default class ItemComparators { 25export default class ItemComparators {
26 static readonly DEFAULT = ItemComparators.sortByTitleAsc;
27
28 static readonly ITEM_SORTS: ItemSort[] = [ 26 static readonly ITEM_SORTS: ItemSort[] = [
29 { name: "title_asc", text: i18n.t("command.sort.byTitleAsc"), fn: ItemComparators.sortByTitleAsc }, 27 {
30 { name: "date_asc", text: i18n.t("command.sort.byDateAsc"), fn: ItemComparators.sortByDateAsc }, 28 name: "title_asc",
29 text: i18n.t("command.sort.byTitleAsc"),
30 fn: ItemComparators.chain(ItemComparators.sortByTitleAsc, ItemComparators.sortByPathAsc),
31 },
32 {
33 name: "date_asc",
34 text: i18n.t("command.sort.byDateAsc"),
35 fn: ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc),
36 },
31 { 37 {
32 name: "date_desc", 38 name: "date_desc",
33 text: i18n.t("command.sort.byDateDesc"), 39 text: i18n.t("command.sort.byDateDesc"),
34 fn: ItemComparators.reverse(ItemComparators.sortByDateAsc), 40 fn: ItemComparators.reverse(ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc)),
35 }, 41 },
36 ]; 42 ];
37 43
44 static readonly DEFAULT = ItemComparators.ITEM_SORTS[0].fn;
45
46 static sortByPathAsc(left: Gallery.Item, right: Gallery.Item): number {
47 return left.path.localeCompare(right.path, undefined, {
48 sensitivity: "base",
49 ignorePunctuation: true,
50 numeric: true,
51 });
52 }
53
38 static sortByTitleAsc(left: Gallery.Item, right: Gallery.Item): number { 54 static sortByTitleAsc(left: Gallery.Item, right: Gallery.Item): number {
39 return left.title.localeCompare(right.title, undefined, { 55 return left.title.localeCompare(right.title, undefined, {
40 sensitivity: "base", 56 sensitivity: "base",
@@ -50,4 +66,11 @@ export default class ItemComparators {
50 static reverse(fn: ItemComparator): ItemComparator { 66 static reverse(fn: ItemComparator): ItemComparator {
51 return (l, r) => -fn(l, r); 67 return (l, r) => -fn(l, r);
52 } 68 }
69
70 static chain(primary: ItemComparator, tieBreaker: ItemComparator): ItemComparator {
71 return (l, r) => {
72 const primaryComparison = primary(l, r);
73 return primaryComparison !== 0 ? primaryComparison : tieBreaker(l, r);
74 };
75 }
53} 76}