Documentation
¶
Overview ¶
Package snaker provides methods to convert CamelCase to and from snake_case.
Correctly recognizes common (Go idiomatic) initialisms (HTTP, XML, etc) and provides a mechanism to override/set recognized initialisms.
Example ¶
package main
import (
"fmt"
"github.com/kenshaw/snaker"
)
func main() {
fmt.Println("Change CamelCase -> snake_case:", snaker.CamelToSnake("AnIdentifier"))
fmt.Println("Change CamelCase -> snake_case (2):", snaker.CamelToSnake("XMLHTTPACL"))
fmt.Println("Change snake_case -> CamelCase:", snaker.SnakeToCamel("an_identifier"))
fmt.Println("Force CamelCase:", snaker.ForceCamelIdentifier("APoorly_named_httpMethod"))
fmt.Println("Force lower camelCase:", snaker.ForceLowerCamelIdentifier("APoorly_named_httpMethod"))
fmt.Println("Force lower camelCase (2):", snaker.ForceLowerCamelIdentifier("XmlHttpACL"))
fmt.Println("Change snake_case identifier -> CamelCase:", snaker.SnakeToCamelIdentifier("__2__xml___thing---"))
}
Output: Change CamelCase -> snake_case: an_identifier Change CamelCase -> snake_case (2): xml_http_acl Change snake_case -> CamelCase: AnIdentifier Force CamelCase: APoorlyNamedHTTPMethod Force lower camelCase: aPoorlyNamedHTTPMethod Force lower camelCase (2): xmlHTTPACL Change snake_case identifier -> CamelCase: XMLThing
Index ¶
- func CamelToSnake(name string) string
- func CamelToSnakeIdentifier(name string) string
- func CommonInitialisms() []string
- func CommonPlurals() []string
- func ForceCamelIdentifier(name string) string
- func ForceLowerCamelIdentifier(name string) string
- func IsInitialism(s string) bool
- func SnakeToCamel(name string) string
- func SnakeToCamelIdentifier(name string) string
- func ToIdentifier(s string) string
- func ToKebab(s string) string
- type Initialisms
- func (ini *Initialisms) Add(initialisms ...string) error
- func (ini *Initialisms) CamelToSnake(name string) string
- func (ini *Initialisms) CamelToSnakeIdentifier(name string) string
- func (ini *Initialisms) ForceCamelIdentifier(name string) string
- func (ini *Initialisms) ForceLowerCamelIdentifier(name string) string
- func (ini *Initialisms) Is(s string) bool
- func (ini *Initialisms) Peek(r []rune) string
- func (ini *Initialisms) Post(pairs ...string) error
- func (ini *Initialisms) SnakeToCamel(name string) string
- func (ini *Initialisms) SnakeToCamelIdentifier(name string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CamelToSnake ¶
CamelToSnake converts name from camel case ("AnIdentifier") to snake case ("an_identifier").
func CamelToSnakeIdentifier ¶
CamelToSnakeIdentifier converts name from camel case to a snake case identifier.
func CommonInitialisms ¶ added in v0.1.6
func CommonInitialisms() []string
CommonInitialisms returns the set of common initialisms.
Originally built from the list in golang.org/x/lint @ 738671d.
Note: golang.org/x/lint has since been deprecated, and some additional initialisms have since been added.
func CommonPlurals ¶ added in v0.4.0
func CommonPlurals() []string
CommonPlurals returns initialisms that have a common plural of s.
func ForceCamelIdentifier ¶
ForceCamelIdentifier forces name to its CamelCase specific to Go ("AnIdentifier").
func ForceLowerCamelIdentifier ¶
ForceLowerCamelIdentifier forces the first portion of an identifier to be lower case ("anIdentifier").
func IsInitialism ¶
IsInitialism indicates whether or not s is a registered initialism.
func SnakeToCamelIdentifier ¶
SnakeToCamelIdentifier converts name to its CamelCase identifier (first letter is capitalized).
func ToIdentifier ¶ added in v0.1.6
ToIdentifier cleans s so that it is usable as an identifier.
Substitutes invalid characters with an underscore, removes any leading numbers/underscores, and removes trailing underscores.
Additionally collapses multiple underscores to a single underscore.
Makes no changes to case.
Types ¶
type Initialisms ¶ added in v0.1.6
type Initialisms struct {
// contains filtered or unexported fields
}
Initialisms is a set of initialisms.
var DefaultInitialisms *Initialisms
DefaultInitialisms is the set of default (common) initialisms used by the package level conversions funcs.
func New ¶ added in v0.1.6
func New(initialisms ...string) (*Initialisms, error)
New creates a new set of initialisms.
func NewDefaultInitialisms ¶ added in v0.2.0
func NewDefaultInitialisms() (*Initialisms, error)
NewDefaultInitialisms creates a set of known, common initialisms.
func (*Initialisms) Add ¶ added in v0.1.6
func (ini *Initialisms) Add(initialisms ...string) error
Add adds a known initialisms.
func (*Initialisms) CamelToSnake ¶ added in v0.1.6
func (ini *Initialisms) CamelToSnake(name string) string
CamelToSnake converts name from camel case ("AnIdentifier") to snake case ("an_identifier").
func (*Initialisms) CamelToSnakeIdentifier ¶ added in v0.1.6
func (ini *Initialisms) CamelToSnakeIdentifier(name string) string
CamelToSnakeIdentifier converts name from camel case to a snake case identifier.
func (*Initialisms) ForceCamelIdentifier ¶ added in v0.1.6
func (ini *Initialisms) ForceCamelIdentifier(name string) string
ForceCamelIdentifier forces name to its CamelCase specific to Go ("AnIdentifier").
func (*Initialisms) ForceLowerCamelIdentifier ¶ added in v0.1.6
func (ini *Initialisms) ForceLowerCamelIdentifier(name string) string
ForceLowerCamelIdentifier forces the first portion of an identifier to be lower case ("anIdentifier").
func (*Initialisms) Is ¶ added in v0.4.0
func (ini *Initialisms) Is(s string) bool
Is indicates whether or not s is a registered initialism.
func (*Initialisms) Peek ¶ added in v0.1.6
func (ini *Initialisms) Peek(r []rune) string
Peek returns the next longest possible initialism in v.
func (*Initialisms) Post ¶ added in v0.2.0
func (ini *Initialisms) Post(pairs ...string) error
Post adds a key, value pair to the initialisms and post map.
func (*Initialisms) SnakeToCamel ¶ added in v0.1.6
func (ini *Initialisms) SnakeToCamel(name string) string
SnakeToCamel converts name to CamelCase.
func (*Initialisms) SnakeToCamelIdentifier ¶ added in v0.1.6
func (ini *Initialisms) SnakeToCamelIdentifier(name string) string
SnakeToCamelIdentifier converts name to its CamelCase identifier (first letter is capitalized).