/strnaming

Package strnaming is used to convert string naming to camelCase, snake_case, kebab-case.

Primary LanguageGoMIT LicenseMIT

strnaming

Godoc Reference Go Report Card Build Status  Release Goproxy.cn

Package strnaming is used to Convert string to camelCase, snake_case, kebab-case.

Contents

API Examples

Install

To start using strnaming, install Go and run go get:

$ go get -u github.com/startdusk/strnaming

This will retrieve the library.

Quick start

camel

package main

import (
	"fmt"

	"github.com/startdusk/strnaming"
)

func main() {
	// camel case
	camel := strnaming.NewCamel()
	fmt.Println(camel.Convert("camelcase_key")) // camelcaseKey

	fmt.Println(camel.Convert("user_id")) // userId

	camel.WithDelimiter('-')
	fmt.Println(camel.Convert("user-id")) // userId

	camel.WithUpperFirst(true)
	fmt.Println(camel.Convert("user_id")) // UserId

	camel.WithCache("user_id", "UserID")
	fmt.Println(camel.Convert("user_id")) // UserID

	fmt.Println(camel.Convert("json_data")) // JsonData
	fmt.Println(camel.Convert("http_test")) // HttpTest

	camel.WithPrefix("My")
	camel.WithUpperFirst(false)
	fmt.Println(camel.Convert("user_name")) // MyuserName
}
using golang style
package main

import (
	"fmt"

	"github.com/startdusk/strnaming"
	"github.com/startdusk/strnaming/style"
)

func main() {
	camel := strnaming.NewCamel()
	fmt.Println(camel.Convert("json_data")) // JsonData
	fmt.Println(camel.Convert("http_test")) // HttpTest
	
	camel.WithStyle(style.NewGolang())
	fmt.Println(camel.Convert("json_data")) // JSONData
	fmt.Println(camel.Convert("http_test")) // HTTPTest
}

snake

package main

import (
	"fmt"

	"github.com/startdusk/strnaming"
)

func main() {
	// snake case
	snake := strnaming.NewSnake()
	fmt.Println(snake.Convert("SnakeKey")) // snake_key

	snake.WithIgnore('-')
	fmt.Println(snake.Convert("My-IDCard")) // my-id_card

	snake.WithScreaming(true)
	fmt.Println(snake.Convert("SnakeKey")) // SNAKE_KEY

	snake.WithCache("UserID", "userid")
	fmt.Println(snake.Convert("UserID")) // userid

	snake.WithPrefix("go")
	snake.WithScreaming(false)
	fmt.Println(snake.Convert("PageSize")) // go_page_size
}

kebab

package main

import (
	"fmt"

	"github.com/startdusk/strnaming"
)

func main() {
	// kebab case
	kebab := strnaming.NewKebab()
	fmt.Println(kebab.Convert("KebabKey")) // kebab-key

	kebab.WithIgnore('@', '.')
	fmt.Println(kebab.Convert("ben_love@gmail.com")) // ben-love@gmail.com

	kebab.WithScreaming(true)
	fmt.Println(kebab.Convert("KebabKey")) // KEBAB-KEY

	kebab.WithCache("UserID", "User-Id")
	fmt.Println(kebab.Convert("UserID")) // User-Id

	kebab.WithPrefix("go")
	kebab.WithScreaming(false)
	fmt.Println(kebab.Convert("PageSize")) // go-page-size
}

CLI Examples

Install

To start using strnaming in command line, install Go and run go get:

$ go get -u github.com/startdusk/strnaming/cmd/strnaming

Quick start

convert json keys to camelcase keys, like:

// ./testdata/test.json

{
  "test_url": "http://json-schema.org/draft-04/schema",
  "another_case": [
    {
      "sub_case": [
        {
          "for_ready": 1234,
          "bba_media": "hahahaha"
        }
      ]
    },
    {
      "sub_url_two": [
        {
          "for_ready_two": "ben",
          "bba_media_two": 2021,
          "key_space": [
            [
              [
                {
                  "low_code": true
                }
              ]
            ]
          ]
        }
      ]
    }
  ]
}

command:

$ strnaming c -f=./testdata/test.json

output:

{
  "anotherCase": [
    {
      "subCase": [
        {
          "bbaMedia": "hahahaha",
          "forReady": 1234
        }
      ]
    },
    {
      "subUrlTwo": [
        {
          "bbaMediaTwo": 2021,
          "forReadyTwo": "ben",
          "keySpace": [
            [
              [
                {
                  "lowCode": true
                }
              ]
            ]
          ]
        }
      ]
    }
  ],
  "testUrl": "http://json-schema.org/draft-04/schema"
}

Help

using main command:

$ strnaming help

output:

NAME:
   strnaming - a cli tool to convert string name

USAGE:
   strnaming [global options] command [command options] [arguments...]

VERSION:
   v0.4.0 linux/amd64

COMMANDS:
   camel, c  convert to camel string
   snake, s  convert to snake string
   kebab, k  convert to kabab string
   help, h   Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h     show help (default: false)
   --version, -v  print the version (default: false)

using sub command like camel:

$ strnaming c -help

output:

NAME:
   strnaming camel - convert to camel string

USAGE:
   strnaming camel [command options] [arguments...]

OPTIONS:
   --file value, -f value       input a json file path (eg: /path/to/strnaming.json)
   --json value, -j value       input a json
   --delimiter value, -d value  using custom delimiter (default: _)
   --upperFirst, --uf           using first char upper (default: false)
   --prefix value, -p value     using prefix
   --cache value, -c value      using cache (eg: -c="user_id" -c="UserID")
   --help, -h                   show help (default: false)

TODO

  • Add prefix for string
  • Cli for command line access
  • Support Golang language naming style
  • Support Golang language naming style for cli tool