Skip to content

Instantly share code, notes, and snippets.

@ivan-hilckov
Created October 29, 2016 22:32
Show Gist options
  • Save ivan-hilckov/1e0b1ea740bf5b8a38dbcc6bf7260dec to your computer and use it in GitHub Desktop.
Save ivan-hilckov/1e0b1ea740bf5b8a38dbcc6bf7260dec to your computer and use it in GitHub Desktop.
export function string2bool(str) {
switch (str) {
case 'true':
return true;
case 'false':
return false;
default:
return str;
}
}
export function getWhere(filters) {
let where;
const or = Object.keys(filters).reduce((prev, current) => {
let result = {};
const filter = filters[current];
if (filter.length) {
if (filter.length > 1) {
result = {
$or: filter.map((name) => ({ [`${current}`]: string2bool(name) })),
};
} else {
result = {
[`${current}`]: string2bool(filter[0]),
};
}
return [...prev, result];
}
return prev;
}, []);
if (or.length > 1) {
where = { $and: or };
} else {
where = or[0];
}
return JSON.stringify(where);
}
export default {
string2bool,
getWhere,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment