strnaming

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2021 License: MIT Imports: 2 Imported by: 2

README

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

Documentation

Overview

Package strnaming is used to replace the naming method of strings.

For example:

package main

import (
	"fmt"

	"github.com/startdusk/strnaming"
)

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

set preifx

camel.WithPrefix("My")
fmt.Println(camel.Convert("user_id")) // MyuserId

set first char upper

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

set prefix

camel.WithPrefix("My")
fmt.Println(camel.Convert("user_name")) // MyuserName

set customize delimiter

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

set cache

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

set style

package main

import (
	"fmt"

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

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Camel

type Camel struct {
	// contains filtered or unexported fields
}

Camel defines a cameler

func NewCamel

func NewCamel() *Camel

NewCamel creates a camel struct

func (*Camel) Convert

func (c *Camel) Convert(str string) string

Convert to camel string

func (*Camel) WithCache

func (c *Camel) WithCache(key, value string) *Camel

WithCache set cache

func (*Camel) WithDelimiter added in v0.2.0

func (c *Camel) WithDelimiter(delimiters ...byte) *Camel

WithDelimiter set delimiter char

func (*Camel) WithPrefix added in v0.2.0

func (c *Camel) WithPrefix(prefix string) *Camel

WithPrefix set prefix

func (*Camel) WithStyle added in v0.5.0

func (c *Camel) WithStyle(style Style) *Camel

WithStyle using lang style

func (*Camel) WithUpperFirst added in v0.4.0

func (c *Camel) WithUpperFirst(upperFirst bool) *Camel

WithUpperFirst set first char upper

type Spacer

type Spacer struct {
	// contains filtered or unexported fields
}

Spacer defines a spacer struct

func NewKebab

func NewKebab() *Spacer

NewKebab creates a kebaber

func NewSnake

func NewSnake() *Spacer

NewSnake creates a snaker

func (*Spacer) Convert

func (c *Spacer) Convert(str string) string

Convert to spacer string

func (*Spacer) WithCache

func (c *Spacer) WithCache(key, value string) *Spacer

WithCache set cache

func (*Spacer) WithIgnore

func (c *Spacer) WithIgnore(ignores ...byte) *Spacer

WithIgnore ignore special char eg $

func (*Spacer) WithPrefix added in v0.2.0

func (c *Spacer) WithPrefix(prefix string) *Spacer

WithPrefix set prefix

func (*Spacer) WithScreaming

func (c *Spacer) WithScreaming(screaming bool) *Spacer

WithScreaming convert all char for upper

type Style added in v0.6.0

type Style interface {
	// Cut according to the separator
	// enter a complete string (elem) and change it to your favorite code style
	Transformation(elem string) string
}

Style defines style of language

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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