Created
October 29, 2016 22:32
-
-
Save ivan-hilckov/1e0b1ea740bf5b8a38dbcc6bf7260dec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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