-
Notifications
You must be signed in to change notification settings - Fork 4
/
CompareSemVer.go
52 lines (42 loc) · 1.09 KB
/
CompareSemVer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package utils
import (
"strconv"
"strings"
)
// CompareSemVer returns 1 if the semantic version `newVer` is greater than
// `oldVer` and -1 otherwise. A semantic version is one that follows the
// format "1.2.3". If either oldVer or newVer startss with the letter "v",
// it will be stripped before comparison, along with any hyphenenated (-)
// suffix. (e.g: "v0.1-alpha" becomes "0.1")
func CompareSemVer(oldVer, newVer string) int {
// any version is better than no version
if newVer == "" {
return -1
}
if oldVer == "" {
return 1
}
// strip "v" prefixes
if newVer[0] == 'v' {
newVer = newVer[1:]
}
if oldVer[0] == 'v' {
oldVer = oldVer[1:]
}
nparts := strings.Split(strings.Split(newVer, "-")[0], ".")
oparts := strings.Split(strings.Split(oldVer, "-")[0], ".")
// compare each part
for i := range nparts {
if len(oparts)-1 < i {
// newVer is longer
return 1
}
// parts should be numeric, otherwise they are skipped
n, err1 := strconv.Atoi(nparts[i])
o, err2 := strconv.Atoi(oparts[i])
if err1 == nil && err2 == nil && n > o {
return 1
}
}
return -1
}