feat(recipe filters): Add alphabetical sorting of keywords and categories to recipe filters

Signed-off-by: Sebastian Fey <info@sebastianfey.de>
This commit is contained in:
Sebastian Fey 2024-07-11 09:09:39 +02:00
Родитель 321c8bd1aa
Коммит 4bce54598b
2 изменённых файлов: 30 добавлений и 8 удалений

Просмотреть файл

@ -15,6 +15,7 @@ import parseSearchString from 'cookbook/js/utils/parseSearchString';
import compareRecipeFilters from 'cookbook/js/utils/compareRecipeFilters';
import { asArray } from 'cookbook/js/helper';
import { Recipe } from 'cookbook/js/Models/schema';
import { caseInsensitiveStringSort } from 'cookbook/js/utils/sortingUtils';
export default function useRecipeFilterControls(props, store) {
/**
@ -153,12 +154,12 @@ export default function useRecipeFilterControls(props, store) {
});
/**
* A unique set of all categories in the recipes.
* A unique sorted set of all categories in the recipes.
* @type {import('vue').ComputedRef<Array<string>>}
*/
const uniqueCategories: ComputedRef<string[]> = computed(() => [
...new Set(rawCategories.value),
]);
const uniqueCategories: ComputedRef<string[]> = computed(() =>
[...new Set(rawCategories.value)].sort(caseInsensitiveStringSort),
);
/**
* An array of all keywords in the recipes. These are neither sorted nor unique
@ -174,11 +175,11 @@ export default function useRecipeFilterControls(props, store) {
});
/**
* A unique set of all keywords in all recipes.
* A unique sorted set of all keywords in all recipes.
*/
const uniqueKeywords: ComputedRef<string[]> = computed(() => [
...new Set(rawKeywords.value),
]);
const uniqueKeywords: ComputedRef<string[]> = computed(() =>
[...new Set(rawKeywords.value)].sort(caseInsensitiveStringSort),
);
function onCategoriesSelectionUpdated() {
// Create new filter from current selection

Просмотреть файл

@ -0,0 +1,21 @@
/* eslint-disable import/prefer-default-export */
/**
* A function that compares two string items. Used to order strings alphabetically case-insensitively.
* @param item1
* @param item2
* @returns {-1|1|0} `1` if `item1` should be sorted behind `item2`, `-1` if `item1` should be sorted before `item2`,
* `0` otherwise.
*/
const caseInsensitiveStringSort = (
item1: string,
item2: string,
): 1 | -1 | 0 => {
const i1 = item1.toLowerCase();
const i2 = item2.toLowerCase();
if (i1 < i2) return -1;
if (i2 < i1) return 1;
return 0;
};
export { caseInsensitiveStringSort };