-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateEmojiArray.js
55 lines (50 loc) · 1.43 KB
/
generateEmojiArray.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
/** @module generateEmojiArray */
import fs from 'fs'
/**
* Return an array with all numbers from `start` to `end`
*
* @param {number} start The first number to be in the Array
* @param {number} end The last number to be in the Array
* @returns {number[]} An Array with all consecutive numbers as elements, ranging from `start` to `end`
*/
Array.range = (start, end) => Array.from(
{ length: (end - start) },
(v, k) => k + start
)
/**
* Synchronously generate an array of emojis from a given Unicode emoji-data input file.
*
* @param {string} [filename=emoji-data.txt] filename The file to read the emoji definition from
* @returns {Array} An array of emojis
*/
export default function generateEmojiArray (filename = 'emoji-data.txt') {
const file = fs.readFileSync(
filename,
{ encoding: 'utf8', flag: 'r' }
).split('\n')
const delimiter = '..'
const emojis = []
file.forEach(line => {
if (
line.includes('# E14.0') // Exclude newer emojis for better compatibility
|| line === ''
|| line.startsWith('#')
) {
return
}
const codepoints = line.split(' ')[0].split(delimiter)
if (codepoints.length > 1) {
emojis.push(
...Array.range(
parseInt(codepoints[0], 16),
parseInt(codepoints[1], 16)
)
)
} else {
emojis.push(
parseInt(codepoints[0], 16)
)
}
})
return emojis.map(e => String.fromCodePoint(e))
}