scs

package module
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 4 Imported by: 1

README

Go Report Card License License Stay with Ukraine

scs - String Case Style for Go

Package scs (String Case Style) provides robust string case conversion utilities for Go applications. It supports conversion between camelCase, kebab-case, PascalCase, and snake_case formats.

Features

  • Convert between four common case styles:
    • camelCase
    • kebab-case
    • PascalCase
    • snake_case
  • Two usage approaches:
    • Direct conversion functions
    • Object-oriented style with chainable methods
  • Proper handling of:
    • Abbreviations (e.g., HTTP, API)
    • Numbers
    • Special characters
  • Thread-safe functions
  • Comprehensive error handling
  • Zero dependencies

Installation

go get -u github.com/goloop/scs

Quick Start

To use this module import it as: github.com/goloop/scs

package main

import (
    "fmt"
    "github.com/goloop/scs"
)

func main() {
    // Direct conversion.
    kebab := scs.StrToKebab("HelloWorld")  // hello-world
    camel := scs.StrToCamel("hello-world") // helloWorld

    // Object-oriented approach.
    style, _ := scs.New(scs.Snake, "HelloWorld")
    fmt.Println(style.Value())  // hello_world

    // Chain conversions.
    style.ToCamel().ToKebab()
    fmt.Println(style.Value())  // hello-world
}
Conversion functions

Example:

package main

import "github.com/goloop/scs"

func main() {
    var s string

    // Simple text
    s = "hello world"
    scs.StrToCamel(s)  // helloWorld
    scs.StrToPascal(s) // HelloWorld
    scs.StrToSnake(s)  // hello_world
    scs.StrToKebab(s)  // hello-world

    // Text with abbreviations
    s = "http to https"
    scs.StrToCamel(s)  // httpToHTTPS
    scs.StrToPascal(s) // HTTPToHTTPS
    scs.StrToSnake(s)  // http_to_https
    scs.StrToKebab(s)  // http-to-https

    // Converting
    s = "http to https"
    camel := scs.StrToCamel(s)            // httpToHTTPS
    pascal, _ := scs.CamelToPascal(camel) // HTTPToHTTPS <nil>
    kebab, _ := scs.PascalToKebab(pascal) // http-to-https <nil>
    snake, _ := scs.KebabToSnake(kebab)   // http_to_https <nil>

    // Use strings.ToUpper(snake) for convert to UPPER_SNAKE_CASE.

    scs.SnakeToPascal(snake) // HTTPToHTTPS <nil>
    scs.CamelToKebab(camel)  // http-to-https <nil>

    // Errors
    scs.CamelToSnake(kebab)  // value http-to-https isn't camelCase style
    scs.PascalToCamel(camel) // value httpToHTTPS isn't PascalCase style

    // Convert anything to anything correctly
    scs.ToCamel(snake)  // httpToHTTPS
    scs.ToPascal(kebab) // HTTPToHTTPS
    scs.ToSnake(s)      // http_to_https
}
Style objects

A safer way. Since each object knows what type it is and knows which conversion rules to use. This removes the need to return a second parameter as err when converting styles.

Example:

package main

import "github.com/goloop/scs"

func main() {
    var s string

    // Simple text
    s = "hello world"
    snake, _ := scs.New(scs.Snake) // scs.New(scs.Snake, s)

    snake.Eat(s)         // hello_world
    snake.Set(s).Value() // hello_world
    snake.IsCamel()      // false
    snake.IsSnake()      // true
    snake.Value()        // hello_world

    camel := snake.CopyToCamel()
    camel.IsSnake() // false
    camel.IsCamel() // true
    camel.Value()   // helloWorld

    // Text with abbreviations
    s = "http to https"
    pascal, _ := scs.New(scs.Pascal, s)
    pascal.Value() // HTTPToHTTPS

    kebab := pascal.CopyToKebab()
    kebab.Value() // http-to-https
}

