-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpackage.ts
More file actions
20 lines (17 loc) · 635 Bytes
/
package.ts
File metadata and controls
20 lines (17 loc) · 635 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { PackageInfo } from './api/package'
import maxSatisfying from 'semver/ranges/max-satisfying'
/**
* Encode a package name for use in npm registry URLs.
* Handles scoped packages (e.g., @scope/name -> @scope%2Fname).
*/
export function encodePackageName(name: string): string {
if (name.startsWith('@')) {
return `@${encodeURIComponent(name.slice(1))}`
}
return encodeURIComponent(name)
}
export function resolveExactVersion(pkg: PackageInfo | undefined, version: string) {
if (!pkg?.distTags)
return null
return pkg.distTags[version] ?? maxSatisfying(Object.keys(pkg.versionsMeta ?? {}), version)
}