Skip to content

Commit

Permalink
Update to add emoji, fix missing emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 12, 2020
1 parent 6a5476a commit 17eb219
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 54 deletions.
102 changes: 54 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
var affixEmoticonModifier = require('nlcst-affix-emoticon-modifier')
var emoticonModifier = require('nlcst-emoticon-modifier')
var emojiModifier = require('nlcst-emoji-modifier')
var emoticons = require('emoticon')
var toString = require('nlcst-to-string')
var gemoji = require('gemoji')
var visit = require('unist-util-visit')
var emoticons = require('emoticon')
var gemoji = require('gemoji')

module.exports = emoji

var own = {}.hasOwnProperty

var type = 'EmoticonNode'

// Map of visitors.
Expand All @@ -18,10 +20,10 @@ var fns = {
decode: toGemoji
}

var unicodes = gemoji.unicode
var names = gemoji.name
var emoji2info = {}

var shortcodes = {}
var emoticon2emoji = {}
var gemoji2emoji = {}

init()

Expand Down Expand Up @@ -55,74 +57,78 @@ function emoji(options) {
}

function visitor(node) {
var data = node.data
var value = toString(node)
var emoji = parse(toString(node))
var info
var data

if (fn) {
fn(node)
}
if (!emoji) return

info = unicodes[value] || shortcodes[value] || emoticons[value]

if (!data) {
data = {}
node.data = data
if (fn) {
node.value = fn(emoji)
}

data.names = info.names.concat()
info = emoji2info[emoji]
data = node.data || (node.data = {})
data.emoji = info.emoji
data.description = info.description
data.names = info.names.concat()
data.tags = info.tags.concat()
}
}

// Replace a unicode emoji with a short-code.
function toGemoji(node) {
var value = toString(node)
var info = (unicodes[value] || emoticons[value] || {}).shortcode

if (info) {
node.value = info
}
// Map a value to an emoji.
function parse(value) {
if (own.call(emoji2info, value)) return value
if (own.call(emoticon2emoji, value)) return emoticon2emoji[value]
if (own.call(gemoji2emoji, value)) return gemoji2emoji[value]
}

// Replace a short-code with a unicode emoji.
function toEmoji(node) {
var value = toString(node)
var info = (shortcodes[value] || emoticons[value] || {}).emoji
// Change to a GitHub emoji short-code.
function toGemoji(emoji) {
return ':' + emoji2info[emoji].names[0] + ':'
}

if (info) {
node.value = info
}
// Change to an emoji.
function toEmoji(emoji) {
return emoji
}

// Construct dictionaries.
function init() {
var key
var shortcode
var result = {}
var length = emoticons.length
var length = gemoji.length
var index = -1
var count
var info
var offset
var subset
var name
var count
var values
var value

while (++index < length) {
info = gemoji[index]
values = info.names
count = values.length
offset = -1

emoji2info[info.emoji] = info

for (key in names) {
shortcode = ':' + key + ':'
shortcodes[shortcode] = names[key]
shortcodes[shortcode].shortcode = shortcode
while (++offset < count) {
value = values[offset]
gemoji2emoji[':' + value + ':'] = info.emoji
}
}

index = -1
length = emoticons.length

while (++index < length) {
name = emoticons[index].name
subset = emoticons[index].emoticons
count = subset.length
info = emoticons[index]
values = info.emoticons
count = values.length
offset = -1

while (++offset < count) {
result[subset[offset]] = names[name]
value = values[offset]
emoticon2emoji[value] = info.emoji
}
}

emoticons = result
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
],
"dependencies": {
"emoticon": "^3.1.0",
"gemoji": "^4.2.0",
"gemoji": "^5.0.0",
"nlcst-affix-emoticon-modifier": "^1.0.0",
"nlcst-emoji-modifier": "^4.0.0",
"nlcst-emoticon-modifier": "^1.0.0",
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ interface Emoticon < Symbol {
}
interface EmoticonData {
emoji: string
names: [string]
description: string?
tags: [string]
Expand Down
60 changes: 55 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ test('emoji', function(t) {
{
position: p(1, 20, 19, 1, 22, 21),
data: {
emoji: '😃',
description: 'grinning face with big eyes',
names: ['smiley'],
description: 'smiling face with open mouth',
tags: ['happy', 'joy', 'haha']
}
},
Expand Down Expand Up @@ -86,8 +87,9 @@ test('emoji', function(t) {
{
position: p(1, 20, 19, 1, 25, 24),
data: {
names: ['sob'],
emoji: '😭',
description: 'loudly crying face',
names: ['sob'],
tags: ['sad', 'cry', 'bawling']
}
},
Expand Down Expand Up @@ -119,7 +121,12 @@ test('emoji', function(t) {
'EmoticonNode',
{
position: p(1, 14, 13, 1, 16, 15),
data: {names: ['cat'], description: 'cat face', tags: ['pet']}
data: {
emoji: '🐱',
description: 'cat face',
names: ['cat'],
tags: ['pet']
}
},
'🐱'
),
Expand All @@ -135,7 +142,12 @@ test('emoji', function(t) {
'EmoticonNode',
{
position: p(1, 22, 21, 1, 24, 23),
data: {names: ['dog'], description: 'dog face', tags: ['pet']}
data: {
emoji: '🐶',
description: 'dog face',
names: ['dog'],
tags: ['pet']
}
},
'🐶'
),
Expand All @@ -149,6 +161,44 @@ test('emoji', function(t) {
'should classify emoji'
)

t.deepEqual(
processor.runSync(
u('RootNode', [
u('ParagraphNode', [
u('SentenceNode', [
u('WordNode', [u('TextNode', 'This')]),
u('WhiteSpaceNode', ' '),
u('WordNode', [u('TextNode', 'makes')]),
u('WhiteSpaceNode', ' '),
u('WordNode', [u('TextNode', 'me')]),
u('WhiteSpaceNode', ' '),
u('WordNode', [u('TextNode', 'feel')]),
u('WhiteSpaceNode', ' '),
u('EmoticonNode', '*weird*'),
u('PunctuationNode', '.')
])
])
])
),
u('RootNode', [
u('ParagraphNode', [
u('SentenceNode', [
u('WordNode', [u('TextNode', 'This')]),
u('WhiteSpaceNode', ' '),
u('WordNode', [u('TextNode', 'makes')]),
u('WhiteSpaceNode', ' '),
u('WordNode', [u('TextNode', 'me')]),
u('WhiteSpaceNode', ' '),
u('WordNode', [u('TextNode', 'feel')]),
u('WhiteSpaceNode', ' '),
u('EmoticonNode', '*weird*'),
u('PunctuationNode', '.')
])
])
]),
'should ignore unknown emoji'
)

retext()
.use(emoji)
.process(fixture, function(err, file) {
Expand Down Expand Up @@ -204,7 +254,7 @@ test('emoji', function(t) {
t.end()
})

// eslint-disable-next-line max-params
// eslint-disable-next-line max-params, unicorn/prevent-abbreviations
function p(sl, sc, so, el, ec, eo) {
return {start: point(sl, sc, so), end: point(el, ec, eo)}
}
Expand Down

0 comments on commit 17eb219

Please sign in to comment.