Skip to content

Commit

Permalink
Refactor code by simplifying it
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Dec 2, 2024
1 parent ec1a6b6 commit 389f05a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
4 changes: 1 addition & 3 deletions constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (c *Constraint) Check(v *Version) bool {
// Prerelease returns true if the version underlying this constraint
// contains a prerelease field.
func (c *Constraint) Prerelease() bool {
return len(c.check.Prerelease()) > 0
return c.check.Prerelease() != ""
}

func (c *Constraint) String() string {
Expand Down Expand Up @@ -216,9 +216,7 @@ func prereleaseCheck(v, c *Version) bool {
return true
}

//-------------------------------------------------------------------
// Constraint functions
//-------------------------------------------------------------------

type operator rune

Expand Down
24 changes: 12 additions & 12 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func newVersion(v string, pattern *regexp.Regexp) (*Version, error) {
for i, str := range segmentsStr {
val, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return nil, fmt.Errorf(
"Error parsing version: %s", err)
return nil, fmt.Errorf("Error parsing version: %s", err)
}

segments[i] = val
Expand Down Expand Up @@ -206,7 +205,7 @@ func allZero(segs []int64) bool {
return true
}

func comparePart(preSelf string, preOther string) int {
func comparePart(preSelf, preOther string) int {
if preSelf == preOther {
return 0
}
Expand Down Expand Up @@ -240,20 +239,21 @@ func comparePart(preSelf string, preOther string) int {
return -1
}

if selfNumeric && !otherNumeric {
switch {
case selfNumeric && !otherNumeric:
return -1
} else if !selfNumeric && otherNumeric {
case !selfNumeric && otherNumeric:
return 1
} else if !selfNumeric && !otherNumeric && preSelf > preOther {
case !selfNumeric && !otherNumeric && preSelf > preOther:
return 1
} else if selfInt > otherInt {
case selfInt > otherInt:
return 1
default:
return -1
}

return -1
}

func comparePrereleases(v string, other string) int {
func comparePrereleases(v, other string) int {
// the same pre release!
if v == other {
return 0
Expand All @@ -272,7 +272,7 @@ func comparePrereleases(v string, other string) int {
}

// loop for parts to find the first difference
for i := 0; i < biggestLen; i = i + 1 {
for i := 0; i < biggestLen; i++ {
partSelfPre := ""
if i < selfPreReleaseLen {
partSelfPre = selfPreReleaseMeta[i]
Expand Down Expand Up @@ -389,7 +389,7 @@ func (v *Version) String() string {
str := strconv.FormatInt(s, 10)
fmtParts[i] = str
}
fmt.Fprintf(&buf, strings.Join(fmtParts, "."))
fmt.Fprint(&buf, strings.Join(fmtParts, "."))
if v.pre != "" {
fmt.Fprintf(&buf, "-%s", v.pre)
}
Expand Down

0 comments on commit 389f05a

Please sign in to comment.