naming

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: MIT Imports: 4 Imported by: 3

README

Naming conventions

GoDoc Build Status Code Coverage Go Report Card

The package naming provides methods to parse and build compound names for variables types, functions, classes or other structures in source code.

Naming conventions bring consistency, better understanding, readability and automation.

List of supported multiple-word identifier formats:

  • camelCase
  • CONSTANT_CASE
  • flatcase
  • kebab-case
  • PascalCase
  • snake_case
  • Train-Case
  • UPPERFLATCASE
Installation

To install it, you need to install Go and set your Go workspace first. Then, download and install it:

$ go get -u github.com/rvflash/naming

Import it in your code:

import "github.com/rvflash/naming"
Prerequisite

naming uses the Go modules that required Go 1.11 or later.

Documentation

Overview

Package naming provides methods to parse and build compound names for variables types, functions, classes or other structures in source code.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(s string) string

CamelCase applies the Fields method on s and returns a string with all the words capitalized after the first one.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.CamelCase("camel case data-19"))
}
Output:

camelCaseData19

func ConstantCase

func ConstantCase(s string) string

ConstantCase applies the Fields method on s and returns a string with all the words capitalized and joined with an underscore.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.ConstantCase("cgo enabled"))
}
Output:

CGO_ENABLED

func Fields

func Fields(s string) []string

Fields splits the string s around each instance of one or more consecutive white space characters, underscore or hyphen. Its also break title words. It returns a slice of substrings of s or an empty slice if s contains only white space, underscore or hyphen.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.Fields("This isA really-sample XML_or_Whatever    data"))
}
Output:

[This is A really sample XML or Whatever data]

func FlatCase

func FlatCase(s string) string

FlatCase applies the Fields method on s and concatenates any words to return only one in lowercase.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.FlatCase("go-toolDir"))
}
Output:

gotooldir

func Join added in v1.1.0

func Join(format func(s string) string, words ...string) string

Join creates a multiple-word identifier based on the given format and list of words.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.Join(naming.PascalCase, "camel", "case", "data-19"))
}
Output:

CamelCaseData19

func KebabCase

func KebabCase(s string) string

KebabCase applies the Fields method on s and returns a string with a hyphen between each word and all of them are lowercase.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.KebabCase("kebab-case Data_19"))
}
Output:

kebab-case-data-19

func PascalCase

func PascalCase(s string) string

PascalCase applies the Fields method on s and returns a string with all the words capitalized.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.PascalCase("camel case data-19"))
}
Output:

CamelCaseData19

func SnakeCase

func SnakeCase(s string) string

SnakeCase applies the Fields method on s and returns a string with an underscore between each word and all of them are lowercase.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.SnakeCase("kebab-case Data_19"))
}
Output:

kebab_case_data_19

func TrainCase

func TrainCase(s string) string

TrainCase applies the Fields method on s and returns a string with all the words capitalized and separated with a hyphen.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.TrainCase("content type"))
}
Output:

Content-Type

func UpperFlatCase

func UpperFlatCase(s string) string

UpperFlatCase applies the Fields method on s and concatenates any words to return only one in uppercase.

Example
package main

import (
	"fmt"

	"github.com/rvflash/naming"
)

func main() {
	fmt.Println(naming.UpperFlatCase("Go_Host arch"))
}
Output:

GOHOSTARCH

Types

This section is empty.

Jump to

Keyboard shortcuts

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