Convert Figma logo to code with AI

iancoleman logostrcase

A golang package for converting to snake_case or CamelCase

1,009
106
1,009
17

Top Related Projects

128,386

A curated list of awesome Go frameworks, libraries and software

122,720

The Go programming language

7,344

[mirror] Go Tools

Quick Overview

The strcase library is a Go package that provides functions for converting strings between different naming conventions, such as camelCase, snake_case, and kebab-case. It is a lightweight and efficient solution for string manipulation tasks in Go projects.

Pros

  • Comprehensive Functionality: The library offers a wide range of string conversion functions, covering common naming conventions used in programming.
  • Efficiency: The implementation is optimized for performance, making it suitable for use in high-throughput applications.
  • Simplicity: The API is straightforward and easy to use, with clear function names and documentation.
  • Cross-platform Compatibility: The library is designed to work seamlessly across different operating systems and platforms.

Cons

  • Limited Scope: The library is focused solely on string case conversion and does not provide additional string manipulation features.
  • Lack of Internationalization: The current version of the library does not handle Unicode characters or non-English languages.
  • Potential Naming Conflicts: The package name strcase may conflict with other libraries or project-specific naming conventions.
  • Dependency Management: The library has no external dependencies, but it may need to be manually imported and managed in larger projects.

Code Examples

// Convert a string to camelCase
camelCased := strcase.ToLowerCamel("my_snake_case_string")
// Output: mySnakeCaseString
// Convert a string to snake_case
snakeCased := strcase.ToSnake("myCamelCaseString")
// Output: my_camel_case_string
// Convert a string to kebab-case
kebabCased := strcase.ToKebab("MyPascalCaseString")
// Output: my-pascal-case-string
// Convert a string to TitleCase
titleCased := strcase.ToTitle("my_snake_case_string")
// Output: My Snake Case String

Getting Started

To use the strcase library in your Go project, follow these steps:

  1. Install the package using the Go package manager:

    go get github.com/iancoleman/strcase
    
  2. Import the package in your Go file:

    import "github.com/iancoleman/strcase"
    
  3. Use the provided functions to convert strings between different naming conventions:

    camelCased := strcase.ToLowerCamel("my_snake_case_string")
    snakeCased := strcase.ToSnake("myCamelCaseString")
    kebabCased := strcase.ToKebab("MyPascalCaseString")
    titleCased := strcase.ToTitle("my_snake_case_string")
    

That's it! The strcase library is now ready to use in your Go project.

Competitor Comparisons

128,386

A curated list of awesome Go frameworks, libraries and software

Pros of Awesome Go

  • Comprehensive collection of Go libraries and tools, covering a wide range of use cases.
  • Active community with frequent updates and contributions.
  • Provides a valuable resource for Go developers to discover and explore new packages.

Cons of Awesome Go

  • The repository is primarily a curated list, rather than a specific library or tool.
  • The large number of entries can make it challenging to navigate and find the most relevant packages for a particular project.
  • The quality and maintenance of individual entries may vary, as the repository relies on community contributions.

Code Comparison

Here's a brief comparison of the code structure between Awesome Go and Strcase:

Awesome Go:

awesome-go/
├── README.md
├── CONTRIBUTING.md
├── MAINTAINERS.md
└── categories/
    ├── database.md
    ├── text-processing.md
    └── ...

Strcase:

strcase/
├── README.md
├── LICENSE
├── strcase.go
└── strcase_test.go

The Awesome Go repository is primarily a curated list of Go packages, organized into various categories, while Strcase is a standalone library focused on string case conversion.

122,720

The Go programming language

Pros of golang/go

  • The Go programming language is a robust, statically-typed language with a focus on simplicity, efficiency, and concurrency.
  • The standard library provided by golang/go is extensive and covers a wide range of functionality, reducing the need for external dependencies.
  • The Go compiler is highly optimized and produces efficient, native binaries.

Cons of golang/go

  • The Go standard library does not include a dedicated string manipulation library like iancoleman/strcase.
  • The Go language has a steeper learning curve compared to some other programming languages, especially for developers coming from dynamic languages.
  • The Go ecosystem may not have as many third-party libraries and tools as some other popular programming languages.

Code Comparison

iancoleman/strcase:

func ToSnakeCase(s string) string {
    return strings.Replace(
        strings.TrimSpace(
            regexp.MustCompile("(^[A-Z])|([A-Z][^A-Z])")
        ).ReplaceAllString(s, "_$2"), "__", "_", -1)
}

golang/go (standard library):

func ToSnakeCase(s string) string {
    var b strings.Builder
    for i := range s {
        c := s[i]
        if i > 0 && unicode.IsUpper(rune(c)) {
            b.WriteByte('_')
        }
        b.WriteByte(byte(unicode.ToLower(rune(c))))
    }
    return b.String()
}

The iancoleman/strcase implementation uses a regular expression to identify the transition points between uppercase and lowercase letters, while the standard library implementation in golang/go iterates through the string and manually checks for uppercase letters.

7,344

[mirror] Go Tools

Pros of golang/tools

  • Comprehensive set of tools for Go development, including a language server, code analysis, and refactoring tools.
  • Actively maintained and updated by the Go team, ensuring compatibility with the latest Go versions.
  • Provides a wide range of functionality beyond just string case conversion.

Cons of golang/tools

  • Larger in scope and complexity compared to iancoleman/strcase, which is a more focused library.
  • May have a steeper learning curve for developers who only need basic string case conversion functionality.
  • Requires installation of the entire golang/tools package, even if you only need the string case conversion functionality.

Code Comparison

iancoleman/strcase:

func ToSnake(s string) string {
    return strings.ToLower(goSnakeCase.ReplaceAllString(s, "_"))
}

golang/tools:

func camelToSnake(s string) string {
    var b strings.Builder
    var lastLower bool
    for i := range s {
        c := s[i]
        if unicode.IsUpper(rune(c)) {
            if lastLower {
                b.WriteByte('_')
            }
            b.WriteByte(byte(unicode.ToLower(rune(c))))
        } else {
            b.WriteByte(byte(c))
        }
        lastLower = unicode.IsLower(rune(c))
    }
    return b.String()
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

strcase

Godoc Reference Build Status Coverage Go Report Card

strcase is a go package for converting string case to various cases (e.g. snake case or camel case) to see the full conversion table below.

Example

s := "AnyKind of_string"
FunctionResult
ToSnake(s)any_kind_of_string
ToSnakeWithIgnore(s, '.')any_kind.of_string
ToScreamingSnake(s)ANY_KIND_OF_STRING
ToKebab(s)any-kind-of-string
ToScreamingKebab(s)ANY-KIND-OF-STRING
ToDelimited(s, '.')any.kind.of.string
ToScreamingDelimited(s, '.', '', true)ANY.KIND.OF.STRING
ToScreamingDelimited(s, '.', ' ', true)ANY.KIND OF.STRING
ToCamel(s)AnyKindOfString
ToLowerCamel(s)anyKindOfString

Install

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

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")

}

NPM DownloadsLast 30 Days