mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
formatters: add location, change address & poi
This commit is contained in:
parent
f86f908dcc
commit
c20fd35c67
5 changed files with 33 additions and 12 deletions
|
@ -2,14 +2,16 @@
|
||||||
|
|
||||||
const formatCoord = require('./coord')
|
const formatCoord = require('./coord')
|
||||||
|
|
||||||
const formatAddress = (latitude, longitude, name) => {
|
const formatAddress = (a) => {
|
||||||
if (!latitude || !longitude || !name) throw new Error('invalid address.')
|
// todo: type-checking, better error msgs
|
||||||
|
if (!a.latitude || !a.longitude || !a.name) throw new Error('invalid address.')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'A',
|
type: 'A',
|
||||||
name,
|
name: a.name,
|
||||||
crd: {
|
crd: {
|
||||||
x: formatCoord(longitude),
|
x: formatCoord(a.longitude),
|
||||||
y: formatCoord(latitude)
|
y: formatCoord(a.latitude)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,5 +7,6 @@ module.exports = {
|
||||||
station: require('./station'),
|
station: require('./station'),
|
||||||
address: require('./address'),
|
address: require('./address'),
|
||||||
poi: require('./poi'),
|
poi: require('./poi'),
|
||||||
|
location: require('./location'),
|
||||||
locationFilter: require('./location-filter')
|
locationFilter: require('./location-filter')
|
||||||
}
|
}
|
||||||
|
|
14
format/location.js
Normal file
14
format/location.js
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const formatLocation = (profile, l) => {
|
||||||
|
if ('string' === typeof l) return profile.formatStation(l)
|
||||||
|
if ('object' === typeof l) {
|
||||||
|
if (l.type === 'station') return profile.formatStation(l.id)
|
||||||
|
if (l.type === 'poi') return profile.formatPoi(l)
|
||||||
|
if (l.type === 'address') return profile.formatAddress(l)
|
||||||
|
throw new Error('invalid location type: ' + l.type)
|
||||||
|
}
|
||||||
|
throw new Error('valid station, address or poi required.')
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = formatLocation
|
|
@ -2,15 +2,17 @@
|
||||||
|
|
||||||
const formatCoord = require('./coord')
|
const formatCoord = require('./coord')
|
||||||
|
|
||||||
const formatPoi = (latitude, longitude, id, name) => {
|
const formatPoi = (p) => {
|
||||||
if (!latitude || !longitude || !id || !name) throw new Error('invalid poi.')
|
// todo: type-checking, better error msgs
|
||||||
|
if (!p.latitude || !p.longitude || !p.id || !p.name) throw new Error('invalid poi.')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'P',
|
type: 'P',
|
||||||
name,
|
name: p.name,
|
||||||
lid: 'L=' + id,
|
lid: 'L=' + p.id,
|
||||||
crd: {
|
crd: {
|
||||||
x: formatCoord(longitude),
|
x: formatCoord(p.longitude),
|
||||||
y: formatCoord(latitude)
|
y: formatCoord(p.latitude)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ const formatLocationFilter = require('../format/location-filter')
|
||||||
const formatPoi = require('../format/poi')
|
const formatPoi = require('../format/poi')
|
||||||
const formatStation = require('../format/station')
|
const formatStation = require('../format/station')
|
||||||
const formatTime = require('../format/time')
|
const formatTime = require('../format/time')
|
||||||
|
const formatLocation = require('../format/location')
|
||||||
|
|
||||||
const id = x => x
|
const id = x => x
|
||||||
|
|
||||||
|
@ -45,7 +46,8 @@ const defaultProfile = {
|
||||||
formatLocationFilter,
|
formatLocationFilter,
|
||||||
formatPoi,
|
formatPoi,
|
||||||
formatStation,
|
formatStation,
|
||||||
formatTime
|
formatTime,
|
||||||
|
formatLocation
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = defaultProfile
|
module.exports = defaultProfile
|
||||||
|
|
Loading…
Add table
Reference in a new issue