Git Product home page Git Product logo

strnaming's Introduction

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("[email protected]")) // [email protected]

	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

strnaming's People

Contributors

startdusk avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.