-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
75 lines (68 loc) · 1.72 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
var emojis = require('./emojis.json')
function toBuffer(v) {
if (!Buffer.isBuffer(v))
return new Buffer(v, 'hex')
return v
}
function convert(buf, fn) {
buf = toBuffer(buf)
var out = ''
var n =0;
for (var i = 0; i < buf.length; i++) {
out += fn(buf.readUInt8(i))
n++
}
return out
}
exports.toUnicode =
exports.toUtf8 = function(buf) { // toUtf8 is legacy, it's inaccurate - JS is utf16
return convert(buf, function(c) {
return emojis[c].char
})
}
exports.toNames = function(buf) {
return convert(buf, function(c) {
return ':'+emojis[c].name+':'
})
}
exports.toCustom = function(buf, fn) {
return convert(buf, function(c) {
return fn(c, emojis[c])
})
}
exports.fromUnicode = function (s) {
s = getSymbols(s)
var buf = new Buffer(s.length)
s.forEach(function (symbol, index) {
for (var i=0; i < emojis.length; i++) {
if (emojis[i].char == symbol) {
buf.writeUIntBE(i, index, 1)
break
}
}
if (i == emojis.length)
throw new Error('Failed to match symbol: ' + symbol + ' (' + symbol.charCodeAt(0) + ' ' + symbol.charCodeAt(1) + ')')
})
return buf
}
// https://mathiasbynens.be/notes/javascript-unicode
// ^ doing the lord's work
function getSymbols(string) {
var length = string.length;
var index = -1;
var output = [];
var character;
var charCode;
while (++index < length) {
character = string.charAt(index);
charCode = character.charCodeAt(0);
if (charCode >= 0xaa && charCode <= 0xDBFF) {
// Note: this doesn’t account for lone high surrogates;
// you’d need even more code for that!
output.push(character + string.charAt(++index));
} else {
output.push(character);
}
}
return output;
}