-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
43 lines (35 loc) · 1.08 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
'use strict'
const fs = require('fs')
const path = require('path')
const getHafasStations = require('db-hafas-stations')
const build = require('synchronous-autocomplete/build')
const tokenize = require('tokenize-db-station-name')
const showError = (err) => {
if (!err) return
console.error(err.stack)
process.exit(1)
}
const writeJSON = (file, data, cb) => {
fs.writeFile(path.join(__dirname, file), JSON.stringify(data), cb)
}
console.info('Collecting search items.')
const items = []
getHafasStations()
.on('data', (station) => {
items.push({
id: station.id,
name: station.name,
weight: station.weight || 1
})
})
.once('end', () => {
console.info('Computing a search index.')
const {tokens, scores, weights, nrOfTokens, originalIds} = build(tokenize, items)
console.info('Writing the index to disk.')
writeJSON('tokens.json', tokens, showError)
writeJSON('scores.json', scores, showError)
writeJSON('weights.json', weights, showError)
writeJSON('nr-of-tokens.json', nrOfTokens, showError)
writeJSON('original-ids.json', originalIds, showError)
})
.once('error', showError)