From 8e33714158520f21d6cf72df24e482fbab96b859 Mon Sep 17 00:00:00 2001 From: Icaruk Date: Fri, 2 Aug 2024 23:42:03 +0200 Subject: [PATCH] fix: getCleanVersion with corrupted version "1" or "1.2" instead "1.2.3" --- package3.json | 2 +- pkg/utils/version/getCleanVersion.go | 24 +++++++++++++++++++---- pkg/utils/version/getCleanVersion_test.go | 10 ++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/package3.json b/package3.json index 5944f48..eca0d57 100644 --- a/package3.json +++ b/package3.json @@ -9,7 +9,7 @@ "node": "20.5.0" }, "dependencies": { - "@dytesdk/react-ui-kit": "^0.0.1" + "surrealdb.js": "^0.11.1" }, "volta": { "node": "20.5.0" diff --git a/pkg/utils/version/getCleanVersion.go b/pkg/utils/version/getCleanVersion.go index 3f90ff6..c52f07c 100644 --- a/pkg/utils/version/getCleanVersion.go +++ b/pkg/utils/version/getCleanVersion.go @@ -1,6 +1,8 @@ package version -import "regexp" +import ( + "regexp" +) func GetCleanVersion(version string) (string, string) { @@ -8,11 +10,25 @@ func GetCleanVersion(version string) (string, string) { return "", "" } - re := regexp.MustCompile(`([^0-9]*)(\d+\.\d+\.\d+)(.*)`) - reSubmatch := re.FindStringSubmatch(version) + re := regexp.MustCompile(`([^0-9]*)(\d?)\.?(\d?)\.?(\d?)(.*)`) + reSubmatch := re.FindStringSubmatch(version) // [0] all, [1] = prefix, [2] = major, [3] = minor, [4] = patch prefix := reSubmatch[1] - cleanVersion := reSubmatch[2] + major := reSubmatch[2] + minor := reSubmatch[3] + patch := reSubmatch[4] + + if major == "" { + major = "0" + } + if minor == "" { + minor = "0" + } + if patch == "" { + patch = "0" + } + + cleanVersion := major + "." + minor + "." + patch return prefix, cleanVersion } diff --git a/pkg/utils/version/getCleanVersion_test.go b/pkg/utils/version/getCleanVersion_test.go index ea671ae..b8b8ce9 100644 --- a/pkg/utils/version/getCleanVersion_test.go +++ b/pkg/utils/version/getCleanVersion_test.go @@ -35,6 +35,16 @@ func TestGetCleanVersion(t *testing.T) { expectedPrefix: "^", expectedVersion: "1.2.3", }, + { + version: "^1", + expectedPrefix: "^", + expectedVersion: "1.0.0", + }, + { + version: "^1.2", + expectedPrefix: "^", + expectedVersion: "1.2.0", + }, } for _, tc := range testCases {