Added regex for user, mention, and tag parsing.

This commit is contained in:
Nick Gerakines 2020-02-29 18:17:58 -05:00
parent 9ad6d6c85d
commit c0ee5ab473
2 changed files with 56 additions and 0 deletions

5
storage/content.go Normal file
View File

@ -0,0 +1,5 @@
package storage
var usernameRegex = `(\w+([\w\.-]+\w+)?)`
var mentionRegex = `(@\w+([\w_\.-]+\w+)?@[\w\.-]*[\w])`
var tagRegex = `(?:^|\B)([#]{1}(\w+))`

51
storage/content_test.go Normal file
View File

@ -0,0 +1,51 @@
package storage
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestActorRegex(t *testing.T) {
ur := regexp.MustCompile(usernameRegex)
mr := regexp.MustCompile(mentionRegex)
tr := regexp.MustCompile(tagRegex)
usernames := map[string]string{
"nick": "nick",
"nick12345": "nick12345",
"!nick": "nick",
"nick!": "nick",
"nick123nick": "nick123nick",
}
mentions := map[string]string{
"@nick@tavern.town": "@nick@tavern.town",
"@nick1@tavern.town": "@nick1@tavern.town",
"@nick!@tavern.town": "",
"@nick!tavern.town": "",
"@nick.tavern.town": "",
"pre\n@nick@tavern.town": "@nick@tavern.town",
"\n@nick@tavern.town": "@nick@tavern.town",
"@nick@tavern.town, post": "@nick@tavern.town",
}
tags := map[string][]string{
"#what": {"#what"},
"#what #what": {"#what", "#what"},
"hello #world": {"#world"},
"hello#world": nil,
}
for i, e := range usernames {
assert.Equalf(t, e, ur.FindString(i), "username '%s' matches '%s'", i, e)
}
for i, e := range mentions {
assert.Equalf(t, e, mr.FindString(i), "mention '%s' matches '%s'", i, e)
}
for i, e := range tags {
assert.Equalf(t, e, tr.FindAllString(i, -1), "tag '%s' matches '%s'", i, e)
}
}