-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathread-trips.js
44 lines (37 loc) · 944 Bytes
/
read-trips.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
'use strict'
const inMemoryStore = require('./lib/in-memory-store')
const expectSorting = require('./lib/expect-sorting')
const readTrips = async (readFile, filters = {}, opt = {}) => {
if (typeof readFile !== 'function') {
throw new TypeError('readFile must be a function')
}
const {
trip: tripFilter,
} = {
trip: () => true,
...filters,
}
if (typeof tripFilter !== 'function') {
throw new TypeError('filters.trip must be a function')
}
const {
createStore,
formatTrip,
} = {
createStore: inMemoryStore,
formatTrip: row => row,
...opt,
}
const checkSorting = expectSorting('trips', (a, b) => {
if (a.trip_id === b.trip_id) return 0
return a.trip_id < b.trip_id ? -1 : 1
})
const trips = createStore() // by ID
for await (const t of await readFile('trips')) {
if (!tripFilter(t)) continue
checkSorting(t)
await trips.set(t.trip_id, formatTrip(t))
}
return trips
}
module.exports = readTrips