-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmojiApplier.js
41 lines (35 loc) · 1.12 KB
/
EmojiApplier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var fs = require('fs')
const emoji = (() => {
let emoji = {};
fs.readFileSync('./emoji.csv').toString().split('\n').forEach(line => {
[ char, word ] = line.split(', ')
emoji[word] = char
})
return emoji
})()
const SEPAR = '\\,\\;\\.'
const RE_SEPAR = new RegExp('[' + SEPAR + ']')
const createWordRegExp = word => new RegExp('([ ^])(' + word + ')([ ' + SEPAR + '$])', 'g')
const emojiRegExp = createWordRegExp(Object.keys(emoji).join('|'))
function findEmojiWords(text) {
const emojiSet = {}
;(text.match(emojiRegExp) || []).forEach(word => {
word = word.replace(RE_SEPAR, '').trim()
emojiSet[word] = emoji[word]
})
return emojiSet
}
function integrateEmoji(text) {
let final = text.substr()
let emojiWords = findEmojiWords(text)
Object.keys(emojiWords).forEach(word => {
final = final.replace(createWordRegExp(word), '$1$2 ' + emojiWords[word] + '$3')
})
// TODO: final = final.replace(emojiRegExp, (a, word) => console.log(arguments)/*'$1$2 ' + emojiWords[word] + '$3'*/)
return final
}
module.exports = {
findEmojiWords: findEmojiWords,
integrateEmoji: integrateEmoji,
emoji: emoji
}