Last active
May 22, 2024 09:28
-
-
Save haroldus-/1826d68caa8cf76138e924a373ae4c40 to your computer and use it in GitHub Desktop.
ruby name capitalisation method with (non-exhaustive) exceptions for hyphens, apostrophes, lowercase (nobiliary) particles, and names with two capital letters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
array.join.strip | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment