A Go library for sanitizing and proper casing of human names written in Latin script. Zero dependencies, MIT licensed.
A port of the propername npm package.
go get github.com/vgavara/go-propernameimport "github.com/vgavara/go-propername"
propername.ToNameCase("john smith") // → "John Smith"
propername.ToNameCase("JOHN SMITH") // → "John Smith"
propername.ToNameCase("ludwig van beethoven") // → "Ludwig van Beethoven"
propername.ToNameCase("mcdonald") // → "McDonald"
propername.ToNameCase("macgregor") // → "MacGregor"
propername.ToNameCase("o'brien") // → "O'Brien"
propername.ToNameCase("marie-anne van der berg") // → "Marie-Anne van der Berg"
// Trim first to sanitize whitespace, then case
propername.ToNameCase(propername.Trim(" john smith ")) // → "John Smith"
// Trim only
propername.Trim(" john smith ") // → "john smith"Converts a human name string to proper name casing. Whitespace structure is preserved as-is — call Trim first if you want whitespace sanitized.
- Splits on whitespace, hyphens (
-), and apostrophe variants (','U+2019,ʼU+02BC) - Capitalizes each word, except particles which stay lowercase unless they appear at the start of the string
- Applies
Mc/Macprefix capitalization rules
Removes leading and trailing whitespace and collapses internal runs of whitespace to a single space.
The following characters are treated as word boundaries (the segment after each is capitalized):
- Whitespace
- Hyphens:
goodfellow-smith→Goodfellow-Smith - Apostrophes (
',',ʼ):o'connor→O'Connor
| Prefix | Example input | Output |
|---|---|---|
Mc |
mcdonald |
McDonald |
Mac |
macgregor |
MacGregor |
Particles are kept lowercase unless they appear as the first word of the string.
| Culture | Particles |
|---|---|
| Dutch | van, de, den, der, ten, ter |
| German | von, zu |
| French / Italian / Spanish / Portuguese | de, di, da, del, della, degli, dei, las, los, le, la, l', d', dos, das, e |
| Welsh | ap, ab, ferch, verch |
| Arabic (romanized) | al, el, bin, bint, ibn, abu |
| Scottish | of |
- Non-Latin scripts (Arabic, Chinese, Japanese, Korean, Cyrillic, etc.)
- Full name parsing (splitting into first / last / middle)
- Prefix/suffix handling (Mr., Dr., PhD, etc.)