// ------------------------------------------------------------------------ // Project active2 // Active Thing (activething.com) git.activething.com/go // // File name snake.go // Created by DEV // Modified 31/03/2024 // // Copyright 2024 activething.com // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ // https://github.com/iancoleman/strcase/blob/v0.3.0/snake.go package strs import "strings" // ToSnake converts a string to snake_case func ToSnake(s string) string { return ToDelimited(s, '_') } func ToSnakeWithIgnore(s string, ignore string) string { return ToUDelimited(s, '_', ignore, false) } // ToUSnake converts a string to SCREAMING_SNAKE_CASE func ToUSnake(s string) string { return ToUDelimited(s, '_', "", true) } // ToKebab converts a string to kebab-case func ToKebab(s string) string { return ToDelimited(s, '-') } // ToUKebab converts a string to SCREAMING-KEBAB-CASE func ToUKebab(s string) string { return ToUDelimited(s, '-', "", true) } // ToDelimited converts a string to delimited.snake.case // (in this case `delimiter = '.'`) func ToDelimited(s string, delimiter uint8) string { return ToUDelimited(s, delimiter, "", false) } // ToUDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE // (in this case `delimiter = '.'; upper = true`) // or delimited.snake.case // (in this case `delimiter = '.'; screaming = false`) func ToUDelimited(s string, delimiter uint8, ignore string, upper bool) string { s = strings.TrimSpace(s) n := strings.Builder{} n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters for i, v := range []byte(s) { vIsCap := v >= 'A' && v <= 'Z' vIsLow := v >= 'a' && v <= 'z' if vIsLow && upper { v += 'A' v -= 'a' } else if vIsCap && !upper { v += 'a' v -= 'A' } // treat acronyms as words, eg for JSONData -> JSON is a whole word if i+1 < len(s) { next := s[i+1] vIsNum := v >= '0' && v <= '9' nextIsCap := next >= 'A' && next <= 'Z' nextIsLow := next >= 'a' && next <= 'z' nextIsNum := next >= '0' && next <= '9' // add underscore if next letter case type is changed if (vIsCap && (nextIsLow || nextIsNum)) || (vIsLow && (nextIsCap || nextIsNum)) || (vIsNum && (nextIsCap || nextIsLow)) { prevIgnore := ignore != "" && i > 0 && strings.ContainsAny(string(s[i-1]), ignore) if !prevIgnore { if vIsCap && nextIsLow { if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap { n.WriteByte(delimiter) } } n.WriteByte(v) if vIsLow || vIsNum || nextIsNum { n.WriteByte(delimiter) } continue } } } if (v == ' ' || v == '_' || v == '-' || v == '.') && !strings.ContainsAny(string(v), ignore) { // replace space/underscore/hyphen/dot with delimiter n.WriteByte(delimiter) } else { n.WriteByte(v) } } return n.String() }