restore former code style 👕

This commit is contained in:
Jannis R 2018-03-12 22:43:50 +01:00
parent 47e26163e5
commit 094e95c9cc
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 252 additions and 295 deletions

View file

@ -1,33 +1,23 @@
"use strict"; 'use strict'
const createClient = require("../.."); const createClient = require('../..')
const insaProfile = require("."); const insaProfile = require('.')
const client = createClient(insaProfile); const client = createClient(insaProfile)
// from Magdeburg-Neustadt to Magdeburg-Buckau // from Magdeburg-Neustadt to Magdeburg-Buckau
// client.journeys('008010226', '008013456', {results: 1}) client.journeys('008010226', '008013456', {results: 1})
client // client.departures('008010226', { duration: 5 })
.departures("008010226", { duration: 5 }) // client.locations('Magdeburg Hbf', {results: 2})
// client; // client.locations('Kunstmuseum Kloster Unser Lieben Frauen Magdeburg', {results: 2})
// .locations("Magdeburg Hbf", { // client.location('008010226') // Magdeburg-Neustadt
// results: 2 // client.nearby({
// }) // type: 'location',
// .locations("Kunstmuseum Kloster Unser Lieben Frauen Magdeburg", { // latitude: 52.148842,
// results: 2 // longitude: 11.641705
// }) // }, {distance: 200})
// client.location('008010226') // Magdeburg-Neustadt
// client
// .nearby(
// {
// type: "location",
// latitude: 52.148842,
// longitude: 11.641705
// },
// { distance: 200 }
// )
.then(data => { .then(data => {
console.log(require("util").inspect(data, { depth: null })); console.log(require('util').inspect(data, { depth: null }))
}) })
.catch(console.error); .catch(console.error)

View file

