Swiss is a collection of functions to help reduce boilerplate code in golang.
import "go/swiss"
- Variables
- func CamelCase(s string) string
- func Chunk[T ~[]E, E any](s T, size int) (chunks []T)
- func Deduplicate[T ~[]E, E comparable](s T) (r T)
- func Deref[T any](v *T) (r T)
- func ExtractURLs(s string) []string
- func FileExists(filePath string) bool
- func IsAlpha(s string) bool
- func IsAlphaNumeric(s string) bool
- func IsEmail(s string) bool
- func IsEmpty(x interface{}) bool
- func IsHexChar(c byte) bool
- func IsLower(s string) bool
- func IsNumeric(s string) bool
- func IsSnakeCase(s string) bool
- func IsURL(s string) bool
- func IsUUID(s string) bool
- func IsUpper(s string) bool
- func Map[S ~[]E, E comparable](s S) map[E]bool
- func PascalCase(s string) string
- func PtrTo[T any](v T) *T
- func RandomSeed()
- func RandomString(length int) string
- func Read(r interface{}) (string, error)
- func Reverse(s string) string
- func Slugify(s string) string
- func SnakeCase(s string) string
- func SwapCase(str string) string
- func TitleCase(s string) string
var ErrReaderNotFound = errors.New("reader could not be found")
var Language = language.English
func CamelCase(s string) string
CamelCase converts a string to camel case.
Example
jsonKeys := []string{"first_name", "last_name"}
for _, key := range jsonKeys {
fmt.Println(CamelCase(key))
}
// Output:
// firstName
// lastName
firstName
lastName
func Chunk[T ~[]E, E any](s T, size int) (chunks []T)
Chunk divides a slice into chunks of the specified size.
func Deduplicate[T ~[]E, E comparable](s T) (r T)
Deduplicate removes duplicate elements from a slice while preserving the order of the elements.
Example
names := []string{"Alice", "Bob", "Alice", "Charlie", "Bob", "David"}
fmt.Println(Deduplicate(names))
// Output:
// [Alice Bob Charlie David]
[Alice Bob Charlie David]
func Deref[T any](v *T) (r T)
Deref returns the value that the pointer points to.
func ExtractURLs(s string) []string
ExtractURLs will take a string and return all URLs found within it in the order they are encountered.
func FileExists(filePath string) bool
FileExists returns true if the file exists
Example
if FileExists("file_test.go") {
fmt.Println("file_test.go exists")
}
if !FileExists("file_test.go.not") {
fmt.Println("file_test.go.not not exists")
}
// Output:
// file_test.go exists
// file_test.go.not not exists
file_test.go exists
file_test.go.not not exists
func IsAlpha(s string) bool
IsAlpha checks if a string is all alphabetic characters.
func IsAlphaNumeric(s string) bool
IsAlphaNumeric checks if a string is all alphanumeric characters.
func IsEmail(s string) bool
IsEmail checks if a string is a valid standard email address. RFC 5322 is too permissive for general use such as quoted strings and local hosts.
func IsEmpty(x interface{}) bool
IsEmpty checks if the given value is empty/zero value.
func IsHexChar(c byte) bool
IsHexChar checks if a character is a valid hexadecimal character.
func IsLower(s string) bool
IsLower checks if a string is all lowercase.
func IsNumeric(s string) bool
IsNumeric checks if a string is all numeric characters.
func IsSnakeCase(s string) bool
IsSnakeCase checks to see if supplied string is in snake case.
func IsURL(s string) bool
IsURL checks if a string is a valid URL.
func IsUUID(s string) bool
IsUUID will take a string and determine if it is a valid UUID, it will return true if it is and false if it is not.
func IsUpper(s string) bool
IsUpper checks if a string is all uppercase.
func Map[S ~[]E, E comparable](s S) map[E]bool
Map creates a map from a slice of keys. The value of each key is a boolean indicating whether the key is present in the slice.
Example
nums := []int{5, 9, 2, 0, 7, 12, 3, 26}
m := Map(nums) // O(n)
find := []int{5, 10}
for _, n := range find {
if m[n] { // O(1)
fmt.Println("Found", n)
} else {
fmt.Println("Not found", n)
}
}
// Output:
// Found 5
// Not found 10
Found 5
Not found 10
func PascalCase(s string) string
PascalCase converts a string to pascal case, it can take in a string that is both title case and camel case and convert it to camel case.
func PtrTo[T any](v T) *T
PtrTo returns a pointer to the value passed in.
Example
printString := func(s *string) {
fmt.Println(*s)
}
printString(PtrTo("easy"))
// Output:
// easy
easy
func RandomSeed()
RandomSeed will generate a seed based on the current UnixNano.
func RandomString(length int) string
RandomString creates an alphanumeric string of a given length.
func Read(r interface{}) (string, error)
Read will read from io.Reader and return the content as a string.
When a struct is provided, the first io.Reader that is encountered will be read. io.ReadCloser will be closed after reading.
When a struct pointer is provided, io.Reader and io.ReadCloser will automatically rewind by swapping out the reader with a new io.ReadCloser with a nopCloser or a bytes.Reader that contains the same content.
Example
request, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
if err != nil {
panic(err)
}
request.Body = io.NopCloser(bytes.NewReader([]byte("<html></html>")))
body, err := Read(request) // pass in *http.Request; will rewind
if err != nil {
panic(err)
}
fmt.Println(body)
body, err = Read(request.Body) // pass in io.ReadCloser; will not rewind
if err != nil {
panic(err)
}
fmt.Println(body)
body, err = Read(request) // pass in request; body is empty
if err != nil {
panic(err)
}
fmt.Println(body)
// Output:
// <html></html>
// <html></html>
<html></html>
<html></html>
func Reverse(s string) string
Reverse returns the string in reverse order. This function is an alias for [bidi.ReverseString].
func Slugify(s string) string
Slugify will take a string and create a dash separated string for use in URLs.
func SnakeCase(s string) string
SnakeCase converts a string to snake case. Inputs can be space separated, camel case, or pascal case.
func SwapCase(str string) string
SwapCase swaps the case of a string.
func TitleCase(s string) string
TitleCase converts a string to title case.
Generated by gomarkdoc