Skip to content
Utility to collect departures, using any HAFAS client.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
.editorconfig
.gitignore
.travis.yml
index.js
license.md
package.json
readme.md
test.js
while.js

readme.md

hafas-collect-departures-at

Utility to collect departures, using any HAFAS client.

npm version build status ISC-licensed chat on gitter support me on Patreon

Installing

npm install hafas-collect-departures-at

Usage

const createHafas = require('vbb-hafas')
const createCollectDeps = require('hafas-collect-departures-at')

const fooStation = '900000100001'
const hafas = createHafas('my-awesome-program')
const collectDeps = createCollectDeps(hafas.departures)
const depsAtFoo = collectDeps(fooStation, Date.now())

const fetchDepsTwice = async () => {
	let iterations = 0
	for await (let deps of depsAtFoo) {
		if (++iterations > 2) break
		console.log(deps)
	}
}

fetchDepsTwice()
.catch(console.error)

Keep in mind that the example above only works in Node 10+ (and Node 9 with the --harmony-async-iteration flag), because it uses async iteration.

You can also use Babel with the async generators plugin (currently in the stage 3 preset) to use this syntax. Alternatively, you can write fetchDepsTwice without async iteration:

const fetchDepsTwice = async () => {
	const iterator = depsAtFoo[Symbol.asyncIterator]()
	let iterations = 0
	while (++iterations <= 2) {
		const result = await iterator.next()
		const deps = result.value
		console.log(deps)
	}
}

while helper

If you don't like to use the async iteration syntax, there's a helper that works like a regular while loop:

const createCollectWhile = require('hafas-collect-departures-at/while')

const shouldPick = (dep, i) => i < 10

const collectWhile = createCollectWhile(vbb.departures)
collectWhile(fooStation, Date.now(), shouldPick)
.then(console.log)
.catch(console.error)

collectWhile() returns a Promise that will resolve with an array of 10 items.

Contributing

If you have a question or have difficulties using hafas-collect-departures-at, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to the issues page.

You can’t perform that action at this time.