diff --git a/format/address.js b/format/address.js index 62ebe39f..b629ecc6 100644 --- a/format/address.js +++ b/format/address.js @@ -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) } } } diff --git a/format/index.js b/format/index.js index 90e2831b..75f69239 100644 --- a/format/index.js +++ b/format/index.js @@ -7,5 +7,6 @@ module.exports = { station: require('./station'), address: require('./address'), poi: require('./poi'), + location: require('./location'), locationFilter: require('./location-filter') } diff --git a/format/location.js b/format/location.js new file mode 100644 index 00000000..3401e32e --- /dev/null +++ b/format/location.js @@ -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 diff --git a/format/poi.js b/format/poi.js index 4af29ea9..cef0c55a 100644 --- a/format/poi.js +++ b/format/poi.js @@ -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) } } } diff --git a/lib/default-profile.js b/lib/default-profile.js index 66995dba..c5257b82 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -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