-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (45 loc) · 1.21 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// --- Dependencies
require('string.fromcodepoint')
const emojiData = require('emoji-datasource')
const _ = require('lodash')
// --- Turn emojis alone or in text into code
const stringify = text => {
let result = ''
// --- Parse emojiData
_.each(emojiData, (val, key) => {
emojiData[key].textValue = String.fromCodePoint(...val.unified.split('-').map(e => `0x${e}`))
})
// --- Parse text
let arrText = _.toArray(text)
// --- Loop to find emoji
_.each(arrText, val => {
// --- Find index emoji to text value
let index = _.findIndex(emojiData, e => {
return e.textValue === val
})
// --- Check index
if(index > -1) {
result += `[${emojiData[index]['unified']}]`
}else {
result += val
}
})
// --- Return text and emoji code
return result
}
// --- Parse the text to find an emoji code
const parse = text => {
// --- Find emoji to emojiData
_.each(emojiData, val => {
let regex = new RegExp('\\['+val.unified+'\\]', 'g')
let emoji = String.fromCodePoint(...val.unified.split('-').map(u => `0x${u}`))
// --- Check text
if(text) {
// --- Replace text to emoji
text = text.replace(regex, emoji)
}
})
// --- Return text and emoji
return text
}
module.exports = {stringify, parse}