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

Change queryParam to queryField in parseParams

parent d2d092e2
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!29Feature param query builder
......@@ -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();
......
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