Skip to content

Commit a8515b9

Browse files
authored
allow searching by GitHub URL and owner name (#1605)
1 parent 5db1e0a commit a8515b9

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

util/search.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { Library, Query } from '~/types';
2+
13
import { getNewArchSupportStatus, NewArchSupportStatus } from './newArchStatus';
24
import { relevance } from './sorting';
35
import { isEmptyOrNull } from './strings';
4-
import { Library, Query } from '../types';
6+
7+
const NPM_NAME_CLEANUP_REGEX = /[-/]/g;
8+
const GITHUB_URL_CLEANUP_REGEX =
9+
/^https?:\/\/(?:www\.)?github\.com\/([^/]+\/[^/]+)(?:$|\/|\.git).*$/;
510

611
function calculateMatchScore(
7-
{ github, npmPkg, topicSearchString, unmaintained }: Library,
12+
{ github, npmPkg, topicSearchString, unmaintained, githubUrl }: Library,
813
querySearch: string
914
) {
1015
const exactNameMatchPoints =
@@ -15,10 +20,17 @@ function calculateMatchScore(
1520

1621
const npmPkgNameMatchPoints =
1722
!isEmptyOrNull(npmPkg) &&
18-
(npmPkg.includes(querySearch) || npmPkg.replaceAll(/[-/]/g, ' ').includes(querySearch))
23+
(npmPkg.includes(querySearch) ||
24+
npmPkg.replaceAll(NPM_NAME_CLEANUP_REGEX, ' ').includes(querySearch))
1925
? 200
2026
: 0;
2127

28+
const gitHubURLOrOwnerMatchPoints =
29+
githubUrl.startsWith(querySearch) ||
30+
githubUrl.replace(GITHUB_URL_CLEANUP_REGEX, '$1').includes(querySearch)
31+
? 150
32+
: 0;
33+
2234
const cleanedUpName = npmPkg
2335
.replace('react-native', '')
2436
.replace('react', '')
@@ -28,7 +40,7 @@ function calculateMatchScore(
2840
const cleanedUpNameMatchPoints =
2941
!isEmptyOrNull(cleanedUpName) &&
3042
(cleanedUpName.includes(querySearch) ||
31-
cleanedUpName.includes(querySearch.replaceAll(/[-/]/g, ' ')))
43+
cleanedUpName.includes(querySearch.replaceAll(NPM_NAME_CLEANUP_REGEX, ' ')))
3244
? 100
3345
: 0;
3446

@@ -47,6 +59,7 @@ function calculateMatchScore(
4759
repoNameMatchPoints +
4860
cleanedUpNameMatchPoints +
4961
npmPkgNameMatchPoints +
62+
gitHubURLOrOwnerMatchPoints +
5063
descriptionMatchPoints +
5164
topicMatchPoints;
5265

@@ -90,7 +103,7 @@ export function handleFilterLibraries({
90103
}))
91104
: libraries;
92105

93-
const fiteredLibraries = processedLibraries.filter(library => {
106+
const filteredLibraries = processedLibraries.filter(library => {
94107
let isTopicMatch = false;
95108
let isSearchMatch = false;
96109

@@ -236,10 +249,10 @@ export function handleFilterLibraries({
236249
});
237250

238251
if (sortBy === 'relevance') {
239-
return relevance(fiteredLibraries);
252+
return relevance(filteredLibraries);
240253
}
241254

242-
return fiteredLibraries;
255+
return filteredLibraries;
243256
}
244257

245258
export function getPageQuery(basePath: string, query: Query) {

0 commit comments

Comments
 (0)