Skip to content

Commit 399eb5b

Browse files
authored
Cleanup and updates (#3)
* Added basic build scripts * make fmt * Added string functions * Basic string functions, definition cleanup
1 parent a31749d commit 399eb5b

File tree

10 files changed

+66
-18
lines changed

10 files changed

+66
-18
lines changed

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
GO_FILES?=$$(find . -name '*.go' | grep -v vendor)
2+
3+
fmt:
4+
@echo "Formatting files"
5+
gofmt -w $(GO_FILES)
6+
goimports -w $(GO_FILES)
7+
8+
pre-commit-hook:
9+
@ln -s scripts/pre-commit .git/hooks/pre-commit
10+
@echo "hook installed."
11+
12+
test:
13+
go test -v ./...

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ module github.com/unidev-platform/golang-core
33
go 1.17
44

55
require github.com/stretchr/testify v1.7.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.0 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
11+
)

scripts/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
make fmt

xcollection/xcollection.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ package xcollection
22

33
import "math/rand"
44

5-
// StringBoolMapKeys - extract map keys as slice
6-
func StringBoolMapKeys(m map[string]bool) []string {
5+
// MapKeys - extract map keys as slice
6+
func MapKeys(m map[string]bool) []string {
77
keys := make([]string, 0, len(m))
88
for kk := range m {
99
keys = append(keys, kk)
1010
}
1111
return keys
1212
}
1313

14-
// StringRandomElement - fetch random element from string slice
15-
func StringRandomElement(slice []string ) string {
14+
// Random - fetch random element from string slice
15+
func Random(slice []string) string {
1616
randomIndex := rand.Intn(len(slice))
1717
return slice[randomIndex]
1818
}

xcollection/xcollection_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package xcollection
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"log"
65
"math/rand"
76
"testing"
87
"time"
8+
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestStringBoolMapKeys(t *testing.T) {
1213

13-
keys := StringBoolMapKeys(map[string]bool{
14+
keys := MapKeys(map[string]bool{
1415
"qwe": true,
1516
"123": true,
1617
})
@@ -20,7 +21,7 @@ func TestStringBoolMapKeys(t *testing.T) {
2021

2122
func TestRandomSelection(t *testing.T) {
2223
rand.Seed(time.Now().Unix())
23-
element := StringRandomElement([]string{"1", "2", "3", "4", "5", "6"})
24+
element := Random([]string{"1", "2", "3", "4", "5", "6"})
2425
log.Printf("Random element: %s", element)
2526
assert.NotNil(t, element)
2627
}

xenv/xenv_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package xenv
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
func TestFetchingCurrentUser(t *testing.T) {

xfiles/xfiles.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package xfiles
22

33
import (
44
"bufio"
5-
"github.com/unidev-platform/golang-core/xcollection"
65
"os"
76
"strings"
7+
8+
"github.com/unidev-platform/golang-core/xcollection"
89
)
910

10-
// ReadDistinctFileLines - Read text file lines as slice without empty and duplicates
11-
func ReadDistinctFileLines(path string)([]string, error) {
11+
// Distinct - Read text file lines as slice without empty and duplicates
12+
func Distinct(path string) ([]string, error) {
1213
var linesMap = make(map[string]bool)
1314

1415
file, err := os.Open(path)
@@ -25,6 +26,6 @@ func ReadDistinctFileLines(path string)([]string, error) {
2526
}
2627
}
2728

28-
return xcollection.StringBoolMapKeys(linesMap), scanner.Err()
29+
return xcollection.MapKeys(linesMap), scanner.Err()
2930

3031
}

xfiles/xfiles_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package xfiles
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
func TestFileLinesExtraction(t *testing.T) {
9-
lines, err := ReadDistinctFileLines("distinct_file_lines_test.txt")
10+
lines, err := Distinct("distinct_file_lines_test.txt")
1011
if err != nil {
1112
panic(err)
1213
}

xstring/xstrings.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package xstring
22

3-
func ExtractStringBetween(rawString string,begin string, end string) []string {
3+
import (
4+
"log"
5+
"strings"
6+
)
47

8+
// Between - extract from input string all substrings located between begin and end.
9+
// Useful for extracting data between HTML tags.
10+
func Between(input string, begin string, end string) []string {
511
var result []string
612

13+
for {
14+
log.Printf("%v", input)
15+
var beginIndex = strings.Index(input, begin)
16+
if beginIndex == -1 {
17+
break
18+
}
19+
part := input[beginIndex+len(begin):]
20+
endIndex := strings.Index(part, end)
21+
if endIndex == -1 {
22+
break
23+
}
24+
finalPart := part[:endIndex]
25+
result = append(result, finalPart)
726

27+
input = input[beginIndex+len(begin)+len(finalPart):]
28+
}
829

930
return result
1031

xstring/xstrings_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package xstring
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
func TestStringExtractions(t *testing.T) {
9-
items := ExtractStringBetween(" 1 qwe 2 1 xxx 2", "1", "2")
10+
items := Between(" 1qwe2 666 1xxx2 000", "1", "2")
1011

1112
assert.Equal(t, 2, len(items))
13+
assert.Contains(t, items, "qwe")
14+
assert.Contains(t, items, "xxx")
1215

13-
}
16+
}

0 commit comments

Comments
 (0)