generated from VLADIMIR/template
add modules
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package formatter
|
||||
|
||||
type IFormatter interface {
|
||||
FormatText(text string) string
|
||||
FormatString(text string) string
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type service struct{}
|
||||
|
||||
func NewFormatter() IFormatter {
|
||||
return &service{}
|
||||
}
|
||||
|
||||
func (s *service) FormatText(text string) string {
|
||||
scanner := bufio.NewScanner(strings.NewReader(text))
|
||||
|
||||
scanner.Split(bufio.ScanLines)
|
||||
|
||||
lines := []string{}
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
var res strings.Builder
|
||||
for i, line := range lines {
|
||||
l := strings.TrimSpace(line)
|
||||
if strings.HasPrefix(l, "--") {
|
||||
l = strings.Replace(l, "--", "—", 1)
|
||||
}
|
||||
if i == 0 && strings.HasPrefix(l, "—") {
|
||||
res.WriteString(" ")
|
||||
}
|
||||
if i > 0 {
|
||||
res.WriteString("\n")
|
||||
if len(l) > 0 {
|
||||
res.WriteString(" ")
|
||||
}
|
||||
}
|
||||
res.WriteString(l)
|
||||
}
|
||||
return res.String()
|
||||
}
|
||||
|
||||
func (s *service) FormatString(text string) string {
|
||||
l := strings.TrimSpace(text)
|
||||
if strings.HasPrefix(l, "--") {
|
||||
l = strings.Replace(l, "--", "—", 1)
|
||||
}
|
||||
return l
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package formatter
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_service_FormatText(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
text string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Простой текст",
|
||||
text: "Привет",
|
||||
want: "Привет",
|
||||
},
|
||||
{
|
||||
name: "Текст с двумя абзацами",
|
||||
text: "Привет\nМир",
|
||||
want: "Привет\n Мир",
|
||||
},
|
||||
{
|
||||
name: "Прямая речь",
|
||||
text: "— Привет",
|
||||
want: " — Привет",
|
||||
},
|
||||
{
|
||||
name: "Прямая речь через 2 минуса",
|
||||
text: "-- Привет",
|
||||
want: " — Привет",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var s service
|
||||
got := s.FormatText(tt.text)
|
||||
if got != tt.want {
|
||||
t.Errorf("FormatText() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user