diff --git a/src/libs/middlewares/parseParams.js b/src/libs/middlewares/parseParams.js index 3f841e15920dccc0d2ff0875f622a743ea957bb9..d7595702916708393934b90989ad7ccfbe49dc6d 100644 --- a/src/libs/middlewares/parseParams.js +++ b/src/libs/middlewares/parseParams.js @@ -18,17 +18,18 @@ const log = require(`${libs}/log`)(module); const _ = require('lodash') -function parseParams(queryParam, arr) { +function parseParams(queryField, arr) { return (req, res, next) => { - req[queryParam] = {}; - if (req.query[queryParam]) { - const params = req.query[queryParam].split(','); + req[queryField] = {}; + if (req.query[queryField]) { + const params = req.query[queryField].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 + // Get the key and the value - state:41 is key 'state' whith value 41. + // kv is then an array [key, value] or [key] if there is no value const kv = param.split(':'); - // Check if there is a value. If there isn't, assign null + // Check if there is a value. If there isn't, assign true obj[kv[0]] = (typeof kv[1] === 'undefined') ? true : kv[1]; } @@ -42,14 +43,14 @@ function parseParams(queryParam, arr) { // 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 + // and req[queryField]["state"] = 41 for (let i = 0; i < intersection.length; ++i) { - req[queryParam][intersection[i]] = obj[intersection[i]]; + req[queryField][intersection[i]] = obj[intersection[i]]; } - req[queryParam].size = intersection.length; + req[queryField].size = intersection.length; } else { - req[queryParam] = obj; - req[queryParam].size = Object.keys(obj).length; + req[queryField] = obj; + req[queryField].size = Object.keys(obj).length; } } next();