Skip to content

Instantly share code, notes, and snippets.

@bolollo
Created July 26, 2018 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bolollo/4b2afabf11c8c35f6ebf1b696545bd14 to your computer and use it in GitHub Desktop.
Save bolollo/4b2afabf11c8c35f6ebf1b696545bd14 to your computer and use it in GitHub Desktop.
capitalizar nombres de municipios
function toTitleCase(str){
str = str.toUpperCase();
str = str.replace(/\S*/g, function(txt){
if(!["DEL","LES","LA","DE", "EL", "I"].includes(txt)){
const re = /(.*)([L|D]')(.*)/i;
if(txt.match(re)){
if("" !== txt.match(re)[1]){
return toTitleCase(txt.match(re)[1]) + txt.match(re)[2].toLowerCase() + toTitleCase(txt.match(re)[3]);
}else{
return txt.match(re)[2].toLowerCase() + toTitleCase(txt.match(re)[3]);
}
}else{
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
} else {
return txt.toLowerCase();
}
});
return str.charAt(0).toUpperCase() + str.substr(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment