snaker

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2021 License: MIT Imports: 4 Imported by: 75

README

snaker Go Package

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

A basic Go example:

// _example/example.go
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---"))
}

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.

Index

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 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 IsIdentifierChar added in v0.1.6

func IsIdentifierChar(ch rune) bool

IsIdentifierChar determines if ch is a valid character for a Go identifier.

See: go/src/go/scanner/scanner.go

func IsInitialism

func IsInitialism(s string) bool

IsInitialism indicates whether or not s is a registered initialism.

func Peek added in v0.1.6

func Peek(r []rune) string

Peek returns the next longest possible initialism in r.

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.

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 a set of default (common) initialisms.

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

NewDefaultInitialisms creates a default set of initialisms.

func (*Initialisms) Add added in v0.1.6

func (ini *Initialisms) Add(initialisms ...string) error

Add adds 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) IsInitialism added in v0.1.6

func (ini *Initialisms) IsInitialism(s string) bool

IsInitialism 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 r.

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

Directories

Path Synopsis
_example/example.go
_example/example.go

Jump to

Keyboard shortcuts

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