Skip to content

Commit

Permalink
fix(text/glitch): parse options correctly
Browse files Browse the repository at this point in the history
Also the `WithRandomization()` method was using the wront type (`int`
instead of `float32`)
  • Loading branch information
andresperezl committed Feb 13, 2023
1 parent b1e38e8 commit 599fe78
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions text/glitch/glitch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Pacakge glitch provides methods to glitch (zalgo) text.
// This implementation is based on https://github.com/combatwombat/Lunicode.js
package glitch

import (
Expand Down Expand Up @@ -61,7 +63,7 @@ func WithMaxHeight(v int) Option {
}

// WithRandomization 0-100%. maxHeight 100 and randomization 20%: the height goes from 80 to 100. randomization 70%, height goes from 30 to 100.
func WithRandomization(v int) Option {
func WithRandomization(v float32) Option {
if v < 0 || v > 100 {
panic("randomization needs to be between 0 and 100")
}
Expand Down Expand Up @@ -163,7 +165,7 @@ func Encode(t []byte, opts ...Option) []byte {
// middle=true, bottom=true, maxHeight=15, randomization=100, unless modified by
// opts
func EncodeString(t string, opts ...Option) string {
encOpts := parseOptions()
encOpts := parseOptions(opts...)

newText := &strings.Builder{}
for _, newChar := range t {
Expand Down
17 changes: 17 additions & 0 deletions text/glitch/glitch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package glitch

import (
"strings"
"testing"
)

func TestEncode(t *testing.T) {
sb := &strings.Builder{}
for i := 0; i < 2000; i++ {
sb.WriteString("a")
zalgo := EncodeString(sb.String(), WithMaxHeight(1), WithRandomization(0))
if sb.Len()*7 != len(zalgo) {
t.Fatalf("expected string to be %d length, but got %d", sb.Len()*7, len(zalgo))
}
}
}

0 comments on commit 599fe78

Please sign in to comment.