split stringify.js

This commit is contained in:
Jannis R 2017-11-12 00:45:51 +01:00
parent 73d083f418
commit e6982753cd
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
11 changed files with 94 additions and 47 deletions

View file

@ -7,7 +7,7 @@
"index.js", "index.js",
"lib", "lib",
"parse", "parse",
"stringify.js" "stringify"
], ],
"author": "Jannis R <mail@jannisr.de>", "author": "Jannis R <mail@jannisr.de>",
"homepage": "https://github.com/derhuerst/hafas-client", "homepage": "https://github.com/derhuerst/hafas-client",

View file

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

@ -0,0 +1,5 @@
'use strict'
const stringifyCoord = x => Math.round(x * 1000000)
module.exports = stringifyCoord

9
stringify/date.js Normal file
View 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
View 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
View 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')
}

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

@ -0,0 +1,5 @@
'use strict'
const stringifyStation = id => ({type: 'S', lid: 'L=' + id})
module.exports = stringifyStation

9
stringify/time.js Normal file
View 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