forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
193 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package names | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// toCamelCase converts a string to CamelCase. | ||
func toCamelCase(in string, initialCap bool) string { | ||
out := strings.Builder{} | ||
|
||
nextIsCap := initialCap | ||
prevIsCap := false | ||
for i, ch := range []byte(in) { | ||
isCap := isCapitalLetter(ch) | ||
isLow := isLowercaseLetter(ch) | ||
isDig := isNumeric(ch) | ||
|
||
if nextIsCap { | ||
if isLow { | ||
ch = toUppercaseLetter(ch) | ||
} | ||
} else if i == 0 { | ||
if isCap { | ||
ch = toLowercaseLetter(ch) | ||
} | ||
} else if prevIsCap && isCap { | ||
ch = toLowercaseLetter(ch) | ||
} | ||
|
||
prevIsCap = isCap | ||
|
||
if isCap || isLow { | ||
out.WriteByte(ch) | ||
nextIsCap = false | ||
} else if isDig { | ||
out.WriteByte(ch) | ||
nextIsCap = true | ||
} else { | ||
nextIsCap = ch == '_' || ch == ' ' || ch == '-' || ch == '.' | ||
} | ||
} | ||
|
||
return out.String() | ||
} | ||
|
||
func toUppercaseLetter(ch byte) byte { | ||
ch += 'A' | ||
ch -= 'a' | ||
return ch | ||
} | ||
|
||
// ToCamelCase converts a string to CamelCase. | ||
func ToCamelCase(in string) string { | ||
return toCamelCase(in, true) | ||
} | ||
|
||
// ToLowerCamelCase converts a string to camelCase. | ||
func ToLowerCamelCase(in string) string { | ||
return toCamelCase(in, false) | ||
} |
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,125 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package names | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestToCamelCase(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "empty", | ||
input: "", | ||
expected: "", | ||
}, | ||
{ | ||
name: "All lower", | ||
input: "lower", | ||
expected: "Lower", | ||
}, | ||
{ | ||
name: "Initial upper", | ||
input: "Lower", | ||
expected: "Lower", | ||
}, | ||
{ | ||
name: "Two words", | ||
input: "Two words", | ||
expected: "TwoWords", | ||
}, | ||
{ | ||
name: "Three words", | ||
input: "TheseThreeWords", | ||
expected: "TheseThreeWords", | ||
}, | ||
{ | ||
name: "A long one", | ||
input: "global-Replication_Group.description", | ||
expected: "GlobalReplicationGroupDescription", | ||
}, | ||
{ | ||
name: "Including a digit", | ||
input: "s3bucket", | ||
expected: "S3Bucket", | ||
}, | ||
{ | ||
name: "Constant case", | ||
input: "CONSTANT_CASE", | ||
expected: "ConstantCase", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
if got, want := ToCamelCase(testCase.input), testCase.expected; got != want { | ||
t.Errorf("got: %s, expected: %s", got, want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestToLowerCamelCase(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "empty", | ||
input: "", | ||
expected: "", | ||
}, | ||
{ | ||
name: "All lower", | ||
input: "lower", | ||
expected: "lower", | ||
}, | ||
{ | ||
name: "Initial upper", | ||
input: "Lower", | ||
expected: "lower", | ||
}, | ||
{ | ||
name: "Two words", | ||
input: "Two words", | ||
expected: "twoWords", | ||
}, | ||
{ | ||
name: "Three words", | ||
input: "TheseThreeWords", | ||
expected: "theseThreeWords", | ||
}, | ||
{ | ||
name: "A long one", | ||
input: "global-Replication_Group.description", | ||
expected: "globalReplicationGroupDescription", | ||
}, | ||
{ | ||
name: "Including a digit", | ||
input: "s3bucket", | ||
expected: "s3Bucket", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
if got, want := ToLowerCamelCase(testCase.input), testCase.expected; got != want { | ||
t.Errorf("got: %s, expected: %s", got, want) | ||
} | ||
}) | ||
} | ||
} |
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