Functions

  • CamelToKebab(camel string) (string, error)

    CamelToKebab converts a camelCase-style string to kebab-case. The conversion will be invalid if the input string is not camelCase style.

  • CamelToPascal(camel string) (string, error)

    CamelToPascal converts a camelCase-style string to PascalCase. The conversion will be invalid if the input string is not camelCase style.

  • CamelToSnake(camel string) (string, error)

    CamelToSnake converts a camelCase-style string to snake_case. The conversion will be invalid if the input string is not camelCase style.

  • KebabToCamel(kebab string) (string, error)

    KebabToCamel converts a kebab-case-style string to camelCase. The conversion will be invalid if the input string is not kebab-case style.

  • KebabToPascal(kebab string) (string, error)

    KebabToPascal converts a kebab-case-style string to PascalCase. The conversion will be invalid if the input string is not kebab-case style.

  • KebabToSnake(kebab string) (string, error)

    KebabToSnake converts a kebab-case-style string to snake_case. The conversion will be invalid if the input string is not kebab-case style.

  • PascalToCamel(pascal string) (string, error)

    PascalToCamel converts a PascalCase-style string to camelCase. The conversion will be invalid if the input string is not PascalCase style.

  • PascalToKebab(pascal string) (string, error)

    PascalToKebab converts a PascalCase-style string to kebab-case. The conversion will be invalid if the input string is not PascalCase style.

  • PascalToSnake(pascal string) (string, error)

    PascalToSnake converts a PascalCase-style string to snake_case. The conversion will be invalid if the input string is not PascalCase style.

  • SnakeToCamel(snake string) (string, error)

    SnakeToCamel converts a snake_case-style string to camelCase. The conversion will be invalid if the input string is not snake_case style.

  • SnakeToKebab(snake string) (string, error)

    SnakeToKebab converts a snake_case-style string to kebab-case. The conversion will be invalid if the input string is not snake_case style.

  • SnakeToPascal(snake string) (string, error)

    SnakeToPascal converts a snake_case-style string to PascalCase. The conversion will be invalid if the input string is not snake_case style.

  • StrIsCamel(s string) bool

    StrIsCamel returns true if string is camelCase.

  • StrIsKebab(s string) bool

    StrIsKebab returns true if string is kebab-case.

  • StrIsPascal(s string) bool

    StrIsPascal returns true if string is PascalCase.

  • StrIsSnake(s string) bool

    StrIsSnake returns true if string is snake_case.

  • StrToCamel(s string) string

    StrToCamel converts a string to camelCase.

  • StrToKebab(s string) string

    StrToKebab converts a string to kebab-case.

  • StrToPascal(s string) string

    StrToPascal converts a string to PascalCase.

  • StrToSnake(s string) string

    StrToSnake converts a string to snake_case.

  • ToCamel(s string) string

    ToCamel converts a string to camelCase. Unlike the StrToCamel function, if the source string already has a certain format, it will be correctly converted to camelCase.

  • ToKebab(s string) string

    ToKebab converts a string to kebab-case. Unlike the StrToKebab function, if the source string already has a certain format, it will be correctly converted to kebab-case.

  • ToPascal(s string) string

    ToPascal converts a string to PascalCase. Unlike the StrToPascal function, if the source string already has a certain format, it will be correctly converted to PascalCase.

  • ToSnake(s string) string

    ToSnake converts a string to snake_case. Unlike the StrToSnake function, if the source string already has a certain format, it will be correctly converted to snake_case.

  • Version() string

    Version returns the version of the module.

  • 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.

StringCaseStyle Object

  • CopyToCamel() (*StringCaseStyle, error)

    CopyToCamel converts an object to Camel Type StringCaseStyle and returns new pointer to it.

  • CopyToKebab() (*StringCaseStyle, error)

    CopyToKebab converts an object to Kebab Type StringCaseStyle and returns new pointer to it.

  • CopyToPascal() (*StringCaseStyle, error)

    CopyToPascal converts an object to Pascal Type StringCaseStyle and returns new pointer to it.

  • CopyToSnake() (*StringCaseStyle, error)

    CopyToSnake converts an object to Snake Type StringCaseStyle and returns new pointer to it.

  • Eat(s string) string

    Eat converts a string to the specified style and stores it as an object value.

  • IsCamel() bool

    IsCamel returns true if object contains camelCase value.

  • IsKebab() bool

    IsKebab returns true if object contains kebab-case value.

  • IsPascal() bool

    IsPascal returns true if object contains PascalCase value.

  • IsSnake() bool

    IsSnake returns true if object contains snake-case value.

  • IsValid() bool

    IsValid returns true if StringCaseStyle is valid.

  • Set(s string) *StringCaseStyle

    Set sets new value.

  • ToCamel() error

    ToCamel converts an object to Camel Type StringCaseStyle.

  • ToKebab() error

    ToKebab converts an object to Kebab Type StringCaseStyle.

  • ToPascal() error

    ToPascal converts an object to Pascal Type StringCaseStyle.

  • ToSnake() error

    ToSnake converts an object to Snake Type StringCaseStyle.

  • Value() string

    Value returns value of the object.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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:

  1. 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"

  2. 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelToKebab

func CamelToKebab(camel string) (string, error)

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

func CamelToPascal(camel string) (string, error)

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

func CamelToSnake(camel string) (string, error)

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

func KebabToCamel(kebab string) (string, error)

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

func KebabToPascal(kebab string) (string, error)

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

func KebabToSnake(kebab string) (string, error)

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

func PascalToCamel(pascal string) (string, error)

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

func PascalToKebab(pascal string) (string, error)

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

func PascalToSnake(pascal string) (string, error)

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

func SnakeToCamel(snake string) (string, error)

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

func SnakeToKebab(snake string) (string, error)

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

func SnakeToPascal(snake string) (string, error)

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

func StrIsCamel(s string) bool

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

func StrIsKebab(s string) bool

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

func StrIsPascal(s string) bool

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

func StrIsSnake(s string) bool

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

func StrToCamel(s string) string

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

func StrToKebab(s string) string

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

func StrToPascal(s string) string

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

func StrToSnake(s string) string

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

func ToCamel(s string) string

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

func ToKebab(s string) string

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

func ToPascal(s string) string

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

func ToSnake(s string) string

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

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"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL