...

Source file src/github.com/gobuffalo/flect/titleize.go

Documentation: github.com/gobuffalo/flect

     1  package flect
     2  
     3  import (
     4  	"strings"
     5  	"unicode"
     6  )
     7  
     8  // Titleize will capitalize the start of each part
     9  //	"Nice to see you!" = "Nice To See You!"
    10  //	"i've read a book! have you?" = "I've Read A Book! Have You?"
    11  //	"This is `code` ok" = "This Is `code` OK"
    12  func Titleize(s string) string {
    13  	return New(s).Titleize().String()
    14  }
    15  
    16  // Titleize will capitalize the start of each part
    17  //	"Nice to see you!" = "Nice To See You!"
    18  //	"i've read a book! have you?" = "I've Read A Book! Have You?"
    19  //	"This is `code` ok" = "This Is `code` OK"
    20  func (i Ident) Titleize() Ident {
    21  	var parts []string
    22  
    23  	// TODO: we need to reconsider the design.
    24  	//       this approach preserves inline code block as is but it also
    25  	//       preserves the other words start with a special character.
    26  	//       I would prefer: "*wonderful* world" to be "*Wonderful* World"
    27  	for _, part := range i.Parts {
    28  		// CAUTION: in unicode, []rune(str)[0] is not rune(str[0])
    29  		runes := []rune(part)
    30  		x := string(unicode.ToTitle(runes[0]))
    31  		if len(runes) > 1 {
    32  			x += string(runes[1:])
    33  		}
    34  		parts = append(parts, x)
    35  	}
    36  
    37  	return New(strings.Join(parts, " "))
    38  }
    39  

View as plain text