Git Product home page Git Product logo

cases's Introduction

cases

Go Reference Build Status

A case conversion library for Go.

cases

The currently supported cases are:

Function Output
cases.ToCamel(s) camelCase
cases.ToPascal(s) PascalCase
cases.ToSnake(s) snake_case
cases.ToScreamingSnake(s) SCREAMING_SNAKE_CASE
cases.ToKebab(s) kebab-case
cases.ToScreamingKebab(s) SCREAMING-KEBAB-CASE
cases.ToTrain(s) Train-Case
cases.ToLower(s) lower case
cases.ToTitle(s) Title Case
cases.ToUpper(s) UPPER CASE
cases.Transform(s, wordFn, delimFn) your own case here

Word boundaries are defined as follows:

  • A set of consecutive Unicode non-letter/number/symbol e.g. foo _bar is two words (foo and bar)
  • A transition from a lowercase letter to an uppercase letter e.g. fooBar is two words (foo and Bar)
  • The second last uppercase letter in a word with multiple uppercase letters e.g. FOOBar is two words (FOO and Bar)

Getting started

Install using

go get -u github.com/rossmacarthur/cases

Now convert a string using the relevant function.

import "github.com/rossmacarthur/cases"

cases.ToSnake("XMLHttpRequest") // returns "xml_http_request"

Customizing

This library also exposes a Transform function which allows flexible customization of the output.

For example if you wanted dotted.snake.case you could do the following.

import (
    "strings"
    "github.com/rossmacarthur/cases"
)

func delimDot(s *strings.Builder) {
    s.WriteRune('.')
}

cases.Transform("XmlHttpRequest", cases.ToLower, delimDot) // returns xml.http.request

Here is a more involved example in order to handle acronyms in PascalCase.

import (
    "strings"
    "github.com/rossmacarthur/cases"
)

// The default ToPascal function has no understanding of acronyms
cases.ToPascal("xml_http_request") // returns "XmlHttpRequest"

// We can instead use Transform directly
writeFn := func(s *strings.Builder, word string) {
    w := strings.ToUpper(asLower)
    if w == "XML" || w == "HTTP" {
        s.WriteString(w)
    } else {
        // fallback to default
        cases.WriteTitle(s, word)
    }
}
cases.Transform("xml_http_request", writeFn, nil) // returns "XMLHTTPRequest"

License

This project is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.

cases's People

Contributors

rossmacarthur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cases's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.