-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathparse-emoji.js
47 lines (44 loc) · 1.21 KB
/
parse-emoji.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
'use strict';
const utils = require('./utils.js');
const parseEmoji = function(version) {
const source = utils.readDataFile(version, 'emoji');
if (!source) {
return;
}
const propertyMap = new Map();
const lines = source.split('\n');
lines.forEach(function(line) {
if (!line || /^#/.test(line)) {
return;
}
const data = line.trim().split(' ; ');
const charRange = data[0].replace('..', '-').trim();
const rangeParts = charRange.split('-');
const property = data[1].split('#')[0].trim();
if (rangeParts.length == 2) {
utils.range(
parseInt(rangeParts[0], 16),
parseInt(rangeParts[1], 16)
).forEach(function(codePoint) {
if (propertyMap.has(property)) {
propertyMap.get(property).add(codePoint);
} else {
propertyMap.set(property, new Set([codePoint]));
}
});
} else {
const codePoint = parseInt(rangeParts, 16);
if (propertyMap.has(property)) {
propertyMap.get(property).add(codePoint);
} else {
propertyMap.set(property, new Set([codePoint]));
}
}
});
const plainObject = {};
for (const [property, codePoints] of propertyMap) {
plainObject[property] = [...codePoints].sort((a, b) => a - b);
}
return plainObject;
};
module.exports = parseEmoji;