strcase

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

README

strcase

Package strcase provides functions for manipulating the case of strings (CamelCase, kebab-case, snake_case, Sentence case, etc). It is based on https://github.com/ettle/strcase, which is Copyright (c) 2020 Liyan David Chang under the MIT License. Its principle difference from other strcase packages is that it preserves acronyms in input text for CamelCase. Therefore, you must call strings.ToLower on any SCREAMING_INPUT_STRINGS before passing them to ToCamel, ToLowerCamel, ToTitle, and ToSentence.

Documentation

Overview

Package strcase provides functions for manipulating the case of strings (CamelCase, kebab-case, snake_case, Sentence case, etc). It is based on https://github.com/ettle/strcase, which is Copyright (c) 2020 Liyan David Chang under the MIT License. Its principle difference from other strcase packages is that it preserves acronyms in input text for CamelCase. Therefore, you must call strings.ToLower on any SCREAMING_INPUT_STRINGS before passing them to ToCamel, ToLowerCamel, ToTitle, and ToSentence.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatList

func FormatList(items ...string) string

FormatList returns a formatted version of the given list of items following these rules:

  • nil => ""
  • "Go" => "Go"
  • "Go", "Python" => "Go and Python"
  • "Go", "Python", "JavaScript" => "Go, Python, and JavaScript"
  • "Go", "Python", "JavaScript", "C" => "Go, Python, JavaScript, and C"

func To

func To(s string, c Cases) string

To converts the given string to the given case.

func ToCamel

func ToCamel(s string) string

ToCamel returns words in CamelCase (capitalized words concatenated together). Also known as UpperCamelCase.

func ToKEBAB

func ToKEBAB(s string) string

ToKEBAB returns words in KEBAB-CASE (upper case words with dashes). Also known as SCREAMING-KEBAB-CASE or SCREAMING-DASH-CASE.

func ToKebab

func ToKebab(s string) string

ToKebab returns words in kebab-case (lower case words with dashes). Also known as dash-case.

func ToLowerCamel

func ToLowerCamel(s string) string

ToLowerCamel returns words in lowerCamelCase (capitalized words concatenated together, with first word lower case). Also known as camelCase or mixedCase.

func ToSNAKE

func ToSNAKE(s string) string

ToSNAKE returns words in SNAKE_CASE (upper case words with underscores). Also known as SCREAMING_SNAKE_CASE or UPPER_CASE.

func ToSentence

func ToSentence(s string) string

ToSentence returns words in Sentence case (lower case words with spaces, with the first word capitalized).

func ToSnake

func ToSnake(s string) string

ToSnake returns words in snake_case (lower case words with underscores).

func ToTitle

func ToTitle(s string) string

ToTitle returns words in Title Case (capitalized words with spaces).

func ToWordCase

func ToWordCase(input string, wordCase WordCases, delimiter rune) string

ToWordCase converts the given input string to the given word case with the given delimiter. Pass 0 for delimeter to use no delimiter.

Types

type Cases

type Cases int32 //enums:enum

Cases is an enum with all of the different string cases.

const (
	// LowerCase is all lower case
	LowerCase Cases = iota

	// UpperCase is all UPPER CASE
	UpperCase

	// SnakeCase is lower_case_words_with_underscores
	SnakeCase

	// SNAKECase is UPPER_CASE_WORDS_WITH_UNDERSCORES
	SNAKECase

	// KebabCase is lower-case-words-with-dashes
	KebabCase

	// KEBABCase is UPPER-CASE-WORDS-WITH-DASHES
	KEBABCase

	// CamelCase is CapitalizedWordsConcatenatedTogether
	CamelCase

	// LowerCamelCase is capitalizedWordsConcatenatedTogether, with the first word lower case
	LowerCamelCase

	// TitleCase is Captitalized Words With Spaces
	TitleCase

	// SentenceCase is Lower case words with spaces, with the first word capitalized
	SentenceCase
)
const CasesN Cases = 10

CasesN is the highest valid value for type Cases, plus one.

func CasesValues

func CasesValues() []Cases

CasesValues returns all possible values for the type Cases.

func (Cases) Desc

func (i Cases) Desc() string

Desc returns the description of the Cases value.

func (Cases) Int64

func (i Cases) Int64() int64

Int64 returns the Cases value as an int64.

func (Cases) MarshalText

func (i Cases) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Cases) SetInt64

func (i *Cases) SetInt64(in int64)

SetInt64 sets the Cases value from an int64.

func (*Cases) SetString

func (i *Cases) SetString(s string) error

SetString sets the Cases value from its string representation, and returns an error if the string is invalid.

func (Cases) String

func (i Cases) String() string

String returns the string representation of this Cases value.

func (*Cases) UnmarshalText

func (i *Cases) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Cases) Values

func (i Cases) Values() []enums.Enum

Values returns all possible values for the type Cases.

type SplitAction

type SplitAction int

SplitAction defines if and how to split a string

const (
	// Noop - Continue to next character
	Noop SplitAction = iota
	// Split - Split between words
	// e.g. to split between wordsWithoutDelimiters
	Split
	// SkipSplit - Split the word and drop the character
	// e.g. to split words with delimiters
	SkipSplit
	// Skip - Remove the character completely
	Skip
)

type WordCases

type WordCases int32 //enums:enum -trim-prefix Word

WordCases is an enumeration of the ways to format a word.

const (
	// WordOriginal indicates to preserve the original input case.
	WordOriginal WordCases = iota

	// WordLowerCase indicates to make all letters lower case (example).
	WordLowerCase

	// WordUpperCase indicates to make all letters upper case (EXAMPLE).
	WordUpperCase

	// WordTitleCase indicates to make only the first letter upper case (Example).
	WordTitleCase

	// WordCamelCase indicates to make only the first letter upper case, except
	// in the first word, in which all letters are lower case (exampleText).
	WordCamelCase

	// WordSentenceCase indicates to make only the first letter upper case, and
	// only for the first word (all other words have fully lower case letters).
	WordSentenceCase
)
const WordCasesN WordCases = 6

WordCasesN is the highest valid value for type WordCases, plus one.

func WordCasesValues

func WordCasesValues() []WordCases

WordCasesValues returns all possible values for the type WordCases.

func (WordCases) Desc

func (i WordCases) Desc() string

Desc returns the description of the WordCases value.

func (WordCases) Int64

func (i WordCases) Int64() int64

Int64 returns the WordCases value as an int64.

func (WordCases) MarshalText

func (i WordCases) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*WordCases) SetInt64

func (i *WordCases) SetInt64(in int64)

SetInt64 sets the WordCases value from an int64.

func (*WordCases) SetString

func (i *WordCases) SetString(s string) error

SetString sets the WordCases value from its string representation, and returns an error if the string is invalid.

func (WordCases) String

func (i WordCases) String() string

String returns the string representation of this WordCases value.

func (*WordCases) UnmarshalText

func (i *WordCases) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (WordCases) Values

func (i WordCases) Values() []enums.Enum

Values returns all possible values for the type WordCases.

Jump to

Keyboard shortcuts

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