Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 1 addition & 25 deletions app/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import { compare, isValid, normalizeRange, satisfies, tryParse } from 'verkit'
import { compare, normalizeRange, satisfies, tryParse } from 'verkit'

/**
* Utilities for handling npm package versions and dist-tags
*/

/**
* Check if a version string is an exact semver version.
* Returns true for "1.2.3", "1.0.0-beta.1", etc.
* Returns false for ranges like "^1.2.3", ">=1.0.0", tags like "latest", etc.
* @param version - The version string to check
* @returns true if the version is an exact semver version
*/
export function isExactVersion(version: string): boolean {
return isValid(version)
}

/** Parsed semver version components */
export interface ParsedVersion {
major: number
Expand Down Expand Up @@ -121,19 +110,6 @@ export function compareVersionGroupKeys(a: string, b: string): number {
return (minorB ?? -1) - (minorA ?? -1)
}

/**
* Sort tags with 'latest' first, then alphabetically
* @param tags - Array of tag names
* @returns New sorted array
*/
export function sortTags(tags: string[]): string[] {
return [...tags].sort((a, b) => {
if (a === 'latest') return -1
if (b === 'latest') return 1
return a.localeCompare(b)
})
}

/**
* Build a map from version strings to their associated dist-tags
* Handles the case where multiple tags point to the same version
Expand Down
59 changes: 0 additions & 59 deletions test/unit/app/utils/versions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,10 @@ import {
getPrereleaseChannel,
getVersionGroupKey,
getVersionGroupLabel,
isExactVersion,
isSameVersionGroup,
parseStableVersion,
sortTags,
} from '~/utils/versions'

describe('isExactVersion', () => {
it('returns true for stable versions', () => {
expect(isExactVersion('1.0.0')).toBe(true)
expect(isExactVersion('0.1.0')).toBe(true)
expect(isExactVersion('10.20.30')).toBe(true)
})

it('returns true for prerelease versions', () => {
expect(isExactVersion('1.0.0-beta.1')).toBe(true)
expect(isExactVersion('1.0.0-alpha.0')).toBe(true)
expect(isExactVersion('5.8.0-rc')).toBe(true)
})

it('returns false for ranges', () => {
expect(isExactVersion('^1.0.0')).toBe(false)
expect(isExactVersion('~1.0.0')).toBe(false)
expect(isExactVersion('>=1.0.0')).toBe(false)
expect(isExactVersion('1.0.x')).toBe(false)
expect(isExactVersion('*')).toBe(false)
})

it('returns false for dist-tags', () => {
expect(isExactVersion('latest')).toBe(false)
expect(isExactVersion('next')).toBe(false)
expect(isExactVersion('beta')).toBe(false)
})

it('returns false for invalid strings', () => {
expect(isExactVersion('')).toBe(false)
expect(isExactVersion('not-a-version')).toBe(false)
})
})

describe('getPrereleaseChannel', () => {
it('returns empty string for stable versions', () => {
expect(getPrereleaseChannel('1.0.0')).toBe('')
Expand Down Expand Up @@ -79,30 +44,6 @@ describe('getPrereleaseChannel', () => {
})
})

describe('sortTags', () => {
it('puts latest first', () => {
expect(sortTags(['beta', 'latest', 'alpha'])).toEqual(['latest', 'alpha', 'beta'])
})

it('sorts alphabetically when no latest', () => {
expect(sortTags(['beta', 'canary', 'alpha'])).toEqual(['alpha', 'beta', 'canary'])
})

it('handles single tag', () => {
expect(sortTags(['latest'])).toEqual(['latest'])
})

it('handles empty array', () => {
expect(sortTags([])).toEqual([])
})

it('does not mutate original array', () => {
const original = ['beta', 'latest']
sortTags(original)
expect(original).toEqual(['beta', 'latest'])
})
})

describe('buildVersionToTagsMap', () => {
it('builds map from simple dist-tags', () => {
const distTags = {
Expand Down
Loading