-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.js
31 lines (26 loc) · 897 Bytes
/
build.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
import fs from 'node:fs/promises'
import fetch from 'node-fetch'
import {tsvParse} from 'd3-dsv'
const response = await fetch(
'https://api.github.com/repos/fnielsen/afinn/contents/afinn/data/AFINN-en-165.txt',
{headers: {'User-Agent': 'request', Accept: 'application/vnd.github.v3.raw'}}
)
const text = await response.text()
/** @type {Record<string, number>} */
const data = {}
const rows = tsvParse('key\tvalue\n' + text)
let index = -1
while (++index < rows.length) {
const {key, value} = rows[index]
if (!key) throw new Error('Expected key in `' + rows[index] + '`')
if (!value) throw new Error('Expected value in `' + rows[index] + '`')
data[key] = Number.parseInt(value, 10)
}
await fs.writeFile(
'index.js',
[
'/**\n * AFINN 165.\n *\n * @type {Record<string, number>}\n */',
'export const afinn165 = ' + JSON.stringify(data, null, 2),
''
].join('\n')
)