From 1d54fe24c01fc3c4a0c4cb2a4c880be04cd20a05 Mon Sep 17 00:00:00 2001 From: Vytor Calixto <vytorcalixto@gmail.com> Date: Wed, 21 Sep 2016 10:35:47 -0300 Subject: [PATCH] Change dimensions middleware to parseParams mw Related: simcaq/SCRUM#18 --- src/libs/middlewares/dimensions.js | 58 -------------------------- src/libs/middlewares/parseParams.js | 64 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 58 deletions(-) delete mode 100644 src/libs/middlewares/dimensions.js create mode 100644 src/libs/middlewares/parseParams.js diff --git a/src/libs/middlewares/dimensions.js b/src/libs/middlewares/dimensions.js deleted file mode 100644 index 618a510e..00000000 --- a/src/libs/middlewares/dimensions.js +++ /dev/null @@ -1,58 +0,0 @@ -/** -* 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()) -*/ - -/** - * This function returns the intersection of two arrays - * @param {array} a [description] - * @param {array} b [description] - * @return {array} [description] - */ -function intersect(a, b) { - 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 (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) { - 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; - } - } - next(); - }; -} - -module.exports = dimensions; diff --git a/src/libs/middlewares/parseParams.js b/src/libs/middlewares/parseParams.js new file mode 100644 index 00000000..b221ef5d --- /dev/null +++ b/src/libs/middlewares/parseParams.js @@ -0,0 +1,64 @@ +/** +* ParseParams middleware +* +* EXAMPLE: +* Use it with no parameters to get all the params specified +* app.get('/', parseParams('dims'), function(req, res, next){}) +* +* Use it with an array of accepted values +* app.get('/', parseParams('filter', ['year', 'location']), function(req, res, next){}) +* +* Use it globally +* app.use(parseParams('dims')) +*/ + +const libs = `${process.cwd()}/libs`; + +const log = require(`${libs}/log`)(module); + + // This function returns the intersection of two arrays +function intersect(a, b) { + let t; + if (b.length > a.length) { + t = b; b = a; a = t; + } + return a.filter((e) => b.indexOf(e) !== -1); +} + +function parseParams(queryParam, arr) { + return (req, res, next) => { + req[queryParam] = {}; + if (req.query[queryParam]) { + const params = req.query[queryParam].split(','); + // Temporary object to hold the params and it's values + const obj = {}; + for (const param of params) { + // Get the key and the value - state:41 is key 'state' whith value 41 + const kv = param.split(':'); + // Check if there is a value. If there isn't, assign null + obj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]; + } + + // If the array exists and is not empty we intersect + if (typeof arr !== 'undefined' && arr.length > 0) { + // Intersect the keys of the obj with the array arr. + // The intersection array is assigned with the keys + const intersection = intersect(arr, Object.keys(obj)); + // This is a bit tricky... + // For each key in the intersection array we get it's value in the obj + // and assign it to the custom attribute in the req obj. + // For example: instersection => ["state"] so + // obj[intersection[i]] (with i=0) is obj["state"], that is 41 + // and req[queryParam]["state"] = 41 + for (let i = 0; i < intersection.length; ++i) { + req[queryParam][intersection[i]] = obj[intersection[i]]; + } + } else { + req[queryParam] = obj; + } + } + next(); + }; +} + +module.exports = parseParams; -- GitLab