forked from mirror/enumer
Use go modules
This commit is contained in:
parent
0fa5a42e5f
commit
b6edbe712f
|
@ -0,0 +1,5 @@
|
|||
module github.com/alvaroloes/enumer
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1
|
|
@ -0,0 +1,2 @@
|
|||
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1 h1:/I3lTljEEDNYLho3/FUB7iD/oc2cEFgVmbHzV+O0PtU=
|
||||
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1/go.mod h1:eD5JxqMiuNYyFNmyY9rkJ/slN8y59oEu4Ei7F8OoKWQ=
|
|
@ -1,3 +0,0 @@
|
|||
To the extent possible under law, Pascal S. de Kloe has waived all
|
||||
copyright and related or neighboring rights to Colfer. This work is
|
||||
published from The Netherlands.
|
|
@ -1,6 +0,0 @@
|
|||
[![GoDoc](https://godoc.org/github.com/pascaldekloe/name?status.svg)](https://godoc.org/github.com/pascaldekloe/name)
|
||||
|
||||
Naming convention library for the Go programming language (golang).
|
||||
|
||||
This is free and unencumbered software released into the
|
||||
[public domain](http://creativecommons.org/publicdomain/zero/1.0).
|
|
@ -1,112 +0,0 @@
|
|||
// Package name implements naming conventions.
|
||||
package name
|
||||
|
||||
import "unicode"
|
||||
|
||||
// CamelCase returns the medial capitals form of word sequence s.
|
||||
// The input can be any case or even just a bunch of words.
|
||||
// Upper case abbreviations are preserved.
|
||||
// Argument upper sets the casing for the first rune.
|
||||
func CamelCase(s string, upper bool) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
out := make([]rune, 1, len(s)+5)
|
||||
for i, r := range s {
|
||||
if i == 0 {
|
||||
if upper {
|
||||
r = unicode.ToUpper(r)
|
||||
}
|
||||
out[0] = r
|
||||
continue
|
||||
}
|
||||
|
||||
if i == 1 {
|
||||
if !upper && unicode.Is(unicode.Lower, r) {
|
||||
out[0] = unicode.ToLower(out[0])
|
||||
}
|
||||
|
||||
upper = false
|
||||
}
|
||||
|
||||
switch {
|
||||
case unicode.IsLetter(r):
|
||||
if upper {
|
||||
r = unicode.ToUpper(r)
|
||||
}
|
||||
|
||||
fallthrough
|
||||
case unicode.IsNumber(r):
|
||||
upper = false
|
||||
out = append(out, r)
|
||||
|
||||
default:
|
||||
upper = true
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// SnakeCase is an alias for Delimit(s, '_').
|
||||
func SnakeCase(s string) string {
|
||||
return Delimit(s, '_')
|
||||
}
|
||||
|
||||
// DotSeparated is an alias for Delimit(s, '.').
|
||||
func DotSeparated(s string) string {
|
||||
return Delimit(s, '.')
|
||||
}
|
||||
|
||||
// Delimit returns word sequence s delimited with sep.
|
||||
// The input can be any case or even just a bunch of words.
|
||||
// Upper case abbreviations are preserved. Use strings.ToLower
|
||||
// and strings.ToUpper to enforce a letter case.
|
||||
func Delimit(s string, sep rune) string {
|
||||
out := make([]rune, 0, len(s)+5)
|
||||
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case unicode.IsUpper(r):
|
||||
if last := len(out) - 1; last >= 0 && unicode.IsLower(out[last]) {
|
||||
out = append(out, sep)
|
||||
}
|
||||
|
||||
case unicode.IsLetter(r):
|
||||
if i := len(out) - 1; i >= 0 {
|
||||
if last := out[i]; unicode.IsUpper(last) {
|
||||
out = out[:i]
|
||||
if i > 0 && out[i-1] != sep {
|
||||
out = append(out, sep)
|
||||
}
|
||||
out = append(out, unicode.ToLower(last))
|
||||
}
|
||||
}
|
||||
|
||||
case !unicode.IsNumber(r):
|
||||
if i := len(out); i != 0 && out[i-1] != sep {
|
||||
out = append(out, sep)
|
||||
}
|
||||
continue
|
||||
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
|
||||
if len(out) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// trim tailing separator
|
||||
if i := len(out) - 1; out[i] == sep {
|
||||
out = out[:i]
|
||||
}
|
||||
|
||||
if len(out) == 1 {
|
||||
out[0] = unicode.ToLower(out[0])
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
package name
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type goldenCase struct {
|
||||
snake, lowerCamel, upperCamel string
|
||||
}
|
||||
|
||||
var goldenCases = []goldenCase{
|
||||
{"", "", ""},
|
||||
{"i", "i", "I"},
|
||||
{"name", "name", "Name"},
|
||||
{"ID", "ID", "ID"},
|
||||
{"wi_fi", "wiFi", "WiFi"},
|
||||
|
||||
// single outer abbreviation
|
||||
{"vitamin_C", "vitaminC", "VitaminC"},
|
||||
{"T_cell", "TCell", "TCell"},
|
||||
|
||||
// double outer abbreviation
|
||||
{"master_DB", "masterDB", "MasterDB"},
|
||||
{"IO_bounds", "IOBounds", "IOBounds"},
|
||||
|
||||
// tripple outer abbreviation
|
||||
{"main_API", "mainAPI", "MainAPI"},
|
||||
{"TCP_conn", "TCPConn", "TCPConn"},
|
||||
|
||||
// inner abbreviation
|
||||
{"raw_URL_query", "rawURLQuery", "RawURLQuery"},
|
||||
|
||||
// numbers
|
||||
{"4x4", "4x4", "4x4"},
|
||||
{"no5", "no5", "No5"},
|
||||
{"DB2", "DB2", "DB2"},
|
||||
{"3M", "3M", "3M"},
|
||||
{"7_up", "7Up", "7Up"},
|
||||
{"20th", "20th", "20th"},
|
||||
}
|
||||
|
||||
func TestSnakeToSnake(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
s := golden.snake
|
||||
if got := SnakeCase(s); got != s {
|
||||
t.Errorf("%q: got %q", s, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLowerCamelToLowerCamel(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
s := golden.lowerCamel
|
||||
if got := CamelCase(s, false); got != s {
|
||||
t.Errorf("%q: got %q", s, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpperCamelToUpperCamel(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
s := golden.upperCamel
|
||||
if got := CamelCase(s, true); got != s {
|
||||
t.Errorf("%q: got %q", s, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnakeToLowerCamel(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
snake, want := golden.snake, golden.lowerCamel
|
||||
if got := CamelCase(snake, false); got != want {
|
||||
t.Errorf("%q: got %q, want %q", snake, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnakeToUpperCamel(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
snake, want := golden.snake, golden.upperCamel
|
||||
if got := CamelCase(snake, true); got != want {
|
||||
t.Errorf("%q: got %q, want %q", snake, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLowerCamelToSnake(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
camel, want := golden.lowerCamel, golden.snake
|
||||
if got := SnakeCase(camel); got != want {
|
||||
t.Errorf("%q: got %q, want %q", camel, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpperCamelToSnake(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
camel, want := golden.upperCamel, golden.snake
|
||||
if got := SnakeCase(camel); got != want {
|
||||
t.Errorf("%q: got %q, want %q", camel, got, want)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package name_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pascaldekloe/name"
|
||||
)
|
||||
|
||||
func ExampleCamelCase() {
|
||||
fmt.Println(name.CamelCase("pascal case", true))
|
||||
fmt.Println(name.CamelCase("snake_to_camel AND CamelToCamel?", false))
|
||||
|
||||
// Output:
|
||||
// PascalCase
|
||||
// snakeToCamelANDCamelToCamel
|
||||
}
|
||||
|
||||
func ExampleDelimit() {
|
||||
// Garbage to Lisp-case:
|
||||
fmt.Println(name.Delimit("* All Hype is aGoodThing (TM)", '-'))
|
||||
|
||||
// Builds a Java property key:
|
||||
fmt.Println(name.DotSeparated("WebCrawler#socketTimeout"))
|
||||
|
||||
// Output:
|
||||
// all-hype-is-a-good-thing-TM
|
||||
// web.crawler.socket.timeout
|
||||
}
|
Loading…
Reference in New Issue