-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
parseData.ts
67 lines (59 loc) · 2.11 KB
/
parseData.ts
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
/* eslint-disable no-nested-ternary */
import { EMOJI, TEXT } from 'emojibase';
import type { EmojiData, EmojiDataMap, ParsedLine, Property } from '../types';
import { extractEmojiVersion } from './extractEmojiVersion';
import { extractLineDescription } from './extractLineDescription';
import { extractUnicodeVersion } from './extractUnicodeVersion';
import { parse } from './parse';
import { spreadHexcode } from './spreadHexcode';
import { verifyTotals } from './verifyTotals';
/**
* Parses the official unicode emoji data.
*
* Example: http://unicode.org/Public/emoji/5.0/emoji-data.txt
*/
export function parseData(version: string, content: string, unicodeVersion?: number): EmojiDataMap {
const { lines, totals } = parse(content);
const data = lines.reduce((map: EmojiDataMap, line: ParsedLine) => {
const [rawHexcode, property, , modifier] = line.fields;
// v11.0 included EP, which reserves slots for future emoji.
// We don't need to pull these in at this time.
if (property === 'Extended_Pictographic') {
return map;
}
const emojiVersion = extractEmojiVersion(line.comment) ?? Number.parseFloat(version);
const emoji: EmojiData = {
description: extractLineDescription(line.comment),
hexcode: '',
property: [(property as Property) || 'Emoji'],
type: EMOJI,
unicodeVersion: extractUnicodeVersion(emojiVersion, unicodeVersion),
version: emojiVersion,
};
spreadHexcode(rawHexcode, (hexcode, range) => {
// v1.0 had a different structure
if (!range && version === '1.0') {
emoji.type = property === 'emoji' ? EMOJI : TEXT;
emoji.property = [
modifier === 'primary' || modifier === 'secondary'
? 'Emoji_Modifier_Base'
: modifier === 'modifier'
? 'Emoji_Modifier'
: 'Emoji',
];
}
if (map[hexcode]) {
// An emoji may belong to multiple properties,
// so keep a unique list of all applicable.
map[hexcode].property = [...new Set([...map[hexcode].property, ...emoji.property])];
} else {
map[hexcode] = {
...emoji,
hexcode,
};
}
});
return map;
}, {});
return verifyTotals('data', version, data, totals);
}