Documentation
¶
Overview ¶
Package mung provides methods to manipulate strings commonly found in PATH-like environment variables.
Index ¶
- func Make[T any](opts ...Option[T]) (t T)
- func Version() string
- func Wrap[T any](t T, opts ...Option[T]) T
- type Config
- func (c Config) All() iter.Seq[string]
- func (c Config) Delim() string
- func (c Config) Filtered() iter.Seq[string]
- func (c Config) Predicate() func(string) bool
- func (c Config) Prefix() []string
- func (c Config) Remove() []string
- func (c Config) Replace() map[string]string
- func (c Config) String() string
- func (c Config) Subject() []string
- func (c Config) Suffix() []string
- type Option
- func WithDelim(delim string) Option[Config]
- func WithFilter(predicate func(string) bool) Option[Config]
- func WithPrefix(prefixes []string) Option[Config]
- func WithPrefixItems(prefixes ...string) Option[Config]
- func WithRemove(removes []string) Option[Config]
- func WithRemoveItems(removes ...string) Option[Config]
- func WithReplace(replace map[string]string) Option[Config]
- func WithReplaceEach(replacements iter.Seq2[string, string]) Option[Config]
- func WithReplaceItem(from, to string) Option[Config]
- func WithReplaceItems(replacements ...map[string]string) Option[Config]
- func WithSubject(subjects []string) Option[Config]
- func WithSubjectItems(subjects ...string) Option[Config]
- func WithSuffix(suffixes []string) Option[Config]
- func WithSuffixItems(suffixes ...string) Option[Config]
Examples ¶
- Config (Complex)
- Config.All
- Config.Delim
- Config.Prefix
- Config.Remove
- Config.Replace
- Config.String
- Config.Subject
- Config.Suffix
- Make
- Version
- WithDelim
- WithFilter
- WithPrefix
- WithPrefix (Order)
- WithPrefixItems
- WithRemove
- WithRemoveItems
- WithReplace
- WithReplaceEach
- WithReplaceItem
- WithReplaceItems
- WithSubject
- WithSubjectItems
- WithSuffix
- WithSuffixItems
- Wrap
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Make ¶
Make returns a new object of type T with the given options applied.
Example ¶
ExampleMake demonstrates creating a new Config with options.
// Create a new Config with PATH-like variable
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
WithDelim(":"),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/bin
func Version ¶
func Version() string
Version returns the version of the mung package.
Example ¶
ExampleVersion demonstrates how to get the version of the mung package.
version := Version() fmt.Println(version)
Output: 0.4.0
func Wrap ¶
Wrap returns t after applying the given options.
Example ¶
ExampleWrap demonstrates applying options to an existing object.
// Create an empty Config
var config Config
// Apply options to the existing Config
config = Wrap(config,
WithSubject([]string{"/usr/local/bin:/usr/bin"}),
WithDelim(":"),
WithSuffix([]string{"/opt/bin"}),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/opt/bin
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config represents the configuration for string munging operations.
Example (Complex) ¶
ExampleConfig_complex demonstrates a more complex workflow.
// Build a PATH-like variable with multiple operations
config := Make(
WithSubject([]string{"/usr/bin:/bin:/usr/local/old:/opt/deprecated"}),
WithDelim(":"),
WithPrefix([]string{"/usr/local/bin"}),
WithSuffix([]string{"/opt/bin"}),
WithRemove([]string{"/usr/local/old", "/opt/deprecated"}),
WithReplace(map[string]string{"/bin": "/sbin"}),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/sbin:/opt/bin
func (Config) All ¶ added in v0.3.0
All returns each string item from the munged sequence.
Example ¶
ExampleConfig_All demonstrates iterating through the processed sequence.
config := Make(
WithSubject([]string{"/usr/bin:/bin"}),
WithDelim(":"),
WithPrefix([]string{"/usr/local/bin"}),
WithSuffix([]string{"/opt/bin"}),
)
// Collect results using the iterator
var result []string
config.All()(func(s string) bool {
result = append(result, s)
return true
})
fmt.Println(strings.Join(result, ", "))
Output: /usr/local/bin, /usr/bin, /bin, /opt/bin
func (Config) Delim ¶
Delim returns the delimiter used for splitting and joining strings.
Example ¶
ExampleConfig_Delim demonstrates getting the delimiter.
config := Make(
WithDelim(":"),
)
delim := config.Delim()
fmt.Printf("Delimiter: %q\n", delim)
Output: Delimiter: ":"
func (Config) Filtered ¶ added in v0.3.0
Filtered returns each string item from the munged sequence that satisfies the predicate function Config.Predicate.
func (Config) Predicate ¶ added in v0.3.0
Predicate returns the predicate function used to select yielded strings.
func (Config) Prefix ¶
Prefix returns the list of strings to be prepended to the result.
Example ¶
ExampleConfig_Prefix demonstrates accessing prefix strings.
config := Make(
WithPrefix([]string{"/usr/local/bin", "/usr/bin"}),
)
prefix := config.Prefix()
fmt.Println(strings.Join(prefix, ", "))
Output: /usr/local/bin, /usr/bin
func (Config) Remove ¶
Remove returns the list of strings to be removed during processing.
Example ¶
ExampleConfig_Remove demonstrates accessing removal strings.
config := Make(
WithRemove([]string{"/usr/bin", "/opt/bin"}),
)
remove := config.Remove()
fmt.Println(strings.Join(remove, ", "))
Output: /usr/bin, /opt/bin
func (Config) Replace ¶
Replace returns a copy of the string replacement map.
Example ¶
ExampleConfig_Replace demonstrates accessing replacement rules.
config := Make(
WithReplace(map[string]string{
"/usr/bin": "/opt/bin",
"/bin": "/sbin",
}),
)
replace := config.Replace()
// Sort for consistent output
keys := []string{"/bin", "/usr/bin"}
for _, k := range keys {
if v, ok := replace[k]; ok {
fmt.Printf("%s -> %s\n", k, v)
}
}
Output: /bin -> /sbin /usr/bin -> /opt/bin
func (Config) String ¶
String returns the munged strings joined with the configuration's delimiter.
Example ¶
ExampleConfig_String demonstrates the String method.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
WithDelim(":"),
)
// Convert the munged result to a string
result := config.String()
fmt.Println(result)
Output: /usr/local/bin:/usr/bin:/bin
func (Config) Subject ¶
Subject returns the subject strings to be processed.
Example ¶
ExampleConfig_Subject demonstrates accessing the subject strings.
config := Make(
WithSubject([]string{"/usr/local/bin", "/usr/bin", "/bin"}),
)
subject := config.Subject()
fmt.Println(strings.Join(subject, ", "))
Output: /usr/local/bin, /usr/bin, /bin
func (Config) Suffix ¶
Suffix returns the list of strings to be appended to the result.
Example ¶
ExampleConfig_Suffix demonstrates accessing suffix strings.
config := Make(
WithSuffix([]string{"/bin", "/opt/bin"}),
)
suffix := config.Suffix()
fmt.Println(strings.Join(suffix, ", "))
Output: /bin, /opt/bin
type Option ¶
type Option[T any] func(T) T
Option functions return their argument with modifications applied.
func WithDelim ¶
WithDelim returns an option that sets the string tokenizing delimiter.
Example ¶
ExampleWithDelim demonstrates setting the delimiter.
// Create config using semicolon as delimiter (Windows PATH-like)
config := Make(
WithSubject([]string{"C:\\Program Files\\App;C:\\Windows"}),
WithDelim(";"),
)
fmt.Println(config.String())
Output: C:\Program Files\App;C:\Windows
func WithFilter ¶ added in v0.4.0
WithFilter returns an option that sets the predicate function used to select yielded strings.
Example ¶
ExampleWithFilter demonstrates filtering elements using a predicate.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
WithDelim(":"),
WithFilter(func(s string) bool {
return !strings.Contains(s, "local")
}),
)
fmt.Println(config.String())
Output: /usr/bin:/bin
func WithPrefix ¶
WithPrefix returns an option that sets all strings to prepend after processing.
Strings are prepended in the order they are given, meaning the trailing argument will lead the result; or, the leading argument is the first to be prepended.
Example ¶
ExampleWithPrefix demonstrates prepending elements.
config := Make(
WithSubject([]string{"/usr/bin:/bin"}),
WithDelim(":"),
WithPrefix([]string{"/usr/local/bin"}),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/bin
Example (Order) ¶
ExampleWithPrefix_order shows how ordering works with multiple prefixes.
// Note: prefixes are prepended in reverse order of arguments
config := Make(
WithSubject([]string{"/bin"}),
WithDelim(":"),
WithPrefix([]string{"/usr/local/bin", "/usr/bin"}),
)
fmt.Println(config.String())
Output: /usr/bin:/usr/local/bin:/bin
func WithPrefixItems ¶
WithPrefixItems returns an option that adds strings to prepend after processing.
Strings are prepended in the order they are given, meaning the trailing argument will lead the result; or, the leading argument is the first to be prepended.
Example ¶
ExampleWithPrefixItems demonstrates adding prefixes.
config := Make(
WithSubject([]string{"/bin"}),
WithDelim(":"),
WithPrefix([]string{"/usr/bin"}),
WithPrefixItems("/usr/local/bin"),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/bin
func WithRemove ¶
WithRemove returns an option that sets all strings to remove during processing.
Example ¶
ExampleWithRemove demonstrates removing elements.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin:/opt/bin"}),
WithDelim(":"),
WithRemove([]string{"/usr/bin", "/opt/bin"}),
)
fmt.Println(config.String())
Output: /usr/local/bin:/bin
func WithRemoveItems ¶
WithRemoveItems returns an option that adds strings to remove during processing.
Example ¶
ExampleWithRemoveItems demonstrates adding items to remove.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin:/opt/bin"}),
WithDelim(":"),
WithRemove([]string{"/usr/bin"}),
WithRemoveItems("/opt/bin"),
)
fmt.Println(config.String())
Output: /usr/local/bin:/bin
func WithReplace ¶
WithReplace returns an option that sets all whole/fixed-string substitution rules to apply after processing.
Example ¶
ExampleWithReplace demonstrates replacing elements.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
WithDelim(":"),
WithReplace(map[string]string{
"/usr/bin": "/opt/bin",
"/bin": "/sbin",
}),
)
fmt.Println(config.String())
Output: /usr/local/bin:/opt/bin:/sbin
func WithReplaceEach ¶ added in v0.2.0
WithReplaceEach returns an option that adds individual whole/fixed-string substitution rules to apply after processing.
The given sequence seq must yield paired strings in which the first string is the item to replace, and the second is the replacement.
Example ¶
ExampleWithReplaceEach demonstrates adding replacement rules using an iterator.
// Define a replacement sequence
replacements := func(yield func(string, string) bool) {
if !yield("/usr/bin", "/opt/bin") {
return
}
_ = yield("/bin", "/sbin")
}
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
WithDelim(":"),
WithReplaceEach(replacements),
)
fmt.Println(config.String())
Output: /usr/local/bin:/opt/bin:/sbin
func WithReplaceItem ¶ added in v0.2.0
WithReplaceItem returns an option that adds an individual whole/fixed-string substitution rule to apply after processing.
Example ¶
ExampleWithReplaceItem demonstrates adding a single replacement rule.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
WithDelim(":"),
WithReplaceItem("/usr/bin", "/opt/bin"),
)
fmt.Println(config.String())
Output: /usr/local/bin:/opt/bin:/bin
func WithReplaceItems ¶
WithReplaceItems returns an option that adds whole/fixed-string substitution rules to apply after processing.
The given replacements must yield maps in which each map's key is the item to replace, and the value is the replacement.
Example ¶
ExampleWithReplaceItems demonstrates adding replacement rules using maps.
replacements := map[string]string{
"/usr/bin": "/opt/bin",
"/bin": "/sbin",
}
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
WithDelim(":"),
WithReplaceItems(replacements),
)
fmt.Println(config.String())
Output: /usr/local/bin:/opt/bin:/sbin
func WithSubject ¶
WithSubject returns an option that sets all subject strings to be processed.
Example ¶
ExampleWithSubject demonstrates setting the subject strings.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin:/bin"}),
WithDelim(":"),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/bin
func WithSubjectItems ¶
WithSubjectItems returns an option that adds subject strings to be processed.
Example ¶
ExampleWithSubjectItems demonstrates adding to subject strings.
config := Make(
WithSubject([]string{"/usr/local/bin"}),
WithDelim(":"),
WithSubjectItems("/usr/bin", "/bin"),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/bin
func WithSuffix ¶
WithSuffix returns an option that sets all strings to append after processing.
Strings are appended in the order they are given, meaning the trailing argument will trail the result; or, the leading argument is the first to be appended.
Example ¶
ExampleWithSuffix demonstrates appending elements.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin"}),
WithDelim(":"),
WithSuffix([]string{"/bin", "/opt/bin"}),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/bin:/opt/bin
func WithSuffixItems ¶
WithSuffixItems returns an option that adds strings to append after processing.
Example ¶
ExampleWithSuffixItems demonstrates adding suffixes.
config := Make(
WithSubject([]string{"/usr/local/bin:/usr/bin"}),
WithDelim(":"),
WithSuffix([]string{"/bin"}),
WithSuffixItems("/opt/bin"),
)
fmt.Println(config.String())
Output: /usr/local/bin:/usr/bin:/bin:/opt/bin