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 formatAddress = (latitude, longitude, name) => {
if (!latitude || !longitude || !name) throw new Error('invalid address.')
const formatAddress = (a) => {
// todo: type-checking, better error msgs
if (!a.latitude || !a.longitude || !a.name) throw new Error('invalid address.')
return {
type: 'A',
name,
name: a.name,
crd: {
x: formatCoord(longitude),
y: formatCoord(latitude)
x: formatCoord(a.longitude),
y: formatCoord(a.latitude)
}
}
}

View file

@ -7,5 +7,6 @@ module.exports = {
station: require('./station'),
address: require('./address'),
poi: require('./poi'),
location: require('./location'),
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 formatPoi = (latitude, longitude, id, name) => {
if (!latitude || !longitude || !id || !name) throw new Error('invalid poi.')
const formatPoi = (p) => {
// todo: type-checking, better error msgs
if (!p.latitude || !p.longitude || !p.id || !p.name) throw new Error('invalid poi.')
return {
type: 'P',
name,
lid: 'L=' + id,
name: p.name,
lid: 'L=' + p.id,
crd: {
x: formatCoord(longitude),
y: formatCoord(latitude)
x: formatCoord(p.longitude),
y: formatCoord(p.latitude)
}
}
}

View file

@ -19,6 +19,7 @@ const formatLocationFilter = require('../format/location-filter')
const formatPoi = require('../format/poi')
const formatStation = require('../format/station')
const formatTime = require('../format/time')
const formatLocation = require('../format/location')
const id = x => x
@ -45,7 +46,8 @@ const defaultProfile = {
formatLocationFilter,
formatPoi,
formatStation,
formatTime
formatTime,
formatLocation
}
module.exports = defaultProfile