snaker

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2025 License: MIT Imports: 3 Imported by: 88

README

snaker Go Package

Package snaker provides methods to convert CamelCase, snake_case, and kebab-case to and from each other. Correctly recognizes common (Go idiomatic) initialisms (HTTP, XML, etc) with the ability to override/set recognized initialisms.

Example

package snaker_test

import (
	"fmt"

	"github.com/kenshaw/snaker"
)

func Example() {
	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
}

See the package example.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelToSnake

func CamelToSnake(name string) string

CamelToSnake converts name from camel case ("AnIdentifier") to snake case ("an_identifier").

func CamelToSnakeIdentifier

func CamelToSnakeIdentifier(name string) string

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

func ForceCamelIdentifier(name string) string

ForceCamelIdentifier forces name to its CamelCase specific to Go ("AnIdentifier").

func ForceLowerCamelIdentifier

func ForceLowerCamelIdentifier(name string) string

ForceLowerCamelIdentifier forces the first portion of an identifier to be lower case ("anIdentifier").

func IsInitialism

func IsInitialism(s string) bool

IsInitialism indicates whether or not s is a registered initialism.

func SnakeToCamel

func SnakeToCamel(name string) string

SnakeToCamel converts name to CamelCase.

func SnakeToCamelIdentifier

func SnakeToCamelIdentifier(name string) string

SnakeToCamelIdentifier converts name to its CamelCase identifier (first letter is capitalized).

func ToIdentifier added in v0.1.6

func ToIdentifier(s string) string

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.

func ToKebab added in v0.4.0

func ToKebab(s string) string

ToKebab changes s to kebab case.

Substitutes invalid characters with a hyphen, removes any leading numbers/hyphens, and removes trailing hyphens.

Additionally collapses multiple hyphens to a single hyphen.

Converts the string to lower 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).

Jump to

Keyboard shortcuts

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