From 541505e720b9e4cfb3a4250879f5c32486b5e54a Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 20 Nov 2017 01:05:48 +0100 Subject: [PATCH] VBB: 9<->12 digit station IDs --- p/vbb/index.js | 9 +++++++-- package.json | 3 ++- test/vbb.js | 39 ++++++++++++++++++--------------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/p/vbb/index.js b/p/vbb/index.js index 9bbfe0bc..dc528c5b 100644 --- a/p/vbb/index.js +++ b/p/vbb/index.js @@ -1,6 +1,7 @@ 'use strict' const shorten = require('vbb-short-station-name') +const {to12Digit, to9Digit} = require('vbb-translate-ids') const _formatStation = require('../../format/station') const _parseLine = require('../../parse/line') @@ -34,14 +35,17 @@ const parseLine = (profile, l) => { const parseLocation = (profile, l) => { const res = _parseLocation(profile, l) - res.name = shorten(res.name) + if (res.type === 'station') { + res.id = to12Digit(res.id) + res.name = shorten(res.name) + } return res } const isIBNR = /^\d{9,}$/ const formatStation = (id) => { - // todo: convert short to long IDs if (!isIBNR.test(id)) throw new Error('station ID must be an IBNR.') + id = to9Digit(id) return _formatStation(id) } @@ -72,6 +76,7 @@ const vbbProfile = { parseLine, parseProducts: modes.parseBitmask, + formatStation, formatProducts } diff --git a/package.json b/package.json index c94c295a..ba054276 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "pinkie-promise": "^2.0.1", "query-string": "^5.0.0", "slugg": "^1.2.0", - "vbb-short-station-name": "^0.4.0" + "vbb-short-station-name": "^0.4.0", + "vbb-translate-ids": "^3.1.0" }, "devDependencies": { "db-stations": "^1.25.0", diff --git a/test/vbb.js b/test/vbb.js index f5f1c9e8..fd4cd809 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -26,9 +26,12 @@ const findStation = (query) => stations(query, true, false) const test = tapePromise(tape) const client = createClient(vbbProfile) +const amrumerStr = '900000009101' +const spichernstr = '900000042101' +const bismarckstr = '900000024201' + test('journeys – station to station', async (t) => { - // U Spichernstr. to U Amrumer Str. - const journeys = await client.journeys('900000042101', '900000009101', { + const journeys = await client.journeys(spichernstr, amrumerStr, { results: 3, when, passedStations: true }) @@ -38,11 +41,11 @@ test('journeys – station to station', async (t) => { for (let journey of journeys) { assertValidStation(t, journey.origin) t.ok(journey.origin.name.indexOf('(Berlin)') === -1) - t.strictEqual(journey.origin.id, '900000042101') + t.strictEqual(journey.origin.id, spichernstr) assertValidWhen(t, journey.departure) assertValidStation(t, journey.destination) - t.strictEqual(journey.destination.id, '900000009101') + t.strictEqual(journey.destination.id, amrumerStr) assertValidWhen(t, journey.arrival) t.ok(Array.isArray(journey.parts)) @@ -53,11 +56,11 @@ test('journeys – station to station', async (t) => { t.ok(part.id) assertValidStation(t, part.origin) t.ok(part.origin.name.indexOf('(Berlin)') === -1) - t.strictEqual(part.origin.id, '900000042101') + t.strictEqual(part.origin.id, spichernstr) assertValidWhen(t, part.departure) assertValidStation(t, part.destination) - t.strictEqual(part.destination.id, '900000009101') + t.strictEqual(part.destination.id, amrumerStr) assertValidWhen(t, part.arrival) assertValidLine(t, part.line) @@ -71,8 +74,7 @@ test('journeys – station to station', async (t) => { }) test('journeys – only subway', async (t) => { - // U Spichernstr. to U Bismarckstr. - const journeys = await client.journeys('900000042101', '900000024201', { + const journeys = await client.journeys(spichernstr, bismarckstr, { results: 20, when, products: { suburban: false, @@ -102,8 +104,7 @@ test('journeys – only subway', async (t) => { test('journeys – fails with no product', async (t) => { try { - // U Spichernstr. to U Bismarckstr. - await client.journeys('900000042101', '900000024201', { + await client.journeys(spichernstr, bismarckstr, { when, products: { suburban: false, @@ -122,8 +123,7 @@ test('journeys – fails with no product', async (t) => { }) test('journey part details', async (t) => { - // U Spichernstr. to U Amrumer Str. - const journeys = await client.journeys('900000042101', '900000009101', { + const journeys = await client.journeys(spichernstr, amrumerStr, { results: 1, when }) @@ -149,8 +149,7 @@ test('journey part details', async (t) => { test('journeys – station to address', async (t) => { - // U Spichernstr. to Torfstraße 17 - const journeys = await client.journeys('900000042101', { + const journeys = await client.journeys(spichernstr, { type: 'address', name: 'Torfstraße 17', latitude: 52.5416823, longitude: 13.3491223 }, {results: 1, when}) @@ -176,8 +175,7 @@ test('journeys – station to address', async (t) => { test('journeys – station to POI', async (t) => { - // U Spichernstr. to ATZE Musiktheater - const journeys = await client.journeys('900000042101', { + const journeys = await client.journeys(spichernstr, { type: 'poi', name: 'ATZE Musiktheater', id: 9980720, latitude: 52.543333, longitude: 13.351686 }, {results: 1, when}) @@ -202,9 +200,8 @@ test('journeys – station to POI', async (t) => { -test('departures', async (t) => { - // U Spichernstr. - const deps = await client.departures('900000042101', {duration: 5, when}) +test.only('departures', async (t) => { + const deps = await client.departures(spichernstr, {duration: 5, when}) t.ok(Array.isArray(deps)) t.deepEqual(deps, deps.sort((a, b) => t.when > b.when)) @@ -214,7 +211,7 @@ test('departures', async (t) => { t.equal(dep.station.name, 'U Spichernstr.') assertValidStation(t, dep.station) - t.strictEqual(dep.station.id, '900000042101') + t.strictEqual(dep.station.id, spichernstr) assertValidWhen(t, dep.when) t.ok(findStation(dep.direction)) @@ -235,7 +232,7 @@ test('departures at 7-digit station', async (t) => { test('nearby', async (t) => { // Berliner Str./Bundesallee - const nearby = await client.nearby(52.4873452,13.3310411, {distance: 200}) + const nearby = await client.nearby(52.4873452, 13.3310411, {distance: 200}) t.ok(Array.isArray(nearby)) for (let n of nearby) assertValidLocation(t, n, false)