-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func toCamelCase(input string) string { | ||
words := splitWords(input) | ||
for i := range words { | ||
if i > 0 { | ||
words[i] = strings.Title(words[i]) | ||
} else { | ||
words[i] = strings.ToLower(words[i]) | ||
} | ||
} | ||
return strings.Join(words, "") | ||
} | ||
|
||
func toPascalCase(input string) string { | ||
words := splitWords(input) | ||
for i := range words { | ||
words[i] = strings.Title(words[i]) | ||
} | ||
return strings.Join(words, "") | ||
} | ||
|
||
func toRandomCase(input string) string { | ||
var result strings.Builder | ||
for _, r := range input { | ||
if rand.Intn(100) < 60 { // 50% doesn't feel random | ||
result.WriteRune(unicode.ToUpper(r)) | ||
} else { | ||
result.WriteRune(unicode.ToLower(r)) | ||
} | ||
} | ||
return result.String() | ||
} | ||
|
||
func toSnakeCase(input string) string { | ||
words := splitWords(input) | ||
for i := range words { | ||
words[i] = strings.ToLower(words[i]) | ||
} | ||
return strings.Join(words, "_") | ||
} | ||
|
||
func splitWords(input string) []string { | ||
return strings.FieldsFunc(input, func(r rune) bool { | ||
return r == ' ' || r == '-' || r == '_' || r == '.' | ||
}) | ||
} | ||
|
||
func main() { | ||
camel := flag.Bool("camel", false, "Convert to CamelCase") | ||
pascal := flag.Bool("pascal", false, "Convert to PascalCase") | ||
random := flag.Bool("random", false, "Convert to rAnDoM") | ||
snake := flag.Bool("snake", false, "Convert to snake_case") | ||
flag.Parse() | ||
|
||
if len(flag.Args()) == 0 { | ||
fmt.Println("Please provide a string to convert.") | ||
os.Exit(1) | ||
} | ||
|
||
input := strings.Join(flag.Args(), " ") | ||
|
||
if *camel { | ||
fmt.Println(toCamelCase(input)) | ||
} else if *pascal { | ||
fmt.Println(toPascalCase(input)) | ||
} else if *random { | ||
fmt.Println(toRandomCase(input)) | ||
} else if *snake { | ||
fmt.Println(toSnakeCase(input)) | ||
} else { | ||
fmt.Println("Please specify a conversion type: --camel, --pascal, --snake, or --random") | ||
} | ||
} |