diff --git a/backend/app/store/formatter.go b/backend/app/store/formatter.go index e51d7192..d1dec9c5 100644 --- a/backend/app/store/formatter.go +++ b/backend/app/store/formatter.go @@ -19,6 +19,14 @@ type CommentConverter interface { Convert(text string) string } +// CommentConverterFunc functional struct implementing CommentConverter +type CommentConverterFunc func(text string) string + +// Convert calls func for given text +func (f CommentConverterFunc) Convert(text string) string { + return f(text) +} + // NewCommentFormatter makes CommentFormatter func NewCommentFormatter(converters ...CommentConverter) *CommentFormatter { return &CommentFormatter{converters: converters} diff --git a/backend/app/store/formatter_test.go b/backend/app/store/formatter_test.go index 08685dc1..11f8b609 100644 --- a/backend/app/store/formatter_test.go +++ b/backend/app/store/formatter_test.go @@ -33,6 +33,12 @@ func TestFormatter_FormatTextNoConvertor(t *testing.T) { assert.Equal(t, "

12345

\n", f.FormatText("12345")) } +func TestFormatter_FormatTextConvertorFunc(t *testing.T) { + fn := CommentConverterFunc(func(text string) string { return "zz!" + text }) + f := NewCommentFormatter(fn) + assert.Equal(t, "zz!

12345

\n", f.FormatText("12345")) +} + func TestFormatter_FormatComment(t *testing.T) { comment := Comment{ Text: "blah\n\nxyz",