@ -1,14 +1,14 @@
"use strict"; 'use strict'
const tapePromise = require("tape-promise").default; const tapePromise = require('tape-promise').default
const tape = require("tape"); const tape = require('tape')
const isRoughlyEqual = require("is-roughly-equal"); const isRoughlyEqual = require('is-roughly-equal')
const validateFptf = require("validate-fptf"); const validateFptf = require('validate-fptf')
const co = require("./co"); const co = require('./co')
const createClient = require(".."); const createClient = require('..')
const insaProfile = require("../p/insa"); const insaProfile = require('../p/insa')
const products = require("../p/insa/products"); const products = require('../p/insa/products')
const { const {
assertValidStation, assertValidStation,
assertValidPoi, assertValidPoi,
@ -19,303 +19,270 @@ const {
hour, hour,
createWhen, createWhen,
assertValidWhen assertValidWhen
} = require("./util.js"); } = require('./util.js')
const when = createWhen("Europe/Berlin", "de-DE"); const when = createWhen('Europe/Berlin', 'de-DE')
const assertValidStationProducts = (t, p) => { const assertValidStationProducts = (t, p) => {
return null; // todo return null; // todo
t.ok(p); t.ok(p)
t.equal(typeof p.nationalExp, "boolean"); t.equal(typeof p.nationalExp, 'boolean')
t.equal(typeof p.national, "boolean"); t.equal(typeof p.national, 'boolean')
t.equal(typeof p.regional, "boolean"); t.equal(typeof p.regional, 'boolean')
t.equal(typeof p.suburban, "boolean"); t.equal(typeof p.suburban, 'boolean')
t.equal(typeof p.tram, "boolean"); t.equal(typeof p.tram, 'boolean')
t.equal(typeof p.bus, "boolean"); t.equal(typeof p.bus, 'boolean')
// console.error(p); // todo // console.error(p); // todo
t.equal(typeof p.tourismTrain, "boolean"); t.equal(typeof p.tourismTrain, 'boolean')
}; }
const isMagdeburgHbf = s => { const isMagdeburgHbf = s => {
return ( return (
s.type === "station" && s.type === 'station' &&
(s.id === "8010224" || s.id === "008010224") && (s.id === '8010224' || s.id === '008010224') &&
s.name === "Magdeburg Hbf" && s.name === 'Magdeburg Hbf' &&
s.location && s.location &&
isRoughlyEqual(s.location.latitude, 52.130352, 0.001) && isRoughlyEqual(s.location.latitude, 52.130352, 0.001) &&
isRoughlyEqual(s.location.longitude, 11.626891, 0.001) isRoughlyEqual(s.location.longitude, 11.626891, 0.001)
); )
}; }
const assertIsMagdeburgHbf = (t, s) => { const assertIsMagdeburgHbf = (t, s) => {
t.equal(s.type, "station"); t.equal(s.type, 'station')
t.ok(s.id === "8010224" || s.id === "008010224", "id should be 8010224"); t.ok(s.id === '8010224' || s.id === '008010224', 'id should be 8010224')
t.equal(s.name, "Magdeburg Hbf"); t.equal(s.name, 'Magdeburg Hbf')
t.ok(s.location); t.ok(s.location)
t.ok(isRoughlyEqual(s.location.latitude, 52.130352, 0.001)); t.ok(isRoughlyEqual(s.location.latitude, 52.130352, 0.001))
t.ok(isRoughlyEqual(s.location.longitude, 11.626891, 0.001)); t.ok(isRoughlyEqual(s.location.longitude, 11.626891, 0.001))
}; }
// todo: this doesnt seem to work // todo: this doesnt seem to work
// todo: DRY with assertValidStationProducts // todo: DRY with assertValidStationProducts
const assertValidProducts = (t, p) => { const assertValidProducts = (t, p) => {
for (let k of Object.keys(products)) { for (let k of Object.keys(products)) {
t.ok("boolean", typeof products[k], "mode " + k + " must be a boolean"); t.ok('boolean', typeof products[k], 'mode ' + k + ' must be a boolean')
} }
}; }
const test = tapePromise(tape); const test = tapePromise(tape)
const client = createClient(insaProfile); const client = createClient(insaProfile)
test( test('Magdeburg Hbf to Magdeburg-Buckau', co(function*(t) {
"Magdeburg Hbf to Magdeburg-Buckau", const magdeburgHbf = '8010224'
co(function*(t) { const magdeburgBuckau = '8013456'
const magdeburgHbf = "8010224";
const magdeburgBuckau = "8013456";
const journeys = yield client.journeys(magdeburgHbf, magdeburgBuckau, { const journeys = yield client.journeys(magdeburgHbf, magdeburgBuckau, {
when, when,
passedStations: true passedStations: true
});
t.ok(Array.isArray(journeys));
t.ok(journeys.length > 0, "no journeys");
for (let journey of journeys) {
assertValidStation(t, journey.origin);
assertValidStationProducts(t, journey.origin.products);
if (journey.origin.products) {
assertValidProducts(t, journey.origin.products);
}
assertValidWhen(t, journey.departure, when);
assertValidStation(t, journey.destination);
assertValidStationProducts(t, journey.origin.products);
if (journey.destination.products) {
assertValidProducts(t, journey.destination.products);
}
assertValidWhen(t, journey.arrival, when);
t.ok(Array.isArray(journey.legs));
t.ok(journey.legs.length > 0, "no legs");
const leg = journey.legs[0];
assertValidStation(t, leg.origin);
assertValidStationProducts(t, leg.origin.products);
assertValidWhen(t, leg.departure, when);
t.equal(typeof leg.departurePlatform, "string");
assertValidStation(t, leg.destination);
assertValidStationProducts(t, leg.origin.products);
assertValidWhen(t, leg.arrival, when);
t.equal(typeof leg.arrivalPlatform, "string");
assertValidLine(t, leg.line);
t.ok(Array.isArray(leg.passed));
for (let stopover of leg.passed) assertValidStopover(t, stopover);
// if (journey.price) assertValidPrice(t, journey.price);
}
t.end();
}) })
);
test( t.ok(Array.isArray(journeys))
"Magdeburg Hbf to 39104 Magdeburg, Sternstr. 10", t.ok(journeys.length > 0, 'no journeys')
co(function*(t) { for (let journey of journeys) {
const magdeburgHbf = "8010224"; assertValidStation(t, journey.origin)
assertValidStationProducts(t, journey.origin.products)
if (journey.origin.products) {
assertValidProducts(t, journey.origin.products)
}
assertValidWhen(t, journey.departure, when)
assertValidStation(t, journey.destination)
assertValidStationProducts(t, journey.origin.products)
if (journey.destination.products) {
assertValidProducts(t, journey.destination.products)
}
assertValidWhen(t, journey.arrival, when)
t.ok(Array.isArray(journey.legs))
t.ok(journey.legs.length > 0, 'no legs')
const leg = journey.legs[0]
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure, when)
t.equal(typeof leg.departurePlatform, 'string')
assertValidStation(t, leg.destination)
assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, leg.arrival, when)
t.equal(typeof leg.arrivalPlatform, 'string')
assertValidLine(t, leg.line)
t.ok(Array.isArray(leg.passed))
for (let stopover of leg.passed) assertValidStopover(t, stopover)
// todo
// if (journey.price) assertValidPrice(t, journey.price)
}
t.end()
}))
test('Magdeburg Hbf to 39104 Magdeburg, Sternstr. 10', co(function*(t) {
const magdeburgHbf = '8010224'
const sternStr = { const sternStr = {
type: "location", type: 'location',
latitude: 52.118745, latitude: 52.118745,
longitude: 11.627117, longitude: 11.627117,
address: "39104 Magdeburg, Sternstr. 10" address: '39104 Magdeburg, Sternstr. 10'
}; }
const journeys = yield client.journeys(magdeburgHbf, sternStr, { const journeys = yield client.journeys(magdeburgHbf, sternStr, {
when when
});
t.ok(Array.isArray(journeys));
t.ok(journeys.length >= 1, "no journeys");
const journey = journeys[0];
const firstLeg = journey.legs[0];
const lastLeg = journey.legs[journey.legs.length - 1];
assertValidStation(t, firstLeg.origin);
assertValidStationProducts(t, firstLeg.origin.products);
if (firstLeg.origin.products)
assertValidProducts(t, firstLeg.origin.products);
assertValidWhen(t, firstLeg.departure, when);
assertValidWhen(t, firstLeg.arrival, when);
assertValidWhen(t, lastLeg.departure, when);
assertValidWhen(t, lastLeg.arrival, when);
const d = lastLeg.destination;
assertValidAddress(t, d);
t.equal(d.address, "39104 Magdeburg, Sternstr. 10");
t.ok(isRoughlyEqual(0.0001, d.latitude, 52.118745));
t.ok(isRoughlyEqual(0.0001, d.longitude, 11.627117));
t.end();
}) })
);
test( t.ok(Array.isArray(journeys))
"Kloster Unser Lieben Frauen to Magdeburg Hbf", t.ok(journeys.length >= 1, 'no journeys')
co(function*(t) { const journey = journeys[0]
const firstLeg = journey.legs[0]
const lastLeg = journey.legs[journey.legs.length - 1]
assertValidStation(t, firstLeg.origin)
assertValidStationProducts(t, firstLeg.origin.products)
if (firstLeg.origin.products)
assertValidProducts(t, firstLeg.origin.products)
assertValidWhen(t, firstLeg.departure, when)
assertValidWhen(t, firstLeg.arrival, when)
assertValidWhen(t, lastLeg.departure, when)
assertValidWhen(t, lastLeg.arrival, when)
const d = lastLeg.destination
assertValidAddress(t, d)
t.equal(d.address, '39104 Magdeburg, Sternstr. 10')
t.ok(isRoughlyEqual(0.0001, d.latitude, 52.118745))
t.ok(isRoughlyEqual(0.0001, d.longitude, 11.627117))
t.end()
}))
test('Kloster Unser Lieben Frauen to Magdeburg Hbf', co(function*(t) {
const kloster = { const kloster = {
type: "location", type: 'location',
latitude: 52.127601, latitude: 52.127601,
longitude: 11.636437, longitude: 11.636437,
name: "Magdeburg, Kloster Unser Lieben Frauen (Denkmal)", name: 'Magdeburg, Kloster Unser Lieben Frauen (Denkmal)',
id: "970012223" id: '970012223'
}; }
const magdeburgHbf = "8010224"; const magdeburgHbf = '8010224'
const journeys = yield client.journeys(kloster, magdeburgHbf, { const journeys = yield client.journeys(kloster, magdeburgHbf, {
when when
});
t.ok(Array.isArray(journeys));
t.ok(journeys.length >= 1, "no journeys");
const journey = journeys[0];
const firstLeg = journey.legs[0];
const lastLeg = journey.legs[journey.legs.length - 1];
const o = firstLeg.origin;
assertValidPoi(t, o);
t.equal(o.name, "Magdeburg, Kloster Unser Lieben Frauen (Denkmal)");
t.ok(isRoughlyEqual(0.0001, o.latitude, 52.127601));
t.ok(isRoughlyEqual(0.0001, o.longitude, 11.636437));
assertValidWhen(t, firstLeg.departure, when);
assertValidWhen(t, firstLeg.arrival, when);
assertValidWhen(t, lastLeg.departure, when);
assertValidWhen(t, lastLeg.arrival, when);
assertValidStation(t, lastLeg.destination);
assertValidStationProducts(t, lastLeg.destination.products);
if (lastLeg.destination.products)
assertValidProducts(t, lastLeg.destination.products);
t.end();
}) })
);
test( t.ok(Array.isArray(journeys))
"Magdeburg-Buckau to Magdeburg-Neustadt with stopover at Magdeburg Hbf", t.ok(journeys.length >= 1, 'no journeys')
co(function*(t) { const journey = journeys[0]
const magdeburgBuckau = "8013456"; const firstLeg = journey.legs[0]
const magdeburgNeustadt = "8010226"; const lastLeg = journey.legs[journey.legs.length - 1]
const magdeburgHbf = "8010224";
const [journey] = yield client.journeys( const o = firstLeg.origin
magdeburgBuckau, assertValidPoi(t, o)
magdeburgNeustadt, t.equal(o.name, 'Magdeburg, Kloster Unser Lieben Frauen (Denkmal)')
{ t.ok(isRoughlyEqual(0.0001, o.latitude, 52.127601))
t.ok(isRoughlyEqual(0.0001, o.longitude, 11.636437))
assertValidWhen(t, firstLeg.departure, when)
assertValidWhen(t, firstLeg.arrival, when)
assertValidWhen(t, lastLeg.departure, when)
assertValidWhen(t, lastLeg.arrival, when)
assertValidStation(t, lastLeg.destination)
assertValidStationProducts(t, lastLeg.destination.products)
if (lastLeg.destination.products)
assertValidProducts(t, lastLeg.destination.products)
t.end()
}))
test('Magdeburg-Buckau to Magdeburg-Neustadt with stopover at Magdeburg Hbf', co(function*(t) {
const magdeburgBuckau = '8013456'
const magdeburgNeustadt = '8010226'
const magdeburgHbf = '8010224'
const [journey] = yield client.journeys(magdeburgBuckau, magdeburgNeustadt, {
via: magdeburgHbf, via: magdeburgHbf,
results: 1, results: 1,
when when
}
);
const i1 = journey.legs.findIndex(
leg => leg.destination.id === magdeburgHbf
);
t.ok(i1 >= 0, "no leg with Magdeburg Hbf as destination");
const i2 = journey.legs.findIndex(
leg => leg.origin.id === magdeburgHbf
);
t.ok(i2 >= 0, "no leg with Magdeburg Hbf as origin");
t.ok(
i2 > i1,
"leg with Magdeburg Hbf as origin must be after leg to it"
);
t.end();
}) })
);
test( const i1 = journey.legs.findIndex(leg => leg.destination.id === magdeburgHbf)
"departures at Magdeburg Hbf", t.ok(i1 >= 0, 'no leg with Magdeburg Hbf as destination')
co(function*(t) {
const magdeburgHbf = "8010224"; const i2 = journey.legs.findIndex(leg => leg.origin.id === magdeburgHbf)
t.ok(i2 >= 0, 'no leg with Magdeburg Hbf as origin')
t.ok(i2 > i1, 'leg with Magdeburg Hbf as origin must be after leg to it')
t.end()
}))
test('departures at Magdeburg Hbf', co(function*(t) {
const magdeburgHbf = '8010224'
const deps = yield client.departures(magdeburgHbf, { const deps = yield client.departures(magdeburgHbf, {
duration: 5, duration: 5,
when when
}); })
t.ok(Array.isArray(deps)); t.ok(Array.isArray(deps))
for (let dep of deps) { for (let dep of deps) {
assertValidStation(t, dep.station); assertValidStation(t, dep.station)
assertValidStationProducts(t, dep.station.products); assertValidStationProducts(t, dep.station.products)
if (dep.station.products) if (dep.station.products) {
assertValidProducts(t, dep.station.products); assertValidProducts(t, dep.station.products)
assertValidWhen(t, dep.when, when); }
assertValidWhen(t, dep.when, when)
} }
t.end(); t.end()
}) }))
);
test( test('nearby Magdeburg Hbf', co(function*(t) {
"nearby Magdeburg Hbf",
co(function*(t) {
const magdeburgHbfPosition = { const magdeburgHbfPosition = {
type: "location", type: 'location',
latitude: 52.130352, latitude: 52.130352,
longitude: 11.626891 longitude: 11.626891
}; }
const nearby = yield client.nearby(magdeburgHbfPosition, { const nearby = yield client.nearby(magdeburgHbfPosition, {
results: 2, results: 2,
distance: 400 distance: 400
}); })
t.ok(Array.isArray(nearby)); t.ok(Array.isArray(nearby))
t.equal(nearby.length, 2); t.equal(nearby.length, 2)
assertIsMagdeburgHbf(t, nearby[0]); assertIsMagdeburgHbf(t, nearby[0])
t.ok(nearby[0].distance >= 0); t.ok(nearby[0].distance >= 0)
t.ok(nearby[0].distance <= 100); t.ok(nearby[0].distance <= 100)
for (let n of nearby) { for (let n of nearby) {
if (n.type === "station") assertValidStation(t, n); if (n.type === 'station') assertValidStation(t, n)
else assertValidLocation(t, n); else assertValidLocation(t, n)
} }
t.end(); t.end()
}) }))
);
test( test('locations named Magdeburg', co(function*(t) {
"locations named Magdeburg", const locations = yield client.locations('Magdeburg', {
co(function*(t) {
const locations = yield client.locations("Magdeburg", {
results: 10 results: 10
}); })
t.ok(Array.isArray(locations)); t.ok(Array.isArray(locations))
t.ok(locations.length > 0); t.ok(locations.length > 0)
t.ok(locations.length <= 10); t.ok(locations.length <= 10)
for (let l of locations) { for (let l of locations) {
if (l.type === "station") assertValidStation(t, l); if (l.type === 'station') assertValidStation(t, l)
else assertValidLocation(t, l); else assertValidLocation(t, l)
} }
t.ok(locations.some(isMagdeburgHbf)); t.ok(locations.some(isMagdeburgHbf))
t.end(); t.end()
}) }))
);
test( test('location', co(function*(t) {
"location", const magdeburgBuckau = '8013456'
co(function*(t) { const loc = yield client.location(magdeburgBuckau)
const magdeburgBuckau = "8013456";
const loc = yield client.location(magdeburgBuckau);
assertValidStation(t, loc); assertValidStation(t, loc)
t.equal(loc.id, magdeburgBuckau); t.equal(loc.id, magdeburgBuckau)
t.end(); t.end()
}) }))
);