From ed0dfd06dd5a4a1096545e97bf00686b8153d3c7 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 27 Nov 2017 19:39:18 +0100 Subject: [PATCH] pull out bitmask parsing from profiles --- format/products-bitmask.js | 14 ++++++++++++++ p/db/index.js | 8 ++++++-- p/db/modes.js | 19 ------------------- p/vbb/index.js | 8 ++++++-- p/vbb/modes.js | 19 ------------------- parse/products-bitmask.js | 16 ++++++++++++++++ 6 files changed, 42 insertions(+), 42 deletions(-) create mode 100644 format/products-bitmask.js create mode 100644 parse/products-bitmask.js diff --git a/format/products-bitmask.js b/format/products-bitmask.js new file mode 100644 index 00000000..9ec158e0 --- /dev/null +++ b/format/products-bitmask.js @@ -0,0 +1,14 @@ +'use strict' + +const createFormatBitmask = (modes) => { + const formatBitmask = (products) => { + let bitmask = 0 + for (let product in products) { + if (products[product] === true) bitmask += modes[product].bitmask + } + return bitmask + } + return formatBitmask +} + +module.exports = createFormatBitmask diff --git a/p/db/index.js b/p/db/index.js index 6cf75c8a..c68ea256 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -4,11 +4,15 @@ const crypto = require('crypto') const _formatStation = require('../../format/station') const _parseLine = require('../../parse/line') +const createParseBitmask = require('../../parse/products-bitmask') +const createFormatBitmask = require('../../format/products-bitmask') const {accessibility, bike} = require('../../format/filters') const modes = require('./modes') const formatLoyaltyCard = require('./loyalty-cards').format +const formatBitmask = createFormatBitmask(modes) + const transformReqBody = (body) => { body.client = {id: 'DB', v: '16040000', type: 'IPH', name: 'DB Navigator'} body.ext = 'DB.R15.12.a' @@ -87,7 +91,7 @@ const formatProducts = (products) => { return { type: 'PROD', mode: 'INC', - value: modes.stringifyBitmask(products) + '' + value: formatBitmask(products) + '' } } @@ -102,7 +106,7 @@ const dbProfile = { // todo: parseLocation parseLine, - parseProducts: modes.parseBitmask, + parseProducts: createParseBitmask(modes.bitmasks), formatStation, formatProducts diff --git a/p/db/modes.js b/p/db/modes.js index 3b0a6a94..9e91d505 100644 --- a/p/db/modes.js +++ b/p/db/modes.js @@ -91,23 +91,4 @@ m.bitmasks[128] = m.subway m.bitmasks[256] = m.tram m.bitmasks[512] = m.taxi -// todo: move up -m.stringifyBitmask = (products) => { - let bitmask = 0 - for (let product in products) { - if (products[product] === true) bitmask += m[product].bitmask - } - return bitmask -} - -// todo: move up -m.parseBitmask = (bitmask) => { - let products = {}, i = 1 - do { - products[m.bitmasks[i].product] = !!(bitmask & i) - i *= 2 - } while (m.bitmasks[i] && m.bitmasks[i].product) - return products -} - module.exports = m diff --git a/p/vbb/index.js b/p/vbb/index.js index 973d531a..f16e84ba 100644 --- a/p/vbb/index.js +++ b/p/vbb/index.js @@ -6,9 +6,13 @@ const {to12Digit, to9Digit} = require('vbb-translate-ids') const _formatStation = require('../../format/station') const _parseLine = require('../../parse/line') const _parseLocation = require('../../parse/location') +const createParseBitmask = require('../../parse/products-bitmask') +const createFormatBitmask = require('../../format/products-bitmask') const modes = require('./modes') +const formatBitmask = createFormatBitmask(modes) + const transformReqBody = (body) => { body.client = {type: 'IPA', id: 'BVG'} body.ext = 'VBB.2' @@ -67,7 +71,7 @@ const formatProducts = (products) => { return { type: 'PROD', mode: 'INC', - value: modes.stringifyBitmask(products) + '' + value: formatBitmask(products) + '' } } @@ -79,7 +83,7 @@ const vbbProfile = { parseStationName: shorten, parseLocation, parseLine, - parseProducts: modes.parseBitmask, + parseProducts: createParseBitmask(modes.bitmasks), formatStation, formatProducts diff --git a/p/vbb/modes.js b/p/vbb/modes.js index 9b78cfd9..0ce23156 100644 --- a/p/vbb/modes.js +++ b/p/vbb/modes.js @@ -123,23 +123,4 @@ m.categories = [ // return m.categories[parseInt(category)] || m.unknown // } -// todo: move up -m.stringifyBitmask = (types) => { - let bitmask = 0 - for (let type in types) { - if (types[type] === true) bitmask += m[type].bitmask - } - return bitmask -} - -// todo: move up -m.parseBitmask = (bitmask) => { - let types = {}, i = 1 - do { - types[m.bitmasks[i].type] = !!(bitmask & i) - i *= 2 - } while (m.bitmasks[i] && m.bitmasks[i].type) - return types -} - module.exports = m diff --git a/parse/products-bitmask.js b/parse/products-bitmask.js new file mode 100644 index 00000000..6f31530c --- /dev/null +++ b/parse/products-bitmask.js @@ -0,0 +1,16 @@ +'use strict' + +const createParseBitmask = (bitmasks) => { + const createBitmask = (bitmask) => { + const products = {} + let i = 1 + do { + products[bitmasks[i].product] = !!(bitmask & i) + i *= 2 + } while (bitmasks[i] && bitmasks[i].product) + return products + } + return createBitmask +} + +module.exports = createParseBitmask