Add maps.MarkDirs

pull/53/head
rwxrob 2 years ago
parent d73b3288ea
commit 06bc522037
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -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()
})
}

@ -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
}

Loading…
Cancel
Save