strcase

package
v0.0.0-...-67c52db Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 1 Imported by: 1

README

strcase

strcase is a go package for converting string case to various cases (e.g. snake case or camel case) to see the full conversion table below.

Example

s := "AnyKind.of_string"
Function Result
ToCamel(s) anyKindOfString
ToPascal(s) AnyKindOfString
ToSnake(s) any_kind_of_string
ToSnakeWithIgnore(s, '.') any_kind.of_string
ToDot(s) any.kind.of.string
ToDotWithIgnore(s, '_') any.kind.of_string
ToKebab(s) any-kind-of-string
ToKebabWithIgnore(s, '.') any-kind.of-string

Usage

package strcase_test

import (
	"fmt"

	"github.com/sraphs/go/x/strcase"
)

func Example() {
	fmt.Println(strcase.ToCamel("AnyKind.of_string"))
	fmt.Println(strcase.ToPascal("AnyKind.of_string"))
	fmt.Println(strcase.ToSnake("AnyKind.of_string"))
	fmt.Println(strcase.ToSnakeWithIgnore("AnyKind.of_string", "."))
	fmt.Println(strcase.ToDot("AnyKind.of_string"))
	fmt.Println(strcase.ToDotWithIgnore("AnyKind.of_string", "_"))
	fmt.Println(strcase.ToKebab("AnyKind.of_string"))
	fmt.Println(strcase.ToKebabWithIgnore("AnyKind.of_string", "."))
	// Output:
	// anyKindOfString
	// AnyKindOfString
	// any_kind_of_string
	// any_kind.of_string
	// any.kind.of.string
	// any.kind.of_string
	// any-kind-of-string
	// any-kind.of-string
}

Custom Acronyms for ToCamel && ToLowerCamel

Often times text can contain specific acronyms which you need to be handled a certain way. Out of the box strcase treats the string "ID" as "Id" or "id" but there is no way to cater for every case in the wild.

To configure your custom acronym globally you can use the following before running any conversion

import (
    "github.com/sraphs/go/x/strcase"
)

func init() {
    // results in "Api" using ToCamel("API")
    // results in "api" using ToLowerCamel("API")
    strcase.ConfigureAcronym("API", "api")
    
    // results in "PostgreSQL" using ToCamel("PostgreSQL")
    // results in "postgreSQL" using ToLowerCamel("PostgreSQL")
    strcase.ConfigureAcronym("PostgreSQL", "PostgreSQL")

}

Documentation

Overview

package strcase is a go package for converting string case to various cases (e.g. snake case or camel case).

Example
package main

import (
	"fmt"

	"github.com/sraphs/go/x/strcase"
)

func main() {
	fmt.Println(strcase.ToCamel("AnyKind.of_string"))
	fmt.Println(strcase.ToPascal("AnyKind.of_string"))
	fmt.Println(strcase.ToSnake("AnyKind.of_string"))
	fmt.Println(strcase.ToSnakeWithIgnore("AnyKind.of_string", "."))
	fmt.Println(strcase.ToDot("AnyKind.of_string"))
	fmt.Println(strcase.ToDotWithIgnore("AnyKind.of_string", "_"))
	fmt.Println(strcase.ToKebab("AnyKind.of_string"))
	fmt.Println(strcase.ToKebabWithIgnore("AnyKind.of_string", "."))
}
Output:

anyKindOfString
AnyKindOfString
any_kind_of_string
any_kind.of_string
any.kind.of.string
any.kind.of_string
any-kind-of-string
any-kind.of-string

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigureAcronym

func ConfigureAcronym(key, val string)

ConfigureAcronym allows you to add additional words which will be considered acronyms

func ToCamel

func ToCamel(s string) string

func ToDelimited

func ToDelimited(s string, delimiter rune) string

ToDelimited converts a string to delimited.snake.case (in this case `delimiter = '.'`)

func ToDot

func ToDot(s string) string

converts a string to dot.case

func ToDotWithIgnore

func ToDotWithIgnore(s string, ignore string) string

func ToKebab

func ToKebab(s string) string

ToKebab converts a string to kebab-case

func ToKebabWithIgnore

func ToKebabWithIgnore(s string, ignore string) string

func ToPascal

func ToPascal(s string) string

func ToSnake

func ToSnake(s string) string

ToSnake converts a string to snake_case

func ToSnakeWithIgnore

func ToSnakeWithIgnore(s string, ignore string) string

Types

This section is empty.

Jump to

Keyboard shortcuts

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