diff --git a/makepurl.go b/makepurl.go index ac3cb8f..7beee98 100644 --- a/makepurl.go +++ b/makepurl.go @@ -8,8 +8,8 @@ import ( ) // CleanVersion returns plain versions unchanged. For version constraints, it -// uses the vers library to extract the minimum bound. If parsing fails, it -// returns the original string. +// uses the vers library to extract the lowest included version. If no included +// minimum can be identified, it returns the original string. func CleanVersion(version, scheme string) string { if version == "" { return "" @@ -19,13 +19,12 @@ func CleanVersion(version, scheme string) string { } r, err := vers.ParseNative(version, scheme) - if err != nil || len(r.Intervals) == 0 { + if err != nil { return version } - // Return the minimum bound from the first interval - if r.Intervals[0].Min != "" { - return r.Intervals[0].Min + if minimum, ok := r.MinimumVersion(); ok { + return minimum } return version diff --git a/makepurl_test.go b/makepurl_test.go index 9d68d81..bc96811 100644 --- a/makepurl_test.go +++ b/makepurl_test.go @@ -16,6 +16,8 @@ func TestCleanVersion(t *testing.T) { {"~1.0.0", "npm", "1.0.0"}, {">=1.0.0", "npm", "1.0.0"}, {">=1.0.0 <2.0.0", "npm", "1.0.0"}, + {">1.0.0", "npm", ">1.0.0"}, + {"2.0.0 || 1.0.0", "npm", "1.0.0"}, // gem constraints {"~> 1.0", "gem", "1.0"}, @@ -27,6 +29,7 @@ func TestCleanVersion(t *testing.T) { // maven constraints {"[1.0,2.0)", "maven", "1.0"}, + {"(1.0,2.0]", "maven", "(1.0,2.0]"}, // cargo constraints {"^1.0.0", "cargo", "1.0.0"}, @@ -66,6 +69,7 @@ func TestBuildPURLString(t *testing.T) { want string }{ {"simple npm", "npm", "lodash", "4.17.21", "", "pkg:npm/lodash@4.17.21"}, + {"exclusive npm constraint", "npm", "lodash", ">1.0.0", "", "pkg:npm/lodash@%3E1.0.0"}, {"scoped npm", "npm", "@babel/core", "7.20.0", "", "pkg:npm/%40babel/core@7.20.0"}, // @ encoded in namespace {"gem", "rubygems", "rails", "7.0.0", "", "pkg:gem/rails@7.0.0"}, {"pypi", "pypi", "requests", "2.28.0", "", "pkg:pypi/requests@2.28.0"},