Skip to content

Commit 6ad2761

Browse files
committed
refactor: extract parseNpmAlias to app/utils/npm/alias.ts
1 parent bd33d4a commit 6ad2761

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

app/components/Package/Dependencies.vue

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import { SEVERITY_TEXT_COLORS, getHighestSeverity } from '#shared/utils/severity'
33
import { getOutdatedTooltip, getVersionClass } from '~/utils/npm/outdated-dependencies'
4+
import { parseNpmAlias } from '~/utils/npm/alias'
45
56
const { t } = useI18n()
67
@@ -91,18 +92,6 @@ function getDepVersionClass(dep: string) {
9192
9293
const numberFormatter = useNumberFormatter()
9394
94-
/**
95-
* Parses npm alias syntax: "npm:real-pkg@^1.0.0"
96-
* Returns { name, range } for the real package, or null if not an alias.
97-
*/
98-
function parseNpmAlias(version: string): { name: string; range: string } | null {
99-
if (!version.startsWith('npm:')) return null
100-
const spec = version.slice(4) // strip 'npm:'
101-
// Handle scoped packages like @scope/pkg@1.0.0 — find @ after position 0
102-
const atIdx = spec.startsWith('@') ? spec.indexOf('@', 1) : spec.indexOf('@')
103-
if (atIdx === -1) return { name: spec, range: '' }
104-
return { name: spec.slice(0, atIdx), range: spec.slice(atIdx + 1) }
105-
}
10695
</script>
10796

10897
<template>

app/utils/npm/alias.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Parses npm alias syntax: "npm:real-pkg@^1.0.0"
3+
* Returns { name, range } for the real package, or null if not an alias.
4+
*
5+
* @example
6+
* parseNpmAlias('npm:real-pkg@^1.0.0') // { name: 'real-pkg', range: '^1.0.0' }
7+
* parseNpmAlias('npm:@scope/pkg@^1.0.0') // { name: '@scope/pkg', range: '^1.0.0' }
8+
* parseNpmAlias('npm:real-pkg') // { name: 'real-pkg', range: '' }
9+
* parseNpmAlias('^1.0.0') // null
10+
*/
11+
export function parseNpmAlias(version: string): { name: string; range: string } | null {
12+
if (!version.startsWith('npm:')) return null
13+
const spec = version.slice(4) // strip 'npm:'
14+
// Handle scoped packages like @scope/pkg@1.0.0 — find @ after position 0
15+
const atIdx = spec.startsWith('@') ? spec.indexOf('@', 1) : spec.indexOf('@')
16+
if (atIdx === -1) return { name: spec, range: '' }
17+
return { name: spec.slice(0, atIdx), range: spec.slice(atIdx + 1) }
18+
}

0 commit comments

Comments
 (0)