Skip to content

Commit

Permalink
read-trips: use for-await, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Aug 30, 2020
1 parent 747798a commit 375999c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
19 changes: 19 additions & 0 deletions examples/read-trips.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const readCsv = require('../read-csv')
const readTrips = require('../read-trips')

const readFile = (file) => {
return readCsv(require.resolve('sample-gtfs-feed/gtfs/' + file + '.txt'))
}

;(async () => {
const trips = await readTrips(readFile)
for await (const trip of trips.values()) {
console.log(trip)
}
})()
.catch((err) => {
console.error(err)
process.exit(1)
})
22 changes: 22 additions & 0 deletions lib/expect-sorting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const expectSorting = (filename, compareFn) => {
let prevRow = null
let rowNr = 0

const checkRow = (row) => {
rowNr++

if (prevRow !== null && compareFn(prevRow, row) > 0) {
const err = new Error(filename + ' is not sorted as needed')
err.previousRow = prevRow
err.row = row
err.rowNr = rowNr
throw err
}
prevRow = row
}
return checkRow
}

module.exports = expectSorting
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"compute-service-breaks.js",
"route-types.js",
"read-stops.js",
"read-trips.js",
"lib",
"examples",
"test"
Expand Down
30 changes: 23 additions & 7 deletions read-trips.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
'use strict'

const inMemoryStore = require('./lib/in-memory-store')
const processFile = require('./lib/process-file')
const expectSorting = require('./lib/expect-sorting')

const noFilter = () => true
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 readTrips = async (readFile, filter = noFilter, opt = {}) => {
const {
createStore,
} = {
createStore: inMemoryStore,
...opt,
}

const trips = createStore()
const processTrip = async (t) => {
if (!filter(t)) return;
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 readFile('trips')) {
if (!tripFilter(t)) continue
checkSorting(t)
await trips.set(t.trip_id, t)
}
await processFile('trips', readFile('trips'), processTrip)

return trips
}
Expand Down
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,36 @@ readTrips(readFile, filter)

Will read `trips.txt` and reduce it into a map `tripId => trip`. Returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/promise).

### `readTrips(readFile, filters = {})`

```js
const readCsv = require('gtfs-utils/read-csv')
const readTrips = require('gtfs-utils/read-trips')

const readFile = name => readCsv('path/to/gtfs/' + name + '.txt')

const trips = readTrips(readFile, {
trip: s => s.trip_id === '1234',
})
for await (const trip of trips) console.log(trip)
```

```js
{
route_id: 'A',
service_id: 'all-day',
trip_id: 'a-downtown-all-day',
trip_headsign: '',
trip_short_name: '',
direction_id: '',
wheelchair_accessible: '',
bikes_allowed: '',
}

```

`readTrips` is an [async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of).

### `parseDate(dateStr, timezone)`

```js
Expand Down

0 comments on commit 375999c

Please sign in to comment.