Functional Weave
Code in Rust

text.initials@1.0.0

README.md

1,789 bytes · view raw

# text.initials

The initials of a personal name, for an avatar, a monogram or an audit column:
"John Smith" is "JS", "Jean-Paul Sartre" is "JPS", "J.R.R. Tolkien" is
"JRRT".

## The rules

- Words are separated by whitespace (the Unicode White_Space set, listed
  explicitly so all three languages agree), and each word is split again at
  hyphens and full stops, so hyphenated forenames and dotted initials each
  contribute a letter.
- Each part contributes its first character, upper-cased. Leading ASCII
  punctuation and typographic quotes are skipped first, so "(John)" gives "J"
  and "“Kit”" gives "K". An apostrophe inside a part is not a split:
  "O'Brien" gives "O", not "OB".
- Name particles (van, von, de, da, di, del, della, der, den, des, du, la,
  le, ter, ten, dos, das, do, bin, ibn) are skipped when they are neither the
  first nor the last word: "Ludwig van Beethoven" is "LB", "José María de la
  Cruz" is "JMC". As the first word they count: "Van Morrison" is "VM".
- The result has no spaces or full stops. Add them yourself ("J. S.") if the
  design wants them. There is no length cap: take the first two characters if
  your avatar only has room for two.

## Letters outside ASCII

Upper-casing uses each language's own per-character mapping, and only when it
maps to exactly one character, so "émile" gives "É" everywhere and "ß" stays
"ß". A script without case contributes its first character as it is:
"李 小龙" gives "李小". Those are the characters a Latin-script reader would
expect; they are not necessarily how people who write in that script
abbreviate names.

Characters are code points: a letter written as a base letter plus a
combining accent contributes only the base letter.

The name must be a string; anything else is an error.