strutils

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 7 Imported by: 3

README ΒΆ

goutils/strings

Go Reference

A collection of utility functions for working with strings in Go.


πŸ“– Usage

import (
    "fmt"
    "github.com/Shresht7/goutils/strings"
)

func main() {
    
    fmt.Println(strings.PadLeft("ABC", "*", 3)) // ***ABC***
    
    fmt.Println(strings.HereDoc(`
        Hello, world!
    `)) // Hello, world!
}

⬆️ Back to Top


πŸ“˜ API Reference

Go Reference

Align

Aligns the given string to the left, right or center.

func Align(str string, opts *AlignOptions) string

Example:

str := strings.Align("ABC", &strings.AlignOptions{
    Width:  9,
    Mode:   strings.AlignCenter,
    Pad: "*",
}) // Output: ***ABC***

⬆️ Back to Top

DetermineIndentation

Determines the minimum and maximum level of indentation for the given lines.

func DetermineIndentation(lines ...string) (min, max int)

Example:

min, max := strings.DetermineIndentation([]string{
    "    Hello, world!",
    "        This is a test.",
    "    Goodbye!",
}...) // Output: 4, 8

⬆️ Back to Top

DetermineWidths

Determines the width of each column in a 2D array of strings

func DetermineWidths(array [][]string) []int

Example:

widths := strings.DetermineWidths([][]string{
    {"Hello", "World!"},
    {"This", "is", "a", "test"},
}) // Output: [5, 6, 1, 4]

⬆️ Back to Top

HereDoc

HereDoc returns a here-document representation of the given string.

func HereDoc(str string) string

Example:

str := strings.HereDoc(`
    Hello, world!
`) // Output: Hello, world!

⬆️ Back to Top

IsSpace

Checks if the given rune is a space character.

func IsSpace(r rune) bool

Example:

fmt.Println(strings.IsSpace(' ')) // true
fmt.Println(strings.IsSpace('\t')) // true
fmt.Println(strings.IsSpace('a')) // false

⬆️ Back to Top

Pad

Pads the given string with the given character to the left, right or center.

func Pad(s, char string, count int) string
func PadLeft(s, char string, count int) string
func PadRight(s, char string, count int) string

Example:

fmt.Println(strings.PadLeft("ABC", "*", 3)) // ***ABC
fmt.Println(strings.PadRight("ABC", "*", 3)) // ABC***
fmt.Println(strings.Pad("ABC", "*", 2)) // **ABC**

⬆️ Back to Top

Dedent

Removes the indentation from the given string.

func Dedent(s string, n int) string

Example:

fmt.Println(strings.Dedent(`
    Hello, world!
        This is a test.
      Goodbye!
`, 2))
// Output:
// Hello, world!
//   This is a test.
// Goodbye!

⬆️ Back to Top


πŸ“‘ License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation ΒΆ

Index ΒΆ

Examples ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var CaseRegex = map[Case]*regexp.Regexp{
	CamelCase: regexp.MustCompile(`^[a-z][a-zA-Z0-9]+(?:[A-Z][a-z0-9]+)*$`),
	SnakeCase: regexp.MustCompile(`^[a-z][a-zA-Z0-9]+(?:[_][a-z][a-z0-9]+)*$`),
	KebabCase: regexp.MustCompile(`^[a-z][a-zA-Z0-9]+(?:[-][a-z][a-z0-9]+)*$`),
	TitleCase: regexp.MustCompile(`^[A-Z][a-zA-Z0-9]+(?:[A-Z][a-zA-Z0-9]+)*$`),
}

Functions ΒΆ

func Align ΒΆ

func Align(str string, opts *AlignOptions) string

func AlignCenter ΒΆ

func AlignCenter(str string) string

Center aligns the string

func AlignLeft ΒΆ

func AlignLeft(str string) string

Left aligns the string

func AlignRight ΒΆ

func AlignRight(str string) string

Right aligns the string

func Capitalize ΒΆ

