2018-07-04 01:52:40 +08:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Ian Coleman
|
2018-07-04 02:24:52 +08:00
|
|
|
* Copyright (c) 2018 Ma_124, <github.com/Ma124>
|
2018-07-04 01:52:40 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-10-09 17:13:05 +08:00
|
|
|
package strcase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-04-19 15:12:49 +08:00
|
|
|
// ToSnake converts a string to snake_case
|
2015-10-09 17:13:05 +08:00
|
|
|
func ToSnake(s string) string {
|
2018-07-04 02:24:52 +08:00
|
|
|
return ToDelimited(s, '_')
|
|
|
|
}
|
|
|
|
|
2020-08-16 20:10:31 +08:00
|
|
|
func ToSnakeWithIgnore(s string, ignore uint8) string {
|
2019-11-13 06:52:45 +08:00
|
|
|
return ToScreamingDelimited(s, '_', ignore, false)
|
2019-09-29 15:08:34 +08:00
|
|
|
}
|
2019-11-13 06:52:45 +08:00
|
|
|
|
2019-04-19 15:12:49 +08:00
|
|
|
// ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE
|
2018-07-04 02:31:25 +08:00
|
|
|
func ToScreamingSnake(s string) string {
|
2019-11-13 06:52:45 +08:00
|
|
|
return ToScreamingDelimited(s, '_', 0, true)
|
2018-07-04 02:31:25 +08:00
|
|
|
}
|
|
|
|
|
2019-04-19 15:12:49 +08:00
|
|
|
// ToKebab converts a string to kebab-case
|
2018-07-04 02:36:59 +08:00
|
|
|
func ToKebab(s string) string {
|
|
|
|
return ToDelimited(s, '-')
|
|
|
|
}
|
|
|
|
|
2019-04-19 15:12:49 +08:00
|
|
|
// ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
|
2018-07-04 02:36:59 +08:00
|
|
|
func ToScreamingKebab(s string) string {
|
2019-11-13 06:52:45 +08:00
|
|
|
return ToScreamingDelimited(s, '-', 0, true)
|
2018-07-04 02:36:59 +08:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:52:45 +08:00
|
|
|
// ToDelimited converts a string to delimited.snake.case
|
|
|
|
// (in this case `delimiter = '.'`)
|
|
|
|
func ToDelimited(s string, delimiter uint8) string {
|
|
|
|
return ToScreamingDelimited(s, delimiter, 0, false)
|
2018-07-04 02:31:25 +08:00
|
|
|
}
|
|
|
|
|
2019-11-13 06:52:45 +08:00
|
|
|
// 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 uint8, screaming bool) string {
|
2017-11-29 08:52:26 +08:00
|
|
|
s = addWordBoundariesToNumbers(s)
|
2015-10-09 17:13:05 +08:00
|
|
|
s = strings.Trim(s, " ")
|
|
|
|
n := ""
|
|
|
|
for i, v := range s {
|
2018-03-11 15:56:25 +08:00
|
|
|
// treat acronyms as words, eg for JSONData -> JSON is a whole word
|
2018-05-19 16:27:10 +08:00
|
|
|
if i+1 < len(s) {
|
|
|
|
next := s[i+1]
|
2019-11-13 06:52:45 +08:00
|
|
|
vIsCap := v >= 'A' && v <= 'Z'
|
|
|
|
vIsLow := v >= 'a' && v <= 'z'
|
|
|
|
nextIsCap := next >= 'A' && next <= 'Z'
|
|
|
|
nextIsLow := next >= 'a' && next <= 'z'
|
2020-08-16 20:10:31 +08:00
|
|
|
// add underscore if next letter case type is changed
|
2019-11-13 06:52:45 +08:00
|
|
|
if (vIsCap && nextIsLow) || (vIsLow && nextIsCap) {
|
2020-08-16 20:10:31 +08:00
|
|
|
if prevIgnore := ignore > 0 && i > 0 && s[i-1] == ignore; !prevIgnore {
|
|
|
|
if i > 0 && vIsCap && nextIsLow {
|
|
|
|
if prevDelim := len(n) > 0 && n[len(n)-1] == delimiter; !prevDelim {
|
|
|
|
n += string(delimiter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n += string(v)
|
|
|
|
if vIsLow {
|
|
|
|
n += string(delimiter)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-09-29 15:08:34 +08:00
|
|
|
}
|
2018-03-11 15:56:25 +08:00
|
|
|
}
|
2018-05-19 16:27:10 +08:00
|
|
|
|
2020-08-16 20:10:31 +08:00
|
|
|
if (v == ' ' || v == '_' || v == '-') && uint8(v) != ignore {
|
|
|
|
// replace space/underscore/hyphen with delimiter
|
|
|
|
n += string(delimiter)
|
2015-10-09 17:13:05 +08:00
|
|
|
} else {
|
2020-08-16 20:10:31 +08:00
|
|
|
n += string(v)
|
2015-10-09 17:13:05 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-04 02:31:25 +08:00
|
|
|
|
|
|
|
if screaming {
|
|
|
|
n = strings.ToUpper(n)
|
|
|
|
} else {
|
|
|
|
n = strings.ToLower(n)
|
|
|
|
}
|
2015-10-09 17:13:05 +08:00
|
|
|
return n
|
|
|
|
}
|