npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

find-hafas-data-in-another-hafas

v4.4.0

Published

Find data from one HAFAS endpoint in another one.

Downloads

19

Readme

find-hafas-data-in-another-hafas

Find data from one HAFAS endpoint in the data of another HAFAS endpoint.

npm version build status ISC-licensed support me via GitHub Sponsors chat with me on Twitter

Public transport providers in Europe often have data about vehicles run by other companies, but almost always it is outdated or imprecise. Consider these examples:

sncf.com showing TGV 6631 bahn.de showing TGV 6631

bahn.de showing IC 2029 sncf.com showing IC 2029

Let's always get the data about a vehicles from the company that actually run it! Given a leg from an endpoint A, you can use this library to fetch more up-to-date data about it from another endpoint B.

Why linked open transit data? explains more about this idea.

Installation

npm install find-hafas-data-in-another-hafas

Usage

const createDbHafas = require('db-hafas')
const createVbbHafas = require('vbb-hafas')
const createFindLeg = require('find-hafas-data-in-another-hafas/find-leg')
const createMergeLegs = require('find-hafas-data-in-another-hafas/merge')

// Note that, for legs to be matched reliably, you need a more
// sophisticated normalization function. Use e.g.
// - https://github.com/derhuerst/tokenize-db-station-name
// - https://github.com/derhuerst/tokenize-vbb-station-name
const normalizeName = name => str.toLowerCase().replace(/\s/g, '')

const dbHafas = createDbHafas('find-db-hafas-leg-in-another-hafas example')
const dbEndpoint = {
	// The endpoint name should be URL-safe & stable, it will be used to compute
	// IDs to be matched against other IDs.
	endpointName: 'db',
	client: dbHafas,
	normalizeStopName: normalizeName,
	normalizeLineName: normalizeName,
}

const vbbHafas = createVbbHafas('find-db-hafas-leg-in-another-hafas example')
const vbbEndpoint = {
	endpointName: 'vbb',
	client: vbbHafas,
	normalizeStopName: normalizeName,
	normalizeLineName: normalizedName,
}

const findLegInAnother = createFindLeg(dbEndpoint, vbbEndpoint)
const mergeLegs = createMergeLegs(dbEndpoint, vbbEndpoint)

const potsdamerPlatz = '8011118'
const südkreuz = '8011113'
const res = await dbHafas.journeys(potsdamerPlatz, südkreuz, {
	results: 1, stopovers: true, tickets: false
})
const [journey] = res.journeys

const dbLeg = journey.legs.find(leg => leg.line) // find non-walking leg
console.log('DB leg', dbLeg)

const vbbLeg = findLegInAnother(dbLeg)
console.log('equivalent VBB leg', leg)

const mergedLeg = mergeLegs(dbLeg, vbbLeg)
console.log('mergedLeg', mergedLeg)

API

find-hafas-data-in-another-hafas provides the following entry points:

  • find-h…/match-line
  • find-h…/match-stop-or-station
  • find-h…/match-stopover
  • find-h…/find-stop
  • find-h…/find-leg
  • find-h…/find-arr-dep
  • find-h…/merge-leg
  • find-h…/merge-arr-dep

match* functions identify two items (e.g. two journey legs) from different endpoints as equivalent (describing the same thing). Given an item from one endpoint, find* functions find the equivalent in the other endpoint. merge* functions merge two equivalents items found using find*.

All of these functions expect the two endpoints to be specified as follows:

{
	endpointName: 'oebb', // a *stable* & URL-safe identifier for the endpoint
	client: …, // a hafas-client@5-compatible API client
	// These should return URL-safe, lower-case versions of stop/line names, with
	// as little meaningless/local additions (e.g. "Bus" or "Bhf") as possible.
	// The results will be used to match stops/lines across endpoints!
	normalizeStopName: name => normalizedName,
	normalizeLineName: name => normalizedName,

	// additional fields to copy over from a stopover from the respective endpoint (optional)
	mergeStopoverAdditionalFields: ['myCustomField'],
}

Related

Contributing

If you have a question or need support using find-hafas-data-in-another-hafas, 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.