mirror of
https://github.com/rwxrob/bonzai
synced 2024-11-18 15:25:45 +00:00
Add maps.MarkDirs
This commit is contained in:
parent
d73b3288ea
commit
06bc522037
13
maps/maps.go
13
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()
|
||||
})
|
||||
}
|
||||
|
@ -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
0
maps/testdata/markdirs/file1
vendored
Normal file
Loading…
Reference in New Issue
Block a user