strcase

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2023 License: MIT Imports: 2 Imported by: 3,381

README

strcase

Godoc Reference Build Status Coverage Go Report Card

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
ToSnake(s) any_kind_of_string
ToSnakeWithIgnore(s, '.') any_kind.of_string
ToScreamingSnake(s) ANY_KIND_OF_STRING
ToKebab(s) any-kind-of-string
ToScreamingKebab(s) ANY-KIND-OF-STRING
ToDelimited(s, '.') any.kind.of.string
ToScreamingDelimited(s, '.', '', true) ANY.KIND.OF.STRING
ToScreamingDelimited(s, '.', ' ', true) ANY.KIND OF.STRING
ToCamel(s) AnyKindOfString
ToLowerCamel(s) anyKindOfString

Install

go get -u github.com/iancoleman/strcase

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/iancoleman/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 converts strings to various cases. See the conversion table below:

| Function                        | Result             |
|---------------------------------|--------------------|
| ToSnake(s)                      | any_kind_of_string |
| ToScreamingSnake(s)             | ANY_KIND_OF_STRING |
| ToKebab(s)                      | any-kind-of-string |
| ToScreamingKebab(s)             | ANY-KIND-OF-STRING |
| ToDelimited(s, '.')             | any.kind.of.string |
| ToScreamingDelimited(s, '.')    | ANY.KIND.OF.STRING |
| ToCamel(s)                      | AnyKindOfString    |
| ToLowerCamel(s)                 | anyKindOfString    |

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigureAcronym added in v0.1.3

func ConfigureAcronym(key, val string)

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

func ToCamel

func ToCamel(s string) string

ToCamel converts a string to CamelCase

func ToDelimited

func ToDelimited(s string, delimiter uint8) string

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

func ToKebab

func ToKebab(s string) string

ToKebab converts a string to kebab-case

func ToLowerCamel

func ToLowerCamel(s string) string

ToLowerCamel converts a string to lowerCamelCase

func ToScreamingDelimited

func ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bool) string

ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `delimiter = '.'; screaming = true`) or delimited.snake.case (in this case `delimiter = '.'; screaming = false`)

func ToScreamingKebab

func ToScreamingKebab(s string) string

ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE

func ToScreamingSnake

func ToScreamingSnake(s string) string

ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE

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