varcaser

package
v0.0.0-...-e3fb03e Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2019 License: MIT Imports: 5 Imported by: 22

Documentation

Overview

Package varcaser provides a way to change the case of variable names.

result := Caser{From: LowerCamelCase, To: KebabCase}.String("someInitMethod")
// "some-init-method"
result := Caser{From: LowerCamelCase,
      To: ScreamingSnakeCase}.String("myConstantVariable")
// "MY_CONSTANT_VARIABLE"

Index

Constants

This section is empty.

Variables

View Source
var ErrInconsistentData = fmt.Errorf("Inconsistent data provided.")

ErrInconsistentData is returned when the input data contradicts itself. In this case, no useful information is returned from Detect().

View Source
var ErrNoData = fmt.Errorf("No data provided.")

ErrNoData is returned when an empty or nil slice is passed in. In this case, no useful information is returned from Detect().

View Source
var ErrNotEnoughData = fmt.Errorf("Not enough data provided.")

ErrNotEnoughData is returned when an accurate determination could not be made. However, the best guess will be provided in the returned CaseConvention.

View Source
var HttpAcronyms = map[string]bool{
	"XSS":  true,
	"SSL":  true,
	"HTTP": true,
	"MD5":  true,
	"TE":   true,
	"DNT":  true,
	"UIDH": true,
	"P3P":  true,
	"WWW":  true,
	"CSP":  true,
	"UA":   true,
}

HttpAcronyms is effectively a set of acronyms that are conventionally uppercased in the HTTP Casing Convention.

View Source
var HttpHeaderCase = CaseConvention{
	JoinStyle:      SimpleJoinStyle("-"),
	InitialCase:    ToHttpTitle,
	SubsequentCase: ToHttpTitle,
	Example:        "HTTP-Header-Case",
}
View Source
var KebabCase = CaseConvention{
	JoinStyle:      SimpleJoinStyle("-"),
	InitialCase:    strings.ToLower,
	SubsequentCase: strings.ToLower,
	Example:        "kebab-case",
}
View Source
var LowerCamelCase = CaseConvention{
	JoinStyle:      camelJoinStyle,
	InitialCase:    strings.ToLower,
	SubsequentCase: ToStrictTitle,
	Example:        "lowerCamelCase",
}
View Source
var LowerCamelCaseKeepCaps = CaseConvention{
	JoinStyle:      camelJoinStyle,
	InitialCase:    strings.ToLower,
	SubsequentCase: strings.Title,
	Example:        "lowerCamelCase",
}
View Source
var LowerSnakeCase = CaseConvention{
	JoinStyle:      SimpleJoinStyle("_"),
	InitialCase:    strings.ToLower,
	SubsequentCase: strings.ToLower,
	Example:        "lower_snake_case",
}
View Source
var ScreamingKebabCase = CaseConvention{
	JoinStyle:      SimpleJoinStyle("-"),
	InitialCase:    strings.ToUpper,
	SubsequentCase: strings.ToUpper,
	Example:        "SCREAMING-KEBAB-CASE",
}
View Source
var ScreamingSnakeCase = CaseConvention{
	JoinStyle:      SimpleJoinStyle("_"),
	InitialCase:    strings.ToUpper,
	SubsequentCase: strings.ToUpper,
	Example:        "SCREAMING_SNAKE_CASE",
}
View Source
var UpperCamelCase = CaseConvention{
	JoinStyle:      camelJoinStyle,
	InitialCase:    ToStrictTitle,
	SubsequentCase: ToStrictTitle,
	Example:        "UpperCamelCase",
}
View Source
var UpperCamelCaseKeepCaps = CaseConvention{
	JoinStyle:      camelJoinStyle,
	InitialCase:    strings.Title,
	SubsequentCase: strings.Title,
	Example:        "UpperCamelCase",
}
View Source
var UpperKebabCase = CaseConvention{
	JoinStyle:      SimpleJoinStyle("-"),
	InitialCase:    ToStrictTitle,
	SubsequentCase: ToStrictTitle,
	Example:        "Upper-Kebab-Case",
}

Functions

func ToHttpTitle

func ToHttpTitle(s string) string

ToHttpTitle returns a string titled the way HTTP Headers title it.

func ToStrictTitle

func ToStrictTitle(s string) string

ToStrictTitle returns the strict titling of a string without preserving existing caps in acronyms.

func UpdateJoinStylePrediction

func UpdateJoinStylePrediction(data string, sep rune) (rune, error)

UpdateJoinStylePrediction works by comparing the data string to the rune that represents an expected JoinStyle, updating that rune as necessary.

Types

type CaseConvention

type CaseConvention struct {
	JoinStyle
	SubsequentCase WordCase
	InitialCase    WordCase
	Example        string // Render the name of this case convention in itself
}

A CaseConvention is a way of writing variable names using separators and casing style.

func (CaseConvention) SplitWords

func (c CaseConvention) SplitWords(s string) []string

SplitWords allows CaseConvention to implement Splitter.

type Caser

type Caser struct {
	From Splitter
	To   CaseConvention
	transform.NopResetter
}

type Caser is a text transformer that takes converts a variable from one casing convention to another.

func (Caser) Bytes

func (c Caser) Bytes(b []byte) []byte

Bytes is provided for compatibility with the Transformer interface. Since Caser has no special treatment of bytes, the bytes are converted to and from strings.

func (Caser) String

func (c Caser) String(s string) string

String returns the representation of a variable name in this Caser's To CaseConvention given a variable name in this Caser's From CaseConvention.

func (Caser) Transform

func (c Caser) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error)

Provided for compatibility with the Transformer interface. Since Caser has no special treatment of bytes, the bytes are converted to and from strings. Will treat the entirety of src as ONE variable name.

type Detected

type Detected struct {
	Split func(string) []string
}

func (Detected) SplitWords

func (d Detected) SplitWords(s string) []string

type JoinStyle

type JoinStyle struct {
	Join  func([]string) string
	Split func(string) []string
}

A JoinStyle is a way of representing how individual components of a variable name are put together, and how to pull them apart.

func SimpleJoinStyle

func SimpleJoinStyle(sep string) JoinStyle

SimpleJoinStyle creates a JoinStyle that just splits and joins by a separator.

type Splitter

type Splitter interface {
	SplitWords(string) []string
}

Splitter is an interface for a type that can decompose a variable name into its component words.

func Detect

func Detect(data []string) (sp Splitter, err error)

Detect returns a Splitter suitable for taking a variable name and turning it into a sequence of words.

type WordCase

type WordCase func(string) string

Jump to

Keyboard shortcuts

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