Last active
June 22, 2016 03:05
-
-
Save MiguelLattuada/9eafa4d2acaec2a17ab35c1cce19b7f5 to your computer and use it in GitHub Desktop.
Custom "OR" match filter, could be used with Angular 1.x, or standalone, here is an example: https://jsfiddle.net/kyf18jtq/, check the console for results.
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
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; | |
var $filter = filterData; | |
var $keys = decoupleObjectKeys(filterData); | |
$elements.forEach(function(element, elementIndex) { | |
var pushToResult = false; | |
$keys.forEach(function(key, keyIndex) { | |
if(checkMatch(element, $filter, key)) pushToResult = true; | |
}); | |
pushToResult && $result.push(element); | |
}); | |
return $result; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment