From 3fc90b4e93a2d02265b8f38c216e2611fe91a810 Mon Sep 17 00:00:00 2001 From: Aryan Baburajan Date: Tue, 21 May 2024 22:23:58 +0530 Subject: [PATCH 1/3] feat: add repository name formatting --- server/api/repositories.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/server/api/repositories.ts b/server/api/repositories.ts index 4b73af0..be4d69a 100644 --- a/server/api/repositories.ts +++ b/server/api/repositories.ts @@ -1,5 +1,15 @@ import { emojify } from 'node-emoji'; +function getDisplayName(name: string) { + const camelPascalSplit = name.replace(/([a-z])([A-Z])/g, '$1 $2'); + const intermediate = camelPascalSplit.replace(/[_-]/g, ' '); + const words = intermediate.split(' '); + const capitalizedWords = words.map( + (word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(), + ); + return capitalizedWords.join(' '); +} + export type Repository = { rank: number; name: string; @@ -57,7 +67,7 @@ export default defineEventHandler(async (event) => { const result: Repository[] = data.search.edges.map(({ node }: any, index: number) => ({ rank: index + 1, - name: node.name, + name: getDisplayName(node.name), ownerName: node.owner.login, image: node.owner.avatarUrl, description: emojify(node.description), From 9374cbe32cdc4727baba91f47d8832ed97e8ec2b Mon Sep 17 00:00:00 2001 From: Aryan Baburajan Date: Tue, 21 May 2024 22:52:30 +0530 Subject: [PATCH 2/3] feat: score based searching algorithm --- pages/index.vue | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/pages/index.vue b/pages/index.vue index 261fa2f..76b7185 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -15,15 +15,32 @@ const hasScrolled = ref(false); const repositories = computed(() => { if (!settings.search && !settings.languages.length) return data.value; - return data.value?.filter((repo) => { - const isLanguage = settings.languages.length - ? settings.languages.includes(repo.language?.name ?? '') - : true; - const isSearch = settings.search - ? repo.name.toLowerCase().includes(settings.search.toLowerCase()) - : true; - return isLanguage && isSearch; - }); + return data.value + ?.filter((repo) => { + const isLanguage = settings.languages.length + ? settings.languages.includes(repo.language?.name ?? '') + : true; + return isLanguage; + }) + .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); + + return scoreB - scoreA; + }); }); const hoverEffect = ref({ height: 0, top: 0, opacity: 0 }); From 04615e270d2ebb47f8ff655374822c592bcee86a Mon Sep 17 00:00:00 2001 From: Colin Lienard <64312634+colinlienard@users.noreply.github.com> Date: Wed, 22 May 2024 19:28:48 +0200 Subject: [PATCH 3/3] Update index.vue --- pages/index.vue | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/pages/index.vue b/pages/index.vue index 76b7185..1bf0bae 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -13,29 +13,32 @@ const languages = ref([]); const topOfTableRef = ref(); 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);