From e6982753cd560a8b99c7e550b6b04f0d33de0e88 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 12 Nov 2017 00:45:51 +0100 Subject: [PATCH] split stringify.js --- package.json | 2 +- stringify.js | 46 ------------------------------------ stringify/address.js | 17 +++++++++++++ stringify/coord.js | 5 ++++ stringify/date.js | 9 +++++++ stringify/filters.js | 11 +++++++++ stringify/index.js | 11 +++++++++ stringify/location-filter.js | 8 +++++++ stringify/poi.js | 18 ++++++++++++++ stringify/station.js | 5 ++++ stringify/time.js | 9 +++++++ 11 files changed, 94 insertions(+), 47 deletions(-) delete mode 100644 stringify.js create mode 100644 stringify/address.js create mode 100644 stringify/coord.js create mode 100644 stringify/date.js create mode 100644 stringify/filters.js create mode 100644 stringify/index.js create mode 100644 stringify/location-filter.js create mode 100644 stringify/poi.js create mode 100644 stringify/station.js create mode 100644 stringify/time.js diff --git a/package.json b/package.json index 720b6374..01e7ae0d 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "index.js", "lib", "parse", - "stringify.js" + "stringify" ], "author": "Jannis R ", "homepage": "https://github.com/derhuerst/hafas-client", diff --git a/stringify.js b/stringify.js deleted file mode 100644 index e7063b0c..00000000 --- a/stringify.js +++ /dev/null @@ -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 -} diff --git a/stringify/address.js b/stringify/address.js new file mode 100644 index 00000000..19edd8e9 --- /dev/null +++ b/stringify/address.js @@ -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 diff --git a/stringify/coord.js b/stringify/coord.js new file mode 100644 index 00000000..67a4d9cb --- /dev/null +++ b/stringify/coord.js @@ -0,0 +1,5 @@ +'use strict' + +const stringifyCoord = x => Math.round(x * 1000000) + +module.exports = stringifyCoord diff --git a/stringify/date.js b/stringify/date.js new file mode 100644 index 00000000..0649e487 --- /dev/null +++ b/stringify/date.js @@ -0,0 +1,9 @@ +'use strict' + +const moment = require('moment-timezone') + +const stringifyDate = (tz, when) => { + moment(when).tz(tz).format('YYYYMMDD') +} + +module.exports = stringifyDate diff --git a/stringify/filters.js b/stringify/filters.js new file mode 100644 index 00000000..45a17f7c --- /dev/null +++ b/stringify/filters.js @@ -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} diff --git a/stringify/index.js b/stringify/index.js new file mode 100644 index 00000000..90e2831b --- /dev/null +++ b/stringify/index.js @@ -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') +} diff --git a/stringify/location-filter.js b/stringify/location-filter.js new file mode 100644 index 00000000..a147b14b --- /dev/null +++ b/stringify/location-filter.js @@ -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 diff --git a/stringify/poi.js b/stringify/poi.js new file mode 100644 index 00000000..f0d9cb32 --- /dev/null +++ b/stringify/poi.js @@ -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 diff --git a/stringify/station.js b/stringify/station.js new file mode 100644 index 00000000..09d916e1 --- /dev/null +++ b/stringify/station.js @@ -0,0 +1,5 @@ +'use strict' + +const stringifyStation = id => ({type: 'S', lid: 'L=' + id}) + +module.exports = stringifyStation diff --git a/stringify/time.js b/stringify/time.js new file mode 100644 index 00000000..a50e4f29 --- /dev/null +++ b/stringify/time.js @@ -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