JSFiddle - React, Tailwind, and code Playground
by lchau
Babel + JSX
function createRegEx(propertyName, regex, caseSensitive = true) {
const options = {
"$regex": String(regex)
};
// ignore whitespace
options.$options = "x";
if (caseSensitive) {
options.$options += "i";
}
return {
[propertyName]: options
};
}
function createChain(...filters) {
const mergeProperties = (accumulator, filter) => _.merge(accumulator, filter);
const asRegEx = (value, key) => createRegEx(key, String(value));
return _.chain(filters)
.reduce(mergeProperties, {})
.map(asRegEx)
.value();
}
const chain = createChain({
a: 1,
z: 'foo'
}, {
b: 2
}, {
c: 3
});
console.log(JSON.stringify({ "$and": chain }, null, 2));
console.log(JSON.stringify({ "$or": chain }, null, 2));