Skip to content

Commit

Permalink
Added function to list files by extension (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
denis256 authored Feb 12, 2022
1 parent 399eb5b commit 16c7308
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion xcollection/xcollection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestStringBoolMapKeys(t *testing.T) {

t.Parallel()
keys := MapKeys(map[string]bool{
"qwe": true,
"123": true,
Expand All @@ -20,6 +20,7 @@ func TestStringBoolMapKeys(t *testing.T) {
}

func TestRandomSelection(t *testing.T) {
t.Parallel()
rand.Seed(time.Now().Unix())
element := Random([]string{"1", "2", "3", "4", "5", "6"})
log.Printf("Random element: %s", element)
Expand Down
2 changes: 1 addition & 1 deletion xenv/xenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestFetchingCurrentUser(t *testing.T) {

t.Parallel()
user := Env("USER", "")

assert.NotEmpty(t, user)
Expand Down
20 changes: 20 additions & 0 deletions xfiles/xfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package xfiles

import (
"bufio"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/unidev-platform/golang-core/xcollection"
Expand All @@ -29,3 +31,21 @@ func Distinct(path string) ([]string, error) {
return xcollection.MapKeys(linesMap), scanner.Err()

}

// Find - extract files matching extension
func Find(dir string, extension string) ([]string, error) {
var files []string
err := filepath.WalkDir(dir, func(s string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if filepath.Ext(d.Name()) == extension {
files = append(files, s)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
10 changes: 10 additions & 0 deletions xfiles/xfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ import (
)

func TestFileLinesExtraction(t *testing.T) {
t.Parallel()
lines, err := Distinct("distinct_file_lines_test.txt")
if err != nil {
panic(err)
}
assert.Equal(t, 6, len(lines))
}

func TestListFiles(t *testing.T) {
t.Parallel()
files, err := Find(".", ".txt")
if err != nil {
panic(err)
}
assert.Equal(t, 1, len(files))
}
1 change: 1 addition & 0 deletions xstring/xstrings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

func TestStringExtractions(t *testing.T) {
t.Parallel()
items := Between(" 1qwe2 666 1xxx2 000", "1", "2")

assert.Equal(t, 2, len(items))
Expand Down

0 comments on commit 16c7308

Please # to comment.