JSFiddle - React, Tailwind, and code Playground
JavaScript
function OrMatchFilter() {
return function(elements, filterData, matchExact) {
if(//we have no elements, return empty
!elements || elements.length === 0) return [];
if(//filter data is not defined or is an empty object
!filterData || (Object.keys(filterData).length === 0 && filterData.constructor === Object)) return elements;
function decoupleObjectKeys(obj) {
var arrayOfKeys = [];
Object.keys(obj).forEach(function(key, index) {
if(//we have no nested lookup
key.indexOf('.') === -1) {
arrayOfKeys.push(key);
} else { //we have nested lookup, shoud add an array for them
var nestedKeys = key.split('.');
arrayOfKeys.push(nestedKeys);
}
});
return arrayOfKeys;
}
function checkMatch(element, filter, key) {
var elementValue, filterValue;
if(//key has nested lookup
key instanceof Array) {
elementValue = '';
filterValue = filter[key.join('.')]; //get the value for nested filter
key.forEach(//get the value from element using nested lookup
function(nestedKey, index) {
elementValue = elementValue[nestedKey] || element[nestedKey];
});
} else if(//key does not have nested lookup
typeof key === "string") {
elementValue = element[key];
filterValue = filter[key];
}
//evaluate matches
filterValue = filterValue? filterValue.toString().toLowerCase(): undefined;
elementValue = elementValue? elementValue.toString().toLowerCase(): undefined;
var regex = new RegExp('^'.concat(filterValue).concat('$'));
if(// possibles matches
!filterValue ||
(matchExact && elementValue && elementValue.match(regex)) ||
(!matchExact && elementValue && elementValue.indexOf(filterValue) !== -1)) return true;
return false;
}
var $result = [];
var $elements = elements;
...