Skip to content

💼 A case conversion library for Go with Unicode support

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

rossmacarthur/cases

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cases

Go Reference Build Status

A case conversion library for Go.

cases

The currently supported cases are:

Function Output
cases.ToCamel(s) camelCase
cases.ToPascal(s) PascalCase
cases.ToSnake(s) snake_case
cases.ToScreamingSnake(s) SCREAMING_SNAKE_CASE
cases.ToKebab(s) kebab-case
cases.ToScreamingKebab(s) SCREAMING-KEBAB-CASE
cases.ToTrain(s) Train-Case
cases.ToLower(s) lower case
cases.ToTitle(s) Title Case
cases.ToUpper(s) UPPER CASE
cases.Transform(s, wordFn, delimFn) your own case here

Word boundaries are defined as follows:

  • A set of consecutive Unicode non-letter/number/symbol e.g. foo _bar is two words (foo and bar)
  • A transition from a lowercase letter to an uppercase letter e.g. fooBar is two words (foo and Bar)
  • The second last uppercase letter in a word with multiple uppercase letters e.g. FOOBar is two words (FOO and Bar)

Getting started

Install using

go get -u github.com/rossmacarthur/cases

Now convert a string using the relevant function.

import "github.com/rossmacarthur/cases"

cases.ToSnake("XMLHttpRequest") // returns "xml_http_request"

Customizing

This library also exposes a Transform function which allows flexible customization of the output.

For example if you wanted dotted.snake.case you could do the following.

import (
    "strings"
    "github.com/rossmacarthur/cases"
)

func delimDot(s *strings.Builder) {
    s.WriteRune('.')
}

cases.Transform("XmlHttpRequest", cases.ToLower, delimDot) // returns xml.http.request

Here is a more involved example in order to handle acronyms in PascalCase.

import (
    "strings"
    "github.com/rossmacarthur/cases"
)

// The default ToPascal function has no understanding of acronyms
cases.ToPascal("xml_http_request") // returns "XmlHttpRequest"

// We can instead use Transform directly
writeFn := func(s *strings.Builder, word string) {
    w := strings.ToUpper(asLower)
    if w == "XML" || w == "HTTP" {
        s.WriteString(w)
    } else {
        // fallback to default
        cases.WriteTitle(s, word)
    }
}
cases.Transform("xml_http_request", writeFn, nil) // returns "XMLHTTPRequest"

License

This project is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.

About

💼 A case conversion library for Go with Unicode support

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Languages