diff --git a/src/libs/middlewares/dimensions.js b/src/libs/middlewares/dimensions.js index c17e4a6d2e00c1e5dc47455f4959cf9f33a0ad09..8532425cf107ee9f19b13009cdbbdc365e3ebbc3 100644 --- a/src/libs/middlewares/dimensions.js +++ b/src/libs/middlewares/dimensions.js @@ -13,36 +13,40 @@ */ 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 - }) + let t; + if (b.length > a.length) { + t = b; b = a; a = t; + } + return a.filter((e) => b.indexOf(e) !== -1); } 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] + return (req, res, next) => { + req.dims = {}; + if (req.query.dims) { + const params = req.query.dims.split(','); + const dimObj = {}; + for (const param of params) { + const kv = param.split(':'); + dimObj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]; } + // for(let i=0; i<params.length; ++i) { + // let 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]] + if (typeof dims !== 'undefined' && dims.length > 0) { + const intersection = intersect(dims, Object.keys(dimObj)); + for (let i = 0; i < intersection.length; ++i) { + req.dims[intersection[i]] = dimObj[intersection[i]]; } } else { - req.dims = dimObj + req.dims = dimObj; } } - console.log(req.dims) - next() - } + next(); + }; } -module.exports = dimensions +module.exports = dimensions;