Compare commits
	
		
			7 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					77ddaa2c2d | ||
| 
						 | 
					77cf97e1f9 | ||
| 
						 | 
					10b280f8d2 | ||
| 
						 | 
					31e1dd6d28 | ||
| 
						 | 
					e6c294d87b | ||
| 
						 | 
					5e0ad22508 | ||
| 
						 | 
					692d1b89fe | 
@ -1,9 +1,10 @@
 | 
			
		||||
sudo: false
 | 
			
		||||
language: go
 | 
			
		||||
go:
 | 
			
		||||
  - 1.7.x
 | 
			
		||||
  - 1.8.x
 | 
			
		||||
  - 1.9.x
 | 
			
		||||
  - 1.10.x
 | 
			
		||||
  - 1.11.x
 | 
			
		||||
  - 1.12.x
 | 
			
		||||
  - 1.13.x
 | 
			
		||||
  - 1.14.x
 | 
			
		||||
  - 1.15.x
 | 
			
		||||
  - master
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										28
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								README.md
									
									
									
									
									
								
							@ -1,6 +1,6 @@
 | 
			
		||||
# strcase
 | 
			
		||||
[](http://godoc.org/github.com/iancoleman/strcase)
 | 
			
		||||
[](https://travis-ci.org/iancoleman/strcase)
 | 
			
		||||
[](https://travis-ci.com/iancoleman/strcase)
 | 
			
		||||
[](http://gocover.io/github.com/iancoleman/strcase)
 | 
			
		||||
[](https://goreportcard.com/report/github.com/iancoleman/strcase)
 | 
			
		||||
 | 
			
		||||
@ -31,3 +31,29 @@ s := "AnyKind of_string"
 | 
			
		||||
```bash
 | 
			
		||||
go get -u github.com/iancoleman/strcase
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
## Custom Acronyms for ToCamel && ToLowerCamel
 | 
			
		||||
 | 
			
		||||
Often times text can contain specific acronyms which you need to be handled a certain way.
 | 
			
		||||
Out of the box `strcase` treats the string "ID" as "Id" or "id" but there is no way to cater
 | 
			
		||||
for every case in the wild.
 | 
			
		||||
 | 
			
		||||
To configure your custom acronym globally you can use the following before running any conversion
 | 
			
		||||
 | 
			
		||||
```go
 | 
			
		||||
import (
 | 
			
		||||
    "github.com/iancoleman/strcase"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
    // results in "Api" using ToCamel("API")
 | 
			
		||||
    // results in "api" using ToLowerCamel("API")
 | 
			
		||||
    strcase.ConfigureAcronym("API", "api")
 | 
			
		||||
    
 | 
			
		||||
    // results in "PostgreSQL" using ToCamel("PostgreSQL")
 | 
			
		||||
    // results in "postgreSQL" using ToLowerCamel("PostgreSQL")
 | 
			
		||||
    strcase.ConfigureAcronym("PostgreSQL", "PostgreSQL")
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
@ -3,3 +3,8 @@ package strcase
 | 
			
		||||
var uppercaseAcronym = map[string]string{
 | 
			
		||||
	"ID": "id",
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ConfigureAcronym allows you to add additional words which will be considered acronyms
 | 
			
		||||
func ConfigureAcronym(key, val string) {
 | 
			
		||||
	uppercaseAcronym[key] = val
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										1
									
								
								camel.go
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								camel.go
									
									
									
									
									
								
							@ -31,6 +31,7 @@ import (
 | 
			
		||||
 | 
			
		||||
// Converts a string to CamelCase
 | 
			
		||||
func toCamelInitCase(s string, initCase bool) string {
 | 
			
		||||
	s = strings.TrimSpace(s)
 | 
			
		||||
	if s == "" {
 | 
			
		||||
		return s
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -68,6 +68,8 @@ func toLowerCamel(tb testing.TB) {
 | 
			
		||||
		{"AnyKind of_string", "anyKindOfString"},
 | 
			
		||||
		{"AnyKind.of-string", "anyKindOfString"},
 | 
			
		||||
		{"ID", "id"},
 | 
			
		||||
		{"some string", "someString"},
 | 
			
		||||
		{" some string", "someString"},
 | 
			
		||||
	}
 | 
			
		||||
	for _, i := range cases {
 | 
			
		||||
		in := i[0]
 | 
			
		||||
@ -83,6 +85,78 @@ func TestToLowerCamel(t *testing.T) {
 | 
			
		||||
	toLowerCamel(t)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestCustomAcronymsToCamel(t *testing.T) {
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name         string
 | 
			
		||||
		acronymKey   string
 | 
			
		||||
		acronymValue string
 | 
			
		||||
		expected     string
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			name:         "API Custom Acronym",
 | 
			
		||||
			acronymKey:   "API",
 | 
			
		||||
			acronymValue: "api",
 | 
			
		||||
			expected:     "Api",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:         "ABCDACME Custom Acroynm",
 | 
			
		||||
			acronymKey:   "ABCDACME",
 | 
			
		||||
			acronymValue: "AbcdAcme",
 | 
			
		||||
			expected:     "AbcdAcme",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:         "PostgreSQL Custom Acronym",
 | 
			
		||||
			acronymKey:   "PostgreSQL",
 | 
			
		||||
			acronymValue: "PostgreSQL",
 | 
			
		||||
			expected:     "PostgreSQL",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	for _, test := range tests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			ConfigureAcronym(test.acronymKey, test.acronymValue)
 | 
			
		||||
			if result := ToCamel(test.acronymKey); result != test.expected {
 | 
			
		||||
				t.Errorf("expected custom acronym result %s, got %s", test.expected, result)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestCustomAcronymsToLowerCamel(t *testing.T) {
 | 
			
		||||
	tests := []struct {
 | 
			
		||||
		name         string
 | 
			
		||||
		acronymKey   string
 | 
			
		||||
		acronymValue string
 | 
			
		||||
		expected     string
 | 
			
		||||
	}{
 | 
			
		||||
		{
 | 
			
		||||
			name:         "API Custom Acronym",
 | 
			
		||||
			acronymKey:   "API",
 | 
			
		||||
			acronymValue: "api",
 | 
			
		||||
			expected:     "api",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:         "ABCDACME Custom Acroynm",
 | 
			
		||||
			acronymKey:   "ABCDACME",
 | 
			
		||||
			acronymValue: "AbcdAcme",
 | 
			
		||||
			expected:     "abcdAcme",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			name:         "PostgreSQL Custom Acronym",
 | 
			
		||||
			acronymKey:   "PostgreSQL",
 | 
			
		||||
			acronymValue: "PostgreSQL",
 | 
			
		||||
			expected:     "postgreSQL",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	for _, test := range tests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			ConfigureAcronym(test.acronymKey, test.acronymValue)
 | 
			
		||||
			if result := ToLowerCamel(test.acronymKey); result != test.expected {
 | 
			
		||||
				t.Errorf("expected custom acronym result %s, got %s", test.expected, result)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BenchmarkToLowerCamel(b *testing.B) {
 | 
			
		||||
	benchmarkCamelTest(b, toLowerCamel)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										19
									
								
								snake.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								snake.go
									
									
									
									
									
								
							@ -64,27 +64,10 @@ func ToDelimited(s string, delimiter uint8) string {
 | 
			
		||||
// or delimited.snake.case
 | 
			
		||||
// (in this case `delimiter = '.'; screaming = false`)
 | 
			
		||||
func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string {
 | 
			
		||||
	s = strings.TrimSpace(s)
 | 
			
		||||
	n := strings.Builder{}
 | 
			
		||||
	n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters
 | 
			
		||||
	start := true
 | 
			
		||||
	spaces := 0
 | 
			
		||||
	for i, v := range []byte(s) {
 | 
			
		||||
		if v == ' ' {
 | 
			
		||||
			spaces++
 | 
			
		||||
			continue
 | 
			
		||||
		} else if start {
 | 
			
		||||
			start = false
 | 
			
		||||
			spaces = 0
 | 
			
		||||
		} else {
 | 
			
		||||
			for ; spaces > 0; spaces-- {
 | 
			
		||||
				if ignore == ' ' {
 | 
			
		||||
					n.WriteByte(' ')
 | 
			
		||||
				} else {
 | 
			
		||||
					n.WriteByte(delimiter)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		vIsCap := v >= 'A' && v <= 'Z'
 | 
			
		||||
		vIsLow := v >= 'a' && v <= 'z'
 | 
			
		||||
		if vIsLow && screaming {
 | 
			
		||||
 | 
			
		||||
@ -54,6 +54,8 @@ func toSnake(tb testing.TB) {
 | 
			
		||||
		{"A1 A2 A3", "a_1_a_2_a_3"},
 | 
			
		||||
		{"AB1AB2AB3", "ab_1_ab_2_ab_3"},
 | 
			
		||||
		{"AB1 AB2 AB3", "ab_1_ab_2_ab_3"},
 | 
			
		||||
		{"some string", "some_string"},
 | 
			
		||||
		{" some string", "some_string"},
 | 
			
		||||
	}
 | 
			
		||||
	for _, i := range cases {
 | 
			
		||||
		in := i[0]
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user