Created
June 1, 2014 15:40
-
-
Save boughtonp/a9c7afc0438d39fe7aaa to your computer and use it in GitHub Desktop.
NameCase - Capitalise first letter of each word, with exceptions (McIntire,Mackie,O'Connor)
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
<cffunction name="NameCase" returntype="String" output=false access="public" | |
hint="Capitalise first letter of each word, with exceptions (McIntire,Mackie,O'Connor)" | |
> | |
<cfargument name="Text" type="String" required=true /> | |
<cfscript> | |
// Uppercase first letter of every word, except special cases... | |
var Name = rereplace | |
( lcase(Arguments.Text) | |
, "\b(?!(?:a[lp]|dell[ae]|de[rl]?|d[ai]|l[eo']|of|the|v[oa]n)\b)\w+" | |
, '\u\0' | |
, 'all' | |
); | |
// Lowercase 's at end of name. | |
Name = rereplace( Name , "'s\b" , '\L\0' , 'all'); | |
// McStuff and MacStuff | |
if ( refind('\bMa?c[a-z]{2,}',Name) ) | |
{ | |
Name = rereplace(Name,'\bMc([a-z])','Mc\u\1','all'); | |
// Only upper Mac if 2+ letters (avoid Mack/etc) and not ending aciozj | |
Name = rereplace(Name,'\bMac([a-z])(?=[a-z]+[^aciozj]\b)','Mac\u\1','all'); | |
// Lower exceptions that don't end in aciozj | |
Name = rereplace | |
( Name | |
, '\bMac(Evicius|Hado|Har|Hin|Hlin|Ias|Iulis|Kie|Kle|Klin|Quarie|Omber' | |
& '|In|Kintosh|Ken|Hen|Hiel|Iol|Kell|Klem|Krell|Lin|Key|Kley|Hell|Hon)\b' | |
, 'Mac\l\1' | |
, 'all' | |
); | |
// Upper exceptions that do end in aciozj | |
Name = rereplace(Name,'\bMac(murdo)','Mac\u\1','all'); | |
} | |
// Only upper Van if 2+ letters (avoid Vance/etc). | |
Name = rereplace(Name,'\bVan([a-z])(?=[a-z]{2,})','Van\u\1','all'); | |
// Uppercase Roman numerals | |
Name = rereplaceNoCase(Name,'\b(?:x{1,3}|xl|lx{0,3})?(?:i{1,3}|i[vx]|vi{0,3})?(?!\S)','\U\0','all'); | |
</cfscript> | |
<cfreturn Name /> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment