diff --git a/maps/maps.go b/maps/maps.go index f21e1a3..f145c2c 100644 --- a/maps/maps.go +++ b/maps/maps.go @@ -1,6 +1,8 @@ package maps import ( + "io/fs" + "os" "path/filepath" "sort" @@ -26,3 +28,14 @@ func Keys[T any](m map[string]T) []string { func CleanPaths(paths []string) []string { return fn.Map(paths, func(i string) string { return filepath.Clean(i) }) } + +// MarkDirs will add an os.PathSeparator to the end of the name if the +// fs.DirEntry is a directory. +func MarkDirs(entries []fs.DirEntry) []string { + return fn.Map(entries, func(f fs.DirEntry) string { + if f.IsDir() { + return f.Name() + string(os.PathSeparator) + } + return f.Name() + }) +} diff --git a/maps/maps_test.go b/maps/maps_test.go index 38d81da..c8ee6b6 100644 --- a/maps/maps_test.go +++ b/maps/maps_test.go @@ -2,7 +2,9 @@ package maps_test import ( "fmt" + "os" + "github.com/rwxrob/bonzai/loop" "github.com/rwxrob/bonzai/maps" ) @@ -30,10 +32,21 @@ func ExampleCleanPaths() { `./thing`, `/sub/../../thing`, } - filter.Println(filter.CleanPaths(paths)) + + loop.Println(maps.CleanPaths(paths)) + // Output: // . // . // . + // thing // /thing } + +func ExampleMarkDirs() { + entries, _ := os.ReadDir("testdata/markdirs") + loop.Println(maps.MarkDirs(entries)) + //Output: + // dir1/ + // file1 +} diff --git a/maps/testdata/markdirs/file1 b/maps/testdata/markdirs/file1 new file mode 100644 index 0000000..e69de29