-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathgen.js
83 lines (68 loc) · 2.22 KB
/
gen.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
76
77
78
79
80
81
82
83
const fs = require('fs')
const path = require('path')
const https = require('https')
const EMOJI_VERSION = '15.1'
main()
async function main () {
const text = await getTestFile(EMOJI_VERSION)
console.log(`Format text to json...`)
const collected = text.trim().split('\n').reduce((accu, line) => {
if (line.startsWith('# group: ')) {
console.log(` Processing ${line.substr(2)}...`)
accu.group = line.substr(9)
} else if (line.startsWith('# subgroup: ')) {
accu.subgroup = line.substr(12)
} else if (line.startsWith('#')) {
accu.comments = accu.comments + line + '\n'
} else {
const meta = parseLine(line)
if (meta) {
meta.category = `${accu.group} (${accu.subgroup})`
meta.group = accu.group
meta.subgroup = accu.subgroup
accu.full.push(meta)
accu.compact.push(meta.char)
} else {
accu.comments = accu.comments.trim() + '\n\n'
}
}
return accu
}, { comments: '', full: [], compact: [] })
console.log(`Processed emojis: ${collected.full.length}`)
console.log('Write file: emoji.json, emoji-compact.json \n')
await writeFiles(collected)
console.log(collected.comments)
}
async function getTestFile (ver) {
const url = `https://unicode.org/Public/emoji/${ver}/emoji-test.txt`
process.stdout.write(`Fetch emoji-test.txt (v${EMOJI_VERSION})`)
return new Promise((resolve, reject) => {
https.get(url, res => {
let text = ''
res.setEncoding('utf8')
res.on('data', (chunk) => {
process.stdout.write('.')
text += chunk
})
res.on('end', () => {
process.stdout.write('\n')
resolve(text)
})
res.on('error', reject)
})
})
}
function parseLine (line) {
const data = line.trim().split(/\s+[;#] /)
if (data.length !== 3) {
return null
}
const [ codes, status, charAndName ] = data
const [ , char, name ] = charAndName.match(/^(\S+) E\d+\.\d+ (.+)$/)
return { codes, char, name }
}
const rel = (...args) => path.resolve(__dirname, ...args)
function writeFiles({ full, compact }) {
fs.writeFileSync(rel('../emoji.json'), JSON.stringify(full), 'utf8')
fs.writeFileSync(rel('../emoji-compact.json'), JSON.stringify(compact), 'utf8')
}