grokify/mogo

View on GitHub
text/emoji/emoji.go

Summary

Maintainability
A
0 mins
Test Coverage
package emoji

/*
https://emojipedia.org/github/
http://emojicodes.com/
https://emojipedia.org/emoji/%F0%9F%86%98/
https://unicode.org/emoji/charts/full-emoji-list.html
*/

import (
    "regexp"
    "strings"
)

const gomojiRaw string = `
:angry:    ๐Ÿ˜     :@
:anguished:    ๐Ÿ˜ง
:astonished:    ๐Ÿ˜ฒ
:confused:    ๐Ÿ˜•    >:\
:cry:    ๐Ÿ˜ข    :'(
:disappointed:    ๐Ÿ˜ž    :(
:dizzy_face:    ๐Ÿ˜ต    #)
:expressionless:    ๐Ÿ˜‘    -_-
:fearful:    ๐Ÿ˜จ    D:
:flushed:    ๐Ÿ˜ณ    :$
:frowning:    ๐Ÿ™    :(
:innocent:    ๐Ÿ˜‡    O:)
:joy:    ๐Ÿ˜‚    :')
:kissing_heart:    :^*
:laughing:    ๐Ÿ˜†    >:)
:no_mouth:    :X
:ok_woman:    *\0/*
:open_mouth:    ๐Ÿ˜ฎ    >:O
:persevere:    ๐Ÿ˜ฃ    >.<
:relaxed:    ๐Ÿ™‚
:relieved:    ๐Ÿ˜Œ
:scream:    ๐Ÿ˜ฑ
:slight_smile:    ๐Ÿ™‚    :)
:smile:    ๐Ÿ˜€    :)
:smiley:    ๐Ÿ˜ƒ    :D
:snowflake:    โ„
:snowman:    โ˜ƒ
:stuck_out_tongue:    ๐Ÿ˜›    :P
:stuck_out_tongue_winking_eye:    ๐Ÿ˜œ    >:P
:sunglasses:    ๐Ÿ˜Ž    B)
:sweat:    ๐Ÿ˜ฐ    ':(
:sweat_smile:    ๐Ÿ˜…    ':)
:victory_hand:    โœŒ
:wink:    ๐Ÿ˜‰    ;)

:+1:    ๐Ÿ‘    +1
:-1:    ๐Ÿ‘Ž    -1
:love_you_gesture:    ๐ŸคŸ
:ok_hand:    ๐Ÿ‘Œ

:alien:    ๐Ÿ‘ฝ
:ghost:    ๐Ÿ‘ป
:goblin:    ๐Ÿ‘บ
:ogre:    ๐Ÿ‘น
:robot:    ๐Ÿค–
:skull:    ๐Ÿ’€

:beer:    ๐Ÿบ
:beers:    ๐Ÿป
:boom:    ๐Ÿ’ฅ
:chopsticks:    ๐Ÿฅข
:droplet:    ๐Ÿ’ง
:exclamation:    โ—    !
:fire:    ๐Ÿ”ฅ
:minus_sign:    โž–
:no_entry:    โ›”
:sos:    ๐Ÿ†˜    SOS
:spoon:    ๐Ÿฅ„
:sun:,:sunny:    โ˜€๏ธ
:umbrella:    โ˜‚๏ธ
:white_check_mark:    โœ…

:black_heart:    ๐Ÿ–ค
:blue_heart;    ๐Ÿ’™
:broken_heart:    ๐Ÿ’”    </3
:green_heart:    ๐Ÿ’š
:heart:    ๐Ÿงก    <3
:heart_declaration:    ๐Ÿ’Ÿ
:heart_exclamation:    โฃ
:orange_heart:    ๐Ÿงก
:purple_heart:    ๐Ÿ’œ
:red_heart:    โค
:revolving_heart:    ๐Ÿ’ž
:two_hearts:    ๐Ÿ’•
:yellow_heart:    ๐Ÿ’›

:kiss_mark:    ๐Ÿ’‹
:love_letter:    ๐Ÿ’Œ

:crying_cat:    ๐Ÿ˜ฟ
:grinning_cat:    ๐Ÿ˜บ
:pouting_cat:    ๐Ÿ˜พ
:weary_cat:    ๐Ÿ™€

:black_flag:    ๐Ÿด
:checkered_flag:    ๐Ÿ
:crossed_flags:    ๐ŸŽŒ
:rainbow_flag:    ๐Ÿณ๏ธโ€๐ŸŒˆ
:triangular_flag:    ๐Ÿšฉ
:white_flag:    ๐Ÿณ
`

