aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/services/itemComparators.ts
diff options
context:
space:
mode:
Diffstat (limited to 'viewer/src/services/itemComparators.ts')
-rw-r--r--viewer/src/services/itemComparators.ts73
1 files changed, 73 insertions, 0 deletions
diff --git a/viewer/src/services/itemComparators.ts b/viewer/src/services/itemComparators.ts
new file mode 100644
index 0000000..bd9accb
--- /dev/null
+++ b/viewer/src/services/itemComparators.ts
@@ -0,0 +1,73 @@
1/* ldgallery - A static generator which turns a collection of tagged
2-- pictures into a searchable web gallery.
3--
4-- Copyright (C) 2019-2020 Guillaume FOUET
5--
6-- This program is free software: you can redistribute it and/or modify
7-- it under the terms of the GNU Affero General Public License as
8-- published by the Free Software Foundation, either version 3 of the
9-- License, or (at your option) any later version.
10--
11-- This program is distributed in the hope that it will be useful,
12-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-- GNU Affero General Public License for more details.
15--
16-- You should have received a copy of the GNU Affero General Public License
17-- along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19import { TranslateResult } from "vue-i18n";
20import i18n from "@/plugins/i18n";
21
22export type ItemComparator = (left: Gallery.Item, right: Gallery.Item) => number;
23export type ItemSort = { text: TranslateResult; fn: ItemComparator };
24
25export default class ItemComparators {
26 static readonly ITEM_SORTS: Record<Gallery.ItemSortStr, ItemSort> = {
27 title_asc: {
28 text: i18n.t("command.sort.byTitleAsc"),
29 fn: ItemComparators.chain(ItemComparators.sortByTitleAsc, ItemComparators.sortByPathAsc),
30 },
31 date_asc: {
32 text: i18n.t("command.sort.byDateAsc"),
33 fn: ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc),
34 },
35 date_desc: {
36 text: i18n.t("command.sort.byDateDesc"),
37 fn: ItemComparators.reverse(ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc)),
38 },
39 };
40
41 static readonly DEFAULT = ItemComparators.ITEM_SORTS.date_asc;
42
43 static sortByPathAsc(left: Gallery.Item, right: Gallery.Item): number {
44 return left.path.localeCompare(right.path, undefined, {
45 sensitivity: "base",
46 ignorePunctuation: true,
47 numeric: true,
48 });
49 }
50
51 static sortByTitleAsc(left: Gallery.Item, right: Gallery.Item): number {
52 return left.title.localeCompare(right.title, undefined, {
53 sensitivity: "base",
54 ignorePunctuation: true,
55 numeric: true,
56 });
57 }
58
59 static sortByDateAsc(left: Gallery.Item, right: Gallery.Item): number {
60 return left.datetime.localeCompare(right.datetime); // TODO: handle timezones
61 }
62
63 static reverse(fn: ItemComparator): ItemComparator {
64 return (l, r) => -fn(l, r);
65 }
66
67 static chain(primary: ItemComparator, tieBreaker: ItemComparator): ItemComparator {
68 return (l, r) => {
69 const primaryComparison = primary(l, r);
70 return primaryComparison !== 0 ? primaryComparison : tieBreaker(l, r);
71 };
72 }
73}