func Capitalize(s string) string

Capitalize the string (make the first letter uppercase)

func Dedent ΒΆ

func Dedent(s string, level int) string

Dedent removes leading spaces and tabs from each line of the given string up to the specified level.

func DetermineIndentation ΒΆ

func DetermineIndentation(lines ...string) (min, max int)

Determines the minimum and maximum level of indentation for the given lines. The lines can be passed as a slice of strings or as a single multi-line string.

func DetermineWidths ΒΆ

func DetermineWidths(array [][]string) []int

Determines the width of each column in a 2D array of strings

func HereDoc ΒΆ

func HereDoc(s string) string

HereDoc returns a here-document representation of the given string. See https://en.m.wikipedia.org/wiki/Here_document for more information.

Example (Multiline) ΒΆ

Multiline example

// Print a here-document
fmt.Println(HereDoc(`
		Hello,
			World!
	`))
Output:

Hello,
	World!
Example (Simple) ΒΆ

Simple example

// Print a here-document
fmt.Println(HereDoc(`
		Hello, World!
	`))
Output:

Hello, World!

func HereDocf ΒΆ

func HereDocf(format string, args ...any) string

HereDocf returns a here-document (https://en.m.wikipedia.org/wiki/Here_document) representation of the given string. The can be formatted using the given arguments as in fmt.Sprintf.

Example (Multiline) ΒΆ

Multiline example

// Print a here-document
fmt.Println(HereDocf(`
		%s,
			%s!
	`, "Hello", "World"))
Output:

Hello,
	World!
Example (Simple) ΒΆ

Simple example

// Print a here-document
fmt.Println(HereDocf(`
		%s
	`, "Hello, World!"))
Output:

Hello, World!

func IsCase ΒΆ

func IsCase(s string, c Case) bool

IsCase returns true if the string is in the specified case

func IsSpace ΒΆ

func IsSpace(r rune) bool

Checks if the given rune is a space character.

func Pad ΒΆ

func Pad(s, char string, count int) string

Apply padding around the string

Example ΒΆ
str := "Golang"
newStr := Pad(str, "|", 2)
fmt.Println(newStr)
Output:

||Golang||

func PadLeft ΒΆ

func PadLeft(s, char string, count int) string

Apply padding to the left of the string

Example ΒΆ
str := "Golang"
newStr := PadLeft(str, ">>", 1)
fmt.Println(newStr)
Output:

>>Golang

func PadRight ΒΆ

func PadRight(s, char string, count int) string

Apply padding to the right of the string

Example ΒΆ
str := "Golang"
newStr := PadRight(str, ">", 3)
fmt.Println(newStr)
Output:

Golang>>>

func ToCamelCase ΒΆ

func ToCamelCase(s string) string

Convert the string to camelCase

func ToCase ΒΆ

func ToCase(s string, c Case) string

Convert the string to the specified case

func ToKebabCase ΒΆ

func ToKebabCase(s string) string

Convert the string to kebab-case

func ToSnakeCase ΒΆ

func ToSnakeCase(s string) string

Convert the string to snake_case

func ToTitleCase ΒΆ

func ToTitleCase(s string) string

Convert the string to TitleCase

func Uncapitalize ΒΆ

func Uncapitalize(s string) string

Uncapitalize the string (make the first letter lowercase)

Types ΒΆ

type AlignOptions ΒΆ

type AlignOptions struct {
	Mode  string
	Split string
	Pad   string
	Width int
}

func (*AlignOptions) AssignValues ΒΆ

func (opts *AlignOptions) AssignValues()

AssignValues assigns default values to the options

type Case ΒΆ

type Case int
const (
	CamelCase Case = iota
	SnakeCase
	KebabCase
	TitleCase
)

func DetermineCase ΒΆ

func DetermineCase(s string) Case

DetermineCase returns the case of the string or -1 if it is not a known case

type StringAndWidthTuple ΒΆ

type StringAndWidthTuple struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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