2016-12-13 17:56:45 +00:00
|
|
|
package emoji
|
|
|
|
|
2017-11-17 16:26:04 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-16 18:32:46 +00:00
|
|
|
"strconv"
|
2017-11-17 16:26:04 +00:00
|
|
|
"strings"
|
|
|
|
)
|
2017-02-07 19:09:07 +00:00
|
|
|
|
2016-12-13 17:56:45 +00:00
|
|
|
// Map returns the emoji at the provided position.
|
|
|
|
// This list is from 0-255
|
2017-02-07 15:17:50 +00:00
|
|
|
func Map(b byte) Words {
|
2016-12-13 19:02:39 +00:00
|
|
|
return mapGen.EmojiWords[int(b)]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the version of the emojisum document currently compiled
|
|
|
|
// against
|
|
|
|
func Version() string {
|
|
|
|
return mapGen.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
var mapGen VersionedMap
|
|
|
|
|
|
|
|
// VersionedMap is the structure used for the `emojimap.json` document
|
|
|
|
type VersionedMap struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
// these are an ordered list, referened by a byte (each byte of a checksum digest)
|
2017-02-07 15:17:50 +00:00
|
|
|
EmojiWords []Words `json:"emojiwords"`
|
2016-12-13 17:56:45 +00:00
|
|
|
}
|
2017-02-07 15:17:50 +00:00
|
|
|
|
|
|
|
// Words are a set of options to represent an emoji.
|
2018-10-15 15:13:43 +00:00
|
|
|
// Possible options could be the ":colon_notation:", a "U+26CF" style
|
|
|
|
// codepoint, or the unicode value itself.
|
2017-02-07 15:17:50 +00:00
|
|
|
type Words []string
|
2017-02-07 19:09:07 +00:00
|
|
|
|
2017-02-07 19:30:57 +00:00
|
|
|
// IsColonNotation checks for whether a word is the :colon_notation: of emoji
|
2017-02-07 19:09:07 +00:00
|
|
|
func IsColonNotation(word string) bool {
|
|
|
|
return strings.HasPrefix(word, ":") && strings.HasSuffix(word, ":")
|
|
|
|
}
|
|
|
|
|
2018-10-15 15:13:43 +00:00
|
|
|
// IsCodepoint checks for whether a word is the "U+1234" codepoint style of emoji. Codepoints can sometimes be a combo, like flags
|
2017-02-07 19:09:07 +00:00
|
|
|
func IsCodepoint(word string) bool {
|
|
|
|
return strings.HasPrefix(strings.ToUpper(word), "U+")
|
|
|
|
}
|
2017-11-17 16:26:04 +00:00
|
|
|
|
|
|
|
var unicodeURL = `http://www.unicode.org/emoji/charts/full-emoji-list.html`
|
|
|
|
|
2018-10-16 18:32:46 +00:00
|
|
|
// UnicodeLinkURL returns a link to unicode.org list for CodePoint, or just the
|
2017-11-17 16:26:04 +00:00
|
|
|
// full list if not a codepoint
|
2018-10-16 18:32:46 +00:00
|
|
|
func UnicodeLinkURL(word string) string {
|
2017-11-17 16:26:04 +00:00
|
|
|
if !IsCodepoint(word) {
|
|
|
|
return unicodeURL
|
|
|
|
}
|
2018-10-16 18:32:46 +00:00
|
|
|
|
2018-10-16 19:37:29 +00:00
|
|
|
return fmt.Sprintf("%s#%s", unicodeURL, strings.Join(strings.Split(strings.TrimPrefix(strings.ToLower(word), "u+"), "u+"), "_"))
|
2018-10-16 18:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CodepointToUnicode takes a "U+26CF" style word and returns the `\U00026CF` formated unicode string
|
|
|
|
func CodepointToUnicode(word string) string {
|
|
|
|
if !IsCodepoint(word) {
|
|
|
|
return word
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret string
|
|
|
|
|
|
|
|
for _, chunk := range strings.Split(strings.TrimPrefix(strings.ToUpper(word), "U+"), "U+") {
|
|
|
|
c, err := strconv.ParseInt(chunk, 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
ret = fmt.Sprintf("%s%c", ret, c)
|
|
|
|
}
|
|
|
|
return ret
|
2017-11-17 16:26:04 +00:00
|
|
|
}
|