Documentation ¶
Overview ¶
Package scs (String Case Style) implements methods for converting string cases between different naming conventions: camelCase, kebab-case, PascalCase, and snake_case.
String Case Styles ¶
The package supports four main case styles:
- camelCase: Words are joined together, first word starts with lowercase, subsequent words start with uppercase (e.g., "helloWorld")
- kebab-case: Words are lowercase and separated by hyphens (e.g., "hello-world")
- PascalCase: Words are joined together, each word starts with uppercase (e.g., "HelloWorld")
- snake_case: Words are lowercase and separated by underscores (e.g., "hello_world")
Usage ¶
The package provides two main ways to work with string case styles:
Direct conversion functions: str := scs.StrToCamel("hello-world") // returns "helloWorld" str := scs.StrToKebab("HelloWorld") // returns "hello-world" str := scs.StrToPascal("hello_world") // returns "HelloWorld" str := scs.StrToSnake("helloWorld") // returns "hello_world"
Object-oriented approach using StringCaseStyle: style, _ := scs.New(scs.Camel, "hello-world") style.Value() // returns "helloWorld" style.ToKebab() // converts to kebab-case style.Value() // returns "hello-world"
Special Cases ¶
The package handles special cases like abbreviations and numbers:
- Abbreviations are preserved: "HTTP API" -> "HTTPApi" (PascalCase)
- Numbers are treated as word boundaries: "web2print" -> "web-2-print" (kebab-case)
Thread Safety ¶
All functions in this package are thread-safe and can be used concurrently. The StringCaseStyle object methods are not thread-safe and should be protected if used concurrently.
Performance ¶
The package uses optimized regular expressions and string operations to provide efficient case conversions. Benchmark tests are available in the scs_test.go file.
Error Handling ¶
Conversion functions that require specific input formats (like CamelToKebab) return errors if the input string doesn't match the expected format. The StringCaseStyle object provides methods to check if conversions were successful.
Index ¶
- func CamelToKebab(camel string) (string, error)
- func CamelToPascal(camel string) (string, error)
- func CamelToSnake(camel string) (string, error)
- func KebabToCamel(kebab string) (string, error)
- func KebabToPascal(kebab string) (string, error)
- func KebabToSnake(kebab string) (string, error)
- func PascalToCamel(pascal string) (string, error)
- func PascalToKebab(pascal string) (string, error)
- func PascalToSnake(pascal string) (string, error)
- func SnakeToCamel(snake string) (string, error)
- func SnakeToKebab(snake string) (string, error)
- func SnakeToPascal(snake string) (string, error)
- func StrIsCamel(s string) bool
- func StrIsKebab(s string) bool
- func StrIsPascal(s string) bool
- func StrIsSnake(s string) bool
- func StrToCamel(s string) string
- func StrToKebab(s string) string
- func StrToPascal(s string) string
- func StrToSnake(s string) string
- func ToCamel(s string) string
- func ToKebab(s string) string
- func ToPascal(s string) string
- func ToSnake(s string) string
- type CaseStyle
- type StringCaseStyle
- func (o *StringCaseStyle) CopyToCamel() (*StringCaseStyle, error)
- func (o *StringCaseStyle) CopyToKebab() (*StringCaseStyle, error)
- func (o *StringCaseStyle) CopyToPascal() (*StringCaseStyle, error)
- func (o *StringCaseStyle) CopyToSnake() (*StringCaseStyle, error)
- func (o *StringCaseStyle) Eat(s string) string
- func (o *StringCaseStyle) IsCamel() bool
- func (o *StringCaseStyle) IsKebab() bool
- func (o *StringCaseStyle) IsPascal() bool
- func (o *StringCaseStyle) IsSnake() bool
- func (o *StringCaseStyle) IsValid() bool
- func (o *StringCaseStyle) Set(s string) *StringCaseStyle
- func (o *StringCaseStyle) ToCamel() error
- func (o *StringCaseStyle) ToKebab() error
- func (o *StringCaseStyle) ToPascal() error
- func (o *StringCaseStyle) ToSnake() error
- func (o *StringCaseStyle) Value() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CamelToKebab ¶
CamelToKebab converts a camelCase-style string to kebab-case.
This function checks if the input string is in camelCase. If it's not, it returns an error.
If the input string is in camelCase, it performs the conversion by inserting hyphens before each capital letter (except the first one) and then converts the string to lowercase.
Note that this conversion could fail if the input string is not in camelCase style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := CamelToKebab("helloWorld") // result: "hello-world", err: nil result, err := CamelToKebab("HelloWorld") // result: "", err: error (not camelCase)
func CamelToPascal ¶
CamelToPascal converts a camelCase-style string to PascalCase.
This function checks if the input string is in camelCase. If it's not, it returns an error.
If the input string is in camelCase, it performs the conversion by inserting spaces before each capital letter (except the first one) and then converts the string to PascalCase.
Note that this conversion could fail if the input string is not in camelCase style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := CamelToPascal("helloWorld") // result: "HelloWorld", err: nil result, err := CamelToPascal("HelloWorld") // result: "HelloWorld", err: nil
func CamelToSnake ¶
CamelToSnake converts a camelCase-style string to snake_case.
This function checks if the input string is in camelCase. If it's not, it returns an error.
If the input string is in camelCase, it performs the conversion by inserting underscores before each capital letter (except the first one) and then converts the string to snake_case.
Note that this conversion could fail if the input string is not in camelCase style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := CamelToSnake("helloWorld") // result: "hello_world", err: nil result, err := CamelToSnake("HelloWorld") // result: "hello_world", err: nil
func KebabToCamel ¶
KebabToCamel converts a kebab-case-style string to camelCase.
This function checks if the input string is in kebab-case. If it's not, it returns an error.
If the input string is in kebab-case, it replaces all hyphens with spaces and converts the resulting string to camelCase. CamelCase is a naming convention in which the first letter of each word (including the first word) is capitalized, and there are no spaces or underscores between words.
Note that this conversion could fail if the input string is not in kebab-case style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := KebabToCamel("hello-world") // result: "helloWorld", err: nil result, err := KebabToCamel("Hello-World") // result: "", err: error (not kebab-case)
func KebabToPascal ¶
KebabToPascal converts a kebab-case-style string to PascalCase.
This function checks if the input string is in kebab-case. If it's not, it returns an error.
If the input string is in kebab-case, it replaces all hyphens with spaces, performs title casing on each word, and joins the words together to form a PascalCase string. PascalCase is a naming convention in which the first letter of each word (including the first word) is capitalized, and there are no underscores or spaces between words.
Note that this conversion could fail if the input string is not in kebab-case style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := KebabToPascal("hello-world") // result: "HelloWorld", err: nil result, err := KebabToPascal("Hello-World") // result: "", err: error (not kebab-case)
func KebabToSnake ¶
KebabToSnake converts a kebab-case-style string to snake_case.
This function checks if the input string is in kebab-case. If it's not, it returns an error.
If the input string is in kebab-case, it replaces all hyphens with underscores, effectively converting the string from kebab-case to snake_case. Snake_case is a naming convention in which words are separated by underscores and there are no capital letters.
Note that this conversion could fail if the input string is not in kebab-case style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := KebabToSnake("hello-world") // result: "hello_world", err: nil result, err := KebabToSnake("Hello-World") // result: "", err: error (not kebab-case)
func PascalToCamel ¶
PascalToCamel converts a PascalCase-style string to camelCase.
This function checks if the input string is in PascalCase. If it's not, it returns an error.
If the input string is in PascalCase, it performs the conversion by inserting a space before each capital letter (except the first one), and then converts the string to camelCase.
Note that this conversion could fail if the input string is not in PascalCase style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := PascalToCamel("HelloWorld") // result: "helloWorld", err: nil result, err := PascalToCamel("helloWorld") // result: "", err: error (not PascalCase)
func PascalToKebab ¶
PascalToKebab converts a PascalCase-style string to kebab-case.
This function checks if the input string is in PascalCase. If it's not, it returns an error.
If the input string is in PascalCase, it performs the conversion by inserting hyphens before each capital letter (except the first one) and then converts the string to lowercase.
Note that this conversion could fail if the input string is not in PascalCase style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := PascalToKebab("HelloWorld") // result: "hello-world", err: nil result, err := PascalToKebab("helloWorld") // result: "", err: error (not PascalCase)
func PascalToSnake ¶
PascalToSnake converts a PascalCase-style string to snake_case.
This function checks if the input string is in PascalCase. If it's not, it returns an error.
If the input string is in PascalCase, it performs the conversion by inserting underscores before each capital letter (except the first one), and then converts the string to snake_case.
Note that this conversion could fail if the input string is not in PascalCase style. In that case, an error will be returned along with an empty string.
Example usage:
result, err := PascalToSnake("HelloWorld") // result: "hello_world", err: nil result, err := PascalToSnake("helloWorld") // result: "", err: error (not PascalCase)
func SnakeToCamel ¶
SnakeToCamel converts a snake_case-style string to camelCase.
This function checks if the input string is in snake_case. If not, it returns an error.
If the input string is in snake_case, it replaces all underscores with spaces and converts the resulting string to camelCase.
Note that the first word in the output string will be in lower case, and the first letter of each subsequent word will be in upper case. All other letters will be lower case.
This conversion could fail if the input string is not in snake_case style. In that case, an error will be returned along with an empty string.
Example usage:
scs.SnakeToCamel("hello_world") // returns "helloWorld", nil scs.SnakeToCamel("HelloWorld") // returns "", error scs.SnakeToCamel("hello-world") // returns "", error
func SnakeToKebab ¶
SnakeToKebab converts a snake_case-style string to kebab-case.
This function checks if the input string is in snake_case. If it's not, it returns an error.
If the input string is in snake_case, it replaces all underscores with hyphens, effectively converting the string from snake_case to kebab-case.
Note that this conversion could fail if the input string is not in snake_case style. In that case, an error will be returned along with an empty string.
Example usage:
scs.SnakeToKebab("hello_world") // returns "hello-world", nil scs.SnakeToKebab("HelloWorld") // returns "", error scs.SnakeToKebab("helloWorld") // returns "", error
func SnakeToPascal ¶
SnakeToPascal converts a snake_case-style string to PascalCase.
This function checks if the input string is in snake_case. If it's not, it returns an error.
If the input string is in snake_case, it replaces all underscores with spaces and converts the resulting string to PascalCase. The PascalCase format starts with an uppercase letter, and includes an uppercase letter at the beginning of each new word.
Note that this conversion could fail if the input string is not in snake_case style. In that case, an error will be returned along with an empty string.
Example usage:
scs.SnakeToPascal("hello_world") // returns "HelloWorld", nil scs.SnakeToPascal("HelloWorld") // returns "", error scs.SnakeToPascal("hello-world") // returns "", error
func StrIsCamel ¶
StrIsCamel returns true if the string is in camelCase.
CamelCase is a naming convention in which the first letter of the first word is lowercase, and the first letter of each subsequent word is capitalized. There are no spaces or underscores between words.
Example usage:
scs.StrIsCamel("helloWorld") // returns true scs.StrIsCamel("HelloWorld") // returns true scs.StrIsCamel("hello_world") // returns false scs.StrIsCamel("hello-World") // returns false
func StrIsKebab ¶
StrIsKebab returns true if the string is in kebab-case.
Kebab-case is a naming convention in which words are separated by hyphens, and all letters are lowercase.
Example usage:
scs.StrIsKebab("hello-world") // returns true scs.StrIsKebab("HelloWorld") // returns false scs.StrIsKebab("hello_world") // returns false scs.StrIsKebab("Hello-World") // returns false
func StrIsPascal ¶
StrIsPascal returns true if the string is in PascalCase.
PascalCase is a naming convention in which the first letter of each word (including the first word) is capitalized, and there are no underscores between words.
Example usage:
scs.StrIsPascal("HelloWorld") // returns true scs.StrIsPascal("helloWorld") // returns false scs.StrIsPascal("hello_world") // returns false scs.StrIsPascal("Hello_World") // returns false
func StrIsSnake ¶
StrIsSnake returns true if the string is in snake_case.
Snake case represents words separated by underscores and does not have any capital letters.
Example usage:
scs.StrIsSnake("hello_world") // returns true scs.StrIsSnake("HelloWorld") // returns false scs.StrIsSnake("hello-world") // returns false
func StrToCamel ¶
StrToCamel converts a string to camelCase.
This function converts the input string to camelCase format. CamelCase is a naming convention in which the first letter of the first word is lowercase, and the first letter of each subsequent word is capitalized. There are no spaces or underscores between words.
Note that this function will convert all characters to lowercase, except for the first letter of the first word, which will be lowercase.
Example usage:
scs.StrToCamel("hello_world") // returns "helloWorld" scs.StrToCamel("hello-world") // returns "helloWorld" scs.StrToCamel("HelloWorld") // returns "helloWorld"
func StrToKebab ¶
StrToKebab converts a string to kebab-case.
This function transforms the input string to kebab-case format. Kebab-case is a naming convention in which words are separated by hyphens, and all letters are lowercase.
Example usage:
scs.StrToKebab("helloWorld") // returns "hello-world" scs.StrToKebab("HelloWorld") // returns "hello-world" scs.StrToKebab("hello_world") // returns "hello-world" scs.StrToKebab("Hello-World") // returns "hello-world"
func StrToPascal ¶
StrToPascal converts a string to PascalCase.
This function transforms the input string to PascalCase format. PascalCase is a naming convention in which the first letter of each word, including the first word, is capitalized, and there are no underscores or spaces between words.
Note that this function will convert all characters to lowercase, except for the first letter of each word, which will be capitalized.
Example usage:
scs.StrToPascal("hello_world") // returns "HelloWorld" scs.StrToPascal("hello world") // returns "HelloWorld" scs.StrToPascal("helloWorld") // returns "Helloworld"
func StrToSnake ¶
StrToSnake converts a string to snake_case.
This function first transforms the input string by replacing all non-alphanumeric characters with spaces.
It then splits the string into chunks at the spaces, converts each chunk to lower case, and finally joins the chunks back together with underscores.
Note that this function will convert upper case letters to lower case, so the output string will be all lower case even if the input string contains upper case letters.
Example usage:
scs.StrToSnake("Hello World") // returns "hello_world" scs.StrToSnake("hello world") // returns "hello_world" scs.StrToSnake("hello-world") // returns "hello_world"
func ToCamel ¶
ToCamel converts a string to camelCase.
This function converts the input string to camelCase format. CamelCase is a naming convention in which the first letter of the first word is lowercase, and the first letter of each subsequent word is capitalized. There are no spaces or underscores between words.
Unlike the StrToCamel function, if the source string already has a certain format such as camelCase, kebab-case, PascalCase, or snake_case, it will be correctly converted to camelCase.
Example usage:
scs.ToCamel("hello_world") // returns "helloWorld" scs.ToCamel("hello-world") // returns "helloWorld" scs.ToCamel("HelloWorld") // returns "helloWorld" scs.ToCamel("snake_case") // returns "snakeCase" scs.ToCamel("kebab-case") // returns "kebabCase" scs.ToCamel("PascalCase") // returns "pascalCase" scs.ToCamel("camelCase") // returns "camelCase"
func ToKebab ¶
ToKebab converts a string to kebab-case.
This function converts the input string to kebab-case format. Kebab-case is a naming convention in which words are separated by hyphens, and all letters are lowercase.
If the source string already has a certain format, such as camelCase, kebab-case, PascalCase, or snake_case, it will be correctly converted to kebab-case.
Example usage:
scs.ToKebab("helloWorld") // returns "hello-world" scs.ToKebab("HelloWorld") // returns "hello-world" scs.ToKebab("hello_world") // returns "hello-world" scs.ToKebab("Hello-World") // returns "hello-world"
func ToPascal ¶
ToPascal converts a string to PascalCase.
This function converts the input string to PascalCase format. PascalCase is a naming convention in which the first letter of each word (including the first word) is capitalized, and there are no underscores, spaces, or hyphens between words.
If the source string already has a certain format, such as camelCase, kebab-case, or snake_case, it will be correctly converted to PascalCase.
Example usage:
scs.ToPascal("hello_world") // returns "HelloWorld" scs.ToPascal("hello-world") // returns "HelloWorld" scs.ToPascal("helloWorld") // returns "HelloWorld" scs.ToPascal("helloWorld123") // returns "HelloWorld123"
func ToSnake ¶
ToSnake converts a string to snake_case.
Unlike the StrToSnake function, this function attempts to correctly handle strings that are already in a certain format (such as CamelCase, KebabCase, or PascalCase) and convert them into snake_case.
The function first checks if the string is in CamelCase, KebabCase, PascalCase, or SnakeCase format. If it is, it uses the corresponding conversion function to convert the string into snake_case.
If the string is not in any recognized format, it defaults to using the StrToSnake function to attempt a conversion.
Example usage:
scs.ToSnake("helloWorld") // returns "hello_world" scs.ToSnake("HelloWorld") // returns "hello_world" scs.ToSnake("hello-world") // returns "hello_world" scs.ToSnake("hello_world") // returns "hello_world" scs.ToSnake("Hello World") // returns "hello_world"
Types ¶
type CaseStyle ¶
type CaseStyle uint8
CaseStyle is string case style type.
const ( // Camel is constant that characterizes string case style as camelCase. Camel CaseStyle = 1 << iota // Kebab is constant that characterizes string case style as kebab-case. Kebab // Pascal is constant that characterizes string case style as PascalCase. Pascal // Snake is constant that characterizes string case style as snake_case. Snake )
type StringCaseStyle ¶ added in v1.0.0
type StringCaseStyle struct {
// contains filtered or unexported fields
}
StringCaseStyle is object of the string case style (SCS). It can be created correctly through the New function only.
func New ¶
func New(style CaseStyle, value ...string) (*StringCaseStyle, error)
New returns a pointer to a string case style object. The style defines the string case style. a string (or list of strings) to format.
This function creates a new instance of the StringCaseStyle struct, which represents a specific string case style. It takes a CaseStyle parameter to determine the desired case style (Camel, Kebab, Pascal, Snake), and one or more string values to be formatted.
The function initializes the `do` field of the StringCaseStyle struct with the appropriate formatting function based on the specified case style. It then joins the input strings with a space separator and applies the formatting function to the resulting string. The formatted string is stored in the `value` field of the StringCaseStyle struct.
If an incorrect case style is provided, the function returns an error along with a nil pointer to the StringCaseStyle.
Example usage:
style, err := scs.New(scs.Camel, "hello", "world")
func (*StringCaseStyle) CopyToCamel ¶ added in v1.0.0
func (o *StringCaseStyle) CopyToCamel() (*StringCaseStyle, error)
CopyToCamel converts an object to Camel Type StringCaseStyle and returns new pointer to it.
func (*StringCaseStyle) CopyToKebab ¶ added in v1.0.0
func (o *StringCaseStyle) CopyToKebab() (*StringCaseStyle, error)
CopyToKebab converts an object to Kebab Type StringCaseStyle and returns new pointer to it.
func (*StringCaseStyle) CopyToPascal ¶ added in v1.0.0
func (o *StringCaseStyle) CopyToPascal() (*StringCaseStyle, error)
CopyToPascal converts an object to Pascal Type StringCaseStyle and returns new pointer to it.
func (*StringCaseStyle) CopyToSnake ¶ added in v1.0.0
func (o *StringCaseStyle) CopyToSnake() (*StringCaseStyle, error)
CopyToSnake converts an object to Snake Type StringCaseStyle and returns new pointer to it.
func (*StringCaseStyle) Eat ¶ added in v1.0.0
func (o *StringCaseStyle) Eat(s string) string
Eat converts a string to the specified style and stores it as the object value.
This method converts the input string to the style defined by the StringCaseStyle object and updates the object's value. The converted value is returned as the result.
Example usage:
style, _ := New(Camel, "hello_world") result := style.Eat("example_input") // result: "exampleInput" // style.Value(): "exampleInput" style, _ = New(Kebab, "hello-world") result = style.Eat("example_input") // result: "example-input" // style.Value(): "example-input"
func (*StringCaseStyle) IsCamel ¶ added in v1.0.0
func (o *StringCaseStyle) IsCamel() bool
IsCamel returns true if the StringCaseStyle object represents a camelCase value.
This method checks if the StringCaseStyle object has a camelCase style. It returns true if the object represents a camelCase value, indicating that the original value or formatted value is in camelCase.
Example usage:
style, _ := New(Camel, "helloWorld") isCamel := style.IsCamel() // isCamel: true style, _ = New(Pascal, "HelloWorld") isCamel = style.IsCamel() // isCamel: false
func (*StringCaseStyle) IsKebab ¶ added in v1.0.0
func (o *StringCaseStyle) IsKebab() bool
IsKebab returns true if the StringCaseStyle object represents a kebab-case value.
This method checks if the StringCaseStyle object has a kebab-case style. It returns true if the object represents a kebab-case value, indicating that the original value or formatted value is in kebab-case.
Example usage:
style, _ := New(Kebab, "hello-world") isKebab := style.IsKebab() // isKebab: true style, _ = New(Pascal, "HelloWorld") isKebab = style.IsKebab() // isKebab: false
func (*StringCaseStyle) IsPascal ¶ added in v1.0.0
func (o *StringCaseStyle) IsPascal() bool
IsPascal returns true if the StringCaseStyle object represents a PascalCase value.
This method checks if the StringCaseStyle object has a PascalCase style. It returns true if the object represents a PascalCase value, indicating that the original value or formatted value is in PascalCase.
Example usage:
style, _ := New(Pascal, "HelloWorld") isPascal := style.IsPascal() // isPascal: true style, _ = New(Snake, "hello_world") isPascal = style.IsPascal() // isPascal: false
func (*StringCaseStyle) IsSnake ¶ added in v1.0.0
func (o *StringCaseStyle) IsSnake() bool
IsSnake returns true if the StringCaseStyle object represents a snake_case value.
This method checks if the StringCaseStyle object has a snake_case style. It returns true if the object represents a snake_case value, indicating that the original value or formatted value is in snake_case.
Example usage:
style, _ := New(Snake, "hello_world") isSnake := style.IsSnake() // isSnake: true style, _ = New(Pascal, "HelloWorld") isSnake = style.IsSnake() // isSnake: false
func (*StringCaseStyle) IsValid ¶ added in v1.0.0
func (o *StringCaseStyle) IsValid() bool
IsValid returns true if the StringCaseStyle object is valid.
This method is used to check the validity of a StringCaseStyle object. It returns true if the object is valid, indicating that the string case style and value have been set successfully.
Example usage:
style, _ := New(Camel, "helloWorld") isValid := style.IsValid() // isValid: true invalidStyle := &StringCaseStyle{isValid: false} isValid := invalidStyle.IsValid() // isValid: false
func (*StringCaseStyle) Set ¶ added in v1.0.0
func (o *StringCaseStyle) Set(s string) *StringCaseStyle
Set sets a new value for the StringCaseStyle object.
This method converts the input string to the style defined by the StringCaseStyle object and sets the converted value as the new value of the object. The updated object is returned for method chaining.
Example usage:
style, _ := New(Camel, "hello_world") result := style.Set("example_input") // result.Value(): "exampleInput" style, _ = New(Kebab, "hello-world") result = style.Set("example_input") // result.value: "example-input"
func (*StringCaseStyle) ToCamel ¶ added in v1.0.0
func (o *StringCaseStyle) ToCamel() error
ToCamel converts an object to Camel Type StringCaseStyle.
func (*StringCaseStyle) ToKebab ¶ added in v1.0.0
func (o *StringCaseStyle) ToKebab() error
ToKebab converts an object to Kebab Type StringCaseStyle.
func (*StringCaseStyle) ToPascal ¶ added in v1.0.0
func (o *StringCaseStyle) ToPascal() error
ToPascal converts an object to Pascal Type StringCaseStyle.
func (*StringCaseStyle) ToSnake ¶ added in v1.0.0
func (o *StringCaseStyle) ToSnake() error
ToSnake converts an object to Snake Type StringCaseStyle.
func (*StringCaseStyle) Value ¶ added in v1.0.0
func (o *StringCaseStyle) Value() string
Value returns the current value of the StringCaseStyle object.
This method returns the current value of the StringCaseStyle object.
Example usage:
style, _ := New(Camel, "Hello World") result := style.Value() // result: "Hello World" style, _ = New(Kebab, "Show me") result = style.Value() // result: "Show me"