name

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: CC0-1.0 Imports: 2 Imported by: 51

README

API Documentation Build Status

About

… a naming-convention library for the Go programming language. The two categories are delimiter-separated and letter case-separated words. Each of the formatting functions support both techniques for input, without any context.

This is free and unencumbered software released into the public domain.

Inspiration

  • name.CamelCase("pascal case", true) returns “PascalCase”
  • name.CamelCase("snake_to_camel AND CamelToCamel?", false) returns “snakeToCamelANDCamelToCamel”
  • name.Delimit("* All Hype is aGoodThing (TM)", '-') returns “all-hype-is-a-good-thing-TM”
  • name.DotSeparated("WebCrawler#socketTimeout") returns “web.crawler.socket.timeout”

Performance

The following results were measured with Go 1.15 on an Intel i5-7500.

name                                                            time/op
Cases/a2B/CamelCase-4                                           38.9ns ± 5%
Cases/a2B/snake_case-4                                          41.1ns ± 1%
Cases/foo-bar/CamelCase-4                                       58.0ns ± 6%
Cases/foo-bar/snake_case-4                                      67.0ns ± 1%
Cases/ProcessHelperFactoryConfig#defaultIDBuilder/CamelCase-4    272ns ± 6%
Cases/ProcessHelperFactoryConfig#defaultIDBuilder/snake_case-4   324ns ± 1%

name                                                            alloc/op
Cases/a2B/CamelCase-4                                            3.00B ± 0%
Cases/a2B/snake_case-4                                           4.00B ± 0%
Cases/foo-bar/CamelCase-4                                        8.00B ± 0%
Cases/foo-bar/snake_case-4                                       16.0B ± 0%
Cases/ProcessHelperFactoryConfig#defaultIDBuilder/CamelCase-4    48.0B ± 0%
Cases/ProcessHelperFactoryConfig#defaultIDBuilder/snake_case-4   64.0B ± 0%

name                                                            allocs/op
Cases/a2B/CamelCase-4                                             1.00 ± 0%
Cases/a2B/snake_case-4                                            1.00 ± 0%
Cases/foo-bar/CamelCase-4                                         1.00 ± 0%
Cases/foo-bar/snake_case-4                                        1.00 ± 0%
Cases/ProcessHelperFactoryConfig#defaultIDBuilder/CamelCase-4     1.00 ± 0%
Cases/ProcessHelperFactoryConfig#defaultIDBuilder/snake_case-4    1.00 ± 0%

Documentation

Overview

Package name implements various naming conventions. The two categories are delimiter-separated and letter case-separated words. Each of the formatting functions support both techniques for input, without any context.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(s string, upper bool) string

CamelCase returns the medial capitals form of the words in s. Words consist of Unicode letters and/or numbers in any order. Upper case sequences [abbreviations] are preserved.

Argument upper forces the letter case for the first rune. Use true for UpperCamelCase, a.k.a. PascalCase. Use false for lowerCamelCase, a.k.a. dromedaryCase.

BUG(pascaldekloe): Abbreviations at the beginning of a name may look odd in lowerCamelCase, i.e., "tCPConn".

BUG(pascaldekloe): CamelCase concatenates abbreviations by design, i.e., "DB-API" becomes "DBAPI".

Example
package main

import (
	"fmt"
	"github.com/pascaldekloe/name"
)

func main() {
	fmt.Println(name.CamelCase("pascal case", true))
	fmt.Println(name.CamelCase("snake_to_camel AND CamelToCamel?", false))

}
Output:

PascalCase
snakeToCamelANDCamelToCamel

func Delimit

func Delimit(s string, sep rune) string

Delimit returns the words in s delimited with separator sep. Words consist of Unicode letters and/or numbers in any order. Upper case sequences [abbreviations] are preserved. Use strings.ToLower or ToUpper to enforce one letter case.

Example
package main

import (
	"fmt"
	"github.com/pascaldekloe/name"
)

func main() {
	// Garbage to lisp-case:
	fmt.Println(name.Delimit("* All Hype is aGoodThing (TM)", '-'))

	// Extract a Java property key:
	fmt.Println(name.DotSeparated("WebCrawler#socketTimeout"))

}
Output:

all-hype-is-a-good-thing-TM
web.crawler.socket.timeout

func DotSeparated

func DotSeparated(s string) string

DotSeparated returns Delimit(s, '.'), a.k.a. the dot notation.

func SnakeCase

func SnakeCase(s string) string

SnakeCase returns Delimit(s, '_'), a.k.a. the snake_case.

Types

This section is empty.

Notes

Bugs

  • Abbreviations at the beginning of a name may look odd in lowerCamelCase, i.e., "tCPConn".

  • CamelCase concatenates abbreviations by design, i.e., "DB-API" becomes "DBAPI".

Jump to

Keyboard shortcuts

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