Skip to content

Commit

Permalink
Update index.vue
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlienard authored May 22, 2024
1 parent 9374cbe commit 04615e2
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,32 @@ const languages = ref<string[]>([]);
const topOfTableRef = ref<HTMLElement>();
const hasScrolled = ref(false);
function calculateMatchScore(repoName: string, searchTerm: string) {
let score = 0;
let index = 0;
for (const char of searchTerm.toLowerCase()) {
index = repoName.toLowerCase().indexOf(char, index);
if (index === -1) break;
score++;
index++;
}
return score;
}
const repositories = computed(() => {
if (!settings.search && !settings.languages.length) return data.value;
const searchTerm = settings.search.toLowerCase();
return data.value
?.filter((repo) => {
const isLanguage = settings.languages.length
? settings.languages.includes(repo.language?.name ?? '')
: true;
return isLanguage;
const isSearch = calculateMatchScore(repo.name, searchTerm) >= searchTerm.length;
return isLanguage && isSearch;
})
.sort((a, b) => {
const calculateMatchScore = (repoName, searchTerm) => {
let score = 0;
let index = 0;
for (let char of searchTerm.toLowerCase()) {
index = repoName.toLowerCase().indexOf(char, index);
if (index === -1) break;
score++;
index++;
}
return score;
};
const searchTerm = settings.search.toLowerCase();
const scoreA = calculateMatchScore(a.name, searchTerm);
const scoreB = calculateMatchScore(b.name, searchTerm);
Expand Down

0 comments on commit 04615e2

Please sign in to comment.