...

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

Documentation: github.com/gobuffalo/flect

     1  package flect
     2  
     3  import (
     4  	"strings"
     5  	"unicode"
     6  )
     7  
     8  // Underscore a string
     9  //	bob dylan --> bob_dylan
    10  //	Nice to see you! --> nice_to_see_you
    11  //	widgetID --> widget_id
    12  func Underscore(s string) string {
    13  	return New(s).Underscore().String()
    14  }
    15  
    16  // Underscore a string
    17  //	bob dylan --> bob_dylan
    18  //	Nice to see you! --> nice_to_see_you
    19  //	widgetID --> widget_id
    20  func (i Ident) Underscore() Ident {
    21  	out := make([]string, 0, len(i.Parts))
    22  	for _, part := range i.Parts {
    23  		var x strings.Builder
    24  		x.Grow(len(part))
    25  		for _, c := range part {
    26  			if unicode.IsLetter(c) || unicode.IsDigit(c) {
    27  				x.WriteRune(c)
    28  			}
    29  		}
    30  		if x.Len() > 0 {
    31  			out = append(out, x.String())
    32  		}
    33  	}
    34  	return New(strings.ToLower(strings.Join(out, "_")))
    35  }
    36  

View as plain text