cases

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: Apache-2.0, MIT Imports: 2 Imported by: 2

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.ToTitle(s) Title Case
cases.ToTrain(s) Train-Case
cases.Transform(s, wordFn, delimFn) your own case here

Word boundaries are defined as follows:

  • A set consecutive Unicode spaces, underscores or hyphens 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"
)

cases.Transform("XmlHttpRequest", cases.ToLower,
    func (s *strings.Builder) { s.WriteRune('.') }) // 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
acronyms := map[string]string{
    "id":   "ID",
    "http": "HTTP",
    "xml":  "XML",
}
writeFn := func(s *strings.Builder, word string) {
    if w, ok := acronyms[word]; ok {
        s.WriteString(w)
    } else {
        // fallback to default
        cases.WriteTitle(s, word)
    }
}
cases.Transform("xml_http_request", writeFn, cases.DelimNone) // 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 DelimNone

func DelimNone(s *strings.Builder)

DelimNone is a delimiter function that does not insert any delimiter.

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 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 Transform

func Transform(s string, wordFn WriteFn, delimFn 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

type DelimFn

type DelimFn = func(s *strings.Builder)

type WriteFn

type WriteFn = func(s *strings.Builder, word string)

Directories

Path Synopsis
go module

Jump to

Keyboard shortcuts

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