-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (162 loc) · 5.62 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var _ = {toArray: require('lodash.toarray')}
class EmojiConverter{
/**
* Create an emoji converter instance
*
* @constructor
* @param sources an array of emoji sources. Default is `[EmojiConverter.EMOJI_DEFAULT_SOURCE]`
*/
constructor(sources){
this.sources = sources
if (!this.sources){
this.sources = [EmojiConverter.EMOJI_DEFAULT_SOURCE]
}
// emojiMap is shortcode:unicode
this.emojiMap = {}
// shortcodeMap is unicode:shortcode
this.shortcodeMap = {}
// aliasMap is alias:shortcode
this.aliasMap = {}
// nameMap is shortcode:name
this.nameMap = {}
// objectMap shortcode:object
this.objectMap = {}
this.sources.forEach(source =>{
// array format (may be missing unicode character. see README.)
if (Array.isArray(source)){
source.forEach(item=>{
var unicode = item.unicode
var shortname = item.shortname
var name = item.name || shortname.replace(/\:/g,'')
var shortnames = [shortname, ...(item.shortname_alternates || []) ]
shortnames.forEach(s=>{
if (unicode){
this.emojiMap[s] = unicode
}
this.aliasMap[s] = shortname
})
if (unicode) { this.shortcodeMap[unicode] = shortname }
this.nameMap[shortname] = item.name || shortname.replace(/\:/g,'')
this.objectMap[shortname] = item
})
return
}
// object format (from emoji-toolkit/emoji_strategy.json)
// iterate through each key (key is the unicode sequence)
Object.keys(source).forEach(sequence=>{
var entry = source[sequence]
var unicode = EmojiConverter.pointsStringToUnicode(entry['unicode_output'])
var shortname = entry.shortname;
var shortnames = [shortname, ...entry.shortname_alternates]
shortnames.forEach(s=>{
this.emojiMap[s.toLowerCase()] = unicode
this.aliasMap[s.toLowerCase()] = shortname
})
this.shortcodeMap[unicode] = shortname
this.nameMap[shortname] = entry.name || shortname.replace(/\:/g,'')
this.objectMap[shortname] = entry
})
})
}
// replacer function takes in (unicode, shortcode, name)
/**
* Replace unicode in a string with a custom function
* @param {string} str the string to operate on
* @param {function} replacer the replacer function
* @returns {string} the resultant string
*/
replaceUnicodeWith(str, replacer){
return _.toArray(str)
.map(s => {
var shortcode = this.shortcodeMap[s];
if (shortcode){
var name = this.nameMap[shortcode]||'';
var obj = this.objectMap[shortcode]||{}
return replacer(s, shortcode, name, obj)
} else { // no short code for this unicode char. i.e. we don't have this.
return s
}
})
.join('')
}
/**
* Replace shortcodes in a string with a custom function
* @param {string} str the string to operate on
* @param {function} replacer the replacer function
* @returns {string} the resultant string
*/
replaceShortcodesWith(str, replacer){
return str.replace(EmojiConverter.SHORTCODE_REGEX, (match)=>{
var short = match.toLowerCase()
var uni = this.emojiMap[short]||''
var name = this.nameMap[short]||''
var obj = this.objectMap[short]||{}
return replacer(uni, short, name, obj)
})
}
/**
* Replace unicode and shortcodes in a string with a custom function
* @param {string} str the string to operate on
* @param {function} replacer the replacer function
* @returns {string} the resultant string
*/
replaceWith(str, replacer){
return this.replaceShortcodesWith(this.replaceUnicode(str), replacer)
}
/**
* Replace unicode in a string with shortcodes
* @param {string} str the string to operate on
* @param {function} replacer the replacer function
* @returns {string} the resultant string
*/
replaceUnicode(str){
return _.toArray(str)
.map(s => this.shortcodeMap[s] || s)
.join('')
}
/**
* Replace shortcodes in a string with unicode
* @param {string} str the string to operate on
* @param {function} replacer the replacer function
* @returns {string} the resultant string
*/
replaceShortcodes(str){
return str.replace(EmojiConverter.SHORTCODE_REGEX, (match)=>{
return this.emojiMap[match.toLowerCase()] || match
})
}
/**
* Replace short codes with their more normalized version.
* @param {string} str the string to operate on
* @returns {string} the resultant string
*/
normalizeShortcodes(str){
return str.replace(EmojiConverter.SHORTCODE_REGEX, (match)=>{
return this.aliasMap[match.toLowerCase()] || match
})
}
}
/**
* Convert a single unicode emoji to a string representing codepoints (such as '1f646-1f3ff-2642')
* @param {string} str the unicode sequence you want converted
* @returns {string}
*/
EmojiConverter.unicodeToPointsString = function(str){
return Array.from(str).map(s => s.codePointAt(0).toString(16) ).join('-')
}
/**
* Convert a string representing unicode codepoints (such as '1f646-1f3ff-2642')
* @param {string} str the unicode sequence you want converted
* @returns {string}
*/
EmojiConverter.pointsStringToUnicode = function(str){
var unicode = ''
var codes = str.split('-')
for (var i = 0; i < codes.length; i ++){
unicode += String.fromCodePoint(Number('0x'+codes[i]))
}
return unicode
}
EmojiConverter.EMOJI_DEFAULT_SOURCE = require('emoji-toolkit/emoji_strategy.json')
EmojiConverter.SHORTCODE_REGEX = /(\:(\w|\+|\-)+\:)/gim
module.exports = EmojiConverter;