Skip to content
Snippets Groups Projects
Commit 1c09bbe8 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Merge branch 'feature_dimensions' into 'development'

Dimensions middleware

This middleware is used to get the dimensions in the query parameter `dims` as an array of dimensions.

EXAMPLE USAGE:

* Use it with no parameters to get all the dimensions in the query: `app.get('/', dimensions(), function(req, res, next){})`
* Use it with an array of accepted values: `app.get('/', dimensions(['year', 'location']), function(req, res, next){})`
* Use it globally: `app.use(dimensions())`

See merge request !9
parents dceb479c b7269e65
No related branches found
No related tags found
1 merge request!9Dimensions middleware
Pipeline #
/**
* Dimensions middleware
*
* EXAMPLE:
* Use it with no parameters to get all the dimensions specified
* app.get('/', dimensions(), function(req, res, next){})
*
* Use it with an array of accepted values
* app.get('/', dimensions(['year', 'location']), function(req, res, next){})
*
* Use it globally
* app.use(dimensions())
*/
function intersect(a, b) {
var t
if (b.length > a.length) t = b, b = a, a = t
return a.filter(function (e) {
if (b.indexOf(e) !== -1) return true
})
}
function dimensions(dims) {
return function(req, res, next) {
req.dims = {}
if(req.query.dims) {
var params = req.query.dims.split(",")
var dimObj = {}
for(var i=0; i<params.length; ++i) {
var kv = params[i].split(":")
dimObj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]
}
// If the dims array exists and is not empty
if(typeof dims !== 'undefined' && dims.length > 0) {
var intersection = intersect(dims, Object.keys(dimObj))
for(var i=0; i<intersection.length; ++i) {
req.dims[intersection[i]] = dimObj[intersection[i]]
}
} else {
req.dims = dimObj
}
}
console.log(req.dims)
next()
}
}
module.exports = dimensions
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment