-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
abstracted case format to individual package
- Loading branch information
Showing
7 changed files
with
587 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package format | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
type Case int | ||
|
||
const ( | ||
//CaseUpper represents case format | ||
CaseUpper = Case(iota) | ||
CaseLower | ||
CaseUpperCamel | ||
CaseLowerCamel | ||
CaseUpperUnderscore | ||
CaseLowerUnderscore | ||
) | ||
|
||
//NewCase create a new case for supplied name or error, supported in case insensitive form "upperCase", "upper", "u" | ||
func NewCase(name string) (Case, error) { | ||
switch strings.ToLower(name) { | ||
case "upper", "u": | ||
return CaseUpper, nil | ||
case "lower", "l": | ||
return CaseLower, nil | ||
case "lowercamel", "lc": | ||
return CaseLowerCamel, nil | ||
case "uppercamel", "uc": | ||
return CaseUpperCamel, nil | ||
case "lowerunderscore", "lu": | ||
return CaseLowerUnderscore, nil | ||
case "upperunderscore", "uu": | ||
return CaseUpperUnderscore, nil | ||
} | ||
return -1, fmt.Errorf("unsupported case format: %s", name) | ||
} | ||
|
||
//String return case format name | ||
func (from Case) String() string { | ||
switch from { | ||
case CaseUpper: | ||
return "Upper" | ||
case CaseLower: | ||
return "Lower" | ||
case CaseUpperCamel: | ||
return "UpperCamel" | ||
case CaseLowerCamel: | ||
return "UpperCamel" | ||
case CaseUpperUnderscore: | ||
return "UpperUnderscore" | ||
case CaseLowerUnderscore: | ||
return "LowerUnderscore" | ||
} | ||
return "UnsupportedCase" | ||
} | ||
|
||
|
||
//Format converts supplied text from Case to | ||
func (from Case) Format(text string, to Case) string { | ||
toUpper := false | ||
toLower := false | ||
toCamel := false | ||
toUnserscore := false | ||
fromCamel := false | ||
fromUnserscore := false | ||
|
||
switch to { | ||
case CaseUpper, CaseUpperUnderscore: | ||
toUpper = true | ||
case CaseLower, CaseLowerUnderscore: | ||
toLower = true | ||
case CaseUpperCamel, CaseLowerCamel: | ||
toCamel = true | ||
} | ||
switch to { | ||
case CaseUpperUnderscore, CaseLowerUnderscore: | ||
toUnserscore = true | ||
} | ||
switch from { | ||
case CaseUpperCamel, CaseLowerCamel: | ||
fromCamel = true | ||
case CaseUpperUnderscore, CaseLowerUnderscore: | ||
fromUnserscore = true | ||
} | ||
underscore := rune('_') | ||
var result = make([]rune, 0) | ||
makeLower := false | ||
makeUpper := false | ||
hasUnderscore := false | ||
for i, r := range text { | ||
first := i == 0 | ||
if toUpper { | ||
makeUpper = true | ||
} else if toLower { | ||
makeLower = true | ||
} | ||
if first { | ||
if to == CaseLowerCamel { | ||
r = unicode.ToLower(r) | ||
} else if to == CaseUpperCamel { | ||
r = unicode.ToUpper(r) | ||
} | ||
} else { | ||
if fromUnserscore { | ||
if toCamel { | ||
if r == underscore { | ||
hasUnderscore = true | ||
continue | ||
} | ||
if hasUnderscore { | ||
makeUpper = true | ||
hasUnderscore = false | ||
} else { | ||
makeLower = true | ||
} | ||
} | ||
} | ||
if unicode.IsUpper(r) && fromCamel { | ||
if toUnserscore { | ||
result = append(result, underscore) | ||
} | ||
} | ||
} | ||
|
||
if makeLower { | ||
r = unicode.ToLower(r) | ||
} else if makeUpper { | ||
r = unicode.ToUpper(r) | ||
} | ||
result = append(result, r) | ||
makeUpper = false | ||
makeLower = false | ||
} | ||
|
||
return string(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package format | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestCase_To(t *testing.T) { | ||
var useCases = []struct { | ||
description string | ||
caseFrom Case | ||
caseTo Case | ||
input string | ||
expect string | ||
}{ | ||
{ | ||
description: "camel to uppercase", | ||
input: "thisIsMyTest", | ||
caseFrom: CaseLowerCamel, | ||
caseTo: CaseUpper, | ||
expect: "THISISMYTEST", | ||
}, | ||
{ | ||
description: "camel to lower underscore", | ||
input: "thisIsMyTest", | ||
caseFrom: CaseLowerCamel, | ||
caseTo: CaseLowerUnderscore, | ||
expect: "this_is_my_test", | ||
}, | ||
{ | ||
description: "camel to upper underscore", | ||
input: "thisIsMyTest", | ||
caseFrom: CaseLowerCamel, | ||
caseTo: CaseUpperUnderscore, | ||
expect: "THIS_IS_MY_TEST", | ||
}, | ||
{ | ||
description: "lower underscore to upper camel", | ||
input: "this_is_my_test", | ||
caseFrom: CaseLowerUnderscore, | ||
caseTo: CaseUpperCamel, | ||
expect: "ThisIsMyTest", | ||
}, | ||
{ | ||
description: "upper underscore to lower camel", | ||
input: "THIS_IS_MY_TEST", | ||
caseFrom: CaseUpperUnderscore, | ||
caseTo: CaseLowerCamel, | ||
expect: "thisIsMyTest", | ||
}, | ||
|
||
{ | ||
description: "upper camel to lower camel", | ||
input: "ThisIsMyTest", | ||
caseFrom: CaseUpperCamel, | ||
caseTo: CaseLowerCamel, | ||
expect: "thisIsMyTest", | ||
}, | ||
} | ||
|
||
for _, useCase := range useCases { | ||
actual := useCase.caseFrom.Format(useCase.input, useCase.caseTo) | ||
assert.Equal(t, useCase.expect, actual, useCase.description) | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.