Add maps.MarkDirs

This commit is contained in:
rwxrob 2022-02-26 00:04:54 -05:00
parent d73b3288ea
commit 06bc522037
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77
3 changed files with 27 additions and 1 deletions

View File

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

View File

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

0
maps/testdata/markdirs/file1 vendored Normal file
View File