R/firstCAPS.R

firstCAPS = function(x, ignore = NULL, lower=NULL)
{
	cap1 = function(x)
	{
		if(length(grep("'", x)) > 0) {
			nomes = unlist(strsplit(x, split = "'")) 
		} else if(length(grep("-", x)) > 0) {
			nomes = unlist(strsplit(x, split = "-"))
		} else {
			nomes = unlist(strsplit(x, split = " "))
		}#en if
		
		n1 = toupper(substring(nomes, 1, 1))
		n2 = tolower(substring(nomes, 2))
		
		if(length(grep("-", x)) > 0) {
			fc = paste(n1, n2, collapse = "-", sep = "") 
		} else if(length(grep("'", x)) > 0){
			fc = paste(n1, n2, collapse = "'", sep = "")
		} else {
			fc = paste(n1, n2, collapse = " ", sep = "")
		}#end if
		
		return(fc)
	}#end function cap1
	
	cap2 = function(x, ig, lw)
	{
		
		x = unlist(strsplit(x, split = " "))

		ig.s = x[x %in% ig]
		
		if(!length(ig.s)) ig.s = ""

		fc = sapply(x, cap1, USE.NAMES = FALSE)
		
		fc[na.omit(match(ig, x))] = ig.s
		
		cap3 = function(x, fc, lw)
		{
			lw.s = x[x %in% lw]

			#return(x)
			if(!length(lw.s)) lw.s = ""

			#return(list(lw, lw.s, x))
			fc[na.omit(match(lw, x))] = tolower(lw.s)

			return(fc)
		}#end function cap3

		fc = cap3(x, fc, lw)
		fc = paste(fc, sep = "", collapse = " ")
		
		return(fc)
	}#end function cap2

	#out = cap2(x, ig = ignore, lw = unique(lower))
	out = sapply(x, cap2, USE.NAMES = FALSE, ig = ignore, lw = unique(lower))

	return(out)
}#end function firstCAPS
salah31416/toolbox documentation built on June 3, 2019, 6:59 p.m.