Created
July 26, 2018 13:02
-
-
Save bolollo/4b2afabf11c8c35f6ebf1b696545bd14 to your computer and use it in GitHub Desktop.
capitalizar nombres de municipios
This file contains 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
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