Skip to content

vgavara/go-propername

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-propername

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.

Installation

go get github.com/vgavara/go-propername

Usage

import "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"

API

ToNameCase(name string) string

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/Mac prefix capitalization rules

Trim(name string) string

Removes leading and trailing whitespace and collapses internal runs of whitespace to a single space.

Supported conventions

Word boundaries

The following characters are treated as word boundaries (the segment after each is capitalized):

  • Whitespace
  • Hyphens: goodfellow-smithGoodfellow-Smith
  • Apostrophes (', ', ʼ): o'connorO'Connor

Prefixes

Prefix Example input Output
Mc mcdonald McDonald
Mac macgregor MacGregor

Lowercase particles

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

Out of scope (v0)

  • Non-Latin scripts (Arabic, Chinese, Japanese, Korean, Cyrillic, etc.)
  • Full name parsing (splitting into first / last / middle)
  • Prefix/suffix handling (Mr., Dr., PhD, etc.)

License

MIT

About

Go package for sanitizing and proper casing of human names in Latin script — MIT licensed

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages