Skip to content

xogas/stringcase

Repository files navigation

stringcase

stringcase is a Go package for converting strings between different naming conventions (snake_case, camelCase, PascalCase, kebab-case, and more).

Install

go get github.com/xogas/stringcase

Examples

Snake case

stringcase.ToSnake("testCase")       // "test_case"
stringcase.ToSnake("JSONData")       // "json_data"

Upper snake case

stringcase.ToSnakeUpper("testCase")   // "TEST_CASE"
stringcase.ToSnakeUpper("jsonData")   // "JSON_DATA"

Camel case

stringcase.ToCamel("test case")   // "testCase"
stringcase.ToCamel("test_case")   // "testCase"
stringcase.ToCamel("Test-Case")   // "testCase"
stringcase.ToCamel("v_2_bate_3")  // "v2Bate3"

Pascal case

stringcase.ToPascal("test case")   // "TestCase"
stringcase.ToPascal("test_case")   // "TestCase"
stringcase.ToPascal("test-case")   // "TestCase"
stringcase.ToPascal("v_2_bate_3")  // "V2Bate3"

Kebab case

stringcase.ToKebab("testCase")       // "test-case"
stringcase.ToKebab("Test Case")      // "test-case"

stringcase.ToKebabUpper("testCase")  // "TEST-CASE"

Custom delimiter

stringcase.ToDelimited("testCase", '.')       // "test.case"
stringcase.ToDelimitedUpper("testCase", '.')  // "TEST.CASE"

Custom acronyms

import "github.com/xogas/stringcase"

func init() {
    // "API" will be converted to "api" in camelCase,
    // and to "Api" in PascalCase
    stringcase.ConfigureAcronyms("API", "api")

    // ToCamel("API") -> "api"
    // ToPascal("API") -> "Api"
}

API Reference

Function Description
ToSnake(s) Converts to snake_case
ToSnakeUpper(s) Converts to UPPER_SNAKE_CASE
ToCamel(s) Converts to camelCase
ToPascal(s) Converts to PascalCase
ToKebab(s) Converts to kebab-case
ToKebabUpper(s) Converts to UPPER-KEBAB-CASE
ToDelimited(s, delimiter) Converts using a custom delimiter
ToDelimitedUpper(s, delimiter) Converts using a custom delimiter, uppercased
ConfigureAcronyms(key, val) Registers an acronym for case conversion

License

The MIT License (MIT) — see license for more details.

About

A Golang package for converting to snake_case or CamelCase

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages