-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (48 loc) · 1.61 KB
/
index.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
'use strict'
const ndjson = require('ndjson')
const filter = require('stream-filter')
const fs = require('fs')
const path = require('path')
const map = require('through2-map')
const hasProp = (o, k) => Object.prototype.hasOwnProperty.call(o, k)
const filterById = (id) => (data) => data && data.id === id
const filterByKeys = (pattern) => (data) => {
if (!data) return false
for (let key in pattern) {
if (!hasProp(data, key)) return false
if (data[key] !== pattern[key]) return false
}
return true
}
const toPromise = (stream) => new Promise((yay, nay) => {
const acc = []
stream.on('error', nay)
.on('data', (data) => acc.push(data))
.on('end', () => yay(acc))
})
const decompress = (route) => {
if (!Array.isArray(route.when)) return route
for (let i = 0; i < route.when.length; i++) {
route.when[i] *= 1000 // decompress
}
return route
}
const base = path.join(__dirname, 'data')
const selector = (file) => function (/* promised, pattern */) {
const args = Array.prototype.slice.call(arguments)
let pattern = args.pop()
let promised = !!args.shift()
let stream = fs.createReadStream(path.join(base, file))
.pipe(ndjson.parse()).pipe(map.obj(decompress))
if (pattern !== 'all') {
if ('string' === typeof pattern)
stream = stream.pipe(filter.obj(filterById(pattern)))
else if (pattern)
stream = stream.pipe(filter.obj(filterByKeys(pattern)))
}
if (promised === true) return toPromise(stream)
else return stream
}
const lines = selector('lines.ndjson')
const schedules = selector('schedules.ndjson')
module.exports = {filterById, filterByKeys, lines, schedules}