Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions src/components/composables/ModFiltersComposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SortingStyle from '../../model/enums/SortingStyle';
import ThunderstoreMod from '../../model/ThunderstoreMod';
import { getStore } from '../../providers/generic/store/StoreProvider';
import { State } from '../../store';
import SearchUtils from '../../utils/SearchUtils';
import SearchUtils, { ExactSearchMatchRank } from '../../utils/SearchUtils';

const store = getStore<State>();

Expand All @@ -15,7 +15,7 @@ const filteredMods = ref<ThunderstoreMod[]>([]);
const filteredModCount = ref(0);

function runFilter() {
let result = sortedMods.value;
let result = sortedMods.value as ThunderstoreMod[];

const searchKeys = SearchUtils.makeKeys(searchFilter.value);
if (searchKeys.length > 0) {
Expand Down Expand Up @@ -44,10 +44,53 @@ function runFilter() {
result = result.filter(x => filterAll.every(c => x.getCategories().includes(c)));
}

if (store.state.modFilters.sortBehaviour === SortingStyle.RELEVANCE) {
result = bumpExactMatches(result, searchFilter.value.trim().toLowerCase());
}

filteredMods.value = result;
filteredModCount.value = result.length;
}

function bumpExactMatches(mods: ThunderstoreMod[], query: string): ThunderstoreMod[] {
if (query.length === 0) {
return mods;
}

const nameMatches: ThunderstoreMod[] = [];
const authorMatches: ThunderstoreMod[] = [];
const softNameMatches: ThunderstoreMod[] = [];
const softAuthorMatches: ThunderstoreMod[] = [];
const others: ThunderstoreMod[] = [];

for (const mod of mods) {
const rank = SearchUtils.getExactMatchRank(query, mod.getName(), mod.getFullName(), mod.getOwner());
switch (rank) {
case ExactSearchMatchRank.NAME:
nameMatches.push(mod);
break;
case ExactSearchMatchRank.AUTHOR:
authorMatches.push(mod);
break;
case ExactSearchMatchRank.SOFT_NAME:
softNameMatches.push(mod);
break;
case ExactSearchMatchRank.SOFT_AUTHOR:
softAuthorMatches.push(mod);
break;
default:
others.push(mod);
break;
}
}

const relevanceMatches = [...nameMatches, ...authorMatches, ...softNameMatches, ...softAuthorMatches];
if (relevanceMatches.length === 0) {
return mods;
}
return [...relevanceMatches, ...others];
}

function runSort() {
const sortDescending = store.state.modFilters.sortDirection === SortDirection.STANDARD;
const sorted = [...store.state.tsMods.mods];
Expand All @@ -66,7 +109,7 @@ function runSort() {
case SortingStyle.RATING:
result = a.getRating() < b.getRating();
break;
default:
default: // case SortingStyle.RELEVANCE
result = true;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/enums/SortingStyle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
DEFAULT: 'Default',
RELEVANCE: 'Relevance',
LAST_UPDATED: 'Last updated',
ALPHABETICAL: 'Alphabetical',
DOWNLOADS: 'Download count',
RATING: 'Rating',
};
};
4 changes: 2 additions & 2 deletions src/store/modules/ModFilterModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
selectedCategoriesToExclude: [],
showDeprecatedPackages: false,
sortDirection: SortDirection.STANDARD,
sortBehaviour: SortingStyle.DEFAULT
sortBehaviour: SortingStyle.RELEVANCE
}),

getters: <GetterTree<State, RootState>>{
Expand All @@ -49,7 +49,7 @@ export default {
state.selectedCategoriesCompareAll = [];
state.selectedCategoriesToExclude = [];
state.sortDirection = SortDirection.STANDARD;
state.sortBehaviour = SortingStyle.DEFAULT;
state.sortBehaviour = SortingStyle.RELEVANCE;
},

selectCategoryToCompareOne: function(state: State, category: string) {
Expand Down
42 changes: 40 additions & 2 deletions src/utils/SearchUtils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
export enum ExactSearchMatchRank {
NONE = 0,
AUTHOR = 1,
NAME = 2,
SOFT_AUTHOR = 3,
SOFT_NAME = 4,
}

const softMatcher = new RegExp(' |_', 'g');

export default class SearchUtils {
public static makeKeys(search: string) {
return search.trim().toLowerCase().split(' ');
console.log(search.trim().toLowerCase().split(softMatcher));
return search.trim().toLowerCase().split(softMatcher);
}

public static isSearched(keys: string[], name: string, description?: string) {
name = name.toLowerCase();
name = name.replaceAll(softMatcher, '').toLowerCase();
if (description) {
description = description.toLowerCase();
} else {
description = '';
}
return keys.every(i => name.indexOf(i) >= 0 || description.indexOf(i) >= 0);
}

public static getExactMatchRank(query: string, name: string, fullName: string, author: string): ExactSearchMatchRank {
if (query.length === 0) {
return ExactSearchMatchRank.NONE;
}
if (name.toLowerCase() === query || fullName.toLowerCase() === query) {
return ExactSearchMatchRank.NAME;
}
if (author.toLowerCase() === query) {
return ExactSearchMatchRank.AUTHOR;
}

const softQuery = query.replaceAll(softMatcher, '').toLowerCase();
const softName = name.replaceAll(softMatcher, '');
const softFullName = fullName.replaceAll(softMatcher, '');
const softAuthor = author.replaceAll(softMatcher, '');

if (softName.toLowerCase() === softQuery || softFullName.toLowerCase() === softQuery) {
console.log('Soft name match');
return ExactSearchMatchRank.SOFT_NAME;
}
if (softAuthor.toLowerCase() === softQuery) {
return ExactSearchMatchRank.SOFT_AUTHOR;
}

return ExactSearchMatchRank.NONE;
}
}
Loading