mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
validate more input
This commit is contained in:
parent
eb2acaded0
commit
ab80e81ccc
2 changed files with 16 additions and 5 deletions
|
@ -1,11 +1,12 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const createFormatBitmask = (modes) => {
|
const createFormatBitmask = (allProducts) => {
|
||||||
const formatBitmask = (products) => {
|
const formatBitmask = (products) => {
|
||||||
if(Object.keys(products).length === 0) throw new Error('products filter must not be empty')
|
if(Object.keys(products).length === 0) throw new Error('products filter must not be empty')
|
||||||
let bitmask = 0
|
let bitmask = 0
|
||||||
for (let product in products) {
|
for (let product in products) {
|
||||||
if (products[product] === true) bitmask += modes[product].bitmask
|
if (!allProducts[product]) throw new Error('unknown product ' + product)
|
||||||
|
if (products[product] === true) bitmask += allProducts[product].bitmask
|
||||||
}
|
}
|
||||||
return bitmask
|
return bitmask
|
||||||
}
|
}
|
||||||
|
|
16
index.js
16
index.js
|
@ -7,12 +7,14 @@ const validateProfile = require('./lib/validate-profile')
|
||||||
const defaultProfile = require('./lib/default-profile')
|
const defaultProfile = require('./lib/default-profile')
|
||||||
const _request = require('./lib/request')
|
const _request = require('./lib/request')
|
||||||
|
|
||||||
|
const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o)
|
||||||
|
|
||||||
const createClient = (profile, request = _request) => {
|
const createClient = (profile, request = _request) => {
|
||||||
profile = Object.assign({}, defaultProfile, profile)
|
profile = Object.assign({}, defaultProfile, profile)
|
||||||
validateProfile(profile)
|
validateProfile(profile)
|
||||||
|
|
||||||
const departures = (station, opt = {}) => {
|
const departures = (station, opt = {}) => {
|
||||||
if ('object' === typeof station) station = profile.formatStation(station.id)
|
if (isObj(station)) station = profile.formatStation(station.id)
|
||||||
else if ('string' === typeof station) station = profile.formatStation(station)
|
else if ('string' === typeof station) station = profile.formatStation(station)
|
||||||
else throw new Error('station must be an object or a string.')
|
else throw new Error('station must be an object or a string.')
|
||||||
|
|
||||||
|
@ -108,7 +110,9 @@ const createClient = (profile, request = _request) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const locations = (query, opt = {}) => {
|
const locations = (query, opt = {}) => {
|
||||||
if ('string' !== typeof query) throw new Error('query must be a string.')
|
if ('string' !== typeof query || !query) {
|
||||||
|
throw new Error('query must be a non-empty string.')
|
||||||
|
}
|
||||||
opt = Object.assign({
|
opt = Object.assign({
|
||||||
fuzzy: true, // find only exact matches?
|
fuzzy: true, // find only exact matches?
|
||||||
results: 10, // how many search results?
|
results: 10, // how many search results?
|
||||||
|
@ -158,7 +162,7 @@ const createClient = (profile, request = _request) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const nearby = (location, opt = {}) => {
|
const nearby = (location, opt = {}) => {
|
||||||
if ('object' !== typeof location || Array.isArray(location)) {
|
if (!isObj(location)) {
|
||||||
throw new Error('location must be an object.')
|
throw new Error('location must be an object.')
|
||||||
} else if (location.type !== 'location') {
|
} else if (location.type !== 'location') {
|
||||||
throw new Error('invalid location object.')
|
throw new Error('invalid location object.')
|
||||||
|
@ -200,6 +204,12 @@ const createClient = (profile, request = _request) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const journeyLeg = (ref, lineName, opt = {}) => {
|
const journeyLeg = (ref, lineName, opt = {}) => {
|
||||||
|
if ('string' !== typeof ref || !ref) {
|
||||||
|
throw new Error('ref must be a non-empty string.')
|
||||||
|
}
|
||||||
|
if ('string' !== typeof lineName || !lineName) {
|
||||||
|
throw new Error('lineName must be a non-empty string.')
|
||||||
|
}
|
||||||
opt = Object.assign({
|
opt = Object.assign({
|
||||||
passedStations: true // return stations on the way?
|
passedStations: true // return stations on the way?
|
||||||
}, opt)
|
}, opt)
|
||||||
|
|
Loading…
Add table
Reference in a new issue