Skip to content
Merged
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
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 ./...
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
1 change: 1 addition & 0 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
make fmt
8 changes: 4 additions & 4 deletions xcollection/xcollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ 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)
}
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]
}
7 changes: 4 additions & 3 deletions xcollection/xcollection_test.go
Original file line number Diff line number Diff line change
@@ -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,
})
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion xenv/xenv_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package xenv

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFetchingCurrentUser(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions xfiles/xfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -25,6 +26,6 @@ func ReadDistinctFileLines(path string)([]string, error) {
}
}

return xcollection.StringBoolMapKeys(linesMap), scanner.Err()
return xcollection.MapKeys(linesMap), scanner.Err()

}
5 changes: 3 additions & 2 deletions xfiles/xfiles_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
23 changes: 22 additions & 1 deletion xstring/xstrings.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 6 additions & 3 deletions xstring/xstrings_test.go
Original file line number Diff line number Diff line change
@@ -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")

}
}