-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (49 loc) · 1.29 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
const emojis = require('./emojis');
let regxArr = [];
const noExtra = s => s.replace(/\ufe0f|\u200d/gm, '');
const toSurrogatePairs = (r) => {
let t;
let n;
/* eslint-disable no-plusplus */
for (t = '', n = 0; n < r.length; n++) {
t += `\\u${`000${r[n].charCodeAt(0).toString(16)}`.substr(-4)}`;
}
return t;
};
const toCodePoint = (t) => {
let n;
let r;
let o;
let h;
// TODO: make this part human readable
/* eslint-disable */
for (n = [], r = 0, o = 0, h = 0; h < t.length;) {
(r = t.charCodeAt(h++)),
o
? (n.push((65536 + ((o - 55296) << 10) + (r - 56320)).toString(16)), (o = 0))
: r >= 55296 && r <= 56319 ? (o = r) : n.push(r.toString(16));
}
/* eslint-enable */
return n.join('-');
};
regxArr = emojis.map(el => toSurrogatePairs(el));
const re = new RegExp(`(${regxArr.join('|')})`, 'g');
regxArr = null;
class Parser {
constructor(path) {
if (!path) {
throw new Error('You should pass images path as the parameter to the constructor');
}
this.path = path;
}
parse(string) {
if (!string) {
return string;
}
return string.replace(
re,
(a, b) => `<img draggable="false" class="emoji" src="${this.path + toCodePoint(noExtra(b))}.png">`);
}
}
module.exports = Parser;
module.exports.default = Parser;