Skip to content

Instantly share code, notes, and snippets.

@haroldus-
haroldus- / namify.rb
Last active May 22, 2024 09:28
ruby name capitalisation method with (non-exhaustive) exceptions for hyphens, apostrophes, lowercase (nobiliary) particles, and names with two capital letters
def namify(value)
array = value.to_s.split(/\b/)
array.each_with_index do |e, i|
next if !!e.match(/\A(-|'|’|‘|d|e|o|t|y)\Z/)
next if !!e.match(/\A(af|da|de|di|do|du|et|la|le|of|on|to|zu)\Z/)
next if !!e.match(/\A(das|del|den|der|des|dit|dos|lis|lix|til|und|van|von)\Z/)
next if !!e.match(/\A(Abdul|Da|De|Della|Der|Di|Du|Fitz|h|La|Ma|Mac|Mak|Mc|O|Van|Vande|Vander|Varta|Vartig)[A-Z\u00c0-\u00cf\u00d0-\u00d6\u00d8-\u00de\u0130\u0152\u0160\u0178\u017d][a-z\u00df\u00e0-\u00ef\u00f0-\u00f6\u00f8-\u00ff\u0131\u0153\u0161\u017e]+/)
next if (array[i-1] == "'" || array[i-1] == "’" || array[i-1] == "‘") && array[i-2] =~ /[a-zA-Z]/ && e == 's' && (array[i+1].nil? || array[i+1] =~ /^\s*$/ )
e.capitalize!
end