diff --git a/channels/emoji.go b/channels/emoji.go index c640264563cec55f6d42d2ab224facbd3a035270..8d0a20a44c5c1eaa9422168fc7013302ce50d3f9 100644 --- a/channels/emoji.go +++ b/channels/emoji.go @@ -2,35 +2,43 @@ package channels import ( "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" "regexp" ) -const findEmoji = "(\\\\u00a9|\\\\u00ae|[\\\\u2000-\\\\u3300]|\\\\ud83c[\\\\ud000-\\\\udfff]|\\\\ud83d[\\\\ud000-\\\\udfff]|\\\\ud83e[\\\\ud000-\\\\udfff])" +// found at https://www.regextester.com/106421 +const findEmoji = `(\\u00a9|\\u00ae|[\\u2000-\\u3300]|\\ud83c[\\ud000-` + + `\\udfff]|\\ud83d[\\ud000-\\udfff]|\\ud83e[\\ud000-\\udfff])` var InvalidReaction = errors.New( "The reaction is not valid, it must be a single emoji") +var compiledRegex *regexp.Regexp + +// compile the regular expression in an init so it is only +// compiled once +func init() { + var err error + compiledRegex, err = regexp.Compile(findEmoji) + if err != nil { + jww.FATAL.Panicf("failed to compile the regex for emoji finding " + + "within channels") + } +} + // ValidateReaction checks that the reaction only contains a single emoji. func ValidateReaction(reaction string) error { + + //make sure it is only only character reactRunes := []rune(reaction) if len(reactRunes) > 1 { return InvalidReaction } - reg, err := regexp.Compile(findEmoji) - if err != nil { - return err - } - - if !reg.Match([]byte(reaction)) { + // make sure it is only one emoji + if !compiledRegex.Match([]byte(reaction)) { return InvalidReaction } - /* - fmt.Println(string(reactRunes[0])) - fmt.Println(reactRunes[1]) - fmt.Println(reactRunes[2]) - - jww.WARN.Printf("Reaction Validation Not Yet Implemented")*/ return nil } diff --git a/channels/emoji_test.go b/channels/emoji_test.go index 672d089b81863d18511cdfb801a105e5e0cd6d50..2486a1c960d66f8b162b3073d26cc184a5b3cb87 100644 --- a/channels/emoji_test.go +++ b/channels/emoji_test.go @@ -1,6 +1,11 @@ package channels -/* +import ( + "fmt" + "regexp" + "testing" +) + func TestValidateReaction(t *testing.T) { r := "🍆" @@ -18,4 +23,4 @@ func TestValidateReaction(t *testing.T) { if err != nil { t.Errorf("Got error: %+v", err) } -}*/ +}