stringcase

package module
v0.0.0-...-9c75179 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2020 License: MIT Imports: 2 Imported by: 0

README

stringcase

Build codecov goreport GoDoc MIT License

Easy string case conversion in Go

Usage

s := "MIXED-caseString.test"
Converter Output
ToSnake(s) mixed_case_string_test
ToUpperSnake(s) MIXED_CASE_STRING_TEST
ToCamel(s) mixedCaseStringTest
ToUpperCamel(s) MixedCaseStringTest
ToKebab(s) mixed-case-string-test
ToUpperKebab(s) MIXED-CASE-STRING-TEST

If you are curious about the rules for converting strings, take a look at the test file for examples.

Install

This package uses go modules and you should be able to use the latest release tag.

go get -u github.com/mrosales/stringcase

Implementation Details

Special Characters

All converters will remove any non-alphanumeric characters. That means things like emojis, punctuation, whitespace and other unicode characters will all be removed.

Word Breaks

Before transforming the string, it is first split into words. This implementation tries to do the right thing™, but it may very well break some part of your use case. If you encounter something that seems incorrect, don't hesitate to file an issue. That being said, it may not be possible to update the library to cover every edge case.

The following cases trigger a word break:

  • Any non alphanumeric character ([^a-zA-Z0-9])

  • Any whitespace (\s+)

  • Transitioning from lowercase to uppercase This attempts to respect Go's preference to capitalize acronyms and abbreviations.

    • fooBar -> foo Bar
    • fooBAR -> foo BAR
    • userID -> user ID
    • userIDFOO -> user IDFOO (nothing we can really do about a string like this)
  • Transitioning from uppercase to lowercase

    • userIDBar -> user ID Bar

Inspiration

  • github.com/iancoleman/strcase
    • This is a great library that I used for some time before this, but I felt like the camelcase implementation in particular seemed inconsistent. For example, I would prefer ToCamel("fooBAR") to return "fooBar" rather than "fooBar". This is not necessarily wrong, but it does not match how ToSnake("fooBAR") for that library returns foo_bar.
    • The change that this stringcase library makes is to first split the string into words and then join them in canonical fashion for each type of casing. This is slightly less efficient, but I find it to produce more predictable results.

Released under the MIT License.

Documentation

Overview

Package stringcase implements utility functions for changing the case of simple strings.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToCamel

func ToCamel(s string) string

ToCamel converts a string to camelCase.

Example
fmt.Println(ToCamel("camel case"))
Output:
camelCase

func ToKebab

func ToKebab(s string) string

ToKebab converts a string to kebab-case.

Example
fmt.Println(ToKebab("kebab case"))
Output:
kebab-case

func ToSnake

func ToSnake(s string) string

ToSnake converts a string to snake_case.

Example
fmt.Println(ToSnake("snake case"))
Output:
snake_case

func ToUpperCamel

func ToUpperCamel(s string) string

ToUpperCamel converts a string to UpperCamelCase (aka PascalCase).

Example
fmt.Println(ToUpperCamel("upper_camel_case"))
Output:
UpperCamelCase

func ToUpperKebab

func ToUpperKebab(s string) string

ToUpperKebab converts a string to UPPER-KEBAB-CASE.

Example
fmt.Println(ToUpperKebab("upper kebab case"))
Output:
UPPER-KEBAB-CASE

func ToUpperSnake

func ToUpperSnake(s string) string

ToUpperSnake converts a string to UPPER_SNAKE_CASE.

Example
fmt.Println(ToUpperSnake("upper snake case"))
Output:
UPPER_SNAKE_CASE

Types

This section is empty.

Jump to

Keyboard shortcuts

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