diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3402915 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +GO_FILES?=$$(find . -name '*.go' | grep -v vendor) + +fmt: + @echo "Formatting files" + gofmt -w $(GO_FILES) + goimports -w $(GO_FILES) + +pre-commit-hook: + @ln -s scripts/pre-commit .git/hooks/pre-commit + @echo "hook installed." + +test: + go test -v ./... \ No newline at end of file diff --git a/go.mod b/go.mod index aca3d12..8d60f12 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,9 @@ module github.com/unidev-platform/golang-core go 1.17 require github.com/stretchr/testify v1.7.0 + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/scripts/pre-commit b/scripts/pre-commit new file mode 100755 index 0000000..e9d996a --- /dev/null +++ b/scripts/pre-commit @@ -0,0 +1 @@ +make fmt \ No newline at end of file diff --git a/xcollection/xcollection.go b/xcollection/xcollection.go index 0dd5db1..edc2704 100644 --- a/xcollection/xcollection.go +++ b/xcollection/xcollection.go @@ -2,8 +2,8 @@ package xcollection import "math/rand" -// StringBoolMapKeys - extract map keys as slice -func StringBoolMapKeys(m map[string]bool) []string { +// MapKeys - extract map keys as slice +func MapKeys(m map[string]bool) []string { keys := make([]string, 0, len(m)) for kk := range m { keys = append(keys, kk) @@ -11,8 +11,8 @@ func StringBoolMapKeys(m map[string]bool) []string { return keys } -// StringRandomElement - fetch random element from string slice -func StringRandomElement(slice []string ) string { +// Random - fetch random element from string slice +func Random(slice []string) string { randomIndex := rand.Intn(len(slice)) return slice[randomIndex] } diff --git a/xcollection/xcollection_test.go b/xcollection/xcollection_test.go index 6f1a9bb..9d50f1a 100644 --- a/xcollection/xcollection_test.go +++ b/xcollection/xcollection_test.go @@ -1,16 +1,17 @@ package xcollection import ( - "github.com/stretchr/testify/assert" "log" "math/rand" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestStringBoolMapKeys(t *testing.T) { - keys := StringBoolMapKeys(map[string]bool{ + keys := MapKeys(map[string]bool{ "qwe": true, "123": true, }) @@ -20,7 +21,7 @@ func TestStringBoolMapKeys(t *testing.T) { func TestRandomSelection(t *testing.T) { rand.Seed(time.Now().Unix()) - element := StringRandomElement([]string{"1", "2", "3", "4", "5", "6"}) + element := Random([]string{"1", "2", "3", "4", "5", "6"}) log.Printf("Random element: %s", element) assert.NotNil(t, element) } diff --git a/xenv/xenv_test.go b/xenv/xenv_test.go index 820842f..9705871 100644 --- a/xenv/xenv_test.go +++ b/xenv/xenv_test.go @@ -1,8 +1,9 @@ package xenv import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestFetchingCurrentUser(t *testing.T) { diff --git a/xfiles/xfiles.go b/xfiles/xfiles.go index c372e80..9e06f32 100644 --- a/xfiles/xfiles.go +++ b/xfiles/xfiles.go @@ -2,13 +2,14 @@ package xfiles import ( "bufio" - "github.com/unidev-platform/golang-core/xcollection" "os" "strings" + + "github.com/unidev-platform/golang-core/xcollection" ) -// ReadDistinctFileLines - Read text file lines as slice without empty and duplicates -func ReadDistinctFileLines(path string)([]string, error) { +// Distinct - Read text file lines as slice without empty and duplicates +func Distinct(path string) ([]string, error) { var linesMap = make(map[string]bool) file, err := os.Open(path) @@ -25,6 +26,6 @@ func ReadDistinctFileLines(path string)([]string, error) { } } - return xcollection.StringBoolMapKeys(linesMap), scanner.Err() + return xcollection.MapKeys(linesMap), scanner.Err() } diff --git a/xfiles/xfiles_test.go b/xfiles/xfiles_test.go index 538a13b..54e78b3 100644 --- a/xfiles/xfiles_test.go +++ b/xfiles/xfiles_test.go @@ -1,12 +1,13 @@ package xfiles import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestFileLinesExtraction(t *testing.T) { - lines, err := ReadDistinctFileLines("distinct_file_lines_test.txt") + lines, err := Distinct("distinct_file_lines_test.txt") if err != nil { panic(err) } diff --git a/xstring/xstrings.go b/xstring/xstrings.go index 2851cd5..f35d274 100644 --- a/xstring/xstrings.go +++ b/xstring/xstrings.go @@ -1,10 +1,31 @@ package xstring -func ExtractStringBetween(rawString string,begin string, end string) []string { +import ( + "log" + "strings" +) +// Between - extract from input string all substrings located between begin and end. +// Useful for extracting data between HTML tags. +func Between(input string, begin string, end string) []string { var result []string + for { + log.Printf("%v", input) + var beginIndex = strings.Index(input, begin) + if beginIndex == -1 { + break + } + part := input[beginIndex+len(begin):] + endIndex := strings.Index(part, end) + if endIndex == -1 { + break + } + finalPart := part[:endIndex] + result = append(result, finalPart) + input = input[beginIndex+len(begin)+len(finalPart):] + } return result diff --git a/xstring/xstrings_test.go b/xstring/xstrings_test.go index ec9ca5c..2023b4b 100644 --- a/xstring/xstrings_test.go +++ b/xstring/xstrings_test.go @@ -1,13 +1,16 @@ package xstring import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestStringExtractions(t *testing.T) { - items := ExtractStringBetween(" 1 qwe 2 1 xxx 2", "1", "2") + items := Between(" 1qwe2 666 1xxx2 000", "1", "2") assert.Equal(t, 2, len(items)) + assert.Contains(t, items, "qwe") + assert.Contains(t, items, "xxx") -} \ No newline at end of file +}