/* * The MIT License (MIT) * * Copyright (c) 2015 Ian Coleman * Copyright (c) 2018 Ma_124, <github.com/Ma124> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, Subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or Substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package bytcase import ( "bytes" ) // Converts a string to CamelCase func toCamelInitCase(s []byte, initCase bool) []byte { s = bytes.TrimSpace(s) if len(s) == 0 { return s } n := make([]byte, 0, len(s)) capNext := initCase prevIsCap := false for i, v := range s { vIsCap := v >= 'A' && v <= 'Z' vIsLow := v >= 'a' && v <= 'z' if capNext { if vIsLow { v += 'A' v -= 'a' } } else if i == 0 { if vIsCap { v += 'a' v -= 'A' } } else if prevIsCap && vIsCap { v += 'a' v -= 'A' } prevIsCap = vIsCap if vIsCap || vIsLow { n = append(n, v) capNext = false } else if vIsNum := v >= '0' && v <= '9'; vIsNum { n = append(n, v) capNext = true } else { capNext = v == '_' || v == ' ' || v == '-' || v == '.' } } return n } // ToCamel converts a string to CamelCase func ToCamel(s []byte) []byte { return toCamelInitCase(s, true) } // ToLowerCamel converts a string to lowerCamelCase func ToLowerCamel(s []byte) []byte { return toCamelInitCase(s, false) }
/* * The MIT License (MIT) * * Copyright (c) 2015 Ian Coleman * Copyright (c) 2018 Ma_124, <github.com/Ma124> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, Subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or Substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package bytcase import ( "bytes" ) // ToSnake converts a string to snake_case func ToSnake(s []byte) []byte { return ToDelimited(s, '_') } func ToSnakeWithIgnore(s []byte, ignore []byte) []byte { return ToScreamingDelimited(s, '_', ignore, false) } // ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE func ToScreamingSnake(s []byte) []byte { return ToScreamingDelimited(s, '_', nil, true) } // ToKebab converts a string to kebab-case func ToKebab(s []byte) []byte { return ToDelimited(s, '-') } // ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE func ToScreamingKebab(s []byte) []byte { return ToScreamingDelimited(s, '-', nil, true) } // ToDelimited converts a string to delimited.snake.case // (in this case `delimiter = '.'`) func ToDelimited(s []byte, delimiter byte) []byte { return ToScreamingDelimited(s, delimiter, nil, false) } // ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE // (in this case `delimiter = '.'; screaming = true`) // or delimited.snake.case // (in this case `delimiter = '.'; screaming = false`) func ToScreamingDelimited(s []byte, delimiter byte, ignore []byte, screaming bool) []byte { s = bytes.TrimSpace(s) n := make([]byte, 0, len(s)+2) for i, v := range s { vIsCap := v >= 'A' && v <= 'Z' vIsLow := v >= 'a' && v <= 'z' if vIsLow && screaming { v += 'A' v -= 'a' } else if vIsCap && !screaming { 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 := len(ignore) > 0 && i > 0 && contains(s[i-1], ignore) if !prevIgnore { if vIsCap && nextIsLow { if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap { n = append(n, delimiter) } } n = append(n, v) if vIsLow || vIsNum || nextIsNum { n = append(n, delimiter) } continue } } } if (v == ' ' || v == '_' || v == '-' || v == '.') && !contains(s[i], ignore) { // replace space/underscore/hyphen/dot with delimiter n = append(n, delimiter) } else { n = append(n, v) } } return n } func contains(s byte, ignore []byte) bool { for _, v := range ignore { if s == v { return true } } return false }
package caseconv import ( "github.com/peyton-spencer/caseconv/bytcase" "github.com/peyton-spencer/caseconv/strcase" ) type Case uint8 const ( CaseSnake Case = iota CaseSnakeScreaming CaseCamel CaseKebab CaseKebabScreaming ) func (c Case) StrCase(s string) string { switch c { case CaseSnake: return strcase.ToSnake(s) case CaseSnakeScreaming: return strcase.ToScreamingSnake(s) case CaseCamel: return strcase.ToCamel(s) case CaseKebab: return strcase.ToKebab(s) case CaseKebabScreaming: return strcase.ToScreamingKebab(s) } return s } func (c Case) BytCase(s []byte) []byte { switch c { case CaseSnake: return bytcase.ToSnake(s) case CaseSnakeScreaming: return bytcase.ToScreamingSnake(s) case CaseCamel: return bytcase.ToCamel(s) case CaseKebab: return bytcase.ToKebab(s) case CaseKebabScreaming: return bytcase.ToScreamingKebab(s) } return s }
/* * The MIT License (MIT) * * Copyright (c) 2015 Ian Coleman * Copyright (c) 2018 Ma_124, <github.com/Ma124> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, Subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or Substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package strcase import ( "strings" ) // Converts a string to CamelCase func toCamelInitCase(s string, initCase bool) string { s = strings.TrimSpace(s) if s == "" { return s } var n strings.Builder n.Grow(len(s)) capNext := initCase prevIsCap := false for i, v := range []byte(s) { vIsCap := v >= 'A' && v <= 'Z' vIsLow := v >= 'a' && v <= 'z' if capNext { if vIsLow { v += 'A' v -= 'a' } } else if i == 0 { if vIsCap { v += 'a' v -= 'A' } } else if prevIsCap && vIsCap { v += 'a' v -= 'A' } prevIsCap = vIsCap if vIsCap || vIsLow { n.WriteByte(v) capNext = false } else if vIsNum := v >= '0' && v <= '9'; vIsNum { n.WriteByte(v) capNext = true } else { capNext = v == '_' || v == ' ' || v == '-' || v == '.' } } return n.String() } // ToCamel converts a string to CamelCase func ToCamel(s string) string { return toCamelInitCase(s, true) } // ToLowerCamel converts a string to lowerCamelCase func ToLowerCamel(s string) string { return toCamelInitCase(s, false) }
/* * The MIT License (MIT) * * Copyright (c) 2015 Ian Coleman * Copyright (c) 2018 Ma_124, <github.com/Ma124> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, Subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or Substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package strcase 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 ToScreamingDelimited(s, '_', ignore, false) } // ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE func ToScreamingSnake(s string) string { return ToScreamingDelimited(s, '_', "", true) } // ToKebab converts a string to kebab-case func ToKebab(s string) string { return ToDelimited(s, '-') } // ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE func ToScreamingKebab(s string) string { return ToScreamingDelimited(s, '-', "", true) } // ToDelimited converts a string to delimited.snake.case // (in this case `delimiter = '.'`) func ToDelimited(s string, delimiter uint8) string { return ToScreamingDelimited(s, delimiter, "", false) } // ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE // (in this case `delimiter = '.'; screaming = true`) // or delimited.snake.case // (in this case `delimiter = '.'; screaming = false`) func ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bool) string { s = strings.TrimSpace(s) var 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 && screaming { v += 'A' v -= 'a' } else if vIsCap && !screaming { 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() }