JSFiddle - React, Tailwind, and code Playground

by lchau

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>

Babel + JSX

const params = {
  keyA: '1',
  keyB: '2',
  keyC: '3',
  keyD: /test/
};
const regex = _.each(params, (value, key, array) => {
  const regex = new RegExp(_.escapeRegExp(value));
	array[key] = regex;
});

function createRegEx(propertyName, regex, caseSensitive = true) {
  const options = {
    "$regex": regex
  };

  // ignore whitespace
  options.$options = "x";
  if (caseSensitive) {
    options.$options += "i";
  }

  return {
    [propertyName]: options
  };
}

function createQuery($regex) {
  const regex = _.map($regex, (value, key) => createRegEx(key, value));
  return regex;
}

console.log({
	$and: createQuery(regex)
});