forked from hpe-storage/common-host-libs
/
strings.go
32 lines (27 loc) · 894 Bytes
/
strings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// (c) Copyright 2019 Hewlett Packard Enterprise Development LP
package util
import (
"regexp"
"strings"
)
var (
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
matchSnakeCase = regexp.MustCompile("(^[A-Za-z])|_([A-Za-z])")
)
// ToSnakeCase converts the given camelCase string into snake_case
func ToSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
// ToCamelCase converts the given snake_case string to camelCase
func ToCamelCase(str string) string {
camelCase := matchSnakeCase.ReplaceAllStringFunc(str, func(s string) string {
return strings.ToUpper(strings.Replace(s, "_", "", -1))
})
if camelCase == "" {
return ""
}
return strings.ToLower(camelCase[:1]) + camelCase[1:]
}