mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
split stringify.js
This commit is contained in:
parent
73d083f418
commit
e6982753cd
11 changed files with 94 additions and 47 deletions
|
@ -7,7 +7,7 @@
|
|||
"index.js",
|
||||
"lib",
|
||||
"parse",
|
||||
"stringify.js"
|
||||
"stringify"
|
||||
],
|
||||
"author": "Jannis R <mail@jannisr.de>",
|
||||
"homepage": "https://github.com/derhuerst/hafas-client",
|
||||
|
|
46
stringify.js
46
stringify.js
|
@ -1,46 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const moment = require('moment-timezone')
|
||||
|
||||
|
||||
|
||||
const date = (tz, when) => moment(when).tz(tz).format('YYYYMMDD')
|
||||
const time = (tz, when) => moment(when).tz(tz).format('HHmmss')
|
||||
|
||||
|
||||
|
||||
// filters
|
||||
const bike = {type: 'BC', mode: 'INC'}
|
||||
const accessibility = {
|
||||
none: {type: 'META', mode: 'INC', meta: 'notBarrierfree'}
|
||||
, partial: {type: 'META', mode: 'INC', meta: 'limitedBarrierfree'}
|
||||
, complete: {type: 'META', mode: 'INC', meta: 'completeBarrierfree'}
|
||||
}
|
||||
|
||||
|
||||
|
||||
const coord = (x) => Math.round(x * 1000000)
|
||||
const station = (id) => ({type: 'S', lid: 'L=' + id})
|
||||
const address = (latitude, longitude, name) => {
|
||||
if (!latitude || !longitude || !name) throw new Error('invalid address.')
|
||||
return {type: 'A', name, crd: {x: coord(longitude), y: coord(latitude)}}
|
||||
}
|
||||
const poi = (latitude, longitude, id, name) => {
|
||||
if (!latitude || !longitude || !id || !name) throw new Error('invalid poi.')
|
||||
return {type: 'P', name, lid: 'L=' + id, crd: {x: coord(longitude), y: coord(latitude)}}
|
||||
}
|
||||
|
||||
const locationFilter = (stations, addresses, poi) => {
|
||||
if (stations && addresses && poi) return 'ALL'
|
||||
return (stations ? 'S' : '')
|
||||
+ (addresses ? 'A' : '')
|
||||
+ (poi ? 'P' : '')
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
date, time,
|
||||
bike, accessibility,
|
||||
coord, station, address, poi, locationFilter
|
||||
}
|
17
stringify/address.js
Normal file
17
stringify/address.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
'use strict'
|
||||
|
||||
const stringifyCoord = require('./coord')
|
||||
|
||||
const stringifyAddress = (latitude, longitude, name) => {
|
||||
if (!latitude || !longitude || !name) throw new Error('invalid address.')
|
||||
return {
|
||||
type: 'A',
|
||||
name,
|
||||
crd: {
|
||||
x: stringifyCoord(longitude),
|
||||
y: stringifyCoord(latitude)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = stringifyAddress
|
5
stringify/coord.js
Normal file
5
stringify/coord.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
'use strict'
|
||||
|
||||
const stringifyCoord = x => Math.round(x * 1000000)
|
||||
|
||||
module.exports = stringifyCoord
|
9
stringify/date.js
Normal file
9
stringify/date.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
'use strict'
|
||||
|
||||
const moment = require('moment-timezone')
|
||||
|
||||
const stringifyDate = (tz, when) => {
|
||||
moment(when).tz(tz).format('YYYYMMDD')
|
||||
}
|
||||
|
||||
module.exports = stringifyDate
|
11
stringify/filters.js
Normal file
11
stringify/filters.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
'use strict'
|
||||
|
||||
const bike = {type: 'BC', mode: 'INC'}
|
||||
|
||||
const accessibility = {
|
||||
none: {type: 'META', mode: 'INC', meta: 'notBarrierfree'},
|
||||
partial: {type: 'META', mode: 'INC', meta: 'limitedBarrierfree'},
|
||||
complete: {type: 'META', mode: 'INC', meta: 'completeBarrierfree'}
|
||||
}
|
||||
|
||||
module.exports = {bike, accessibility}
|
11
stringify/index.js
Normal file
11
stringify/index.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
date: require('./date'),
|
||||
time: require('./time'),
|
||||
filters: require('./filters'),
|
||||
station: require('./station'),
|
||||
address: require('./address'),
|
||||
poi: require('./poi'),
|
||||
locationFilter: require('./location-filter')
|
||||
}
|
8
stringify/location-filter.js
Normal file
8
stringify/location-filter.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
'use strict'
|
||||
|
||||
const stringifyLocationFilter = (stations, addresses, poi) => {
|
||||
if (stations && addresses && poi) return 'ALL'
|
||||
return (stations ? 'S' : '') + (addresses ? 'A' : '') + (poi ? 'P' : '')
|
||||
}
|
||||
|
||||
module.exports = stringifyLocationFilter
|
18
stringify/poi.js
Normal file
18
stringify/poi.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
'use strict'
|
||||
|
||||
const stringifyCoord = require('./coord')
|
||||
|
||||
const stringifyPoi = (latitude, longitude, id, name) => {
|
||||
if (!latitude || !longitude || !id || !name) throw new Error('invalid poi.')
|
||||
return {
|
||||
type: 'P',
|
||||
name,
|
||||
lid: 'L=' + id,
|
||||
crd: {
|
||||
x: stringifyCoord(longitude),
|
||||
y: stringifyCoord(latitude)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = stringifyPoi
|
5
stringify/station.js
Normal file
5
stringify/station.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
'use strict'
|
||||
|
||||
const stringifyStation = id => ({type: 'S', lid: 'L=' + id})
|
||||
|
||||
module.exports = stringifyStation
|
9
stringify/time.js
Normal file
9
stringify/time.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
'use strict'
|
||||
|
||||
const moment = require('moment-timezone')
|
||||
|
||||
const stringifyTime = (tz, when) => {
|
||||
return moment(when).tz(tz).format('HHmmss')
|
||||
}
|
||||
|
||||
module.exports = stringifyTime
|
Loading…
Add table
Reference in a new issue