Skip to content

Commit cd01091

Browse files
committed
feat: parse search query to extact essential package info
1 parent c52d259 commit cd01091

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

app/pages/search.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ const updateUrlPage = debounce((page: number) => {
3333
const { model: searchQuery, provider: searchProvider } = useGlobalSearch()
3434
const query = computed(() => searchQuery.value)
3535
36+
// Parses the raw search query to extract package info, such as scope, name, version.
37+
// Uses this info to provide a strippedQuery (the query, but without version info) that is used for searching.
38+
const parsedQuery = computed(() => {
39+
const q = query.value.trim()
40+
// Regex matches a (un)scoped package and optionally extracts versioning info using the following syntax: @scope/specifier@version
41+
// It makes use of 4 capture groups to extract this info.
42+
const match = q.match(/^(?:@([^/]+)\/)?([^/@ ]+)(?:@([^ ]*))?(.*)/)
43+
if (!match) return { scope: null, name: q, version: null, strippedQuery: q }
44+
45+
const [, scope, specifier, version, trailing] = match
46+
// Reconstruct the query without the version info, essentially stripping the version data:
47+
// anything directly after the @ for the version specifier is stripped.
48+
const name = scope ? `@${scope}/${specifier}` : (specifier ?? '')
49+
const strippedQuery = `${name} ${trailing ?? ''}`.trim()
50+
51+
return { scope: scope ?? null, name: name, version: version || null, strippedQuery }
52+
})
53+
3654
// Track if page just loaded (for hiding "Searching..." during view transition)
3755
const hasInteracted = shallowRef(false)
3856
onMounted(() => {

0 commit comments

Comments
 (0)