-
Notifications
You must be signed in to change notification settings - Fork 645
/
pack.js
38 lines (31 loc) · 967 Bytes
/
pack.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
var fs = require('fs')
var path = require('path')
var efrt = require('efrt')
// var nlpPlugin = require('compromise-plugin')
console.log('\n 🕑 - packing lexicon..')
var outFile = path.join(__dirname, '../src/World/_data.js')
var lexicon = require('../data')
//turn them into a series of flat-arrays
let words = Object.keys(lexicon)
let packed = {}
words.forEach(word => {
let tags = lexicon[word]
if (typeof tags === 'string') {
tags = [tags]
}
tags.forEach(tag => {
packed[tag] = packed[tag] || []
packed[tag].push(word)
})
})
//pack each array into a tiny string
Object.keys(packed).forEach(tag => {
packed[tag] = efrt.pack(packed[tag])
})
//write it to a file in ./src
fs.writeFileSync(outFile, 'module.exports=' + JSON.stringify(packed, null, 2), 'utf8')
//get filesize
var stats = fs.statSync(outFile)
let size = (stats['size'] / 1000.0).toFixed(1)
console.log(' - packed into ' + size + 'k\n')
console.log(' done!\n')