/*
:relaxed: Y
:smiley:    Y
:relieved: Y
:green_heart:    Y
:+1: Y
:ok_hand:    Y
:sunny:    Y
:beers:    Y
:white_check_mark:    Y

:frowning: Y
:anguished: Y
:open_mouth:    Y
:confused:    Y
:scream:    Y
:broken_heart:    Y
:boom: Y
:exclamation: Y
:fire:    Y
:-1: Y
:umbrella:    Y
:sos:    Y

> DOWN_EMOJI = %w(:frowning: :anguished: :open_mouth: :confused: :scream: :broken_heart: :boom: :exclamation: :fire: :-1: :umbrella: :sos:)
> UP_EMOJI = %w(:relaxed: :smiley: :relieved: :green_heart: :+1: :ok_hand: :sunny: :beers: :white_check_mark:)
*/

var rxEmojiShortcode *regexp.Regexp = regexp.MustCompile(`:[\+\-]?[0-9a-z_]+:`)

func GetEmojiDataShortcodeMap() map[string]Emoji {
    data := map[string]Emoji{}
    rx := regexp.MustCompile(`\s+`)
    lines := strings.Split(gomojiRaw, "\n")
    for _, line := range lines {
        line = strings.TrimSpace(line)
        if strings.Index(line, ":") != 0 {
            continue
        }
        line = rx.ReplaceAllString(line, " ")
        parts := strings.Split(line, " ")
        if len(parts) == 2 || len(parts) == 3 {
            emo := Emoji{
                Shortcode:   strings.TrimSpace(parts[0]),
                Unicode:     strings.TrimSpace(parts[1]),
                ShortcodeRx: regexp.MustCompile(regexp.QuoteMeta(parts[0]))}
            if len(parts) == 3 {
                emo.ASCII = strings.TrimSpace(parts[2])
            }
            data[parts[0]] = emo
        } else if len(parts) > 3 {
            panic("E_BAD_FORMATTING")
        }
    }
    return data
}

type Emoji struct {
    ASCII       string
    Shortcode   string
    Unicode     string
    ShortcodeRx *regexp.Regexp
}

type EmojiType int

const (
    Shortcode EmojiType = iota
    ASCII
    Unicode
)

type Converter struct {
    data map[string]Emoji
}

func NewConverter() Converter { return Converter{data: GetEmojiDataShortcodeMap()} }

func (conv *Converter) ConvertShortcodesString(input string, emoType EmojiType) string {
    if emoType == Shortcode {
        return input
    }
    matches := rxEmojiShortcode.FindAllString(input, -1)
    output := input
    for _, emoShortcode := range matches {
        if einfo, ok := conv.data[emoShortcode]; ok {
            if emoType == Unicode {
                if len(einfo.Unicode) > 0 {
                    output = einfo.ShortcodeRx.ReplaceAllString(output, einfo.Unicode)
                } else {
                    output = einfo.ShortcodeRx.ReplaceAllString(output, einfo.ASCII)
                }
            } else {
                if len(einfo.ASCII) > 0 {
                    output = einfo.ShortcodeRx.ReplaceAllString(output, einfo.ASCII)
                } else {
                    output = einfo.ShortcodeRx.ReplaceAllString(output, einfo.Unicode)
                }
            }
        }
    }
    return output
}