Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mentionutil: Add a mentions extractor #66

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions mentionnutil/extractor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mentionnutil

import (
"regexp"
"strings"

"github.com/upfluence/pkg/slices"
"github.com/upfluence/pkg/stringutil"
)

var mentionRegex = regexp.MustCompile(`\B@[\w\.]+[\w]+`)

func ExtractMentions(s string) []string {
mentions := slices.Map(
mentionRegex.FindAllString(s, -1),
func(v string) string {
return strings.TrimPrefix(v, "@")
},
)

if len(mentions) == 0 {
return nil
}

var set stringutil.Set

set.Add(mentions...)

return set.Strings()
}
37 changes: 37 additions & 0 deletions mentionnutil/extractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mentionnutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestExtractMentions(t *testing.T) {
for _, tt := range []struct {
name string
input string
expect []string
}{
{
name: "empty",
},
{
name: "no mentions",
input: "foo bar",
},
{
name: "multi mentions",
input: "foo bar @buz. @taz_ @dot.name hello @buz @with_1",
expect: []string{"buz", "taz_", "dot.name", "with_1"},
},
{
name: "youtube url",
input: "foo bar @buz. https://www.youtube.com/@SalonViking test",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bad thing of this it's this catch also handle from tiktok or instagram urls but we didn't know at the end at which social media it's linked. This could lead to some confusion. Maybe must be more restrictive ?

expect: []string{"buz", "SalonViking"},
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.ElementsMatch(t, tt.expect, ExtractMentions(tt.input))
})
}
}
Loading