Skip to content

Commit 123a992

Browse files
committed
refactor: update regex query to use named capture groups
1 parent c32e845 commit 123a992

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

app/pages/search.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ const parsedQuery = computed(() => {
3939
const q = query.value.trim()
4040
// Regex matches a (un)scoped package and optionally extracts versioning info using the following syntax: @scope/specifier@version
4141
// It makes use of 4 capture groups to extract this info.
42-
const match = q.match(/^(?:@([^/]+)\/)?([^/@ ]+)(?:@([^ ]*))?(.*)/)
42+
const match = q.match(
43+
/^(?:@(?<scope>[^/]+)\/)?(?<specifier>[^/@ ]+)(?:@(?<version>[^ ]*))?(?<trailing>.*)/,
44+
)
4345
if (!match) return { scope: null, name: q, version: null, strippedQuery: q }
4446
45-
const [, scope, specifier, version, trailing] = match
47+
const { scope, specifier, version, trailing } = match.groups ?? {}
4648
// Reconstruct the query without the version info, essentially stripping the version data:
4749
// anything directly after the @ for the version specifier is stripped.
4850
const name = scope ? `@${scope}/${specifier}` : (specifier ?? '')

0 commit comments

Comments
 (0)