generated from VLADIMIR/template
add modules
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package cleaner
|
||||
|
||||
type ICleaner interface {
|
||||
ClearCode(code string) string
|
||||
ClearText(text string) string
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package cleaner
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
replaceMap = map[string]string{
|
||||
"a": "а",
|
||||
"e": "е",
|
||||
"o": "о",
|
||||
"c": "с",
|
||||
"p": "р",
|
||||
"x": "х",
|
||||
"y": "у",
|
||||
"k": "к",
|
||||
"m": "м",
|
||||
"t": "т",
|
||||
"h": "н",
|
||||
"b": "в",
|
||||
"u": "и",
|
||||
}
|
||||
re = regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||||
)
|
||||
|
||||
type service struct{}
|
||||
|
||||
func NewCleaner() ICleaner {
|
||||
return &service{}
|
||||
}
|
||||
|
||||
func (s *service) ClearCode(code string) string {
|
||||
code = strings.TrimPrefix(code, "(")
|
||||
code = strings.TrimPrefix(code, "[")
|
||||
code = strings.TrimSuffix(code, ")")
|
||||
code = strings.TrimSuffix(code, "]")
|
||||
code = strings.ToLower(code)
|
||||
code = strings.TrimSpace(code)
|
||||
code = strings.ReplaceAll(code, "-", "")
|
||||
for latin, cyrillic := range replaceMap {
|
||||
code = strings.ReplaceAll(code, latin, cyrillic)
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
func (s *service) ClearText(text string) string {
|
||||
return strings.TrimSpace(re.ReplaceAllString(text, ""))
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package cleaner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_service_ClearCode(t *testing.T) {
|
||||
tests := []struct {
|
||||
code string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
code: "ы",
|
||||
want: "ы",
|
||||
},
|
||||
{
|
||||
code: "Ы",
|
||||
want: "ы",
|
||||
},
|
||||
{
|
||||
code: "Ы-1",
|
||||
want: "ы1",
|
||||
},
|
||||
{
|
||||
code: "[Ы]",
|
||||
want: "ы",
|
||||
},
|
||||
{
|
||||
code: "([Ы])",
|
||||
want: "ы",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(fmt.Sprintf("%s->%s", tt.code, tt.want), func(t *testing.T) {
|
||||
var s service
|
||||
got := s.ClearCode(tt.code)
|
||||
if got != tt.want {
|
||||
t.Errorf("ClearCode() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_service_ClearText(t *testing.T) {
|
||||
tests := []struct {
|
||||
text string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
text: "text",
|
||||
want: "text",
|
||||
},
|
||||
{
|
||||
text: "text ([Ы])",
|
||||
want: "text",
|
||||
},
|
||||
{
|
||||
text: "text ([Ы-3])",
|
||||
want: "text",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.text, func(t *testing.T) {
|
||||
var s service
|
||||
got := s.ClearText(tt.text)
|
||||
if got != tt.want {
|
||||
t.Errorf("ClearText() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user