Skip to content

jordanm-code/swiss

Repository files navigation

Swiss

Swiss is a collection of functions to help reduce boilerplate code in golang.

Godoc

swiss

import "go/swiss"

Index

Variables

var ErrReaderNotFound = errors.New("reader could not be found")

var ErrTimeFormat = errors.New("unrecognized time format")

var Language = language.English

func CamelCase

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

Output

firstName
lastName

func Chunk

func Chunk[T ~[]E, E any](s T, size int) (chunks []T)

Chunk divides a slice into chunks of the specified size.

func Deduplicate

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]

Output

[Alice Bob Charlie David]

func Deref

func Deref[T any](v *T) (r T)

Deref returns the value that the pointer points to.

func ExtractURLs

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

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

Output

file_test.go exists
file_test.go.not not exists

func IsAlpha

func IsAlpha(s string) bool

IsAlpha checks if a string is all alphabetic characters.

func IsAlphaNumeric

func IsAlphaNumeric(s string) bool

IsAlphaNumeric checks if a string is all alphanumeric characters.

func IsEmail

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

func IsEmpty(x interface{}) bool

IsEmpty checks if the given value is empty/zero value.

func IsHexChar

func IsHexChar(c byte) bool

IsHexChar checks if a character is a valid hexadecimal character.

func IsLower

func IsLower(s string) bool

IsLower checks if a string is all lowercase.

func IsNumeric

func IsNumeric(s string) bool

IsNumeric checks if a string is all numeric characters.

func IsSnakeCase

func IsSnakeCase(s string) bool

IsSnakeCase checks to see if supplied string is in snake case.

func IsURL

func IsURL(s string) bool

IsURL checks if a string is a valid URL.

func IsUUID

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

func IsUpper(s string) bool

IsUpper checks if a string is all uppercase.

func Map

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

Output

Found 5
Not found 10

func PascalCase

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

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

Output

easy

func RandomSeed

func RandomSeed()

RandomSeed will generate a seed based on the current UnixNano.

func RandomString

func RandomString(length int) string

RandomString creates an alphanumeric string of a given length.

func Read

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>

Output

<html></html>
<html></html>

func Reverse

func Reverse(s string) string

Reverse returns the string in reverse order. This function is an alias for [bidi.ReverseString].

func Slugify

func Slugify(s string) string

Slugify will take a string and create a dash separated string for use in URLs.

func SnakeCase

func SnakeCase(s string) string

SnakeCase converts a string to snake case. Inputs can be space separated, camel case, or pascal case.

func SwapCase

func SwapCase(str string) string

SwapCase swaps the case of a string.

func TimeFromHumanReadable

func TimeFromHumanReadable(s string) (time.Time, error)

TimeFromHumanReadable will take in a string and try to parse out a time.

Example

fmt.Println(TimeFromHumanReadable("yesterday at 4AM"))

// Output:
// 2024-06-07 04:00:00 +0000 UTC

Output

2024-06-07 04:00:00 +0000 UTC

func TitleCase

func TitleCase(s string) string

TitleCase converts a string to title case.

Generated by gomarkdoc

About

go swiss army knife

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published