stringutils

package
v1.4.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2018 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package stringutils is the obligatory add-on for convenience functions related to strings.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contract

func Contract(m map[string]string, f func(string, string) string) []string

Contract takes a map[string]string and contracts pairs of key and values.

Example

Contract takes a map[string]string and pipes key-value pairs through a function and returns accumulated results.

package main

import (
	"fmt"
	"sort"

	"github.com/bpicode/fritzctl/stringutils"
)

func main() {
	m := map[string]string{"key1": "value1", "key2": "value2"}
	vs := stringutils.Contract(m, func(k string, v string) string { return v })
	sort.Strings(vs)
	fmt.Println(vs)
}
Output:

[value1 value2]

func DefaultIf

func DefaultIf(value, defaultValue, condition string) string

DefaultIf falls back to a default value if the passed value is equal to the condition parameter.

Example

DefaultIf takes three strings v, d, c and returns (v != c)? v : d.

package main

import (
	"fmt"

	"github.com/bpicode/fritzctl/stringutils"
)

func main() {
	s := stringutils.DefaultIf("xxx", "[CENSORED]", "xxx")
	fmt.Println(s)
	s = stringutils.DefaultIf("abc", "[CENSORED]", "xxx")
	fmt.Println(s)
}
Output:

[CENSORED]
abc

func DefaultIfEmpty

func DefaultIfEmpty(value, defaultValue string) string

DefaultIfEmpty falls back to a default value if the passed value is empty

Example

DefaultIfEmpty takes two strings v, d and returns (v != "")? v : d.

package main

import (
	"fmt"

	"github.com/bpicode/fritzctl/stringutils"
)

func main() {
	s := stringutils.DefaultIfEmpty("my string", "<no value>")
	fmt.Println(s)
	s = stringutils.DefaultIfEmpty("", "<no value>")
	fmt.Println(s)
}
Output:

my string
<no value>

func ErrorMessages

func ErrorMessages(errs []error) []string

ErrorMessages accumulates the error messages from slice of errors.

Example

ErrorMessages extracts the messages from a slice of errors.

package main

import (
	"errors"
	"fmt"

	"github.com/bpicode/fritzctl/stringutils"
)

func main() {
	es := []error{errors.New("an_error"), errors.New("another_error")}
	ms := stringutils.ErrorMessages(es)
	fmt.Println(ms)
}
Output:

[an_error another_error]

func Keys added in v1.4.17

func Keys(m map[string]string) []string

Keys extracts the key of a map[string]string and returns them as a slice of strings.

Example

Keys extracts the keys of a map[string]string.

package main

import (
	"fmt"
	"sort"

	"github.com/bpicode/fritzctl/stringutils"
)

func main() {
	m := map[string]string{"key1": "value1", "key2": "value2"}
	ks := stringutils.Keys(m)
	sort.Strings(ks)
	fmt.Println(ks)
}
Output:

[key1 key2]

func Quote

func Quote(s []string) []string

Quote takes a slice of strings and returns a slice of strings where each element of the input is put in double quotation marks.

Example

Quote puts double quotation marks around each element of a string slice.

package main

import (
	"fmt"

	"github.com/bpicode/fritzctl/stringutils"
)

func main() {
	s := []string{"hello", "world"}
	q := stringutils.Quote(s)
	fmt.Println(q)
}
Output:

["hello" "world"]

func Transform

func Transform(s []string, f func(string) string) []string

Transform takes a slice of strings, transforms each element and returns a slice of strings containing the transformed values

Example

Transform applies a given operation to all elements of a string slice.

package main

import (
	"fmt"
	"strings"

	"github.com/bpicode/fritzctl/stringutils"
)

func main() {
	s := []string{"hello", "world"}
	u := stringutils.Transform(s, strings.ToUpper)
	fmt.Println(u)
}
Output:

[HELLO WORLD]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL