mirror of https://github.com/rwxrob/bonzai
Improve organization of functional packages
parent
b5ac9f2ca6
commit
8e2e560e80
@ -1,13 +1,32 @@
|
||||
package mapf_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/rwxrob/bonzai/each"
|
||||
"github.com/rwxrob/bonzai/fn"
|
||||
"github.com/rwxrob/bonzai/mapf"
|
||||
)
|
||||
|
||||
func ExampleMarkDirs() {
|
||||
entries, _ := os.ReadDir("testdata/markdirs")
|
||||
each.Println(fn.Map(entries, mapf.MarkDirs))
|
||||
//Output:
|
||||
// dir1/
|
||||
// file1
|
||||
}
|
||||
|
||||
func ExampleHashComment() {
|
||||
fn.A[string]{"foo", "bar"}.M(mapf.HashComment).P()
|
||||
each.Println(fn.Map([]string{"foo", "bar"}, mapf.HashComment))
|
||||
// Output:
|
||||
// # foo
|
||||
// # bar
|
||||
}
|
||||
|
||||
func ExampleEscSpace() {
|
||||
s := []string{"one here", "and another one"}
|
||||
each.Println(fn.Map(s, mapf.EscSpace))
|
||||
// Output:
|
||||
// one\ here
|
||||
// and\ another\ \ \ \ one
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
package maps
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/rwxrob/bonzai/fn"
|
||||
)
|
||||
|
||||
// Note to maintainers: This file contains maps that require additional
|
||||
// arguments and are therefore not able to call simple map functions
|
||||
// from the mapf package. Please keep simple mapf-able maps in
|
||||
// simple.go instead.
|
||||
|
||||
// Prefix adds a prefix to the string.
|
||||
func Prefix(in []string, pre string) []string {
|
||||
return fn.Map(in, func(i string) string { return pre + i })
|
||||
}
|
||||
|
||||
// Keys returns the keys in lexicographically sorted order.
|
||||
func Keys[T any](m map[string]T) []string {
|
||||
keys := []string{}
|
||||
for k, _ := range m {
|
||||
keys = append(keys, k)
|
||||
sort.Strings(keys)
|
||||
}
|
||||
return keys
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package maps
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/rwxrob/bonzai/fn"
|
||||
)
|
||||
|
||||
// Prefix returns a new slice with prefix added to each string.
|
||||
func Prefix(in []string, pre string) []string {
|
||||
return fn.Map(in, func(i string) string { return pre + i })
|
||||
}
|
||||
|
||||
// Keys returns the keys in lexicographically sorted order.
|
||||
func Keys[T any](m map[string]T) []string {
|
||||
keys := []string{}
|
||||
for k, _ := range m {
|
||||
keys = append(keys, k)
|
||||
sort.Strings(keys)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// CleanPaths runs filepath.Clean on each item in the slice and returns.
|
||||
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()
|
||||
})
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package maps
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rwxrob/bonzai/fn"
|
||||
"github.com/rwxrob/bonzai/mapf"
|
||||
)
|
||||
|
||||
// Note to maintainers: This file contains simple maps that are
|
||||
// implemented in the mapf package. Please keep complex maps in
|
||||
// complex.go instead.
|
||||
|
||||
// MarkDirs will add an os.PathSeparator to the end of the name if the
|
||||
// fs.DirEntry is a directory.
|
||||
func MarkDirs(s []fs.DirEntry) []string { return fn.Map(s, mapf.MarkDirs) }
|
||||
|
||||
// Base extracts the filepath.Base of each path.
|
||||
func Base(s []string) []string { return fn.Map(s, filepath.Base) }
|
||||
|
||||
// HashComment add the "# " prefix to each.
|
||||
func HashComment(s []string) []string { return fn.Map(s, mapf.HashComment) }
|
||||
|
||||
// EscSpace replaces all spaces with backslashed spaces.
|
||||
func EscSpace(s []string) []string { return fn.Map(s, mapf.EscSpace) }
|
@ -0,0 +1,43 @@
|
||||
package maps_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/rwxrob/bonzai/each"
|
||||
"github.com/rwxrob/bonzai/maps"
|
||||
)
|
||||
|
||||
func ExampleMarkDirs() {
|
||||
entries, _ := os.ReadDir("testdata/markdirs")
|
||||
each.Println(maps.MarkDirs(entries))
|
||||
//Output:
|
||||
// dir1/
|
||||
// file1
|
||||
}
|
||||
|
||||
func ExampleBase() {
|
||||
paths := []string{
|
||||
`some/thing /here`,
|
||||
`other/thing`,
|
||||
`foo`,
|
||||
}
|
||||
each.Println(maps.Base(paths))
|
||||
//Output:
|
||||
// here
|
||||
// thing
|
||||
// foo
|
||||
}
|
||||
|
||||
func ExampleHashComment() {
|
||||
each.Println(maps.HashComment([]string{"foo", "bar"}))
|
||||
// Output:
|
||||
// # foo
|
||||
// # bar
|
||||
}
|
||||
|
||||
func ExampleEscSpace() {
|
||||
each.Println(maps.EscSpace([]string{"some thing", "one other thing"}))
|
||||
// Output:
|
||||
// some\ thing
|
||||
// one\ other\ \ \ thing
|
||||
}
|
Loading…
Reference in New Issue