cases

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: Apache-2.0, MIT Imports: 2 Imported by: 1

README

cases

Go Reference Build Status

A case conversion library for Go.

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DelimHyphen

func DelimHyphen(s *strings.Builder)

DelimHyphen is a delimiter function that inserts a hyphen.

func DelimSpace added in v0.1.3

func DelimSpace(s *strings.Builder)

DelimSpace is a delimiter function that inserts a space.

func DelimUnderscore

func DelimUnderscore(s *strings.Builder)

DelimUnderscore is a delimiter function that inserts an underscore.

func ToCamel

func ToCamel(s string) string

ToCamel converts a string to camelCase.

func ToKebab

func ToKebab(s string) string

ToKebab converts a string to kebab-case.

func ToLower added in v0.2.0

func ToLower(s string) string

ToLower converts a string to lower case.

func ToPascal

func ToPascal(s string) string

ToPascal converts a string to PascalCase.

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_SKAKE_CASE.

func ToSnake

func ToSnake(s string) string

ToSnake converts a string to snake_case.

func ToTitle

func ToTitle(s string) string

ToTitle converts a string to Title Case.

func ToTrain

func ToTrain(s string) string

ToTrain converts a string to Train-Case.

func ToUpper added in v0.2.0

func ToUpper(s string) string

ToUpper converts a string to UPPER CASE.

func Transform

func Transform(s string, wf writeFn, df delimFn) string

Transform reconstructs the string using the given functions.

wordFn is called for each word and delimFn is called for each word boundary.

func WriteLower

func WriteLower(s *strings.Builder, word string)

WriteLower writes the word in lowercase.

func WriteTitle

func WriteTitle(s *strings.Builder, word string)

WriteTitle writes the word in title case.

func WriteUpper

func WriteUpper(s *strings.Builder, word string)

WriteUpper writes the word in uppercase.

Types

This section is empty.

Jump to

Keyboard shortcuts

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