Documentation
¶
Overview ¶
Package camelcase converts dash/dot/underscore/space separated (and mixed-case) strings into camelCase or PascalCase, modeled on the npm "camelcase" package that Express-adjacent tooling uses to normalize option keys, identifiers, and header-derived names. It exposes CamelCase for the default lower-first form, PascalCase for the upper-first form, and CamelCaseWith for callers that want to choose the style through an Options value, all built on only the Go standard library.
Reach for this package whenever human-written or wire-format names have to be turned into program-friendly identifiers: converting "user-id" or "user_id" from a config file into "userId", mapping "Content-Type" style header names to struct-field-like tokens, or normalizing the many separator conventions a single input source might mix together. Keeping the word-splitting rules in one place means "foo-bar", "foo_bar", "foo bar", and "fooBar" all collapse to the same canonical output regardless of how the caller happened to write them.
Word splitting is driven by two kinds of boundary. Delimiters — spaces, hyphens, underscores, and every other character that is neither a letter nor a digit — separate words and are discarded. Case transitions are also boundaries: a lower-case or digit followed by an upper-case letter starts a new word (so "fooBar" splits into "foo" and "Bar"), and a run of upper-case letters immediately followed by a lower-case letter breaks just before that final upper-case letter (so "HTTPServer" splits into "HTTP" and "Server", and "FOOBar" into "FOO" and "Bar"). Each recovered word is then normalized by lower-casing it entirely and, for every word after the first in camelCase or every word in PascalCase, upper-casing its leading rune.
The semantics around edges and degenerate input are deliberate. Leading and trailing separators are ignored and consecutive separators collapse, so "__foo__bar__" yields "fooBar"; an input that contains no letters or digits (or the empty string) yields "". Runs of consecutive upper-case letters are lower-cased by default rather than preserved, matching the npm package's non-preserveConsecutiveUppercase default, so "FOOBar" becomes "fooBar" and not "fOOBar". Embedded digits are preserved and treated as ordinary in-word characters, so "foo2bar" stays "foo2bar" rather than gaining a spurious word boundary at the digit. The transformation operates on runes, so multi-byte letters are handled without corrupting the encoding.
Parity with the Node original covers the common cases: the same separator set, the same case-boundary detection, and the same lower-first/upper-first output for typical identifiers. The intentional differences are idiomatic. The API is three exported Go functions plus an Options struct rather than a single JavaScript function with an options object, and this port implements only the default behavior of the more exotic npm flags (it does not offer locale-specific casing or a preserveConsecutiveUppercase toggle). Where a choice had to be made the default lodash/camelcase behavior is followed so that results line up with what Express developers expect for ordinary ASCII input.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CamelCase ¶
CamelCase converts s to camelCase. The first word is lower-cased and every subsequent word is title-cased, for example "foo-bar_baz", "foo bar" and "Foo Bar" all become "fooBar"/"fooBarBaz".
Example ¶
ExampleCamelCase converts a delimiter-separated name into camelCase. Spaces, hyphens, underscores, and other non-alphanumeric characters all act as word boundaries and are discarded. The first word is lower-cased and every subsequent word is title-cased, so "foo-bar-baz" becomes "fooBarBaz". This is the transform used to turn config keys or header-derived names into program-friendly identifiers.
package main
import (
"fmt"
"github.com/malcolmston/express/camelcase"
)
func main() {
fmt.Println(camelcase.CamelCase("foo-bar-baz"))
}
Output: fooBarBaz
Example (Acronym) ¶
ExampleCamelCase_acronym demonstrates how runs of consecutive uppercase letters are handled. A case transition marks a word boundary, and a run of uppercase letters followed by a lowercase letter breaks just before that final letter, so "HTTPServer" splits into "HTTP" and "Server". Each word is then lower-cased and re-capitalized, collapsing the acronym, so the camelCase result is "httpServer".
package main
import (
"fmt"
"github.com/malcolmston/express/camelcase"
)
func main() {
fmt.Println(camelcase.CamelCase("HTTPServer"))
}
Output: httpServer
func CamelCaseWith ¶
CamelCaseWith converts s to camelCase or PascalCase according to opts.
func PascalCase ¶
PascalCase converts s to PascalCase (also called UpperCamelCase). Every word, including the first, is title-cased, for example "foo-bar" becomes "FooBar".
Example ¶
ExamplePascalCase converts a name into PascalCase (also called UpperCamelCase), where every word including the first is title-cased. It shares the same word-splitting rules as CamelCase but upper-cases the leading character of the result. Here "foo-bar" becomes "FooBar". This form is handy for deriving exported-style type or constructor names.
package main
import (
"fmt"
"github.com/malcolmston/express/camelcase"
)
func main() {
fmt.Println(camelcase.PascalCase("foo-bar"))
}
Output: FooBar