Browse Source

Let's not complicate it

Piotr Czajkowski 3 years ago
parent
commit
6718dafa6e
2 changed files with 8 additions and 6 deletions
  1. 6 4
      app.go
  2. 2 2
      app_test.go

+ 6 - 4
app.go

@@ -27,12 +27,13 @@ func encodeWord(word []rune, wordLength int) {
 }
 
 //EncodeText returns encoded provided text and sorted array of encoded words
-func EncodeText(text []rune) (string, []string) {
+func EncodeText(text string) (string, []string) {
+	runes := []rune(text)
 	var currentWord []rune
 	var newString []rune
 	var encodedWords []string
 
-	for _, item := range text {
+	for _, item := range runes {
 		if unicode.IsPunct(item) || unicode.IsSpace(item) {
 			currentWordLength := len(currentWord)
 			if currentWordLength >= 4 {
@@ -104,11 +105,12 @@ func decodeWord(word []rune, wordLength int, encodedWords []string) (string, []s
 }
 
 //DecodeText returns decoded provided text using provided array of encoded words
-func DecodeText(text []rune, encodedWords []string) string {
+func DecodeText(text string, encodedWords []string) string {
+	runes := []rune(text)
 	var currentWord []rune
 	var newString []rune
 
-	for _, item := range text {
+	for _, item := range runes {
 		if unicode.IsPunct(item) || unicode.IsSpace(item) {
 			currentWordLength := len(currentWord)
 			if currentWordLength >= 4 {

+ 2 - 2
app_test.go

@@ -16,7 +16,7 @@ func TestDecodeEncode(t *testing.T) {
 	}
 
 	for _, item := range testCases {
-		encodedText, encodedWords := EncodeText([]rune(item.text))
+		encodedText, encodedWords := EncodeText(item.text)
 		if encodedText == item.text {
 			t.Errorf("Encoded text '%s' should be different than provided text '%s'!", encodedText, item.text)
 		}
@@ -26,7 +26,7 @@ func TestDecodeEncode(t *testing.T) {
 			t.Errorf("There should be %d encoded words instead of %d! %v", item.count, count, encodedWords)
 		}
 
-		decodedText := DecodeText([]rune(encodedText), encodedWords)
+		decodedText := DecodeText(encodedText, encodedWords)
 		if decodedText != item.text {
 			t.Errorf("Decoded text '%s' should be same as provided text '%s'!", decodedText, item.text)
 		}