diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0390c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +## OS stuff +.DS_Store + +## IDE stuff +.idea + +## Output +out/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1f2666e --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# SPoNgEBoB +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fcalini%2Fspongebob.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fcalini%2Fspongebob?ref=badge_shield) + +Simple project for converting normal text to sPonGeBOb tExT + +It uses _CUTTING EDGE™_ technology like *MARKOV CHAINS* to generate REALISTICⓇ SPonGeBoB text.️ + +## Getting it +```sh +go get -u github.com/calini/spongebob +``` + +## Using the CLI + +```sh +spongebob "hello world" +> hELlO wOrlD +``` + +## Building it manually +```sh +go build -o ./spongebob + +./spongebob "hello world" +> hELlO wOrlD +``` + +## Using it as a library +```go +package main + +import ( + "fmt" + + "github.com/calini/spongebob" +) + +func main() { + fmt.Println(spongebob.Text("Hello world!")) +} +``` + + +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fcalini%2Fspongebob.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fcalini%2Fspongebob?ref=badge_large) \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0d83749 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/calini/spongebob + +go 1.13 diff --git a/main.go b/main.go new file mode 100644 index 0000000..db14dad --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "bufio" + "fmt" + "github.com/calini/spongebob/spongebob" + "os" + "strings" +) + +func main() { + // if string passed as param, return them + if len(os.Args) >= 2 { + for i := range os.Args { + os.Args[i] = spongebob.Text(os.Args[i]) + } + print(strings.Join(os.Args[1:], " ")) + os.Exit(0) + } + + // else, get stdin + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + fmt.Println(spongebob.Text(scanner.Text())) + } +} diff --git a/spongebob/text.go b/spongebob/text.go new file mode 100644 index 0000000..d944ed7 --- /dev/null +++ b/spongebob/text.go @@ -0,0 +1,37 @@ +package spongebob + +import ( + "bytes" + "math/rand" + "time" + "unicode" +) + +func init() { + rand.Seed(time.Now().UTC().UnixNano()) +} + +// Text transforms a normal string into A spOnGEbOb sTrINg +func Text(str string) string { + var result bytes.Buffer + upper := false + + for _, c := range str { + if unicode.IsLetter(rune(c)) { + // instead of flipping a coin every time, + // we create a poor man's Markov chain + if rand.Float32() < 0.6 { + upper = !upper + } + + if upper { + c = unicode.ToUpper(rune(c)) + } else { + c = unicode.ToLower(rune(c)) + } + } + result.WriteRune(c) + } + + return result.String() +} diff --git a/spongebob/text_test.go b/spongebob/text_test.go new file mode 100644 index 0000000..b5aee43 --- /dev/null +++ b/spongebob/text_test.go @@ -0,0 +1,16 @@ +package spongebob + +import ( + "strings" + "testing" +) + +// TestText checks that transformation is happening without ruining context +func TestText(t *testing.T) { + input := "Hello world!" + result := Text(input) + + if !strings.EqualFold(input, result) { + t.Errorf("Should have same content except capitalisation: \"%s\" - \"%s\"", input, result) + } +}