diff options
author | pacien | 2020-09-24 22:18:44 +0200 |
---|---|---|
committer | pacien | 2020-09-25 09:17:34 +0200 |
commit | c108a99e128c02d20930775222f6118a460f80b1 (patch) | |
tree | b252b90359dcc291e10574e97fc808fec09f564c /viewer/src/services | |
parent | 070c35fe96cec7ddb4d43f54f334739b2f996fed (diff) | |
download | ldgallery-c108a99e128c02d20930775222f6118a460f80b1.tar.gz |
viewer/services/itemComparators: make the orders linear
By using the item path as a tie-breaker.
GitHub: closes #255
Diffstat (limited to 'viewer/src/services')
-rw-r--r-- | viewer/src/services/itemComparators.ts | 33 |
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 | |||
23 | export type ItemSort = { name: Gallery.ItemSortStr; text: TranslateResult; fn: ItemComparator }; | 23 | export type ItemSort = { name: Gallery.ItemSortStr; text: TranslateResult; fn: ItemComparator }; |
24 | 24 | ||
25 | export default class ItemComparators { | 25 | export 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 | } |