Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Fixed bug when commas are inside queries #31

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ const responseBody = (obj, collection) => {
const parse_query = (req_q) => {
let query = {};
let q = {};
req_q.split(',').forEach(i => (q[i.split(':')[0]] = i.split(':')[1]));
req_q.split(',')
.reduce((acc, curr) => {
curr.includes(":") ? acc.push(curr) : acc[acc.length - 1] += ("," + curr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may just be my personal opinion or preference, but I would like to see an explanation for this logic, so other contributors don't need to reverse engineer it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say the query is name:Hello, Jsonbox,age:>15. Normally, this would be split up into this array ["name:Hello", "Jsonbox", "age:>15" ]. This isn't the right array and the reduce function solves this, by iterating over the false array. If the element in the array includes a :, the reduce function leaves it untouched, otherwise the reduce function appends the element to the previous one.
Dunno what's so hard to understand about it, much of the codebase is written in similiar way without any comments.

return acc;
}, [])
.forEach(i => (q[i.split(':')[0]] = i.split(':')[1]));
Object.keys(q).forEach((key) => {
const value = q[key];
if (value.startsWith('>=') || value.startsWith('<=') || value.startsWith('>') || value.startsWith('<') || value.startsWith('=')) {
Expand Down