Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
calini committed Jan 1, 2021
0 parents commit 4e85ed2
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## OS stuff
.DS_Store

## IDE stuff
.idea

## Output
out/
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/calini/spongebob

go 1.13
26 changes: 26 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()))
}
}
37 changes: 37 additions & 0 deletions spongebob/text.go
Original file line number Diff line number Diff line change
@@ -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()
}
16 changes: 16 additions & 0 deletions spongebob/text_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 4e85ed2

Please sign in to comment.