Skip to content
Open
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
36 changes: 34 additions & 2 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,42 @@ function runFilter() {
result = result.filter(x => filterAll.every(c => x.getCategories().includes(c)));
}

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 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;
default:
others.push(mod);
break;
}
}

if (nameMatches.length === 0 && authorMatches.length === 0) {
return mods;
}
return [...nameMatches, ...authorMatches, ...others];
}

function runSort() {
const sortDescending = store.state.modFilters.sortDirection === SortDirection.STANDARD;
const sorted = [...store.state.tsMods.mods];
Expand Down
19 changes: 19 additions & 0 deletions src/utils/SearchUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export enum ExactSearchMatchRank {
NONE = 0,
AUTHOR = 1,
NAME = 2,
}

export default class SearchUtils {
public static makeKeys(search: string) {
return search.trim().toLowerCase().split(' ');
Expand All @@ -8,4 +14,17 @@ export default class SearchUtils {
description = description.toLowerCase();
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;
}
return ExactSearchMatchRank.NONE;
}
}
Loading