Skip to content

Commit e2965ca

Browse files
committed
✨ feat: strutil - add new func: NumVersion for get version
1 parent 390ec5d commit e2965ca

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

strutil/parse.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package strutil
22

33
import (
44
"errors"
5+
"regexp"
56
"strconv"
67
"strings"
78
"time"
@@ -10,6 +11,13 @@ import (
1011
"github.com/gookit/goutil/byteutil"
1112
)
1213

14+
var regNumVersion = regexp.MustCompile(`[0-9][\d.]+([_-]\d+)?`)
15+
16+
// NumVersion parse input string, get valid number version. eg: go-1.22.3 -> 1.22.3
17+
func NumVersion(s string) string {
18+
return regNumVersion.FindString(s)
19+
}
20+
1321
// MustToTime convert date string to time.Time
1422
func MustToTime(s string, layouts ...string) time.Time {
1523
t, err := ToTime(s, layouts...)

strutil/parse_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import (
99
"github.com/gookit/goutil/timex"
1010
)
1111

12+
func TestGetVersion(t *testing.T) {
13+
is := assert.New(t)
14+
is.Eq("1.2.0", strutil.NumVersion("v1.2.0"))
15+
is.Eq("3.19.6", strutil.NumVersion("flutter-3.19.6"))
16+
is.Eq("3.19.6.3", strutil.NumVersion("flutter-3.19.6.3"))
17+
is.Eq("1.8.0_442", strutil.NumVersion("corretto-1.8.0_442"))
18+
is.Eq("1.8.0-442", strutil.NumVersion("corretto-1.8.0-442"))
19+
is.Eq("20.16.0", strutil.NumVersion("node-v20.16.0-win-x64"))
20+
}
21+
1222
func TestToTime(t *testing.T) {
1323
is := assert.New(t)
1424

0 commit comments

Comments
 (0)