formatters: add location, change address & poi

This commit is contained in:
Jannis R 2017-11-12 19:15:13 +01:00
parent f86f908dcc
commit c20fd35c67
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 33 additions and 12 deletions

View file

@ -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)
} }
} }
} }

View file

@ -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
View 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

View file

@ -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)
} }
} }
} }

View file

@ -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