Beispiel #1
0
func TestFieldFinder(t *testing.T) {
	is := is.New(t)

	s := []byte("A field finder will be able to find fields.")
	var fn anno.Finder
	fn = anno.FieldFunc("thiskind", func(f []byte) (bool, []byte) {
		if string(f) == "field" {
			return true, f
		}
		if string(f) == "able" {
			return true, f
		}
		if string(f) == "find" {
			return true, f
		}
		return false, f
	})
	notes, err := fn.Find(s)
	is.NoErr(err)

	is.Equal(len(notes), 3)
	is.Equal(notes[0].Val, []byte("field"))
	is.Equal(notes[0].Start, 2)
	is.Equal(notes[0].End(), 2+len(notes[0].Val))
	is.Equal(notes[0].Kind, "thiskind")

	is.Equal(notes[1].Val, []byte("able"))
	is.Equal(notes[1].Start, 23)
	is.Equal(notes[1].End(), 23+len(notes[1].Val))
	is.Equal(notes[1].Kind, "thiskind")

	is.Equal(notes[2].Val, []byte("find"))
	is.Equal(notes[2].Start, 8)
	is.Equal(notes[2].End(), 8+len(notes[2].Val))
	is.Equal(notes[2].Kind, "thiskind")

	// sort the notes
	sort.Sort(notes)

	is.Equal(len(notes), 3)
	is.Equal(notes[0].Val, []byte("field"))
	is.Equal(notes[0].Start, 2)
	is.Equal(notes[0].End(), 2+len(notes[0].Val))
	is.Equal(notes[0].Kind, "thiskind")

	is.Equal(notes[1].Val, []byte("find"))
	is.Equal(notes[1].Start, 8)
	is.Equal(notes[1].End(), 8+len(notes[2].Val))
	is.Equal(notes[1].Kind, "thiskind")

	is.Equal(notes[2].Val, []byte("able"))
	is.Equal(notes[2].Start, 23)
	is.Equal(notes[2].End(), 23+len(notes[1].Val))
	is.Equal(notes[2].Kind, "thiskind")

}
Beispiel #2
0
package emoji

import (
	"bytes"

	"github.com/matryer/anno"
	"gopkg.in/kyokomi/emoji.v1"
)

// Find finds raw :emoji: fields.
var Find = anno.FieldFunc("emoji", func(s []byte) (bool, []byte) {
	trimmedS := anno.TrimPunctuation(s)
	return bytes.HasPrefix(trimmedS, []byte(":")) && bytes.HasSuffix(trimmedS, []byte(":")), trimmedS
})

// Expand takes a single emoji source and returns the actual
// characters.
func Expand(b string) string {
	return emoji.CodeMap()[b]
}