use coroutine from Babel

This commit is contained in:
Jannis R 2018-01-23 01:49:41 +01:00
parent 0ee53ff624
commit f19a66df8a
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 64 additions and 34 deletions

View file

@ -45,7 +45,6 @@
"vbb-translate-ids": "^3.1.0" "vbb-translate-ids": "^3.1.0"
}, },
"devDependencies": { "devDependencies": {
"co": "^4.6.0",
"db-stations": "^1.34.0", "db-stations": "^1.34.0",
"is-coordinates": "^2.0.2", "is-coordinates": "^2.0.2",
"is-roughly-equal": "^0.1.0", "is-roughly-equal": "^0.1.0",

31
test/co.js Normal file
View file

@ -0,0 +1,31 @@
// https://github.com/babel/babel/blob/3c8d831fe41f502cbe2459a271d19c7329ffe369/packages/babel-helpers/src/helpers.js#L242-L270
const co = (fn) => {
return function run () {
const self = this, args = arguments
return new Promise((resolve, reject) => {
const gen = fn.apply(self, args)
const step = (key, arg) => {
try {
var info = gen[key](arg)
var value = info.value
} catch (error) {
reject(error)
return
}
if (info.done) resolve(value)
else Promise.resolve(value).then(_next, _throw)
}
const _next = (value) => {
step('next', value)
}
const _throw = (err) => {
step('throw', err)
}
_next()
})
}
}
module.exports = co

View file

@ -3,9 +3,9 @@
const getStations = require('db-stations').full const getStations = require('db-stations').full
const tapePromise = require('tape-promise').default const tapePromise = require('tape-promise').default
const tape = require('tape') const tape = require('tape')
const co = require('co')
const isRoughlyEqual = require('is-roughly-equal') const isRoughlyEqual = require('is-roughly-equal')
const co = require('./co')
const createClient = require('..') const createClient = require('..')
const dbProfile = require('../p/db') const dbProfile = require('../p/db')
const modes = require('../p/db/modes') const modes = require('../p/db/modes')
@ -92,7 +92,7 @@ const assertValidPrice = (t, p) => {
const test = tapePromise(tape) const test = tapePromise(tape)
const client = createClient(dbProfile) const client = createClient(dbProfile)
test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) { test('Berlin Jungfernheide to München Hbf', co(function* (t) {
const journeys = yield client.journeys('8011167', '8000261', { const journeys = yield client.journeys('8011167', '8000261', {
when, passedStations: true when, passedStations: true
}) })
@ -151,7 +151,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('Berlin Jungfernheide to Torfstraße 17', co.wrap(function* (t) { test('Berlin Jungfernheide to Torfstraße 17', co(function* (t) {
const journeys = yield client.journeys('8011167', { const journeys = yield client.journeys('8011167', {
type: 'location', address: 'Torfstraße 17', type: 'location', address: 'Torfstraße 17',
latitude: 52.5416823, longitude: 13.3491223 latitude: 52.5416823, longitude: 13.3491223
@ -180,7 +180,7 @@ test('Berlin Jungfernheide to Torfstraße 17', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('Berlin Jungfernheide to ATZE Musiktheater', co.wrap(function* (t) { test('Berlin Jungfernheide to ATZE Musiktheater', co(function* (t) {
const journeys = yield client.journeys('8011167', { const journeys = yield client.journeys('8011167', {
type: 'location', id: '991598902', name: 'ATZE Musiktheater', type: 'location', id: '991598902', name: 'ATZE Musiktheater',
latitude: 52.542417, longitude: 13.350437 latitude: 52.542417, longitude: 13.350437
@ -209,7 +209,7 @@ test('Berlin Jungfernheide to ATZE Musiktheater', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('Berlin Hbf to München Hbf with stopover at Hannover Hbf', co.wrap(function* (t) { test('Berlin Hbf to München Hbf with stopover at Hannover Hbf', co(function* (t) {
const berlinHbf = '8011160' const berlinHbf = '8011160'
const münchenHbf = '8000261' const münchenHbf = '8000261'
const hannoverHbf = '8000152' const hannoverHbf = '8000152'
@ -228,7 +228,7 @@ test('Berlin Hbf to München Hbf with stopover at Hannover Hbf', co.wrap(functio
t.end() t.end()
})) }))
test('departures at Berlin Jungfernheide', co.wrap(function* (t) { test('departures at Berlin Jungfernheide', co(function* (t) {
const deps = yield client.departures('8011167', { const deps = yield client.departures('8011167', {
duration: 5, when duration: 5, when
}) })
@ -247,7 +247,7 @@ test('departures at Berlin Jungfernheide', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('departures with station object', co.wrap(function* (t) { test('departures with station object', co(function* (t) {
yield client.departures({ yield client.departures({
type: 'station', type: 'station',
id: '8011167', id: '8011167',
@ -263,7 +263,7 @@ test('departures with station object', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('nearby Berlin Jungfernheide', co.wrap(function* (t) { test('nearby Berlin Jungfernheide', co(function* (t) {
const nearby = yield client.nearby({ const nearby = yield client.nearby({
type: 'location', type: 'location',
latitude: 52.530273, latitude: 52.530273,
@ -287,7 +287,7 @@ test('nearby Berlin Jungfernheide', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('locations named Jungfernheide', co.wrap(function* (t) { test('locations named Jungfernheide', co(function* (t) {
const locations = yield client.locations('Jungfernheide', { const locations = yield client.locations('Jungfernheide', {
results: 10 results: 10
}) })

View file

@ -4,12 +4,12 @@
// const getStations = require('db-stations').full // const getStations = require('db-stations').full
const tapePromise = require('tape-promise').default const tapePromise = require('tape-promise').default
const tape = require('tape') const tape = require('tape')
const co = require('co')
const isRoughlyEqual = require('is-roughly-equal') const isRoughlyEqual = require('is-roughly-equal')
const validateFptf = require('validate-fptf') const validateFptf = require('validate-fptf')
const validateLineWithoutMode = require('./validate-line-without-mode') const validateLineWithoutMode = require('./validate-line-without-mode')
const co = require('./co')
const createClient = require('..') const createClient = require('..')
const oebbProfile = require('../p/oebb') const oebbProfile = require('../p/oebb')
const products = require('../p/oebb/products') const products = require('../p/oebb/products')
@ -110,7 +110,7 @@ const assertValidLine = (t, l) => { // with optional mode
const test = tapePromise(tape) const test = tapePromise(tape)
const client = createClient(oebbProfile) const client = createClient(oebbProfile)
test('Salzburg Hbf to Wien Westbahnhof', co.wrap(function* (t) { test('Salzburg Hbf to Wien Westbahnhof', co(function* (t) {
const salzburgHbf = '8100002' const salzburgHbf = '8100002'
const wienWestbahnhof = '1291501' const wienWestbahnhof = '1291501'
const journeys = yield client.journeys(salzburgHbf, wienWestbahnhof, { const journeys = yield client.journeys(salzburgHbf, wienWestbahnhof, {
@ -175,7 +175,7 @@ test('Salzburg Hbf to Wien Westbahnhof', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('Salzburg Hbf to 1220 Wien, Wagramer Straße 5', co.wrap(function* (t) { test('Salzburg Hbf to 1220 Wien, Wagramer Straße 5', co(function* (t) {
const salzburgHbf = '8100002' const salzburgHbf = '8100002'
const wagramerStr = { const wagramerStr = {
type: 'location', type: 'location',
@ -213,7 +213,7 @@ test('Salzburg Hbf to 1220 Wien, Wagramer Straße 5', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('Albertina to Salzburg Hbf', co.wrap(function* (t) { test('Albertina to Salzburg Hbf', co(function* (t) {
const albertina = { const albertina = {
type: 'location', type: 'location',
latitude: 48.204699, latitude: 48.204699,
@ -252,7 +252,7 @@ test('Albertina to Salzburg Hbf', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('Wien to Klagenfurt Hbf with stopover at Salzburg Hbf', co.wrap(function* (t) { test('Wien to Klagenfurt Hbf with stopover at Salzburg Hbf', co(function* (t) {
const wien = '1190100' const wien = '1190100'
const klagenfurtHbf = '8100085' const klagenfurtHbf = '8100085'
const salzburgHbf = '8100002' const salzburgHbf = '8100002'
@ -271,7 +271,7 @@ test('Wien to Klagenfurt Hbf with stopover at Salzburg Hbf', co.wrap(function* (
t.end() t.end()
})) }))
test('leg details for Wien Westbahnhof to München Hbf', co.wrap(function* (t) { test('leg details for Wien Westbahnhof to München Hbf', co(function* (t) {
const wienWestbahnhof = '1291501' const wienWestbahnhof = '1291501'
const muenchenHbf = '8000261' const muenchenHbf = '8000261'
const journeys = yield client.journeys(wienWestbahnhof, muenchenHbf, { const journeys = yield client.journeys(wienWestbahnhof, muenchenHbf, {
@ -297,7 +297,7 @@ test('leg details for Wien Westbahnhof to München Hbf', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('departures at Salzburg Hbf', co.wrap(function* (t) { test('departures at Salzburg Hbf', co(function* (t) {
const salzburgHbf = '8100002' const salzburgHbf = '8100002'
const deps = yield client.departures(salzburgHbf, { const deps = yield client.departures(salzburgHbf, {
duration: 5, when duration: 5, when
@ -318,7 +318,7 @@ test('departures at Salzburg Hbf', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('nearby Salzburg Hbf', co.wrap(function* (t) { test('nearby Salzburg Hbf', co(function* (t) {
const salzburgHbfPosition = { const salzburgHbfPosition = {
type: 'location', type: 'location',
longitude: 13.045604, longitude: 13.045604,
@ -343,7 +343,7 @@ test('nearby Salzburg Hbf', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('locations named Salzburg', co.wrap(function* (t) { test('locations named Salzburg', co(function* (t) {
const locations = yield client.locations('Salzburg', { const locations = yield client.locations('Salzburg', {
results: 10 results: 10
}) })
@ -361,7 +361,7 @@ test('locations named Salzburg', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('radar Salzburg', co.wrap(function* (t) { test('radar Salzburg', co(function* (t) {
const vehicles = yield client.radar(47.827203, 13.001261, 47.773278, 13.07562, { const vehicles = yield client.radar(47.827203, 13.001261, 47.773278, 13.07562, {
duration: 5 * 60, when duration: 5 * 60, when
}) })

View file

@ -5,9 +5,9 @@ const isRoughlyEqual = require('is-roughly-equal')
const stations = require('vbb-stations-autocomplete') const stations = require('vbb-stations-autocomplete')
const tapePromise = require('tape-promise').default const tapePromise = require('tape-promise').default
const tape = require('tape') const tape = require('tape')
const co = require('co')
const shorten = require('vbb-short-station-name') const shorten = require('vbb-short-station-name')
const co = require('./co')
const createClient = require('..') const createClient = require('..')
const vbbProfile = require('../p/vbb') const vbbProfile = require('../p/vbb')
const { const {
@ -59,7 +59,7 @@ const amrumerStr = '900000009101'
const spichernstr = '900000042101' const spichernstr = '900000042101'
const bismarckstr = '900000024201' const bismarckstr = '900000024201'
test('journeys  station to station', co.wrap(function* (t) { test('journeys  station to station', co(function* (t) {
const journeys = yield client.journeys(spichernstr, amrumerStr, { const journeys = yield client.journeys(spichernstr, amrumerStr, {
results: 3, when, passedStations: true results: 3, when, passedStations: true
}) })
@ -112,7 +112,7 @@ test('journeys  station to station', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('journeys  only subway', co.wrap(function* (t) { test('journeys  only subway', co(function* (t) {
const journeys = yield client.journeys(spichernstr, bismarckstr, { const journeys = yield client.journeys(spichernstr, bismarckstr, {
results: 20, when, results: 20, when,
products: { products: {
@ -141,7 +141,7 @@ test('journeys  only subway', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('journeys  fails with no product', co.wrap(function* (t) { test('journeys  fails with no product', co(function* (t) {
try { try {
yield client.journeys(spichernstr, bismarckstr, { yield client.journeys(spichernstr, bismarckstr, {
when, when,
@ -161,7 +161,7 @@ test('journeys  fails with no product', co.wrap(function* (t) {
} }
})) }))
test('journey leg details', co.wrap(function* (t) { test('journey leg details', co(function* (t) {
const journeys = yield client.journeys(spichernstr, amrumerStr, { const journeys = yield client.journeys(spichernstr, amrumerStr, {
results: 1, when results: 1, when
}) })
@ -187,7 +187,7 @@ test('journey leg details', co.wrap(function* (t) {
test('journeys  station to address', co.wrap(function* (t) { test('journeys  station to address', co(function* (t) {
const journeys = yield client.journeys(spichernstr, { const journeys = yield client.journeys(spichernstr, {
type: 'location', address: 'Torfstraße 17', type: 'location', address: 'Torfstraße 17',
latitude: 52.5416823, longitude: 13.3491223 latitude: 52.5416823, longitude: 13.3491223
@ -214,7 +214,7 @@ test('journeys  station to address', co.wrap(function* (t) {
test('journeys  station to POI', co.wrap(function* (t) { test('journeys  station to POI', co(function* (t) {
const journeys = yield client.journeys(spichernstr, { const journeys = yield client.journeys(spichernstr, {
type: 'location', id: '9980720', name: 'ATZE Musiktheater', type: 'location', id: '9980720', name: 'ATZE Musiktheater',
latitude: 52.543333, longitude: 13.351686 latitude: 52.543333, longitude: 13.351686
@ -241,7 +241,7 @@ test('journeys  station to POI', co.wrap(function* (t) {
test('journeys with stopover', co.wrap(function* (t) { test('journeys with stopover', co(function* (t) {
const halleschesTor = '900000012103' const halleschesTor = '900000012103'
const leopoldplatz = '900000009102' const leopoldplatz = '900000009102'
const [journey] = yield client.journeys(spichernstr, halleschesTor, { const [journey] = yield client.journeys(spichernstr, halleschesTor, {
@ -261,7 +261,7 @@ test('journeys with stopover', co.wrap(function* (t) {
test('departures', co.wrap(function* (t) { test('departures', co(function* (t) {
const deps = yield client.departures(spichernstr, {duration: 5, when}) const deps = yield client.departures(spichernstr, {duration: 5, when})
t.ok(Array.isArray(deps)) t.ok(Array.isArray(deps))
@ -282,7 +282,7 @@ test('departures', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('departures with station object', co.wrap(function* (t) { test('departures with station object', co(function* (t) {
yield client.departures({ yield client.departures({
type: 'station', type: 'station',
id: spichernstr, id: spichernstr,
@ -298,7 +298,7 @@ test('departures with station object', co.wrap(function* (t) {
t.end() t.end()
})) }))
test('departures at 7-digit station', co.wrap(function* (t) { test('departures at 7-digit station', co(function* (t) {
const eisenach = '8010097' // see derhuerst/vbb-hafas#22 const eisenach = '8010097' // see derhuerst/vbb-hafas#22
yield client.departures(eisenach, {when}) yield client.departures(eisenach, {when})
t.pass('did not fail') t.pass('did not fail')
@ -308,7 +308,7 @@ test('departures at 7-digit station', co.wrap(function* (t) {
test('nearby', co.wrap(function* (t) { test('nearby', co(function* (t) {
// Berliner Str./Bundesallee // Berliner Str./Bundesallee
const nearby = yield client.nearby({ const nearby = yield client.nearby({
type: 'location', type: 'location',
@ -337,7 +337,7 @@ test('nearby', co.wrap(function* (t) {
test('locations', co.wrap(function* (t) { test('locations', co(function* (t) {
const locations = yield client.locations('Alexanderplatz', {results: 10}) const locations = yield client.locations('Alexanderplatz', {results: 10})
t.ok(Array.isArray(locations)) t.ok(Array.isArray(locations))
@ -356,7 +356,7 @@ test('locations', co.wrap(function* (t) {
test('radar', co.wrap(function* (t) { test('radar', co(function* (t) {
const vehicles = yield client.radar(52.52411, 13.41002, 52.51942, 13.41709, { const vehicles = yield client.radar(52.52411, 13.41002, 52.51942, 13.41709, {
duration: 5 * 60, when duration: 5 * 60, when
}) })