JSFiddle - React, Tailwind, and code Playground

by odiseo

JavaScript

function espFilterDecode (input='') {
    const res = [];
    const filters = input.split('&');
    
    filters.forEach((filter) => {
    	//where filter follows the shape of first_name__ISW=lisa
  
    	const expression = {};
      // attribute is everything before the __
      expression.attribute = filter.substr(0, filter.indexOf('__')); 
      
      // operator is everything between the __ and =
      expression.operator = filter.substring(filter.indexOf("__")+2, filter.lastIndexOf("="));
			
      // value is everything after =
      expression.value = filter.substr(filter.indexOf("=")+1, filter.length);

      
      res.push(expression);
    });
    
   	return res;
}

function espFilterEncode (input=[]) {
		let res = '';
    input.forEach((expression) => {
    	const strExpression = `${expression.attribute}__${expression.operator}=${expression.value}`;
  		res += strExpression + '&';
    });
    
    //removing last & of the string
    res = res.substring(0, res.length - 1);
	
   	return res;
}

function getFreeStyleCondition(input='') {
	//everything before &location
  const indexOfLocation = input.indexOf('location__');
  if (indexOfLocation > -1) {
  	  const res =  input.substr(0, indexOfLocation);
  		return res;
  }
	return input;
}

function getRestrictedCondition(input='') {
	//everything starting at location and until the end of the string
  const res =  input.substr(input.indexOf('location__'), input.length);
  return res;
}

const stringExpression = "location__EQ=California&job_role__EQ=Killer";
const decodedArray = espFilterDecode(stringExpression);
console.log(decodedArray);

const encodedExpression = espFilterEncode(decodedArray);
console.log(encodedExpression);

const stringCondition = '$confirmation.label__EQ=No&OR$confirmation.label__EQ=Maybe&location__EQ=California&job_role__EQ=Killer';
const freeStyleCondition = getFreeStyleCondition(stringCondition);
console.log(freeStyleCondition);

const testFreeStyle =...