Skip to content

Instantly share code, notes, and snippets.

@aMarCruz
Created September 1, 2025 19:59
Show Gist options
  • Select an option

  • Save aMarCruz/95175cfa57caf8bfcb5f3727a189c257 to your computer and use it in GitHub Desktop.

Select an option

Save aMarCruz/95175cfa57caf8bfcb5f3727a189c257 to your computer and use it in GitHub Desktop.
Test that `semver.satisfies` performs a conversion from `#` to `#.x.x` and not a coercion to `#.0.0`
import semver from 'semver'
const SEMVER1 = '1.2.0'
const SEMVER2 = '1.2.8'
const tests = [
'1', // === '1.x.x'
'1.2',
'1.2.0',
'1.2.x',
'=1',
'=1.2',
'=1.2.0',
'=1.2.x',
'~1', // === '1.x.x' ?
'~1.2',
'~1.2.0', // === '1.2.x'
'^1',
'^1.2',
'^1.2.0',
'<1',
'<1.2',
'<1.2.0',
'<=1',
'<=1.2',
'<=1.2.0',
'>1',
'>1.2',
'>1.2.0',
'>=1',
'>=1.2',
'>=1.2.0',
] as const
const showit = (x: string) => {
const test: [string, string][] = tests.map((c) => [x, c])
for (const [version, condition] of test) {
const spc = ''.padEnd(9 - condition.length)
const result = semver.satisfies(version, condition)
console.log(`semver.satisfies('${version}', '${condition}')${spc}// ${result}`)
}
}
console.log(showit(SEMVER1))
console.log('so...')
console.log(showit(SEMVER2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment