Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code by simplifying it #136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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