github.com/blend/go-sdk@v1.20220411.3/stringutil/title.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package stringutil
     9  
    10  import (
    11  	"bytes"
    12  	"unicode"
    13  )
    14  
    15  // Title returns a string in title case.
    16  func Title(corpus string) string {
    17  	output := bytes.NewBuffer(nil)
    18  	runes := []rune(corpus)
    19  
    20  	haveSeenLetter := false
    21  	var r rune
    22  	for x := 0; x < len(runes); x++ {
    23  		r = runes[x]
    24  
    25  		if unicode.IsLetter(r) {
    26  			if !haveSeenLetter {
    27  				output.WriteRune(unicode.ToUpper(r))
    28  				haveSeenLetter = true
    29  			} else {
    30  				output.WriteRune(unicode.ToLower(r))
    31  			}
    32  		} else {
    33  			output.WriteRune(r)
    34  			haveSeenLetter = false
    35  		}
    36  	}
    37  	return output.String()
    38  }